snd.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. ///////////////////////////////////////////////////////////////////////////////
  19. //
  20. // snd.cpp
  21. //
  22. // History:
  23. // 04/28/95 JMI Started.
  24. //
  25. // 01/29/97 JMI Added callback on done and GetSample() query.
  26. //
  27. // 05/09/97 JMI Added ability to loop sub samples.
  28. //
  29. // 06/12/97 JMI Added user value.
  30. //
  31. // 07/15097 JRD Added members to hold sound volume information
  32. //
  33. // 08/05/97 JMI Added Pause(), IsPaused(), and Resume().
  34. //
  35. //////////////////////////////////////////////////////////////////////////////
  36. //
  37. // See CPP for description of this API.
  38. //
  39. //////////////////////////////////////////////////////////////////////////////
  40. #ifndef SND_H
  41. #define SND_H
  42. #include <stdio.h> // For FILE*
  43. #include "System.h"
  44. // If PATHS_IN_INCLUDES macro is defined, we can utilized relative
  45. // paths to a header file. In this case we generally go off of our
  46. // RSPiX root directory. System.h MUST be included before this macro
  47. // is evaluated. System.h is the header that, based on the current
  48. // platform (or more so in this case on the compiler), defines
  49. // PATHS_IN_INCLUDES. Blue.h includes system.h so you can include that
  50. // instead.
  51. #ifdef PATHS_IN_INCLUDES
  52. #include "GREEN/Mix/mix.h"
  53. #include "GREEN/Sample/sample.h"
  54. #else
  55. #include "mix.h"
  56. #include "sample.h"
  57. #endif // PATHS_IN_INCLUDES
  58. //////////////////////////////////////////////////////////////////////////////
  59. // Macros.
  60. //////////////////////////////////////////////////////////////////////////////
  61. //////////////////////////////////////////////////////////////////////////////
  62. // Types.
  63. //////////////////////////////////////////////////////////////////////////////
  64. // Forward declare RSnd for typedef.
  65. class RSnd;
  66. // Handy-dandy typedef.
  67. typedef RSnd* PSND;
  68. class RSnd
  69. {
  70. public:
  71. // Default constructor.
  72. RSnd();
  73. // Destructor.
  74. ~RSnd();
  75. /////////////////////////// Typedefs ///////////////////////////////////////
  76. public:
  77. typedef enum
  78. { // States for m_sState and GetState().
  79. Stopped, // Stopped completely.
  80. Starting, // Just started, no data sent yet.
  81. Queueing, // Sending data, none yet played.
  82. Playing, // Sending data, data playing.
  83. Ending // Done sending, still playing.
  84. } State;
  85. // Callback when done playing/streaming.
  86. typedef void (*DoneCall)( // Returns nothing.
  87. RSnd* psnd); // This RSnd.
  88. ////////////////////////// Querries ///////////////////////////////////////
  89. public:
  90. // Returns the current state of the play/stream process.
  91. State GetState(void) { return m_sState; }
  92. // Gets/returns the current position of the audio in bytes.
  93. long GetPos(void);
  94. // Gets/returns the current time of the audio in milliseconds.
  95. long GetTime(void);
  96. // Gets/returns the current sample.
  97. RSample* GetSample(void)
  98. {
  99. return m_psample;
  100. }
  101. // Query paused status of current play or stream.
  102. short IsPaused(void)
  103. {
  104. return m_mix.IsChannelPaused();
  105. }
  106. ////////////////////////// Methods ////////////////////////////////////////
  107. public:
  108. // Reset object. Release RSample and reset variables.
  109. void Reset(void);
  110. // Load a sound file a lReadBufSize byte piece at a time and play
  111. // lPlayBufSize byte pieces at a time.
  112. // Returns 0 on success.
  113. short Stream( // Returns 0 on success.
  114. char* pszSampleName, // In: Name of sample file.
  115. long lPlayBufSize, // In: Size of play buffer in bytes.
  116. long lReadBufSize, // In: Size of file read buffer in bytes.
  117. UCHAR ucMainVolume = 255, // In: Primary Volume (0 - 255)
  118. UCHAR ucVolume2 = 255); // In: Secondary Volume (0 - 255)
  119. // Plays RSample supplied via ptr with buffer size of lPlayBufSize
  120. // (this is the size of the chunks sent to RMix).
  121. // Note on looping:
  122. // Start End
  123. // 1-----------------------------------------------------------4
  124. // 2=======================================3
  125. // lLoopStartTime lLoopEndTime
  126. //
  127. // In a looping scenario, 1..2 of the sample is played, then 2..3
  128. // is repeated until m_sLoop is FALSE, at which time, once 3 is reached,
  129. // 3..4 is played.
  130. short Play( // Returns 0 on success.
  131. RSample* psample, // In: Sample to play.
  132. long lPlayBufSize, // In: Size of play buffer in bytes.
  133. UCHAR ucMainVolume = 255, // In: Primary Volume (0 - 255)
  134. UCHAR ucVolume2 = 255, // In: Secondary Volume (0 - 255)
  135. long lLoopStartTime = -1, // In: Where to loop back to in milliseconds.
  136. // -1 indicates no looping (unless m_sLoop is
  137. // explicitly set).
  138. long lLoopEndTime = 0); // In: Where to loop back from in milliseconds.
  139. // In: If less than 1, the end + lLoopEndTime is used.
  140. // Aborts current play or stream.
  141. // Returns 0 on success.
  142. short Abort(void);
  143. // Pause current play or stream.
  144. void Pause(void)
  145. {
  146. m_mix.PauseChannel();
  147. }
  148. // Resume current play or stream.
  149. void Resume(void)
  150. {
  151. m_mix.ResumeChannel();
  152. }
  153. // Set or clear (if psndfx is NULL) a RSndFx for this.
  154. void SetFx( // Returns nothing.
  155. RSndFx* psndfx) // FX for this. Clears current, if NULL.
  156. {
  157. m_mix.SetFx(psndfx);
  158. }
  159. ////////////////////////// Internal Methods ///////////////////////////////
  160. protected:
  161. // Initialize instantiable members.
  162. void Init(void);
  163. // Called from StreamCallStatic.
  164. // Sends back current volume information to RMix
  165. // Returns pointer to next buffer to play or NULL to end.
  166. void* StreamCall( RMix::Msg msg,
  167. void* pData,
  168. ULONG* pulNewBufSize,
  169. ULONG ulUser,
  170. UCHAR* pucVolume = NULL,
  171. UCHAR* pucVol2 = NULL);
  172. // Callback from blue regarding playing buffer(s).
  173. // Returns pointer to next buffer to play or NULL to end.
  174. static void* StreamCallStatic(RMix::Msg msg,
  175. void* pData,
  176. ULONG* pulNewBufSize,
  177. ULONG ulUser,
  178. UCHAR* pucVolume = NULL,
  179. UCHAR* pucVol2 = NULL);
  180. // Called from PlayCallStatic.
  181. // Returns pointer to next buffer to play or NULL to end.
  182. void* PlayCall(RMix::Msg msg,
  183. void* pData,
  184. ULONG* pulNewBufSize,
  185. UCHAR* pucVolume = NULL,
  186. UCHAR* pucVol2 = NULL);
  187. // Callback from blue regarding playing buffer(s).
  188. // Sends back current volume information to RMix
  189. // Returns pointer to next buffer to play or NULL to end
  190. static void* PlayCallStatic(RMix::Msg msg,
  191. void* pData,
  192. ULONG* pulNewBufSize,
  193. ULONG ulUser,
  194. UCHAR* pucVolume = NULL,
  195. UCHAR* pucVol2 = NULL);
  196. ////////////////////////// Member vars ////////////////////////////////////
  197. public:
  198. short m_sLoop; // If TRUE, Play() will loop until FALSE.
  199. long m_lLoopStartPos; // Where to loop back to in bytes.
  200. // Play() only.
  201. long m_lLoopEndPos; // Where to loop back from in bytes.
  202. // Play() only.
  203. DoneCall m_dcUser; // User callback when done playing/streaming
  204. // a sample.
  205. ULONG m_ulUser; // User value -- set as you please.
  206. short m_sChannelVolume;// 0-255 = Primary (local) Volume
  207. short m_sTypeVolume; // 0-255 = Secondary (category) Volume
  208. protected:
  209. long m_lBufSize; // Buffer unit to stream in.
  210. State m_sState; // One of the enums above representing
  211. // this RSnd's state.
  212. RMix m_mix; // For playing/mixing sound data.
  213. PSAMPLE m_psample; // Sample to be streamed.
  214. short m_sOwnSample; // TRUE if RSnd allocated m_psample, FALSE
  215. // otherwise.
  216. ULONG m_ulRemaining; // Amount left of sample data to be played.
  217. ULONG m_ulSampleSize;// Overall sample size.
  218. /////////////////////// Static members /////////////////////////////////
  219. };
  220. #endif // SND_H
  221. //////////////////////////////////////////////////////////////////////////////
  222. // EOF
  223. //////////////////////////////////////////////////////////////////////////////