sound_stream.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. Copyright (C) 2005 Michael Liebscher
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. /*
  16. * sound_stream.c: Sound Stream manager.
  17. *
  18. * Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
  19. * Date: 2004
  20. *
  21. * Acknowledgement:
  22. * Portion of this code was derived from Quake II Evolved.
  23. *
  24. */
  25. #include "../wolfiphone.h"
  26. typedef struct
  27. {
  28. char introName[ MAX_GAMEPATH ];
  29. char loopName[ MAX_GAMEPATH ];
  30. _boolean looping;
  31. filehandle_t *hFile;
  32. int start;
  33. int rate;
  34. unsigned format;
  35. void *vorbisFile;
  36. } musicTrack_t;
  37. // anything greater than 1<<13 caused crashes on iphone OS 2.1 (on a 3G iphone)
  38. #define BUFFER_SIZE (1<<13)
  39. PRIVATE musicTrack_t bgTrack;
  40. PRIVATE channel_t *s_streamingChannel;
  41. extern void Sound_StopBGTrack( void );
  42. /*
  43. -----------------------------------------------------------------------------
  44. Function: ovc_read -OGG read Callback. Reads data from a stream.
  45. Parameters:
  46. ptr -[in] Storage location for data.
  47. size -[in] Item size in bytes.
  48. nmemb -[in] Maximum number of items to be read.
  49. datasource -[in] music track data structure.
  50. Returns: Nothing
  51. Notes:
  52. -----------------------------------------------------------------------------
  53. */
  54. PRIVATE size_t ovc_read( void *ptr, size_t size, size_t nmemb, void *datasource )
  55. {
  56. musicTrack_t *track = (musicTrack_t *)datasource;
  57. if( ! size || ! nmemb )
  58. {
  59. return 0;
  60. }
  61. return FS_ReadFile( ptr, size, nmemb, track->hFile );
  62. }
  63. /*
  64. -----------------------------------------------------------------------------
  65. Function: ovc_read -OGG seek Callback. Moves the file pointer to a specified
  66. location.
  67. Parameters:
  68. datasource -[in] music track data structure.
  69. offset -[in] Number of bytes from whence.
  70. whence -[in] Initial position.
  71. Returns:
  72. If successful, fseek returns 0. Otherwise, it returns a nonzero
  73. value.
  74. Notes:
  75. -----------------------------------------------------------------------------
  76. */
  77. PRIVATE int ovc_seek( void *datasource, ogg_int64_t offset, int whence )
  78. {
  79. musicTrack_t *track = (musicTrack_t *)datasource;
  80. return FS_FileSeek( track->hFile, offset, whence );
  81. }
  82. /*
  83. -----------------------------------------------------------------------------
  84. Function: ovc_close -OGG close Callback. Closes a stream.
  85. Parameters: datasource -[in] music track data structure.
  86. Returns: 0 if the stream is successfully closed, otherwise nonzero.
  87. Notes:
  88. -----------------------------------------------------------------------------
  89. */
  90. PRIVATE int ovc_close( void *datasource )
  91. {
  92. return 0;
  93. }
  94. /*
  95. -----------------------------------------------------------------------------
  96. Function: ovc_tell -OGG tell Callback. Gets the current position of a file
  97. pointer.
  98. Parameters: datasource -[in] music track data structure.
  99. Returns: The current file position.
  100. Notes:
  101. -----------------------------------------------------------------------------
  102. */
  103. PRIVATE long ovc_tell( void *datasource )
  104. {
  105. musicTrack_t *track = (musicTrack_t *)datasource;
  106. return FS_FileTell( track->hFile );
  107. }
  108. /*
  109. -----------------------------------------------------------------------------
  110. Function: Sound_OpenBGTrack -OGG read Callback.
  111. Parameters:
  112. name -[in] File name to open.
  113. track -[in/out] Music track data structure.
  114. Returns: False on error, otherwise true.
  115. Notes:
  116. -----------------------------------------------------------------------------
  117. */
  118. PRIVATE _boolean Sound_OpenBGTrack( const char *name, musicTrack_t *track )
  119. {
  120. OggVorbis_File *vorbisFile;
  121. vorbis_info *vorbisInfo;
  122. ov_callbacks vorbisCallbacks = {ovc_read, ovc_seek, ovc_close, ovc_tell};
  123. int ret;
  124. extern cvar_t *music;
  125. if ( music->value == 0 ) {
  126. return 0;
  127. }
  128. track->hFile = FS_OpenFile( name, 0 );
  129. if( ! track->hFile )
  130. {
  131. return false;
  132. }
  133. track->vorbisFile = vorbisFile = Z_Malloc( sizeof( OggVorbis_File ) );
  134. if( (ret = ov_open_callbacks( track, vorbisFile, NULL, 0, vorbisCallbacks )) < 0 )
  135. {
  136. switch( ret )
  137. {
  138. case OV_EREAD:
  139. Com_DPrintf( "A read from media returned an error.(%s)\n", name );
  140. break;
  141. case OV_ENOTVORBIS:
  142. Com_DPrintf( "Bitstream is not Vorbis data.(%s)\n", name );
  143. break;
  144. case OV_EVERSION:
  145. Com_DPrintf( "Vorbis version mismatch.(%s)\n", name );
  146. break;
  147. case OV_EBADHEADER:
  148. Com_DPrintf( "Invalid Vorbis bitstream header.(%s)\n", name );
  149. break;
  150. case OV_EFAULT:
  151. Com_DPrintf( "Internal logic fault; indicates a bug or heap/stack corruption.(%s)\n", name );
  152. break;
  153. }
  154. Com_DPrintf( "Could not open OGG stream (%s)\n", name );
  155. return false;
  156. }
  157. vorbisInfo = ov_info( vorbisFile, -1 );
  158. if( vorbisInfo->channels != 1 && vorbisInfo->channels != 2 )
  159. {
  160. Com_DPrintf( "Only mono and stereo OGG files supported (%s)\n", name );
  161. return false;
  162. }
  163. track->start = ov_raw_tell( vorbisFile );
  164. track->rate = vorbisInfo->rate;
  165. track->format = (vorbisInfo->channels == 2) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
  166. return true;
  167. }
  168. /*
  169. -----------------------------------------------------------------------------
  170. Function: Sound_CloseBGTrack -Close out background music track.
  171. Parameters: track -[in] Music track to close.
  172. Returns: Nothing
  173. Notes:
  174. -----------------------------------------------------------------------------
  175. */
  176. PRIVATE void Sound_CloseBGTrack( musicTrack_t *track )
  177. {
  178. if( track->vorbisFile )
  179. {
  180. ov_clear( track->vorbisFile );
  181. Z_Free( track->vorbisFile );
  182. track->vorbisFile = NULL;
  183. }
  184. if( track->hFile )
  185. {
  186. FS_CloseFile( track->hFile );
  187. }
  188. }
  189. /*
  190. -----------------------------------------------------------------------------
  191. Function: Sound_StreamBGTrack -Called each frame to update streaming music
  192. track.
  193. Parameters: Nothing
  194. Returns: Nothing
  195. Notes:
  196. -----------------------------------------------------------------------------
  197. */
  198. PUBLIC void Sound_StreamBGTrack( void )
  199. {
  200. W8 data[BUFFER_SIZE];
  201. int processed, queued, state;
  202. int size, read, dummy;
  203. unsigned buffer;
  204. if( ! s_musicVolume->value )
  205. {
  206. return;
  207. }
  208. if( ! s_streamingChannel )
  209. {
  210. return;
  211. }
  212. // Unqueue and delete any processed buffers
  213. pfalGetSourcei( s_streamingChannel->sourceName, AL_BUFFERS_PROCESSED, &processed );
  214. if( processed > 0 )
  215. {
  216. while (processed--)
  217. {
  218. pfalSourceUnqueueBuffers( s_streamingChannel->sourceName, 1, &buffer );
  219. pfalDeleteBuffers( 1, &buffer );
  220. }
  221. }
  222. // Make sure we always have at least 4 buffers in the queue
  223. pfalGetSourcei( s_streamingChannel->sourceName, AL_BUFFERS_QUEUED, &queued );
  224. while( queued < 4 )
  225. {
  226. size = 0;
  227. // Stream from disk
  228. while( size < BUFFER_SIZE )
  229. {
  230. read = ov_read( bgTrack.vorbisFile, (char *)data + size, BUFFER_SIZE - size, &dummy );
  231. if( read == 0 )
  232. {
  233. // End of file
  234. if( ! bgTrack.looping)
  235. {
  236. // Close the intro track
  237. Sound_CloseBGTrack( &bgTrack );
  238. // Open the loop track
  239. if( ! Sound_OpenBGTrack( bgTrack.loopName, &bgTrack ) )
  240. {
  241. Sound_StopBGTrack();
  242. return;
  243. }
  244. bgTrack.looping = true;
  245. }
  246. // Restart the track, skipping over the header
  247. ov_raw_seek( bgTrack.vorbisFile, (ogg_int64_t)bgTrack.start );
  248. // Try streaming again
  249. read = ov_read( bgTrack.vorbisFile, (char *)data + size, BUFFER_SIZE - size, &dummy );
  250. }
  251. if( read <= 0 )
  252. {
  253. // An error occurred
  254. Sound_StopBGTrack();
  255. return;
  256. }
  257. size += read;
  258. }
  259. // Upload and queue the new buffer
  260. pfalGenBuffers( 1, &buffer );
  261. pfalBufferData( buffer, bgTrack.format, data, size, bgTrack.rate );
  262. pfalSourceQueueBuffers( s_streamingChannel->sourceName, 1, &buffer );
  263. queued++;
  264. }
  265. // Update volume
  266. pfalSourcef( s_streamingChannel->sourceName, AL_GAIN, s_musicVolume->value );
  267. // If not playing, then do so
  268. pfalGetSourcei( s_streamingChannel->sourceName, AL_SOURCE_STATE, &state );
  269. if( state != AL_PLAYING )
  270. {
  271. pfalSourcePlay(s_streamingChannel->sourceName);
  272. }
  273. }
  274. /*
  275. -----------------------------------------------------------------------------
  276. Function: Sound_StartStreaming -Start streaming background music track.
  277. Parameters: Nothing
  278. Returns: Nothing
  279. Notes:
  280. -----------------------------------------------------------------------------
  281. */
  282. PUBLIC void Sound_StartStreaming( void )
  283. {
  284. if( ! sound_initialized )
  285. {
  286. return;
  287. }
  288. if( s_streamingChannel )
  289. {
  290. return;
  291. }
  292. s_streamingChannel = Sound_PickChannel( 0, 0 );
  293. if( ! s_streamingChannel )
  294. {
  295. return;
  296. }
  297. s_streamingChannel->streaming = true;
  298. // hmmm...
  299. pfalDeleteSources( 1, &s_streamingChannel->sourceName );
  300. pfalGenSources( 1, &s_streamingChannel->sourceName );
  301. // Set up the source
  302. pfalSourcei( s_streamingChannel->sourceName, AL_BUFFER, 0 );
  303. pfalSourcei( s_streamingChannel->sourceName, AL_LOOPING, AL_FALSE );
  304. pfalSourcei( s_streamingChannel->sourceName, AL_SOURCE_RELATIVE, AL_TRUE );
  305. pfalSourcefv( s_streamingChannel->sourceName, AL_POSITION, vec3_origin );
  306. pfalSourcefv( s_streamingChannel->sourceName, AL_VELOCITY, vec3_origin );
  307. pfalSourcef( s_streamingChannel->sourceName, AL_REFERENCE_DISTANCE, 1.0 );
  308. pfalSourcef( s_streamingChannel->sourceName, AL_MAX_DISTANCE, 1.0 );
  309. pfalSourcef( s_streamingChannel->sourceName, AL_ROLLOFF_FACTOR, 0.0 );
  310. }
  311. /*
  312. -----------------------------------------------------------------------------
  313. Function: Sound_StopStreaming -Stop playing streaming music track.
  314. Parameters: Nothing
  315. Returns: Nothing
  316. Notes:
  317. -----------------------------------------------------------------------------
  318. */
  319. PUBLIC void Sound_StopStreaming( void )
  320. {
  321. int processed;
  322. unsigned buffer;
  323. if( ! sound_initialized )
  324. {
  325. return;
  326. }
  327. if( ! s_streamingChannel )
  328. {
  329. return;
  330. }
  331. s_streamingChannel->streaming = false;
  332. pfalSourceStop( s_streamingChannel->sourceName );
  333. pfalGetSourcei( s_streamingChannel->sourceName, AL_BUFFERS_PROCESSED, &processed );
  334. if( processed > 0 )
  335. {
  336. while( processed-- )
  337. {
  338. pfalSourceUnqueueBuffers( s_streamingChannel->sourceName, 1, &buffer );
  339. pfalDeleteBuffers( 1, &buffer );
  340. }
  341. }
  342. pfalSourcei( s_streamingChannel->sourceName, AL_BUFFER, 0 );
  343. // hmmm...
  344. pfalDeleteSources( 1, &s_streamingChannel->sourceName );
  345. pfalGenSources( 1, &s_streamingChannel->sourceName );
  346. s_streamingChannel = NULL;
  347. }
  348. /*
  349. -----------------------------------------------------------------------------
  350. Function: Sound_StartBGTrack -Play background music track.
  351. Parameters:
  352. introTrack -[in] File name of intro track.
  353. loopTrack -[in] File name of loop track.
  354. Returns: Nothing
  355. Notes:
  356. -----------------------------------------------------------------------------
  357. */
  358. PUBLIC void Sound_StartBGTrack( const char *introTrack, const char *loopTrack )
  359. {
  360. if( ! sound_initialized )
  361. {
  362. return;
  363. }
  364. Sound_StopBGTrack();
  365. my_strlcpy( bgTrack.introName, introTrack, sizeof( bgTrack.introName ) );
  366. my_strlcpy( bgTrack.loopName, loopTrack, sizeof( bgTrack.loopName) );
  367. Sound_StartStreaming();
  368. if( ! Sound_OpenBGTrack( bgTrack.introName, &bgTrack ) )
  369. {
  370. Sound_StopBGTrack();
  371. return;
  372. }
  373. Sound_StreamBGTrack();
  374. }
  375. /*
  376. -----------------------------------------------------------------------------
  377. Function: Sound_StopBGTrack -Stop playing background track.
  378. Parameters: Nothing
  379. Returns: Nothing
  380. Notes:
  381. -----------------------------------------------------------------------------
  382. */
  383. PUBLIC void Sound_StopBGTrack( void )
  384. {
  385. if( ! sound_initialized )
  386. {
  387. return;
  388. }
  389. Sound_StopStreaming();
  390. Sound_CloseBGTrack( &bgTrack );
  391. memset( &bgTrack, 0, sizeof( musicTrack_t ) );
  392. }