2 * $Logfile: /Freespace2/code/Sound/AudioStr.cpp $
7 * Routines to stream large WAV files from disk
10 * Revision 1.4 2002/06/05 04:03:33 relnev
11 * finished cfilesystem.
13 * removed some old code.
15 * fixed mouse save off-by-one.
19 * Revision 1.3 2002/05/27 18:42:50 theoddone33
20 * Fix missing audiostr_* symbols
22 * Revision 1.2 2002/05/07 03:16:52 theoddone33
23 * The Great Newline Fix
25 * Revision 1.1.1.1 2002/05/03 03:28:10 root
29 * 5 9/14/99 1:32a Jimb
30 * Commented out Int3() that was hanging Jim's machine. Happens before
31 * sm2-07 command brief.
33 * 4 7/14/99 12:09p Jefff
34 * Make sure we're not servicing a bogus audiostream. Check for "used"
35 * after the critical section lock.
37 * 3 12/17/98 4:01p Andsager
38 * up wavedata buffer size to 180000 to allow stereo 16b/22KHz streaming
40 * 2 10/07/98 10:53a Dave
43 * 1 10/07/98 10:51a Dave
45 * 53 6/28/98 6:35p Lawrance
46 * move re-entrancy semaphore into audiostream class
48 * 52 5/24/98 4:42p Dan
49 * AL: Fix several bugs related to pausing and enabling/disabling event
52 * 51 5/21/98 11:57a Lawrance
53 * fix potential bug with transitions for music when in packfiles
55 * 50 5/15/98 9:09p Lawrance
56 * The last of the multi-threading fixes
58 * 49 5/15/98 7:57p Duncan
59 * AL: Fix race condition with music streaming
61 * 48 5/15/98 10:13a Lawrance
62 * remove unused audiostream member
64 * 47 5/14/98 5:45p Lawrance2
65 * Put critical section around audiostream destroying
67 * 46 5/12/98 5:40p Lawrance
68 * Add critical section code to the service buffer call.. since it is
69 * possible to release buffers while in this call
71 * 45 5/10/98 3:49p Sandeep
72 * Fix problem with having the audio streaming while trying to close down
75 * 44 4/30/98 4:53p John
76 * Restructured and cleaned up cfile code. Added capability to read off
77 * of CD-ROM drive and out of multiple pack files.
79 * 43 4/26/98 3:30a Lawrance
80 * Fix a couple of potential bugs
82 * 42 4/21/98 10:18a Dan
84 * 41 4/17/98 6:59a John
85 * Changed code the used 'new' and 'delete' to use 'malloc' and 'free'
86 * instead. Had to manually can constructors/destructors.
88 * 40 4/13/98 10:18a John
91 * 39 4/13/98 10:16a John
92 * Switched gettime back to timer_get_milliseconds, which is now thread
95 * 38 4/12/98 11:08p Lawrance
96 * switch back to using gettime() in separate threads
98 * 37 4/12/98 5:31p Lawrance
99 * use timer_get_milliseconds() instead of gettime()
101 * 36 4/06/98 12:36a Lawrance
102 * Ensure all non-music ADPCM files get decompressed to 8 bit.
104 * 35 4/03/98 4:56p Lawrance
105 * Upu the max audio streams to 30
107 * 34 3/31/98 4:50p Dan
108 * AL: Clean up all audio streams if necessary in
109 * event_music_level_close()
111 * 33 3/23/98 4:12p Lawrance
112 * Fix subtle bug with looping and fading out songs
114 * 32 2/18/98 5:49p Lawrance
115 * Even if the ADPCM codec is unavailable, allow game to continue.
117 * 31 2/15/98 4:43p Lawrance
118 * work on real-time voice
120 * 30 1/19/98 11:37p Lawrance
121 * Fixing Optimization build warnings
123 * 29 1/17/98 4:41p Lawrance
124 * Fix problem with multiple audio streams using the same buffers
126 * 28 1/16/98 11:49a Lawrance
127 * Use own internal timer for fading.
129 * 27 12/28/97 12:43p John
130 * Put in support for reading archive files; Made missionload use the
131 * cf_get_file_list function. Moved demos directory out of data tree.
133 * 26 12/27/97 8:08p Lawrance
134 * If an audiostream doesn't exists, it can't be playing
136 * 25 12/18/97 3:30p Lawrance
137 * Fix bug that sometimes caused music with no volume to not get stopped
140 * 24 12/17/97 10:17p Allender
141 * redid streadming code to use mmio* functions instead of cf* functions.
142 * Our functions are not reentrant!
144 * 23 12/10/97 10:04p Lawrance
145 * modify what happens in Audio_stream constructor
147 * 22 12/09/97 6:14p Lawrance
150 * 21 12/08/97 6:21p Lawrance
151 * fix problems with signaling that end-of-file has been reached
153 * 20 12/05/97 10:50a Lawrance
154 * improve how silence bytes are written on transitions
156 * 19 12/04/97 5:35p Lawrance
157 * fix bug that may have caused errors when writing silence
159 * 18 11/28/97 2:09p Lawrance
160 * Overhaul how ADPCM conversion works... use much less memory... safer
163 * 17 10/03/97 8:24a Lawrance
164 * When unpausing, be sure to retain looping status
166 * 16 9/24/97 5:30p Lawrance
167 * fix bug that was messing up streaming of 8 bit audio
169 * 15 9/18/97 10:31p Lawrance
170 * add functions to pause and unpause all audio streams
172 * 14 9/09/97 3:39p Sandeep
173 * warning level 4 bugs
185 #include <mmsystem.h>
190 #include "audiostr.h"
191 #include "cfile.h" // needed for cf_get_path
193 #include "sound.h" /* for Snd_sram */
199 #define SUCCESS TRUE // Error returns for all member functions
200 #define FAILURE FALSE
205 typedef BOOL (*TIMERCALLBACK)(DWORD);
207 #define BIGBUF_SIZE 180000 // This can be reduced to 88200 once we don't use any stereo
208 //#define BIGBUF_SIZE 88300 // This can be reduced to 88200 once we don't use any stereo
209 unsigned char *Wavedata_load_buffer = NULL; // buffer used for cueing audiostreams
210 unsigned char *Wavedata_service_buffer = NULL; // buffer used for servicing audiostreams
212 CRITICAL_SECTION Global_service_lock;
214 #define COMPRESSED_BUFFER_SIZE 88300
215 unsigned char *Compressed_buffer = NULL; // Used to load in compressed data during a cueing interval
216 unsigned char *Compressed_service_buffer = NULL; // Used to read in compressed data during a service interval
218 #define AS_HIGHEST_MAX 999999999 // max uncompressed filesize supported is 999 meg
224 // Wrapper class for Windows multimedia timer services. Provides
225 // both periodic and one-shot events. User must supply callback
226 // for periodic events.
232 void constructor(void);
233 void destructor(void);
234 BOOL Create (UINT nPeriod, UINT nRes, DWORD dwUser, TIMERCALLBACK pfnCallback);
236 static void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);
237 TIMERCALLBACK m_pfnCallback;
249 // WAV file class (read-only).
262 BOOL Open (LPSTR pszFilename);
264 int Read (BYTE * pbDest, UINT cbSize, int service=1);
265 UINT GetNumBytesRemaining (void) { return (m_nDataSize - m_nBytesPlayed); }
266 UINT GetUncompressedAvgDataRate (void) { return (m_nUncompressedAvgDataRate); }
267 UINT GetDataSize (void) { return (m_nDataSize); }
268 UINT GetNumBytesPlayed (void) { return (m_nBytesPlayed); }
269 BYTE GetSilenceData (void);
270 WAVEFORMATEX m_wfmt; // format of wave file used by Direct Sound
271 WAVEFORMATEX * m_pwfmt_original; // foramt of wave file from actual wave source
272 UINT m_total_uncompressed_bytes_read;
273 UINT m_max_uncompressed_bytes_to_read;
274 UINT m_bits_per_sample_uncompressed;
277 UINT m_data_offset; // number of bytes to actual wave data
278 int m_data_bytes_left;
281 UINT m_wave_format; // format of wave source (ie WAVE_FORMAT_PCM, WAVE_FORMAT_ADPCM)
282 UINT m_nBlockAlign; // wave data block alignment spec
283 UINT m_nUncompressedAvgDataRate; // average wave data rate
284 UINT m_nDataSize; // size of data chunk
285 UINT m_nBytesPlayed; // offset into data chunk
286 BOOL m_abort_next_read;
288 HACMSTREAM m_hStream;
290 WAVEFORMATEX m_wfxDest;
295 // AudioStreamServices
297 // DirectSound apportions services on a per-window basis to allow
298 // sound from background windows to be muted. The AudioStreamServices
299 // class encapsulates the initialization of DirectSound services.
301 // Each window that wants to create AudioStream objects must
302 // first create and initialize an AudioStreamServices object.
303 // All AudioStream objects must be destroyed before the associated
304 // AudioStreamServices object is destroyed.
305 class AudioStreamServices
308 void Constructor(void);
310 LPDIRECTSOUND GetPDS (void) { return m_pds; }
318 // Audio stream interface class for playing WAV files using DirectSound.
319 // Users of this class must create AudioStreamServices object before
320 // creating an AudioStream object.
336 BOOL Create (LPSTR pszFilename, AudioStreamServices * pass);
338 void Play (long volume, int looping);
339 int Is_Playing(){ return(m_fPlaying); }
340 int Is_Paused(){ return(m_bIsPaused); }
341 int Is_Past_Limit() { return m_bPastLimit; }
342 void Stop (int paused=0);
343 void Stop_and_Rewind (void);
344 void Fade_and_Destroy (void);
345 void Fade_and_Stop(void);
346 void Set_Volume(long vol);
349 void Set_Byte_Cutoff(unsigned int num_bytes_cutoff);
350 void Set_Default_Volume(long converted_volume) { m_lDefaultVolume = converted_volume; }
351 long Get_Default_Volume() { return m_lDefaultVolume; }
352 unsigned int Get_Bytes_Committed(void);
353 int Is_looping() { return m_bLooping; }
356 UINT m_bits_per_sample_uncompressed;
360 BOOL WriteWaveData (UINT cbSize, UINT* num_bytes_written,int service=1);
361 BOOL WriteSilence (UINT cbSize);
362 DWORD GetMaxWriteSize (void);
363 BOOL ServiceBuffer (void);
364 static BOOL TimerCallback (DWORD dwUser);
366 AudioStreamServices * m_pass; // ptr to AudioStreamServices object
367 LPDIRECTSOUNDBUFFER m_pdsb; // ptr to Direct Sound buffer
368 WaveFile * m_pwavefile; // ptr to WaveFile object
369 Timer m_timer; // ptr to Timer object
370 BOOL m_fCued; // semaphore (stream cued)
371 BOOL m_fPlaying; // semaphore (stream playing)
372 DSBUFFERDESC m_dsbd; // Direct Sound buffer description
373 LONG m_lInService; // reentrancy semaphore
374 UINT m_cbBufOffset; // last write position
375 UINT m_nBufLength; // length of sound buffer in msec
376 UINT m_cbBufSize; // size of sound buffer in bytes
377 UINT m_nBufService; // service interval in msec
378 UINT m_nTimeStarted; // time (in system time) playback started
380 BOOL m_bLooping; // whether or not to loop playback
381 BOOL m_bFade; // fade out music
382 BOOL m_bDestroy_when_faded;
383 LONG m_lVolume; // volume of stream ( 0 -> -10 000 )
384 LONG m_lCutoffVolume;
385 BOOL m_bIsPaused; // stream is stopped, but not rewinded
386 UINT m_silence_written; // number of bytes of silence written to buffer
387 UINT m_bReadingDone; // no more bytes to be read from disk, still have remaining buffer to play
388 DWORD m_fade_timer_id; // timestamp so we know when to start fade
389 DWORD m_finished_id; // timestamp so we know when we've played #bytes required
390 BOOL m_bPastLimit; // flag to show we've played past the number of bytes requred
391 LONG m_lDefaultVolume;
394 CRITICAL_SECTION write_lock;
398 // AudioStreamServices class implementation
400 ////////////////////////////////////////////////////////////
403 void AudioStreamServices::Constructor(void)
405 // Initialize member data
408 // It would seem to make sense to initialize DirectSound here,
409 // but because there could be an error, it's best done in a
410 // separate member function, ::Initialize.
414 extern LPDIRECTSOUND pDirectSound; // From Sound.cpp
418 BOOL AudioStreamServices::Initialize ()
421 BOOL fRtn = SUCCESS; // assume success
424 m_pds = pDirectSound;
433 // AudioStream class implementation
435 ////////////////////////////////////////////////////////////
437 // The following constants are the defaults for our streaming buffer operation.
438 const UINT DefBufferLength = 2000; // default buffer length in msec
439 const UINT DefBufferServiceInterval = 250; // default buffer service interval in msec
442 AudioStream::AudioStream (void)
444 InitializeCriticalSection( &write_lock );
449 AudioStream::~AudioStream (void)
451 DeleteCriticalSection( &write_lock );
455 void AudioStream::Init_Data ()
461 m_bPastLimit = FALSE;
463 m_bDestroy_when_faded = FALSE;
465 m_lCutoffVolume = -10000;
467 m_silence_written = 0;
468 m_bReadingDone = FALSE;
472 m_fPlaying = m_fCued = FALSE;
473 m_lInService = FALSE;
475 m_nBufLength = DefBufferLength;
477 m_nBufService = DefBufferServiceInterval;
482 BOOL AudioStream::Create (LPSTR pszFilename, AudioStreamServices * pass)
484 BOOL fRtn = SUCCESS; // assume success
492 if (pszFilename && m_pass) {
493 // Create a new WaveFile object
495 m_pwavefile = (WaveFile *)malloc(sizeof(WaveFile));
502 m_pwavefile->m_bits_per_sample_uncompressed = m_bits_per_sample_uncompressed;
503 if (m_pwavefile->Open (pszFilename)) {
504 // Calculate sound buffer size in bytes
505 // Buffer size is average data rate times length of buffer
506 // No need for buffer to be larger than wave data though
507 m_cbBufSize = (m_pwavefile->GetUncompressedAvgDataRate () * m_nBufLength) / 1000;
508 nprintf(("SOUND", "SOUND => Stream buffer created using %d bytes\n", m_cbBufSize));
509 // m_cbBufSize = (m_cbBufSize > m_pwavefile->GetDataSize ()) ? m_pwavefile->GetDataSize () : m_cbBufSize;
511 //nprintf(("Sound", "SOUND => average data rate = %d\n\r", m_pwavefile->GetUncompressedAvgDataRate ()));
512 //nprintf(("Sound", "SOUND => m_cbBufSize = %d\n\r", m_cbBufSize));
514 // Create sound buffer
516 memset (&m_dsbd, 0, sizeof (DSBUFFERDESC));
517 m_dsbd.dwSize = sizeof (DSBUFFERDESC);
518 m_dsbd.dwBufferBytes = m_cbBufSize;
519 m_dsbd.lpwfxFormat = &m_pwavefile->m_wfmt;
520 m_dsbd.dwFlags = DSBCAPS_STATIC | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_LOCSOFTWARE;
522 hr = (m_pass->GetPDS ())->CreateSoundBuffer (&m_dsbd, &m_pdsb, NULL);
526 Snd_sram += m_cbBufSize;
529 // Error, unable to create DirectSound buffer
530 nprintf(("Sound", "SOUND => Error, unable to create DirectSound buffer\n\r"));
531 if (hr == DSERR_BADFORMAT) {
532 nprintf(("Sound", "SOUND => Bad format (probably ADPCM)\n\r"));
539 // Error opening file
540 nprintf(("SOUND", "SOUND => Failed to open wave file: %s\n\r", pszFilename));
541 m_pwavefile->Close();
548 // Error, unable to create WaveFile object
549 nprintf(("Sound", "SOUND => Failed to create WaveFile object %s\n\r", pszFilename));
554 // Error, passed invalid parms
563 BOOL AudioStream::Destroy (void)
567 EnterCriticalSection(&write_lock);
572 // Release DirectSound buffer
576 Snd_sram -= m_cbBufSize;
579 // Delete WaveFile object
581 m_pwavefile->Close();
588 LeaveCriticalSection(&write_lock);
595 // Writes wave data to sound buffer. This is a helper method used by Create and
596 // ServiceBuffer; it's not exposed to users of the AudioStream class.
597 BOOL AudioStream::WriteWaveData (UINT size, UINT *num_bytes_written, int service)
600 LPBYTE lpbuf1 = NULL;
601 LPBYTE lpbuf2 = NULL;
604 DWORD dwbyteswritten1 = 0;
605 DWORD dwbyteswritten2 = 0;
607 unsigned char *uncompressed_wave_data;
609 *num_bytes_written = 0;
611 if ( size == 0 || m_bReadingDone ) {
615 if ( !m_pdsb || !m_pwavefile ) {
620 EnterCriticalSection(&Global_service_lock);
624 uncompressed_wave_data = Wavedata_service_buffer;
626 uncompressed_wave_data = Wavedata_load_buffer;
629 int num_bytes_read = 0;
631 // Lock the sound buffer
632 hr = m_pdsb->Lock (m_cbBufOffset, size, (void**)(&lpbuf1), &dwsize1, (void**)(&lpbuf2), &dwsize2, 0);
634 // Write data to sound buffer. Because the sound buffer is circular, we may have to
635 // do two write operations if locked portion of buffer wraps around to start of buffer.
638 num_bytes_read = m_pwavefile->Read(uncompressed_wave_data, dwsize1+dwsize2,service);
639 if ( num_bytes_read == -1 ) {
640 // means nothing left to read!
645 if ( num_bytes_read > 0 ) {
646 if ( (unsigned int)num_bytes_read > dwsize1 ) {
647 dwbyteswritten1 = dwsize1;
648 dwbyteswritten2 = num_bytes_read - dwsize1;
650 memcpy(lpbuf1, uncompressed_wave_data, dwsize1);
652 memcpy(lpbuf2, uncompressed_wave_data+dwsize1, num_bytes_read-dwsize1);
654 dwbyteswritten1 = num_bytes_read;
656 memcpy(lpbuf1, uncompressed_wave_data, num_bytes_read);
660 // Update our buffer offset and unlock sound buffer
661 m_cbBufOffset = (m_cbBufOffset + dwbyteswritten1 + dwbyteswritten2) % m_cbBufSize;
662 *num_bytes_written = dwbyteswritten1 + dwbyteswritten2;
663 m_pdsb->Unlock (lpbuf1, dwsize1, lpbuf2, dwsize2);
666 // Error locking sound buffer
667 nprintf(("SOUND", "SOUND ==> Error, unable to lock sound buffer in AudioStr\n"));
672 LeaveCriticalSection(&Global_service_lock);
681 // Writes silence to sound buffer. This is a helper method used by
682 // ServiceBuffer; it's not exposed to users of the AudioStream class.
683 BOOL AudioStream::WriteSilence (UINT size)
686 LPBYTE lpbuf1 = NULL;
687 LPBYTE lpbuf2 = NULL;
690 DWORD dwbyteswritten1 = 0;
691 DWORD dwbyteswritten2 = 0;
694 // Lock the sound buffer
695 hr = m_pdsb->Lock (m_cbBufOffset, size, (void**)(&lpbuf1), &dwsize1, (void**)(&lpbuf2), &dwsize2, 0);
698 // Get silence data for this file format. Although word sizes vary for different
699 // wave file formats, ::Lock will always return pointers on word boundaries.
700 // Because silence data for 16-bit PCM formats is 0x0000 or 0x00000000, we can
701 // get away with writing bytes and ignoring word size here.
702 BYTE bSilence = m_pwavefile->GetSilenceData ();
704 // Write silence to sound buffer. Because the sound buffer is circular, we may have to
705 // do two write operations if locked portion of buffer wraps around to start of buffer.
706 memset (lpbuf1, bSilence, dwsize1);
707 dwbyteswritten1 = dwsize1;
709 // Second write required?
711 memset (lpbuf2, bSilence, dwsize2);
712 dwbyteswritten2 = dwsize2;
715 // Update our buffer offset and unlock sound buffer
716 m_cbBufOffset = (m_cbBufOffset + dwbyteswritten1 + dwbyteswritten2) % m_cbBufSize;
717 // m_pdsb->Unlock (lpbuf1, dwbyteswritten1, lpbuf2, dwbyteswritten2);
718 m_pdsb->Unlock (lpbuf1, dwsize1, lpbuf2, dwsize2);
721 // Error locking sound buffer
722 nprintf(("SOUND", "SOUND ==> Error, unable to lock sound buffer in AudioStr\n"));
732 // Helper function to calculate max size of sound buffer write operation, i.e. how much
733 // free space there is in buffer.
734 DWORD AudioStream::GetMaxWriteSize (void)
736 DWORD dwWriteCursor, dwPlayCursor, dwMaxSize;
738 // Get current play position
739 if (m_pdsb->GetCurrentPosition (&dwPlayCursor, &dwWriteCursor) == DS_OK) {
740 if (m_cbBufOffset <= dwPlayCursor) {
741 // Our write position trails play cursor
742 dwMaxSize = dwPlayCursor - m_cbBufOffset;
745 else {// (m_cbBufOffset > dw7Cursor)
746 // Play cursor has wrapped
747 dwMaxSize = m_cbBufSize - m_cbBufOffset + dwPlayCursor;
751 // GetCurrentPosition call failed
756 // nprintf(("Alan","Max write size: %d\n", dwMaxSize));
763 // Routine to service buffer requests initiated by periodic timer.
765 // Returns TRUE if buffer serviced normally; otherwise returns FALSE.
766 #define FADE_VOLUME_INTERVAL 400 // 100 == 1db
767 #define VOLUME_ATTENUATION_BEFORE_CUTOFF 3000 // 12db
768 BOOL AudioStream::ServiceBuffer (void)
773 if ( status != ASF_USED )
776 EnterCriticalSection(&write_lock);
778 // status may have changed, so lets check once again
779 if ( status != ASF_USED ){
780 LeaveCriticalSection(&write_lock);
784 // Check for reentrance
785 if (InterlockedExchange (&m_lInService, TRUE) == FALSE) {
786 if ( m_bFade == TRUE ) {
787 if ( m_lCutoffVolume == -10000 ) {
789 // nprintf(("Alan","Volume is: %d\n",vol));
790 m_lCutoffVolume = max(vol - VOLUME_ATTENUATION_BEFORE_CUTOFF, -10000);
794 vol = vol - FADE_VOLUME_INTERVAL; // decrease by 1db
795 // nprintf(("Alan","Volume is now: %d\n",vol));
798 // nprintf(("Sound","SOUND => Volume for stream sound is %d\n",vol));
799 // nprintf(("Alan","Cuttoff Volume is: %d\n",m_lCutoffVolume));
800 if ( vol < m_lCutoffVolume ) {
802 m_lCutoffVolume = -10000;
803 if ( m_bDestroy_when_faded == TRUE ) {
804 LeaveCriticalSection(&write_lock);
806 // Reset reentrancy semaphore
807 InterlockedExchange (&m_lInService, FALSE);
812 // Reset reentrancy semaphore
813 LeaveCriticalSection(&write_lock);
814 InterlockedExchange (&m_lInService, FALSE);
820 // All of sound not played yet, send more data to buffer
821 DWORD dwFreeSpace = GetMaxWriteSize ();
823 // Determine free space in sound buffer
826 // Some wave data remains, but not enough to fill free space
827 // Send wave data to buffer, fill remainder of free space with silence
828 uint num_bytes_written;
830 if (WriteWaveData (dwFreeSpace, &num_bytes_written) == SUCCESS) {
831 // nprintf(("Alan","Num bytes written: %d\n", num_bytes_written));
833 if ( m_pwavefile->m_total_uncompressed_bytes_read >= m_pwavefile->m_max_uncompressed_bytes_to_read ) {
834 m_fade_timer_id = timer_get_milliseconds() + 1700; // start fading 1.7 seconds from now
835 m_finished_id = timer_get_milliseconds() + 2000; // 2 seconds left to play out buffer
836 m_pwavefile->m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
839 if ( (m_fade_timer_id>0) && ((uint)timer_get_milliseconds() > m_fade_timer_id) ) {
844 if ( (m_finished_id>0) && ((uint)timer_get_milliseconds() > m_finished_id) ) {
849 if ( (num_bytes_written < dwFreeSpace) && m_bReadingDone ) {
850 int num_bytes_silence;
851 num_bytes_silence = dwFreeSpace - num_bytes_written;
853 if ( num_bytes_silence > 0 ) {
855 m_silence_written += num_bytes_silence;
856 if (WriteSilence (num_bytes_silence) == FAILURE) {
861 if ( m_silence_written >= m_cbBufSize ) {
862 m_silence_written = 0;
864 if ( m_bDestroy_when_faded == TRUE ) {
865 LeaveCriticalSection(&write_lock);
867 // Reset reentrancy semaphore
868 InterlockedExchange (&m_lInService, FALSE);
872 // All of sound has played, stop playback or loop again
873 if ( m_bLooping && !m_bFade) {
874 Play(m_lVolume, m_bLooping);
884 // Error writing wave data
890 // Reset reentrancy semaphore
891 InterlockedExchange (&m_lInService, FALSE);
893 // Service routine reentered. Do nothing, just return
897 LeaveCriticalSection(&write_lock);
902 void AudioStream::Cue (void)
904 UINT num_bytes_written;
910 m_bPastLimit = FALSE;
912 m_lCutoffVolume = -10000;
914 m_bDestroy_when_faded = FALSE;
919 // Reset file ptr, etc
922 // Reset DirectSound buffer
923 m_pdsb->SetCurrentPosition (0);
925 // Fill buffer with wave data
926 WriteWaveData (m_cbBufSize, &num_bytes_written,0);
934 void AudioStream::Play (long volume, int looping)
939 if ( m_bIsPaused == FALSE)
943 // Cue for playback if necessary
953 // Begin DirectSound playback
954 HRESULT hr = m_pdsb->Play (0, 0, DSBPLAY_LOOPING);
956 m_nTimeStarted = timer_get_milliseconds();
958 // Kick off timer to service buffer
959 m_timer.constructor();
961 m_timer.Create (m_nBufService, m_nBufService, DWORD (this), TimerCallback);
963 // Playback begun, no longer cued
968 // If the buffer was lost, try to restore it
969 if ( hr == DSERR_BUFFERLOST ) {
970 hr = m_pdsb->Restore();
972 hr = m_pdsb->Play (0, 0, DSBPLAY_LOOPING);
975 nprintf(("Sound", "Sound => Lost a buffer, tried restoring but got %s\n", get_DSERR_text(hr) ));
976 Int3(); // get Alan, he wants to see this
981 nprintf(("Sound", "Sound => Play failed with return value %s\n", get_DSERR_text(hr) ));
987 // Timer callback for Timer object created by ::Play method.
988 BOOL AudioStream::TimerCallback (DWORD dwUser)
990 // dwUser contains ptr to AudioStream object
991 AudioStream * pas = (AudioStream *) dwUser;
993 return (pas->ServiceBuffer ());
996 void AudioStream::Set_Byte_Cutoff(unsigned int byte_cutoff)
998 if ( m_pwavefile == NULL )
1001 m_pwavefile->m_max_uncompressed_bytes_to_read = byte_cutoff;
1004 unsigned int AudioStream::Get_Bytes_Committed(void)
1006 if ( m_pwavefile == NULL )
1009 return m_pwavefile->m_total_uncompressed_bytes_read;
1014 void AudioStream::Fade_and_Destroy (void)
1017 m_bDestroy_when_faded = TRUE;
1021 void AudioStream::Fade_and_Stop (void)
1024 m_bDestroy_when_faded = FALSE;
1029 void AudioStream::Stop(int paused)
1032 // Stop DirectSound playback
1035 m_bIsPaused = paused;
1037 // Delete Timer object
1038 m_timer.destructor();
1043 void AudioStream::Stop_and_Rewind (void)
1046 // Stop DirectSound playback
1049 // Delete Timer object
1050 m_timer.destructor();
1055 m_fCued = FALSE; // this will cause wave file to start from beginning
1056 m_bReadingDone = FALSE;
1060 void AudioStream::Set_Volume(long vol)
1068 Assert( vol >= -10000 && vol <= 0 );
1069 h_result = m_pdsb->SetVolume(vol);
1071 if ( h_result != DS_OK )
1072 nprintf(("Sound","SOUND => SetVolume() failed with code '%s'\n", get_DSERR_text(h_result) ));
1077 long AudioStream::Get_Volume()
1083 void Timer::constructor(void)
1090 void Timer::destructor(void)
1093 timeKillEvent (m_nIDTimer);
1100 BOOL Timer::Create (UINT nPeriod, UINT nRes, DWORD dwUser, TIMERCALLBACK pfnCallback)
1102 BOOL bRtn = SUCCESS; // assume success
1104 Assert(pfnCallback);
1105 Assert(nPeriod > 10);
1106 Assert(nPeriod >= nRes);
1108 m_nPeriod = nPeriod;
1111 m_pfnCallback = pfnCallback;
1113 if ((m_nIDTimer = timeSetEvent (m_nPeriod, m_nRes, TimeProc, (DWORD) this, TIME_PERIODIC)) == NULL) {
1121 // Timer proc for multimedia timer callback set with timeSetTime().
1123 // Calls procedure specified when Timer object was created. The
1124 // dwUser parameter contains "this" pointer for associated Timer object.
1126 void CALLBACK Timer::TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
1128 // dwUser contains ptr to Timer object
1129 Timer * ptimer = (Timer *) dwUser;
1131 // Call user-specified callback and pass back user specified data
1132 (ptimer->m_pfnCallback) (ptimer->m_dwUser);
1136 // WaveFile class implementation
1138 ////////////////////////////////////////////////////////////
1141 void WaveFile::Init(void)
1143 // Init data members
1146 m_pwfmt_original = NULL;
1148 m_nUncompressedAvgDataRate = 0;
1151 m_total_uncompressed_bytes_read = 0;
1152 m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
1155 m_abort_next_read = FALSE;
1159 void WaveFile::Close(void)
1162 if (m_pwfmt_original) {
1163 free(m_pwfmt_original);
1164 m_pwfmt_original = NULL;
1167 if ( m_hStream_open ) {
1168 ACM_stream_close((void*)m_hStream);
1175 mmioClose( cfp, 0 );
1182 BOOL WaveFile::Open (LPSTR pszFilename)
1186 BOOL fRtn = SUCCESS; // assume success
1187 PCMWAVEFORMAT pcmwf;
1188 char fullpath[_MAX_PATH];
1190 m_total_uncompressed_bytes_read = 0;
1191 m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
1193 int FileSize, FileOffset;
1195 if ( !cf_find_file_location(pszFilename, CF_TYPE_ANY, fullpath, &FileSize, &FileOffset )) {
1199 cfp = mmioOpen(fullpath, NULL, MMIO_ALLOCBUF | MMIO_READ);
1200 if ( cfp == NULL ) {
1204 // Skip the "RIFF" tag and file size (8 bytes)
1205 // Skip the "WAVE" tag (4 bytes)
1206 mmioSeek( cfp, 12+FileOffset, SEEK_SET );
1208 // Now read RIFF tags until the end of file
1209 uint tag, size, next_chunk;
1211 while(done == FALSE) {
1212 if ( mmioRead(cfp, (char *)&tag, sizeof(uint)) != sizeof(uint) )
1215 if ( mmioRead(cfp, (char *)&size, sizeof(uint)) != sizeof(uint) )
1218 next_chunk = mmioSeek( cfp, 0, SEEK_CUR );
1222 case 0x20746d66: // The 'fmt ' tag
1223 mmioRead( cfp, (char *)&pcmwf, sizeof(PCMWAVEFORMAT) );
1224 if ( pcmwf.wf.wFormatTag != WAVE_FORMAT_PCM ) {
1225 mmioRead( cfp, (char *)&cbExtra, sizeof(short) );
1228 // Allocate memory for WAVEFORMATEX structure + extra bytes
1229 if ( (m_pwfmt_original = (WAVEFORMATEX *) malloc ( sizeof(WAVEFORMATEX)+cbExtra )) != NULL ){
1230 Assert(m_pwfmt_original != NULL);
1231 // Copy bytes from temporary format structure
1232 memcpy (m_pwfmt_original, &pcmwf, sizeof(pcmwf));
1233 m_pwfmt_original->cbSize = cbExtra;
1235 // Read those extra bytes, append to WAVEFORMATEX structure
1237 mmioRead( cfp, (char *)((ubyte *)(m_pwfmt_original) + sizeof(WAVEFORMATEX)), cbExtra );
1241 Int3(); // malloc failed
1246 case 0x61746164: // the 'data' tag
1247 m_nDataSize = size; // This is size of data chunk. Compressed if ADPCM.
1248 m_data_bytes_left = size;
1249 m_data_offset = mmioSeek( cfp, 0, SEEK_CUR);
1253 default: // unknown, skip it
1257 mmioSeek( cfp, next_chunk, SEEK_SET );
1260 // At this stage, examine source format, and set up WAVEFORATEX structure for DirectSound.
1261 // Since DirectSound only supports PCM, force this structure to be PCM compliant. We will
1262 // need to convert data on the fly later if our souce is not PCM
1263 switch ( m_pwfmt_original->wFormatTag ) {
1264 case WAVE_FORMAT_PCM:
1265 m_wave_format = WAVE_FORMAT_PCM;
1266 m_wfmt.wBitsPerSample = m_pwfmt_original->wBitsPerSample;
1269 case WAVE_FORMAT_ADPCM:
1270 m_wave_format = WAVE_FORMAT_ADPCM;
1271 m_wfmt.wBitsPerSample = 16;
1275 nprintf(("SOUND", "SOUND => Not supporting %d format for playing wave files\n"));
1282 // Set up the WAVEFORMATEX structure to have the right PCM characteristics
1283 m_wfmt.wFormatTag = WAVE_FORMAT_PCM;
1284 m_wfmt.nChannels = m_pwfmt_original->nChannels;
1285 m_wfmt.nSamplesPerSec = m_pwfmt_original->nSamplesPerSec;
1287 m_wfmt.nBlockAlign = (unsigned short)(( m_wfmt.nChannels * m_wfmt.wBitsPerSample ) / 8);
1288 m_wfmt.nAvgBytesPerSec = m_wfmt.nBlockAlign * m_wfmt.nSamplesPerSec;
1290 // Init some member data from format chunk
1291 m_nBlockAlign = m_pwfmt_original->nBlockAlign;
1292 m_nUncompressedAvgDataRate = m_wfmt.nAvgBytesPerSec;
1294 // Cue for streaming
1301 // Handle all errors here
1302 nprintf(("SOUND","SOUND ==> Could not open wave file %s for streaming\n",pszFilename));
1307 mmioClose( cfp, 0 );
1310 if (m_pwfmt_original)
1312 free(m_pwfmt_original);
1313 m_pwfmt_original = NULL;
1323 // Set the file pointer to the start of wave data
1325 BOOL WaveFile::Cue (void)
1327 BOOL fRtn = SUCCESS; // assume success
1330 m_total_uncompressed_bytes_read = 0;
1331 m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
1333 rval = mmioSeek( cfp, m_data_offset, SEEK_SET );
1338 m_data_bytes_left = m_nDataSize;
1339 m_abort_next_read = FALSE;
1347 // Returns number of bytes actually read.
1349 // Returns -1 if there is nothing more to be read. This function can return 0, since
1350 // sometimes the amount of bytes requested is too small for the ACM decompression to
1351 // locate a suitable block
1352 int WaveFile::Read(BYTE *pbDest, UINT cbSize, int service)
1354 unsigned char *dest_buf=NULL, *uncompressed_wave_data;
1355 int rc, uncompressed_bytes_written;
1356 unsigned int src_bytes_used, convert_len, num_bytes_desired=0, num_bytes_read;
1358 // nprintf(("Alan","Reqeusted: %d\n", cbSize));
1362 uncompressed_wave_data = Wavedata_service_buffer;
1364 uncompressed_wave_data = Wavedata_load_buffer;
1367 switch ( m_wave_format ) {
1368 case WAVE_FORMAT_PCM:
1369 num_bytes_desired = cbSize;
1373 case WAVE_FORMAT_ADPCM:
1374 if ( !m_hStream_open ) {
1375 if ( !ACM_stream_open(m_pwfmt_original, &m_wfxDest, (void**)&m_hStream), m_bits_per_sample_uncompressed ) {
1382 num_bytes_desired = cbSize;
1385 dest_buf = Compressed_service_buffer;
1387 dest_buf = Compressed_buffer;
1390 if ( num_bytes_desired <= 0 ) {
1391 num_bytes_desired = 0;
1392 // nprintf(("Alan","No bytes required for ADPCM time interval\n"));
1394 num_bytes_desired = ACM_query_source_size((void*)m_hStream, cbSize);
1395 // nprintf(("Alan","Num bytes desired: %d\n", num_bytes_desired));
1400 nprintf(("SOUND", "SOUND => Not supporting %d format for playing wave files\n"));
1410 // read data from disk
1411 if ( m_data_bytes_left <= 0 ) {
1413 uncompressed_bytes_written = 0;
1417 if ( m_data_bytes_left > 0 && num_bytes_desired > 0 ) {
1420 if ( num_bytes_desired <= (unsigned int)m_data_bytes_left ) {
1421 num_bytes_read = num_bytes_desired;
1424 num_bytes_read = m_data_bytes_left;
1427 actual_read = mmioRead( cfp, (char *)dest_buf, num_bytes_read );
1428 if ( (actual_read <= 0) || (m_abort_next_read) ) {
1430 uncompressed_bytes_written = 0;
1434 if ( num_bytes_desired >= (unsigned int)m_data_bytes_left ) {
1435 m_abort_next_read = 1;
1438 num_bytes_read = actual_read;
1441 // convert data if necessary, to PCM
1442 if ( m_wave_format == WAVE_FORMAT_ADPCM ) {
1443 if ( num_bytes_read > 0 ) {
1444 rc = ACM_convert((void*)m_hStream, dest_buf, num_bytes_read, uncompressed_wave_data, BIGBUF_SIZE, &convert_len, &src_bytes_used);
1448 if ( convert_len == 0 ) {
1453 Assert(src_bytes_used <= num_bytes_read);
1454 if ( src_bytes_used < num_bytes_read ) {
1455 // seek back file pointer to reposition before unused source data
1456 mmioSeek(cfp, src_bytes_used - num_bytes_read, SEEK_CUR);
1459 // Adjust number of bytes left
1460 m_data_bytes_left -= src_bytes_used;
1461 m_nBytesPlayed += src_bytes_used;
1462 uncompressed_bytes_written = convert_len;
1464 // Successful read, keep running total of number of data bytes read
1468 // Successful read, keep running total of number of data bytes read
1469 // Adjust number of bytes left
1470 m_data_bytes_left -= num_bytes_read;
1471 m_nBytesPlayed += num_bytes_read;
1472 uncompressed_bytes_written = num_bytes_read;
1478 uncompressed_bytes_written = 0;
1481 m_total_uncompressed_bytes_read += uncompressed_bytes_written;
1482 // nprintf(("Alan","Read: %d\n", uncompressed_bytes_written));
1483 return (uncompressed_bytes_written);
1489 // Returns 8 bits of data representing silence for the Wave file format.
1491 // Since we are dealing only with PCM format, we can fudge a bit and take
1492 // advantage of the fact that for all PCM formats, silence can be represented
1493 // by a single byte, repeated to make up the proper word size. The actual size
1494 // of a word of wave data depends on the format:
1496 // PCM Format Word Size Silence Data
1497 // 8-bit mono 1 byte 0x80
1498 // 8-bit stereo 2 bytes 0x8080
1499 // 16-bit mono 2 bytes 0x0000
1500 // 16-bit stereo 4 bytes 0x00000000
1502 BYTE WaveFile::GetSilenceData (void)
1504 BYTE bSilenceData = 0;
1506 // Silence data depends on format of Wave file
1507 if (m_pwfmt_original) {
1508 if (m_wfmt.wBitsPerSample == 8) {
1509 // For 8-bit formats (unsigned, 0 to 255)
1510 // Packed DWORD = 0x80808080;
1511 bSilenceData = 0x80;
1513 else if (m_wfmt.wBitsPerSample == 16) {
1514 // For 16-bit formats (signed, -32768 to 32767)
1515 // Packed DWORD = 0x00000000;
1516 bSilenceData = 0x00;
1526 return (bSilenceData);
1529 AudioStreamServices * m_pass = NULL; // ptr to AudioStreamServices object
1531 #define MAX_AUDIO_STREAMS 30
1532 AudioStream Audio_streams[MAX_AUDIO_STREAMS];
1533 #endif // !PLAT_UNIX
1535 int Audiostream_inited = 0;
1537 void audiostream_init()
1541 if ( Audiostream_inited == 1 )
1547 if ( !ACM_is_inited() ) {
1551 // Create and initialize AudioStreamServices object.
1552 // This must be done once and only once for each window that uses
1553 // streaming services.
1554 m_pass = (AudioStreamServices *)malloc(sizeof(AudioStreamServices));
1557 m_pass->Constructor();
1558 m_pass->Initialize();
1560 if ( !pDirectSound ) {
1565 // Allocate memory for the buffer which holds the uncompressed wave data that is streamed from the
1566 // disk during a load/cue
1567 if ( Wavedata_load_buffer == NULL ) {
1568 Wavedata_load_buffer = (unsigned char*)malloc(BIGBUF_SIZE);
1569 Assert(Wavedata_load_buffer != NULL);
1572 // Allocate memory for the buffer which holds the uncompressed wave data that is streamed from the
1573 // disk during a service interval
1574 if ( Wavedata_service_buffer == NULL ) {
1575 Wavedata_service_buffer = (unsigned char*)malloc(BIGBUF_SIZE);
1576 Assert(Wavedata_service_buffer != NULL);
1579 // Allocate memory for the buffer which holds the compressed wave data that is read from the hard disk
1580 if ( Compressed_buffer == NULL ) {
1581 Compressed_buffer = (unsigned char*)malloc(COMPRESSED_BUFFER_SIZE);
1582 Assert(Compressed_buffer != NULL);
1585 if ( Compressed_service_buffer == NULL ) {
1586 Compressed_service_buffer = (unsigned char*)malloc(COMPRESSED_BUFFER_SIZE);
1587 Assert(Compressed_service_buffer != NULL);
1590 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1591 Audio_streams[i].Init_Data();
1592 Audio_streams[i].status = ASF_FREE;
1593 Audio_streams[i].type = ASF_NONE;
1596 InitializeCriticalSection( &Global_service_lock );
1599 Audiostream_inited = 1;
1602 // Close down the audiostream system. Must call audiostream_init() before any audiostream functions can
1604 void audiostream_close()
1607 if ( Audiostream_inited == 0 )
1613 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1614 if ( Audio_streams[i].status == ASF_USED ) {
1615 Audio_streams[i].status = ASF_FREE;
1616 Audio_streams[i].Destroy();
1620 // Destroy AudioStreamServices object
1626 // free global buffers
1627 if ( Wavedata_load_buffer ) {
1628 free(Wavedata_load_buffer);
1629 Wavedata_load_buffer = NULL;
1632 if ( Wavedata_service_buffer ) {
1633 free(Wavedata_service_buffer);
1634 Wavedata_service_buffer = NULL;
1637 if ( Compressed_buffer ) {
1638 free(Compressed_buffer);
1639 Compressed_buffer = NULL;
1642 if ( Compressed_service_buffer ) {
1643 free(Compressed_service_buffer);
1644 Compressed_service_buffer = NULL;
1647 DeleteCriticalSection( &Global_service_lock );
1649 Audiostream_inited = 0;
1652 // Open a digital sound file for streaming
1654 // input: filename => disk filename of sound file
1655 // type => what type of audio stream do we want to open:
1660 // returns: success => handle to identify streaming sound
1662 int audiostream_open( char * filename, int type )
1669 if (!Audiostream_inited || !snd_is_inited())
1672 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1673 if ( Audio_streams[i].status == ASF_FREE ) {
1674 Audio_streams[i].status = ASF_USED;
1675 Audio_streams[i].type = type;
1680 if ( i == MAX_AUDIO_STREAMS ) {
1681 nprintf(("Sound", "SOUND => No more audio streams available!\n"));
1688 Audio_streams[i].m_bits_per_sample_uncompressed = 8;
1690 case ASF_EVENTMUSIC:
1691 Audio_streams[i].m_bits_per_sample_uncompressed = 16;
1698 rc = Audio_streams[i].Create(filename, m_pass);
1700 Audio_streams[i].status = ASF_FREE;
1709 void audiostream_close_file(int i, int fade)
1711 if (!Audiostream_inited)
1720 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1722 if ( Audio_streams[i].status == ASF_USED ) {
1723 if ( fade == TRUE ) {
1724 Audio_streams[i].Fade_and_Destroy();
1727 Audio_streams[i].Destroy();
1733 void audiostream_close_all(int fade)
1740 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1741 if ( Audio_streams[i].status == ASF_FREE )
1744 audiostream_close_file(i, fade);
1749 extern int ds_convert_volume(float volume);
1751 void audiostream_play(int i, float volume, int looping)
1753 if (!Audiostream_inited)
1762 Assert(looping >= 0);
1763 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1765 // convert from 0->1 to -10000->0 for volume
1766 int converted_volume;
1767 if ( volume == -1 ) {
1768 converted_volume = Audio_streams[i].Get_Default_Volume();
1771 Assert(volume >= 0.0f && volume <= 1.0f );
1772 converted_volume = ds_convert_volume(volume);
1775 Assert( Audio_streams[i].status == ASF_USED );
1776 Audio_streams[i].Set_Default_Volume(converted_volume);
1777 Audio_streams[i].Play(converted_volume, looping);
1781 void audiostream_stop(int i, int rewind, int paused)
1783 if (!Audiostream_inited) return;
1791 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1792 Assert( Audio_streams[i].status == ASF_USED );
1795 Audio_streams[i].Stop_and_Rewind();
1797 Audio_streams[i].Stop(paused);
1801 int audiostream_is_playing(int i)
1810 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1811 if ( Audio_streams[i].status != ASF_USED )
1814 return Audio_streams[i].Is_Playing();
1819 void audiostream_set_volume_all(float volume, int type)
1826 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1827 if ( Audio_streams[i].status == ASF_FREE )
1830 if ( Audio_streams[i].type == type ) {
1831 int converted_volume;
1832 converted_volume = ds_convert_volume(volume);
1833 Audio_streams[i].Set_Volume(converted_volume);
1840 void audiostream_set_volume(int i, float volume)
1848 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1849 Assert( volume >= 0 && volume <= 1);
1851 if ( Audio_streams[i].status == ASF_FREE )
1854 int converted_volume;
1855 converted_volume = ds_convert_volume(volume);
1856 Audio_streams[i].Set_Volume(converted_volume);
1861 int audiostream_is_paused(int i)
1870 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1871 if ( Audio_streams[i].status == ASF_FREE )
1875 is_paused = Audio_streams[i].Is_Paused();
1881 void audiostream_set_byte_cutoff(int i, unsigned int cutoff)
1889 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1890 Assert( cutoff > 0 );
1892 if ( Audio_streams[i].status == ASF_FREE )
1895 Audio_streams[i].Set_Byte_Cutoff(cutoff);
1900 unsigned int audiostream_get_bytes_committed(int i)
1909 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1911 if ( Audio_streams[i].status == ASF_FREE )
1914 unsigned int num_bytes_committed;
1915 num_bytes_committed = Audio_streams[i].Get_Bytes_Committed();
1916 return num_bytes_committed;
1920 int audiostream_done_reading(int i)
1929 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1931 if ( Audio_streams[i].status == ASF_FREE )
1935 done_reading = Audio_streams[i].Is_Past_Limit();
1936 return done_reading;
1941 int audiostream_is_inited()
1943 return Audiostream_inited;
1946 // pause a single audio stream, indentified by handle i.
1947 void audiostream_pause(int i)
1955 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1956 if ( Audio_streams[i].status == ASF_FREE )
1959 if ( audiostream_is_playing(i) == TRUE ) {
1960 audiostream_stop(i, 0, 1);
1965 // pause all audio streams that are currently playing.
1966 void audiostream_pause_all()
1973 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1974 if ( Audio_streams[i].status == ASF_FREE )
1977 audiostream_pause(i);
1982 // unpause the audio stream identified by handle i.
1983 void audiostream_unpause(int i)
1993 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1994 if ( Audio_streams[i].status == ASF_FREE )
1997 if ( audiostream_is_paused(i) == TRUE ) {
1998 is_looping = Audio_streams[i].Is_looping();
1999 audiostream_play(i, -1.0f, is_looping);
2004 // unpause all audio streams that are currently paused
2005 void audiostream_unpause_all()
2012 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
2013 if ( Audio_streams[i].status == ASF_FREE )
2016 audiostream_unpause(i);