| 1 | ////////////////////////////////////////////////////////////////////////////////cxx . . $ `<file>/RSPiX/Src/GREEN/Mix/MixBuf.h`/ |
| 2 | // |
| 3 | // Copyright 2016 RWS Inc, All Rights Reserved |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or modify |
| 6 | // it under the terms of version 2 of the GNU General Public License as published by |
| 7 | // the Free Software Foundation |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | // |
| 18 | // MixBuf.h |
| 19 | |
| 20 | #ifndef MIXBUF_H |
| 21 | #define MIXBUF_Hcxx . . $ `RSPiX/Src/GREEN/Mix/MixBuf.h:21:9`! |
| 22 | |
| 23 | /////////////////////////////////////////////////////////////////////////////// |
| 24 | // Macros. |
| 25 | /////////////////////////////////////////////////////////////////////////////// |
| 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | // Types. |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | |
| 31 | // Forward declare class for handy-dandy typedef. |
| 32 | class RMixBufcxx . . $ RMixBuf#; |
| 33 | |
| 34 | // Handy-dandy typedef. |
| 35 | typedef RMixBufcxx . . $ RMixBuf#* PMIXBUFcxx . . $ PMIXBUF#; |
| 36 | |
| 37 | // Class definition. |
| 38 | class RMixBufcxx . . $ RMixBuf# |
| 39 | { |
| 40 | public: // <Con|De>struction. |
| 41 | // Default constructor. |
| 42 | RMixBufcxx . . $ RMixBuf#RMixBuf(49f6e7a06ebc5aa8).(); |
| 43 | // Constructor Especial. |
| 44 | RMixBufcxx . . $ RMixBuf#RMixBuf(e1e8b00c42c42532).( // Returns whatever it is a constructor returns. |
| 45 | uint8_tcxx . . $ uint8_t#* pu8Dst, // In: Destination buffer. |
| 46 | uint32_tcxx . . $ uint32_t# ulSize); // In: Size of destination buffer in bytes. |
| 47 | // Destructor. |
| 48 | ~cxx . . $ RMixBuf#`~RMixBuf`(49f6e7a06ebc5aa8).RMixBufcxx . . $ RMixBuf#(); |
| 49 | |
| 50 | public: // Methods. |
| 51 | // Silence buffer. |
| 52 | void Silencecxx . . $ RMixBuf#Silence(49f6e7a06ebc5aa8).(void); |
| 53 | |
| 54 | // Set size of mix buffer in bytes. (Allocates buffer). |
| 55 | // Returns 0 on success. |
| 56 | int16_t SetSizecxx . . $ RMixBuf#SetSize(f0a409fc622e509e).(uint32_tcxx . . $ uint32_t# ulSize); |
| 57 | |
| 58 | // Set the destination buffer. |
| 59 | void SetDestcxx . . $ RMixBuf#SetDest(e1e8b00c42c42532).( |
| 60 | uint8_tcxx . . $ uint8_t#* pu8Dst, // In: Destination buffer. |
| 61 | uint32_tcxx . . $ uint32_t# ulSize); // In: Size of destination buffer in bytes. |
| 62 | |
| 63 | // Mix data in. |
| 64 | // Returns 0 on success. |
| 65 | int16_t Mixcxx . . $ RMixBuf#Mix(a7c00f1d2a9f71aa).( uint32_tcxx . . $ uint32_t# ulStartPos, |
| 66 | U8cxx . . $ U8#* pu8Data, |
| 67 | uint32_tcxx . . $ uint32_t# ulSize, |
| 68 | int32_t lSampleRate, |
| 69 | int32_t lBitsPerSample, |
| 70 | int32_t lNumChannels, |
| 71 | uint8_tcxx . . $ uint8_t# ucVolume = uint8_tcxx . . $ uint8_t#(255), |
| 72 | uint8_tcxx . . $ uint8_t# ucVol2 = uint8_tcxx . . $ uint8_t#(255) ); |
| 73 | |
| 74 | // Prepare for destination. If necessary, converts to destination format. |
| 75 | void PrepareForDestcxx . . $ RMixBuf#PrepareForDest(49f6e7a06ebc5aa8).(void); |
| 76 | |
| 77 | public: // Querries. |
| 78 | |
| 79 | // Get ptr to destination data. |
| 80 | void* GetDstDatacxx . . $ RMixBuf#GetDstData(50c7df7de5344ebb).(void) { return m_pu8Dstcxx . . $ RMixBuf#m_pu8Dst.; } |
| 81 | |
| 82 | // Get ptr to mix data. |
| 83 | void* GetMixDatacxx . . $ RMixBuf#GetMixData(50c7df7de5344ebb).(void) { return m_pu8Mixcxx . . $ RMixBuf#m_pu8Mix.; } |
| 84 | |
| 85 | // Get size of destination buffer in bytes. |
| 86 | uint32_tcxx . . $ uint32_t# GetDstSizecxx . . $ RMixBuf#GetDstSize(db52760e79bf9054).(void) { return m_ulDstSizecxx . . $ RMixBuf#m_ulDstSize.; } |
| 87 | |
| 88 | // Get size of mix buffer in bytes. |
| 89 | uint32_tcxx . . $ uint32_t# GetMixSizecxx . . $ RMixBuf#GetMixSize(db52760e79bf9054).(void) { return m_ulMixSizecxx . . $ RMixBuf#m_ulMixSize.; } |
| 90 | |
| 91 | // Returns number of RMixBufs allocated. |
| 92 | static int16_t Numcxx . . $ RMixBuf#Num(4c3706f7850cf040).(void) { return ms_sNumBufscxx . . $ RMixBuf#ms_sNumBufs.; } |
| 93 | |
| 94 | |
| 95 | public: // Statics. |
| 96 | |
| 97 | // Sets the global volume for mixed sounds: |
| 98 | static void SetVolumecxx . . $ RMixBuf#SetVolume(ac50b47256ae7414).(uint8_tcxx . . $ uint8_t# ucVolume) |
| 99 | { |
| 100 | ms_ucGlobalVolumecxx . . $ RMixBuf#ms_ucGlobalVolume. = ucVolume; |
| 101 | } |
| 102 | |
| 103 | // Reads the current global volume for mixed sounds: |
| 104 | static uint8_tcxx . . $ uint8_t# GetVolumecxx . . $ RMixBuf#GetVolume(7c4be072098b358f).() |
| 105 | { |
| 106 | return ms_ucGlobalVolumecxx . . $ RMixBuf#ms_ucGlobalVolume.; |
| 107 | } |
| 108 | |
| 109 | // Allows user to set the cut off volume for mixing sound: |
| 110 | static void SetCutOffcxx . . $ RMixBuf#SetCutOff(68e3ae50fe6578a9).(int16_t sVolume) |
| 111 | { |
| 112 | if ( (sVolume >= 0) && (sVolume < 256) ) |
| 113 | { |
| 114 | ms_sCutOffVolumecxx . . $ RMixBuf#ms_sCutOffVolume. = sVolume; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | protected: // Internal use. |
| 119 | // Intialize members. |
| 120 | void Initcxx . . $ RMixBuf#Init(49f6e7a06ebc5aa8).(void); |
| 121 | // Free dynamic data and re-init. |
| 122 | void Resetcxx . . $ RMixBuf#Reset(49f6e7a06ebc5aa8).(void); |
| 123 | |
| 124 | protected: // Members. |
| 125 | U8cxx . . $ U8#* m_pu8Mixcxx . . $ RMixBuf#m_pu8Mix.; // Mix buffer. |
| 126 | U8cxx . . $ U8#* m_pu8Dstcxx . . $ RMixBuf#m_pu8Dst.; // Destination buffer. |
| 127 | int16_t m_sOwnMixBufcxx . . $ RMixBuf#m_sOwnMixBuf.; // TRUE if RMixBuf allocated the mix buffer. |
| 128 | uint32_tcxx . . $ uint32_t# m_ulMixSizecxx . . $ RMixBuf#m_ulMixSize.; // Size of mix buffer in bytes. |
| 129 | uint32_tcxx . . $ uint32_t# m_ulDstSizecxx . . $ RMixBuf#m_ulDstSize.; // Size of dst buffer in bytes. |
| 130 | |
| 131 | static int16_t ms_sNumBufscxx . . $ RMixBuf#ms_sNumBufs.; // Number of RMixBufs allocated. |
| 132 | static uint8_tcxx . . $ uint8_t# ms_ucGlobalVolumecxx . . $ RMixBuf#ms_ucGlobalVolume.;// Scale all mixes relative to this |
| 133 | |
| 134 | public: // It is safe to change these. |
| 135 | int16_t m_sInUsecxx . . $ RMixBuf#m_sInUse.; // TRUE if in use, FALSE otherwise. |
| 136 | |
| 137 | static int32_t ms_lSampleRatecxx . . $ RMixBuf#ms_lSampleRate.; // Sample rate for audio |
| 138 | // playback/mix. |
| 139 | |
| 140 | static int32_t ms_lSrcBitsPerSamplecxx . . $ RMixBuf#ms_lSrcBitsPerSample.; // Sample size in bits for sample data. |
| 141 | // 0 for no preference. |
| 142 | static int32_t ms_lMixBitsPerSamplecxx . . $ RMixBuf#ms_lMixBitsPerSample.; // Sample size in bits for mixing. |
| 143 | static int32_t ms_lDstBitsPerSamplecxx . . $ RMixBuf#ms_lDstBitsPerSample.; // Sample size in bits for Blue data. |
| 144 | |
| 145 | static int32_t ms_lNumChannelscxx . . $ RMixBuf#ms_lNumChannels.; // Number of channels (mono |
| 146 | // or stereo). |
| 147 | static int16_t ms_sCutOffVolumecxx . . $ RMixBuf#ms_sCutOffVolume.; // when to not mix samples... |
| 148 | }; |
| 149 | |
| 150 | ////////////////////////////////////////////////////////////////////////////// |
| 151 | // |
| 152 | // Build table for dynamic volume adjustment (DVA) - now available to the |
| 153 | // the outside world |
| 154 | // |
| 155 | ////////////////////////////////////////////////////////////////////////////// |
| 156 | |
| 157 | // Cutting back on the number of volume levels is strictly to save memory |
| 158 | #define DVA_RESOLUTIONcxx . . $ `RSPiX/Src/GREEN/Mix/MixBuf.h:158:9`! 1 // significant volume change in 0-255 |
| 159 | #define DVA_SHIFTcxx . . $ `RSPiX/Src/GREEN/Mix/MixBuf.h:159:9`! 0 // must match the resolution in shift bits |
| 160 | #define DVA_SIZEcxx . . $ `RSPiX/Src/GREEN/Mix/MixBuf.h:160:9`! (256>>DVA_SHIFTcxx . . $ `RSPiX/Src/GREEN/Mix/MixBuf.h:159:9`!) // size of tables |
| 161 | #define DVA_LOW_OFFSETcxx . . $ `RSPiX/Src/GREEN/Mix/MixBuf.h:161:9`! (256 * DVA_SIZE * 2) |
| 162 | |
| 163 | class CDVAcxx . . $ CDVA# // a complete dummy |
| 164 | { |
| 165 | public: |
| 166 | ////////////////////////////////////////////////////////////////////////////// |
| 167 | // Allow use of mixing volume for other applications: |
| 168 | // A level of 255 is identity. |
| 169 | ////////////////////////////////////////////////////////////////////////////// |
| 170 | inline uint8_tcxx . . $ uint8_t# ScaleBytecxx . . $ CDVA#ScaleByte(777233acce9323bd).(uint8_tcxx . . $ uint8_t# ucByte,uint8_tcxx . . $ uint8_t# ucLevel) |
| 171 | { |
| 172 | return uint8_tcxx . . $ uint8_t#(CDVAcxx . . $ CDVA#::ms_asHighBytecxx . . $ CDVA#ms_asHighByte.[DVA_SIZEcxx . . $ `RSPiX/Src/GREEN/Mix/MixBuf.h:160:9`! + |
| 173 | (ucLevel>>DVA_SHIFTcxx . . $ `RSPiX/Src/GREEN/Mix/MixBuf.h:159:9`!)][ucByte]); |
| 174 | } |
| 175 | |
| 176 | // More efficient for a block of data scaled the same: |
| 177 | inline void ScaleBytescxx . . $ CDVA#ScaleBytes(5c496fadacfecf0c).(int16_t sNumBytes,uint8_tcxx . . $ uint8_t#* pucBytesIn, |
| 178 | uint8_tcxx . . $ uint8_t#* pucBytesOut,uint8_tcxx . . $ uint8_t# ucLevel) |
| 179 | { |
| 180 | ASSERTcxx . . $ `RSPiX/Src/BLUE/unix/UnixBlue.h:107:10`!(pucBytesIn); |
| 181 | ASSERTcxx . . $ `RSPiX/Src/BLUE/unix/UnixBlue.h:107:10`!(pucBytesOut); |
| 182 | |
| 183 | // Unsigned, always true. --ryan. |
| 184 | //ASSERT(ucLevel < 256); |
| 185 | |
| 186 | int16_t* psTable = CDVAcxx . . $ CDVA#::ms_asHighBytecxx . . $ CDVA#ms_asHighByte.[DVA_SIZEcxx . . $ `RSPiX/Src/GREEN/Mix/MixBuf.h:160:9`! + (ucLevel>>DVA_SHIFTcxx . . $ `RSPiX/Src/GREEN/Mix/MixBuf.h:159:9`!)]; |
| 187 | |
| 188 | int16_t i; |
| 189 | for (i=0;i < sNumBytes;i++) |
| 190 | { |
| 191 | pucBytesOut[i] = uint8_tcxx . . $ uint8_t#(psTable[pucBytesIn[i]]); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // we can also scale 16-bit values, but this is not yet accessible to the user. |
| 196 | |
| 197 | //---------------------------------------------------------------------- |
| 198 | CDVAcxx . . $ CDVA#CDVA(49f6e7a06ebc5aa8).(); |
| 199 | ~cxx . . $ CDVA#`~CDVA`(49f6e7a06ebc5aa8).CDVAcxx . . $ CDVA#(){}; // nothing to do.... |
| 200 | //---------------------------------------------------------------------- |
| 201 | // To save on registers, make this the same array: |
| 202 | static int16_t ms_asHighBytecxx . . $ CDVA#ms_asHighByte.[DVA_SIZEcxx . . $ `RSPiX/Src/GREEN/Mix/MixBuf.h:160:9`! * 2][256]; // for 16-bit sound |
| 203 | |
| 204 | }; |
| 205 | |
| 206 | |
| 207 | #endif // MIXBUF_H |
| 208 | /////////////////////////////////////////////////////////////////////////////// |
| 209 | // EOF |
| 210 | /////////////////////////////////////////////////////////////////////////////// |