MixBuf.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ////////////////////////////////////////////////////////////////////////////////
  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. #ifndef MIXBUF_H
  20. #define MIXBUF_H
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // Macros.
  23. ///////////////////////////////////////////////////////////////////////////////
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // Types.
  26. ///////////////////////////////////////////////////////////////////////////////
  27. // Forward declare class for handy-dandy typedef.
  28. class RMixBuf;
  29. // Handy-dandy typedef.
  30. typedef RMixBuf* PMIXBUF;
  31. // Class definition.
  32. class RMixBuf
  33. {
  34. public: // <Con|De>struction.
  35. // Default constructor.
  36. RMixBuf();
  37. // Constructor Especial.
  38. RMixBuf( // Returns whatever it is a constructor returns.
  39. UCHAR* pu8Dst, // In: Destination buffer.
  40. ULONG ulSize); // In: Size of destination buffer in bytes.
  41. // Destructor.
  42. ~RMixBuf();
  43. public: // Methods.
  44. // Silence buffer.
  45. void Silence(void);
  46. // Set size of mix buffer in bytes. (Allocates buffer).
  47. // Returns 0 on success.
  48. short SetSize(ULONG ulSize);
  49. // Set the destination buffer.
  50. void SetDest(
  51. UCHAR* pu8Dst, // In: Destination buffer.
  52. ULONG ulSize); // In: Size of destination buffer in bytes.
  53. // Mix data in.
  54. // Returns 0 on success.
  55. short Mix( ULONG ulStartPos,
  56. U8* pu8Data,
  57. ULONG ulSize,
  58. long lSampleRate,
  59. long lBitsPerSample,
  60. long lNumChannels,
  61. UCHAR ucVolume = UCHAR(255),
  62. UCHAR ucVol2 = UCHAR(255) );
  63. // Prepare for destination. If necessary, converts to destination format.
  64. void PrepareForDest(void);
  65. public: // Querries.
  66. // Get ptr to destination data.
  67. void* GetDstData(void) { return m_pu8Dst; }
  68. // Get ptr to mix data.
  69. void* GetMixData(void) { return m_pu8Mix; }
  70. // Get size of destination buffer in bytes.
  71. ULONG GetDstSize(void) { return m_ulDstSize; }
  72. // Get size of mix buffer in bytes.
  73. ULONG GetMixSize(void) { return m_ulMixSize; }
  74. // Returns number of RMixBufs allocated.
  75. static short Num(void) { return ms_sNumBufs; }
  76. public: // Statics.
  77. // Sets the global volume for mixed sounds:
  78. static void SetVolume(UCHAR ucVolume)
  79. {
  80. ms_ucGlobalVolume = ucVolume;
  81. }
  82. // Reads the current global volume for mixed sounds:
  83. static UCHAR GetVolume()
  84. {
  85. return ms_ucGlobalVolume;
  86. }
  87. // Allows user to set the cut off volume for mixing sound:
  88. static void SetCutOff(short sVolume)
  89. {
  90. if ( (sVolume >= 0) && (sVolume < 256) )
  91. {
  92. ms_sCutOffVolume = sVolume;
  93. }
  94. }
  95. protected: // Internal use.
  96. // Intialize members.
  97. void Init(void);
  98. // Free dynamic data and re-init.
  99. void Reset(void);
  100. protected: // Members.
  101. U8* m_pu8Mix; // Mix buffer.
  102. U8* m_pu8Dst; // Destination buffer.
  103. short m_sOwnMixBuf; // TRUE if RMixBuf allocated the mix buffer.
  104. ULONG m_ulMixSize; // Size of mix buffer in bytes.
  105. ULONG m_ulDstSize; // Size of dst buffer in bytes.
  106. static short ms_sNumBufs; // Number of RMixBufs allocated.
  107. static UCHAR ms_ucGlobalVolume;// Scale all mixes relative to this
  108. public: // It is safe to change these.
  109. short m_sInUse; // TRUE if in use, FALSE otherwise.
  110. static long ms_lSampleRate; // Sample rate for audio
  111. // playback/mix.
  112. static long ms_lSrcBitsPerSample; // Sample size in bits for sample data.
  113. // 0 for no preference.
  114. static long ms_lMixBitsPerSample; // Sample size in bits for mixing.
  115. static long ms_lDstBitsPerSample; // Sample size in bits for Blue data.
  116. static long ms_lNumChannels; // Number of channels (mono
  117. // or stereo).
  118. static short ms_sCutOffVolume; // when to not mix samples...
  119. };
  120. //////////////////////////////////////////////////////////////////////////////
  121. //
  122. // Build table for dynamic volume adjustment (DVA) - now available to the
  123. // the outside world
  124. //
  125. //////////////////////////////////////////////////////////////////////////////
  126. // Cutting back on the number of volume levels is strictly to save memory
  127. #define DVA_RESOLUTION 1 // significant volume change in 0-255
  128. #define DVA_SHIFT 0 // must match the resolution in shift bits
  129. #define DVA_SIZE (256>>DVA_SHIFT) // size of tables
  130. #define DVA_LOW_OFFSET (256 * DVA_SIZE * 2)
  131. class CDVA // a complete dummy
  132. {
  133. public:
  134. //////////////////////////////////////////////////////////////////////////////
  135. // Allow use of mixing volume for other applications:
  136. // A level of 255 is identity.
  137. //////////////////////////////////////////////////////////////////////////////
  138. inline UCHAR ScaleByte(UCHAR ucByte,UCHAR ucLevel)
  139. {
  140. return UCHAR(CDVA::ms_asHighByte[DVA_SIZE +
  141. (ucLevel>>DVA_SHIFT)][ucByte]);
  142. }
  143. // More efficient for a block of data scaled the same:
  144. inline void ScaleBytes(short sNumBytes,UCHAR* pucBytesIn,
  145. UCHAR* pucBytesOut,UCHAR ucLevel)
  146. {
  147. ASSERT(pucBytesIn);
  148. ASSERT(pucBytesOut);
  149. // Unsigned, always true. --ryan.
  150. //ASSERT(ucLevel < 256);
  151. short* psTable = CDVA::ms_asHighByte[DVA_SIZE + (ucLevel>>DVA_SHIFT)];
  152. short i;
  153. for (i=0;i < sNumBytes;i++)
  154. {
  155. pucBytesOut[i] = UCHAR(psTable[pucBytesIn[i]]);
  156. }
  157. }
  158. // we can also scale 16-bit values, but this is not yet accessible to the user.
  159. //----------------------------------------------------------------------
  160. CDVA();
  161. ~CDVA(){}; // nothing to do....
  162. //----------------------------------------------------------------------
  163. // To save on registers, make this the same array:
  164. static short ms_asHighByte[DVA_SIZE * 2][256]; // for 16-bit sound
  165. };
  166. #endif // MIXBUF_H
  167. ///////////////////////////////////////////////////////////////////////////////
  168. // EOF
  169. ///////////////////////////////////////////////////////////////////////////////