2 * $Logfile: /Freespace2/code/Sound/AudioStr.cpp $
7 * Routines to stream large WAV files from disk
10 * Revision 1.2 2002/05/07 03:16:52 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:10 root
17 * 5 9/14/99 1:32a Jimb
18 * Commented out Int3() that was hanging Jim's machine. Happens before
19 * sm2-07 command brief.
21 * 4 7/14/99 12:09p Jefff
22 * Make sure we're not servicing a bogus audiostream. Check for "used"
23 * after the critical section lock.
25 * 3 12/17/98 4:01p Andsager
26 * up wavedata buffer size to 180000 to allow stereo 16b/22KHz streaming
28 * 2 10/07/98 10:53a Dave
31 * 1 10/07/98 10:51a Dave
33 * 53 6/28/98 6:35p Lawrance
34 * move re-entrancy semaphore into audiostream class
36 * 52 5/24/98 4:42p Dan
37 * AL: Fix several bugs related to pausing and enabling/disabling event
40 * 51 5/21/98 11:57a Lawrance
41 * fix potential bug with transitions for music when in packfiles
43 * 50 5/15/98 9:09p Lawrance
44 * The last of the multi-threading fixes
46 * 49 5/15/98 7:57p Duncan
47 * AL: Fix race condition with music streaming
49 * 48 5/15/98 10:13a Lawrance
50 * remove unused audiostream member
52 * 47 5/14/98 5:45p Lawrance2
53 * Put critical section around audiostream destroying
55 * 46 5/12/98 5:40p Lawrance
56 * Add critical section code to the service buffer call.. since it is
57 * possible to release buffers while in this call
59 * 45 5/10/98 3:49p Sandeep
60 * Fix problem with having the audio streaming while trying to close down
63 * 44 4/30/98 4:53p John
64 * Restructured and cleaned up cfile code. Added capability to read off
65 * of CD-ROM drive and out of multiple pack files.
67 * 43 4/26/98 3:30a Lawrance
68 * Fix a couple of potential bugs
70 * 42 4/21/98 10:18a Dan
72 * 41 4/17/98 6:59a John
73 * Changed code the used 'new' and 'delete' to use 'malloc' and 'free'
74 * instead. Had to manually can constructors/destructors.
76 * 40 4/13/98 10:18a John
79 * 39 4/13/98 10:16a John
80 * Switched gettime back to timer_get_milliseconds, which is now thread
83 * 38 4/12/98 11:08p Lawrance
84 * switch back to using gettime() in separate threads
86 * 37 4/12/98 5:31p Lawrance
87 * use timer_get_milliseconds() instead of gettime()
89 * 36 4/06/98 12:36a Lawrance
90 * Ensure all non-music ADPCM files get decompressed to 8 bit.
92 * 35 4/03/98 4:56p Lawrance
93 * Upu the max audio streams to 30
95 * 34 3/31/98 4:50p Dan
96 * AL: Clean up all audio streams if necessary in
97 * event_music_level_close()
99 * 33 3/23/98 4:12p Lawrance
100 * Fix subtle bug with looping and fading out songs
102 * 32 2/18/98 5:49p Lawrance
103 * Even if the ADPCM codec is unavailable, allow game to continue.
105 * 31 2/15/98 4:43p Lawrance
106 * work on real-time voice
108 * 30 1/19/98 11:37p Lawrance
109 * Fixing Optimization build warnings
111 * 29 1/17/98 4:41p Lawrance
112 * Fix problem with multiple audio streams using the same buffers
114 * 28 1/16/98 11:49a Lawrance
115 * Use own internal timer for fading.
117 * 27 12/28/97 12:43p John
118 * Put in support for reading archive files; Made missionload use the
119 * cf_get_file_list function. Moved demos directory out of data tree.
121 * 26 12/27/97 8:08p Lawrance
122 * If an audiostream doesn't exists, it can't be playing
124 * 25 12/18/97 3:30p Lawrance
125 * Fix bug that sometimes caused music with no volume to not get stopped
128 * 24 12/17/97 10:17p Allender
129 * redid streadming code to use mmio* functions instead of cf* functions.
130 * Our functions are not reentrant!
132 * 23 12/10/97 10:04p Lawrance
133 * modify what happens in Audio_stream constructor
135 * 22 12/09/97 6:14p Lawrance
138 * 21 12/08/97 6:21p Lawrance
139 * fix problems with signaling that end-of-file has been reached
141 * 20 12/05/97 10:50a Lawrance
142 * improve how silence bytes are written on transitions
144 * 19 12/04/97 5:35p Lawrance
145 * fix bug that may have caused errors when writing silence
147 * 18 11/28/97 2:09p Lawrance
148 * Overhaul how ADPCM conversion works... use much less memory... safer
151 * 17 10/03/97 8:24a Lawrance
152 * When unpausing, be sure to retain looping status
154 * 16 9/24/97 5:30p Lawrance
155 * fix bug that was messing up streaming of 8 bit audio
157 * 15 9/18/97 10:31p Lawrance
158 * add functions to pause and unpause all audio streams
160 * 14 9/09/97 3:39p Sandeep
161 * warning level 4 bugs
172 #include <mmsystem.h>
176 #include "audiostr.h"
177 #include "cfile.h" // needed for cf_get_path
179 #include "sound.h" /* for Snd_sram */
185 #define SUCCESS TRUE // Error returns for all member functions
186 #define FAILURE FALSE
189 typedef BOOL (*TIMERCALLBACK)(DWORD);
191 #define BIGBUF_SIZE 180000 // This can be reduced to 88200 once we don't use any stereo
192 //#define BIGBUF_SIZE 88300 // This can be reduced to 88200 once we don't use any stereo
193 unsigned char *Wavedata_load_buffer = NULL; // buffer used for cueing audiostreams
194 unsigned char *Wavedata_service_buffer = NULL; // buffer used for servicing audiostreams
196 CRITICAL_SECTION Global_service_lock;
198 #define COMPRESSED_BUFFER_SIZE 88300
199 unsigned char *Compressed_buffer = NULL; // Used to load in compressed data during a cueing interval
200 unsigned char *Compressed_service_buffer = NULL; // Used to read in compressed data during a service interval
202 #define AS_HIGHEST_MAX 999999999 // max uncompressed filesize supported is 999 meg
208 // Wrapper class for Windows multimedia timer services. Provides
209 // both periodic and one-shot events. User must supply callback
210 // for periodic events.
216 void constructor(void);
217 void destructor(void);
218 BOOL Create (UINT nPeriod, UINT nRes, DWORD dwUser, TIMERCALLBACK pfnCallback);
220 static void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);
221 TIMERCALLBACK m_pfnCallback;
233 // WAV file class (read-only).
246 BOOL Open (LPSTR pszFilename);
248 int Read (BYTE * pbDest, UINT cbSize, int service=1);
249 UINT GetNumBytesRemaining (void) { return (m_nDataSize - m_nBytesPlayed); }
250 UINT GetUncompressedAvgDataRate (void) { return (m_nUncompressedAvgDataRate); }
251 UINT GetDataSize (void) { return (m_nDataSize); }
252 UINT GetNumBytesPlayed (void) { return (m_nBytesPlayed); }
253 BYTE GetSilenceData (void);
254 WAVEFORMATEX m_wfmt; // format of wave file used by Direct Sound
255 WAVEFORMATEX * m_pwfmt_original; // foramt of wave file from actual wave source
256 UINT m_total_uncompressed_bytes_read;
257 UINT m_max_uncompressed_bytes_to_read;
258 UINT m_bits_per_sample_uncompressed;
261 UINT m_data_offset; // number of bytes to actual wave data
262 int m_data_bytes_left;
265 UINT m_wave_format; // format of wave source (ie WAVE_FORMAT_PCM, WAVE_FORMAT_ADPCM)
266 UINT m_nBlockAlign; // wave data block alignment spec
267 UINT m_nUncompressedAvgDataRate; // average wave data rate
268 UINT m_nDataSize; // size of data chunk
269 UINT m_nBytesPlayed; // offset into data chunk
270 BOOL m_abort_next_read;
272 HACMSTREAM m_hStream;
274 WAVEFORMATEX m_wfxDest;
279 // AudioStreamServices
281 // DirectSound apportions services on a per-window basis to allow
282 // sound from background windows to be muted. The AudioStreamServices
283 // class encapsulates the initialization of DirectSound services.
285 // Each window that wants to create AudioStream objects must
286 // first create and initialize an AudioStreamServices object.
287 // All AudioStream objects must be destroyed before the associated
288 // AudioStreamServices object is destroyed.
289 class AudioStreamServices
292 void Constructor(void);
294 LPDIRECTSOUND GetPDS (void) { return m_pds; }
302 // Audio stream interface class for playing WAV files using DirectSound.
303 // Users of this class must create AudioStreamServices object before
304 // creating an AudioStream object.
320 BOOL Create (LPSTR pszFilename, AudioStreamServices * pass);
322 void Play (long volume, int looping);
323 int Is_Playing(){ return(m_fPlaying); }
324 int Is_Paused(){ return(m_bIsPaused); }
325 int Is_Past_Limit() { return m_bPastLimit; }
326 void Stop (int paused=0);
327 void Stop_and_Rewind (void);
328 void Fade_and_Destroy (void);
329 void Fade_and_Stop(void);
330 void Set_Volume(long vol);
333 void Set_Byte_Cutoff(unsigned int num_bytes_cutoff);
334 void Set_Default_Volume(long converted_volume) { m_lDefaultVolume = converted_volume; }
335 long Get_Default_Volume() { return m_lDefaultVolume; }
336 unsigned int Get_Bytes_Committed(void);
337 int Is_looping() { return m_bLooping; }
340 UINT m_bits_per_sample_uncompressed;
344 BOOL WriteWaveData (UINT cbSize, UINT* num_bytes_written,int service=1);
345 BOOL WriteSilence (UINT cbSize);
346 DWORD GetMaxWriteSize (void);
347 BOOL ServiceBuffer (void);
348 static BOOL TimerCallback (DWORD dwUser);
350 AudioStreamServices * m_pass; // ptr to AudioStreamServices object
351 LPDIRECTSOUNDBUFFER m_pdsb; // ptr to Direct Sound buffer
352 WaveFile * m_pwavefile; // ptr to WaveFile object
353 Timer m_timer; // ptr to Timer object
354 BOOL m_fCued; // semaphore (stream cued)
355 BOOL m_fPlaying; // semaphore (stream playing)
356 DSBUFFERDESC m_dsbd; // Direct Sound buffer description
357 LONG m_lInService; // reentrancy semaphore
358 UINT m_cbBufOffset; // last write position
359 UINT m_nBufLength; // length of sound buffer in msec
360 UINT m_cbBufSize; // size of sound buffer in bytes
361 UINT m_nBufService; // service interval in msec
362 UINT m_nTimeStarted; // time (in system time) playback started
364 BOOL m_bLooping; // whether or not to loop playback
365 BOOL m_bFade; // fade out music
366 BOOL m_bDestroy_when_faded;
367 LONG m_lVolume; // volume of stream ( 0 -> -10 000 )
368 LONG m_lCutoffVolume;
369 BOOL m_bIsPaused; // stream is stopped, but not rewinded
370 UINT m_silence_written; // number of bytes of silence written to buffer
371 UINT m_bReadingDone; // no more bytes to be read from disk, still have remaining buffer to play
372 DWORD m_fade_timer_id; // timestamp so we know when to start fade
373 DWORD m_finished_id; // timestamp so we know when we've played #bytes required
374 BOOL m_bPastLimit; // flag to show we've played past the number of bytes requred
375 LONG m_lDefaultVolume;
378 CRITICAL_SECTION write_lock;
382 // AudioStreamServices class implementation
384 ////////////////////////////////////////////////////////////
387 void AudioStreamServices::Constructor(void)
389 // Initialize member data
392 // It would seem to make sense to initialize DirectSound here,
393 // but because there could be an error, it's best done in a
394 // separate member function, ::Initialize.
398 extern LPDIRECTSOUND pDirectSound; // From Sound.cpp
402 BOOL AudioStreamServices::Initialize ()
405 BOOL fRtn = SUCCESS; // assume success
408 m_pds = pDirectSound;
417 // AudioStream class implementation
419 ////////////////////////////////////////////////////////////
421 // The following constants are the defaults for our streaming buffer operation.
422 const UINT DefBufferLength = 2000; // default buffer length in msec
423 const UINT DefBufferServiceInterval = 250; // default buffer service interval in msec
426 AudioStream::AudioStream (void)
428 InitializeCriticalSection( &write_lock );
433 AudioStream::~AudioStream (void)
435 DeleteCriticalSection( &write_lock );
439 void AudioStream::Init_Data ()
445 m_bPastLimit = FALSE;
447 m_bDestroy_when_faded = FALSE;
449 m_lCutoffVolume = -10000;
451 m_silence_written = 0;
452 m_bReadingDone = FALSE;
456 m_fPlaying = m_fCued = FALSE;
457 m_lInService = FALSE;
459 m_nBufLength = DefBufferLength;
461 m_nBufService = DefBufferServiceInterval;
466 BOOL AudioStream::Create (LPSTR pszFilename, AudioStreamServices * pass)
468 BOOL fRtn = SUCCESS; // assume success
476 if (pszFilename && m_pass) {
477 // Create a new WaveFile object
479 m_pwavefile = (WaveFile *)malloc(sizeof(WaveFile));
486 m_pwavefile->m_bits_per_sample_uncompressed = m_bits_per_sample_uncompressed;
487 if (m_pwavefile->Open (pszFilename)) {
488 // Calculate sound buffer size in bytes
489 // Buffer size is average data rate times length of buffer
490 // No need for buffer to be larger than wave data though
491 m_cbBufSize = (m_pwavefile->GetUncompressedAvgDataRate () * m_nBufLength) / 1000;
492 nprintf(("SOUND", "SOUND => Stream buffer created using %d bytes\n", m_cbBufSize));
493 // m_cbBufSize = (m_cbBufSize > m_pwavefile->GetDataSize ()) ? m_pwavefile->GetDataSize () : m_cbBufSize;
495 //nprintf(("Sound", "SOUND => average data rate = %d\n\r", m_pwavefile->GetUncompressedAvgDataRate ()));
496 //nprintf(("Sound", "SOUND => m_cbBufSize = %d\n\r", m_cbBufSize));
498 // Create sound buffer
500 memset (&m_dsbd, 0, sizeof (DSBUFFERDESC));
501 m_dsbd.dwSize = sizeof (DSBUFFERDESC);
502 m_dsbd.dwBufferBytes = m_cbBufSize;
503 m_dsbd.lpwfxFormat = &m_pwavefile->m_wfmt;
504 m_dsbd.dwFlags = DSBCAPS_STATIC | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_LOCSOFTWARE;
506 hr = (m_pass->GetPDS ())->CreateSoundBuffer (&m_dsbd, &m_pdsb, NULL);
510 Snd_sram += m_cbBufSize;
513 // Error, unable to create DirectSound buffer
514 nprintf(("Sound", "SOUND => Error, unable to create DirectSound buffer\n\r"));
515 if (hr == DSERR_BADFORMAT) {
516 nprintf(("Sound", "SOUND => Bad format (probably ADPCM)\n\r"));
523 // Error opening file
524 nprintf(("SOUND", "SOUND => Failed to open wave file: %s\n\r", pszFilename));
525 m_pwavefile->Close();
532 // Error, unable to create WaveFile object
533 nprintf(("Sound", "SOUND => Failed to create WaveFile object %s\n\r", pszFilename));
538 // Error, passed invalid parms
547 BOOL AudioStream::Destroy (void)
551 EnterCriticalSection(&write_lock);
556 // Release DirectSound buffer
560 Snd_sram -= m_cbBufSize;
563 // Delete WaveFile object
565 m_pwavefile->Close();
572 LeaveCriticalSection(&write_lock);
579 // Writes wave data to sound buffer. This is a helper method used by Create and
580 // ServiceBuffer; it's not exposed to users of the AudioStream class.
581 BOOL AudioStream::WriteWaveData (UINT size, UINT *num_bytes_written, int service)
584 LPBYTE lpbuf1 = NULL;
585 LPBYTE lpbuf2 = NULL;
588 DWORD dwbyteswritten1 = 0;
589 DWORD dwbyteswritten2 = 0;
591 unsigned char *uncompressed_wave_data;
593 *num_bytes_written = 0;
595 if ( size == 0 || m_bReadingDone ) {
599 if ( !m_pdsb || !m_pwavefile ) {
604 EnterCriticalSection(&Global_service_lock);
608 uncompressed_wave_data = Wavedata_service_buffer;
610 uncompressed_wave_data = Wavedata_load_buffer;
613 int num_bytes_read = 0;
615 // Lock the sound buffer
616 hr = m_pdsb->Lock (m_cbBufOffset, size, (void**)(&lpbuf1), &dwsize1, (void**)(&lpbuf2), &dwsize2, 0);
618 // Write data to sound buffer. Because the sound buffer is circular, we may have to
619 // do two write operations if locked portion of buffer wraps around to start of buffer.
622 num_bytes_read = m_pwavefile->Read(uncompressed_wave_data, dwsize1+dwsize2,service);
623 if ( num_bytes_read == -1 ) {
624 // means nothing left to read!
629 if ( num_bytes_read > 0 ) {
630 if ( (unsigned int)num_bytes_read > dwsize1 ) {
631 dwbyteswritten1 = dwsize1;
632 dwbyteswritten2 = num_bytes_read - dwsize1;
634 memcpy(lpbuf1, uncompressed_wave_data, dwsize1);
636 memcpy(lpbuf2, uncompressed_wave_data+dwsize1, num_bytes_read-dwsize1);
638 dwbyteswritten1 = num_bytes_read;
640 memcpy(lpbuf1, uncompressed_wave_data, num_bytes_read);
644 // Update our buffer offset and unlock sound buffer
645 m_cbBufOffset = (m_cbBufOffset + dwbyteswritten1 + dwbyteswritten2) % m_cbBufSize;
646 *num_bytes_written = dwbyteswritten1 + dwbyteswritten2;
647 m_pdsb->Unlock (lpbuf1, dwsize1, lpbuf2, dwsize2);
650 // Error locking sound buffer
651 nprintf(("SOUND", "SOUND ==> Error, unable to lock sound buffer in AudioStr\n"));
656 LeaveCriticalSection(&Global_service_lock);
665 // Writes silence to sound buffer. This is a helper method used by
666 // ServiceBuffer; it's not exposed to users of the AudioStream class.
667 BOOL AudioStream::WriteSilence (UINT size)
670 LPBYTE lpbuf1 = NULL;
671 LPBYTE lpbuf2 = NULL;
674 DWORD dwbyteswritten1 = 0;
675 DWORD dwbyteswritten2 = 0;
678 // Lock the sound buffer
679 hr = m_pdsb->Lock (m_cbBufOffset, size, (void**)(&lpbuf1), &dwsize1, (void**)(&lpbuf2), &dwsize2, 0);
682 // Get silence data for this file format. Although word sizes vary for different
683 // wave file formats, ::Lock will always return pointers on word boundaries.
684 // Because silence data for 16-bit PCM formats is 0x0000 or 0x00000000, we can
685 // get away with writing bytes and ignoring word size here.
686 BYTE bSilence = m_pwavefile->GetSilenceData ();
688 // Write silence to sound buffer. Because the sound buffer is circular, we may have to
689 // do two write operations if locked portion of buffer wraps around to start of buffer.
690 memset (lpbuf1, bSilence, dwsize1);
691 dwbyteswritten1 = dwsize1;
693 // Second write required?
695 memset (lpbuf2, bSilence, dwsize2);
696 dwbyteswritten2 = dwsize2;
699 // Update our buffer offset and unlock sound buffer
700 m_cbBufOffset = (m_cbBufOffset + dwbyteswritten1 + dwbyteswritten2) % m_cbBufSize;
701 // m_pdsb->Unlock (lpbuf1, dwbyteswritten1, lpbuf2, dwbyteswritten2);
702 m_pdsb->Unlock (lpbuf1, dwsize1, lpbuf2, dwsize2);
705 // Error locking sound buffer
706 nprintf(("SOUND", "SOUND ==> Error, unable to lock sound buffer in AudioStr\n"));
716 // Helper function to calculate max size of sound buffer write operation, i.e. how much
717 // free space there is in buffer.
718 DWORD AudioStream::GetMaxWriteSize (void)
720 DWORD dwWriteCursor, dwPlayCursor, dwMaxSize;
722 // Get current play position
723 if (m_pdsb->GetCurrentPosition (&dwPlayCursor, &dwWriteCursor) == DS_OK) {
724 if (m_cbBufOffset <= dwPlayCursor) {
725 // Our write position trails play cursor
726 dwMaxSize = dwPlayCursor - m_cbBufOffset;
729 else {// (m_cbBufOffset > dw7Cursor)
730 // Play cursor has wrapped
731 dwMaxSize = m_cbBufSize - m_cbBufOffset + dwPlayCursor;
735 // GetCurrentPosition call failed
740 // nprintf(("Alan","Max write size: %d\n", dwMaxSize));
747 // Routine to service buffer requests initiated by periodic timer.
749 // Returns TRUE if buffer serviced normally; otherwise returns FALSE.
750 #define FADE_VOLUME_INTERVAL 400 // 100 == 1db
751 #define VOLUME_ATTENUATION_BEFORE_CUTOFF 3000 // 12db
752 BOOL AudioStream::ServiceBuffer (void)
757 if ( status != ASF_USED )
760 EnterCriticalSection(&write_lock);
762 // status may have changed, so lets check once again
763 if ( status != ASF_USED ){
764 LeaveCriticalSection(&write_lock);
768 // Check for reentrance
769 if (InterlockedExchange (&m_lInService, TRUE) == FALSE) {
770 if ( m_bFade == TRUE ) {
771 if ( m_lCutoffVolume == -10000 ) {
773 // nprintf(("Alan","Volume is: %d\n",vol));
774 m_lCutoffVolume = max(vol - VOLUME_ATTENUATION_BEFORE_CUTOFF, -10000);
778 vol = vol - FADE_VOLUME_INTERVAL; // decrease by 1db
779 // nprintf(("Alan","Volume is now: %d\n",vol));
782 // nprintf(("Sound","SOUND => Volume for stream sound is %d\n",vol));
783 // nprintf(("Alan","Cuttoff Volume is: %d\n",m_lCutoffVolume));
784 if ( vol < m_lCutoffVolume ) {
786 m_lCutoffVolume = -10000;
787 if ( m_bDestroy_when_faded == TRUE ) {
788 LeaveCriticalSection(&write_lock);
790 // Reset reentrancy semaphore
791 InterlockedExchange (&m_lInService, FALSE);
796 // Reset reentrancy semaphore
797 LeaveCriticalSection(&write_lock);
798 InterlockedExchange (&m_lInService, FALSE);
804 // All of sound not played yet, send more data to buffer
805 DWORD dwFreeSpace = GetMaxWriteSize ();
807 // Determine free space in sound buffer
810 // Some wave data remains, but not enough to fill free space
811 // Send wave data to buffer, fill remainder of free space with silence
812 uint num_bytes_written;
814 if (WriteWaveData (dwFreeSpace, &num_bytes_written) == SUCCESS) {
815 // nprintf(("Alan","Num bytes written: %d\n", num_bytes_written));
817 if ( m_pwavefile->m_total_uncompressed_bytes_read >= m_pwavefile->m_max_uncompressed_bytes_to_read ) {
818 m_fade_timer_id = timer_get_milliseconds() + 1700; // start fading 1.7 seconds from now
819 m_finished_id = timer_get_milliseconds() + 2000; // 2 seconds left to play out buffer
820 m_pwavefile->m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
823 if ( (m_fade_timer_id>0) && ((uint)timer_get_milliseconds() > m_fade_timer_id) ) {
828 if ( (m_finished_id>0) && ((uint)timer_get_milliseconds() > m_finished_id) ) {
833 if ( (num_bytes_written < dwFreeSpace) && m_bReadingDone ) {
834 int num_bytes_silence;
835 num_bytes_silence = dwFreeSpace - num_bytes_written;
837 if ( num_bytes_silence > 0 ) {
839 m_silence_written += num_bytes_silence;
840 if (WriteSilence (num_bytes_silence) == FAILURE) {
845 if ( m_silence_written >= m_cbBufSize ) {
846 m_silence_written = 0;
848 if ( m_bDestroy_when_faded == TRUE ) {
849 LeaveCriticalSection(&write_lock);
851 // Reset reentrancy semaphore
852 InterlockedExchange (&m_lInService, FALSE);
856 // All of sound has played, stop playback or loop again
857 if ( m_bLooping && !m_bFade) {
858 Play(m_lVolume, m_bLooping);
868 // Error writing wave data
874 // Reset reentrancy semaphore
875 InterlockedExchange (&m_lInService, FALSE);
877 // Service routine reentered. Do nothing, just return
881 LeaveCriticalSection(&write_lock);
886 void AudioStream::Cue (void)
888 UINT num_bytes_written;
894 m_bPastLimit = FALSE;
896 m_lCutoffVolume = -10000;
898 m_bDestroy_when_faded = FALSE;
903 // Reset file ptr, etc
906 // Reset DirectSound buffer
907 m_pdsb->SetCurrentPosition (0);
909 // Fill buffer with wave data
910 WriteWaveData (m_cbBufSize, &num_bytes_written,0);
918 void AudioStream::Play (long volume, int looping)
923 if ( m_bIsPaused == FALSE)
927 // Cue for playback if necessary
937 // Begin DirectSound playback
938 HRESULT hr = m_pdsb->Play (0, 0, DSBPLAY_LOOPING);
940 m_nTimeStarted = timer_get_milliseconds();
942 // Kick off timer to service buffer
943 m_timer.constructor();
945 m_timer.Create (m_nBufService, m_nBufService, DWORD (this), TimerCallback);
947 // Playback begun, no longer cued
952 // If the buffer was lost, try to restore it
953 if ( hr == DSERR_BUFFERLOST ) {
954 hr = m_pdsb->Restore();
956 hr = m_pdsb->Play (0, 0, DSBPLAY_LOOPING);
959 nprintf(("Sound", "Sound => Lost a buffer, tried restoring but got %s\n", get_DSERR_text(hr) ));
960 Int3(); // get Alan, he wants to see this
965 nprintf(("Sound", "Sound => Play failed with return value %s\n", get_DSERR_text(hr) ));
971 // Timer callback for Timer object created by ::Play method.
972 BOOL AudioStream::TimerCallback (DWORD dwUser)
974 // dwUser contains ptr to AudioStream object
975 AudioStream * pas = (AudioStream *) dwUser;
977 return (pas->ServiceBuffer ());
980 void AudioStream::Set_Byte_Cutoff(unsigned int byte_cutoff)
982 if ( m_pwavefile == NULL )
985 m_pwavefile->m_max_uncompressed_bytes_to_read = byte_cutoff;
988 unsigned int AudioStream::Get_Bytes_Committed(void)
990 if ( m_pwavefile == NULL )
993 return m_pwavefile->m_total_uncompressed_bytes_read;
998 void AudioStream::Fade_and_Destroy (void)
1001 m_bDestroy_when_faded = TRUE;
1005 void AudioStream::Fade_and_Stop (void)
1008 m_bDestroy_when_faded = FALSE;
1013 void AudioStream::Stop(int paused)
1016 // Stop DirectSound playback
1019 m_bIsPaused = paused;
1021 // Delete Timer object
1022 m_timer.destructor();
1027 void AudioStream::Stop_and_Rewind (void)
1030 // Stop DirectSound playback
1033 // Delete Timer object
1034 m_timer.destructor();
1039 m_fCued = FALSE; // this will cause wave file to start from beginning
1040 m_bReadingDone = FALSE;
1044 void AudioStream::Set_Volume(long vol)
1052 Assert( vol >= -10000 && vol <= 0 );
1053 h_result = m_pdsb->SetVolume(vol);
1055 if ( h_result != DS_OK )
1056 nprintf(("Sound","SOUND => SetVolume() failed with code '%s'\n", get_DSERR_text(h_result) ));
1061 long AudioStream::Get_Volume()
1067 void Timer::constructor(void)
1074 void Timer::destructor(void)
1077 timeKillEvent (m_nIDTimer);
1084 BOOL Timer::Create (UINT nPeriod, UINT nRes, DWORD dwUser, TIMERCALLBACK pfnCallback)
1086 BOOL bRtn = SUCCESS; // assume success
1088 Assert(pfnCallback);
1089 Assert(nPeriod > 10);
1090 Assert(nPeriod >= nRes);
1092 m_nPeriod = nPeriod;
1095 m_pfnCallback = pfnCallback;
1097 if ((m_nIDTimer = timeSetEvent (m_nPeriod, m_nRes, TimeProc, (DWORD) this, TIME_PERIODIC)) == NULL) {
1105 // Timer proc for multimedia timer callback set with timeSetTime().
1107 // Calls procedure specified when Timer object was created. The
1108 // dwUser parameter contains "this" pointer for associated Timer object.
1110 void CALLBACK Timer::TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
1112 // dwUser contains ptr to Timer object
1113 Timer * ptimer = (Timer *) dwUser;
1115 // Call user-specified callback and pass back user specified data
1116 (ptimer->m_pfnCallback) (ptimer->m_dwUser);
1120 // WaveFile class implementation
1122 ////////////////////////////////////////////////////////////
1125 void WaveFile::Init(void)
1127 // Init data members
1130 m_pwfmt_original = NULL;
1132 m_nUncompressedAvgDataRate = 0;
1135 m_total_uncompressed_bytes_read = 0;
1136 m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
1139 m_abort_next_read = FALSE;
1143 void WaveFile::Close(void)
1146 if (m_pwfmt_original) {
1147 free(m_pwfmt_original);
1148 m_pwfmt_original = NULL;
1151 if ( m_hStream_open ) {
1152 ACM_stream_close((void*)m_hStream);
1159 mmioClose( cfp, 0 );
1166 BOOL WaveFile::Open (LPSTR pszFilename)
1170 BOOL fRtn = SUCCESS; // assume success
1171 PCMWAVEFORMAT pcmwf;
1172 char fullpath[_MAX_PATH];
1174 m_total_uncompressed_bytes_read = 0;
1175 m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
1177 int FileSize, FileOffset;
1179 if ( !cf_find_file_location(pszFilename, CF_TYPE_ANY, fullpath, &FileSize, &FileOffset )) {
1183 cfp = mmioOpen(fullpath, NULL, MMIO_ALLOCBUF | MMIO_READ);
1184 if ( cfp == NULL ) {
1188 // Skip the "RIFF" tag and file size (8 bytes)
1189 // Skip the "WAVE" tag (4 bytes)
1190 mmioSeek( cfp, 12+FileOffset, SEEK_SET );
1192 // Now read RIFF tags until the end of file
1193 uint tag, size, next_chunk;
1195 while(done == FALSE) {
1196 if ( mmioRead(cfp, (char *)&tag, sizeof(uint)) != sizeof(uint) )
1199 if ( mmioRead(cfp, (char *)&size, sizeof(uint)) != sizeof(uint) )
1202 next_chunk = mmioSeek( cfp, 0, SEEK_CUR );
1206 case 0x20746d66: // The 'fmt ' tag
1207 mmioRead( cfp, (char *)&pcmwf, sizeof(PCMWAVEFORMAT) );
1208 if ( pcmwf.wf.wFormatTag != WAVE_FORMAT_PCM ) {
1209 mmioRead( cfp, (char *)&cbExtra, sizeof(short) );
1212 // Allocate memory for WAVEFORMATEX structure + extra bytes
1213 if ( (m_pwfmt_original = (WAVEFORMATEX *) malloc ( sizeof(WAVEFORMATEX)+cbExtra )) != NULL ){
1214 Assert(m_pwfmt_original != NULL);
1215 // Copy bytes from temporary format structure
1216 memcpy (m_pwfmt_original, &pcmwf, sizeof(pcmwf));
1217 m_pwfmt_original->cbSize = cbExtra;
1219 // Read those extra bytes, append to WAVEFORMATEX structure
1221 mmioRead( cfp, (char *)((ubyte *)(m_pwfmt_original) + sizeof(WAVEFORMATEX)), cbExtra );
1225 Int3(); // malloc failed
1230 case 0x61746164: // the 'data' tag
1231 m_nDataSize = size; // This is size of data chunk. Compressed if ADPCM.
1232 m_data_bytes_left = size;
1233 m_data_offset = mmioSeek( cfp, 0, SEEK_CUR);
1237 default: // unknown, skip it
1241 mmioSeek( cfp, next_chunk, SEEK_SET );
1244 // At this stage, examine source format, and set up WAVEFORATEX structure for DirectSound.
1245 // Since DirectSound only supports PCM, force this structure to be PCM compliant. We will
1246 // need to convert data on the fly later if our souce is not PCM
1247 switch ( m_pwfmt_original->wFormatTag ) {
1248 case WAVE_FORMAT_PCM:
1249 m_wave_format = WAVE_FORMAT_PCM;
1250 m_wfmt.wBitsPerSample = m_pwfmt_original->wBitsPerSample;
1253 case WAVE_FORMAT_ADPCM:
1254 m_wave_format = WAVE_FORMAT_ADPCM;
1255 m_wfmt.wBitsPerSample = 16;
1259 nprintf(("SOUND", "SOUND => Not supporting %d format for playing wave files\n"));
1266 // Set up the WAVEFORMATEX structure to have the right PCM characteristics
1267 m_wfmt.wFormatTag = WAVE_FORMAT_PCM;
1268 m_wfmt.nChannels = m_pwfmt_original->nChannels;
1269 m_wfmt.nSamplesPerSec = m_pwfmt_original->nSamplesPerSec;
1271 m_wfmt.nBlockAlign = (unsigned short)(( m_wfmt.nChannels * m_wfmt.wBitsPerSample ) / 8);
1272 m_wfmt.nAvgBytesPerSec = m_wfmt.nBlockAlign * m_wfmt.nSamplesPerSec;
1274 // Init some member data from format chunk
1275 m_nBlockAlign = m_pwfmt_original->nBlockAlign;
1276 m_nUncompressedAvgDataRate = m_wfmt.nAvgBytesPerSec;
1278 // Cue for streaming
1285 // Handle all errors here
1286 nprintf(("SOUND","SOUND ==> Could not open wave file %s for streaming\n",pszFilename));
1291 mmioClose( cfp, 0 );
1294 if (m_pwfmt_original)
1296 free(m_pwfmt_original);
1297 m_pwfmt_original = NULL;
1307 // Set the file pointer to the start of wave data
1309 BOOL WaveFile::Cue (void)
1311 BOOL fRtn = SUCCESS; // assume success
1314 m_total_uncompressed_bytes_read = 0;
1315 m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
1317 rval = mmioSeek( cfp, m_data_offset, SEEK_SET );
1322 m_data_bytes_left = m_nDataSize;
1323 m_abort_next_read = FALSE;
1331 // Returns number of bytes actually read.
1333 // Returns -1 if there is nothing more to be read. This function can return 0, since
1334 // sometimes the amount of bytes requested is too small for the ACM decompression to
1335 // locate a suitable block
1336 int WaveFile::Read(BYTE *pbDest, UINT cbSize, int service)
1338 unsigned char *dest_buf=NULL, *uncompressed_wave_data;
1339 int rc, uncompressed_bytes_written;
1340 unsigned int src_bytes_used, convert_len, num_bytes_desired=0, num_bytes_read;
1342 // nprintf(("Alan","Reqeusted: %d\n", cbSize));
1346 uncompressed_wave_data = Wavedata_service_buffer;
1348 uncompressed_wave_data = Wavedata_load_buffer;
1351 switch ( m_wave_format ) {
1352 case WAVE_FORMAT_PCM:
1353 num_bytes_desired = cbSize;
1357 case WAVE_FORMAT_ADPCM:
1358 if ( !m_hStream_open ) {
1359 if ( !ACM_stream_open(m_pwfmt_original, &m_wfxDest, (void**)&m_hStream), m_bits_per_sample_uncompressed ) {
1366 num_bytes_desired = cbSize;
1369 dest_buf = Compressed_service_buffer;
1371 dest_buf = Compressed_buffer;
1374 if ( num_bytes_desired <= 0 ) {
1375 num_bytes_desired = 0;
1376 // nprintf(("Alan","No bytes required for ADPCM time interval\n"));
1378 num_bytes_desired = ACM_query_source_size((void*)m_hStream, cbSize);
1379 // nprintf(("Alan","Num bytes desired: %d\n", num_bytes_desired));
1384 nprintf(("SOUND", "SOUND => Not supporting %d format for playing wave files\n"));
1394 // read data from disk
1395 if ( m_data_bytes_left <= 0 ) {
1397 uncompressed_bytes_written = 0;
1401 if ( m_data_bytes_left > 0 && num_bytes_desired > 0 ) {
1404 if ( num_bytes_desired <= (unsigned int)m_data_bytes_left ) {
1405 num_bytes_read = num_bytes_desired;
1408 num_bytes_read = m_data_bytes_left;
1411 actual_read = mmioRead( cfp, (char *)dest_buf, num_bytes_read );
1412 if ( (actual_read <= 0) || (m_abort_next_read) ) {
1414 uncompressed_bytes_written = 0;
1418 if ( num_bytes_desired >= (unsigned int)m_data_bytes_left ) {
1419 m_abort_next_read = 1;
1422 num_bytes_read = actual_read;
1425 // convert data if necessary, to PCM
1426 if ( m_wave_format == WAVE_FORMAT_ADPCM ) {
1427 if ( num_bytes_read > 0 ) {
1428 rc = ACM_convert((void*)m_hStream, dest_buf, num_bytes_read, uncompressed_wave_data, BIGBUF_SIZE, &convert_len, &src_bytes_used);
1432 if ( convert_len == 0 ) {
1437 Assert(src_bytes_used <= num_bytes_read);
1438 if ( src_bytes_used < num_bytes_read ) {
1439 // seek back file pointer to reposition before unused source data
1440 mmioSeek(cfp, src_bytes_used - num_bytes_read, SEEK_CUR);
1443 // Adjust number of bytes left
1444 m_data_bytes_left -= src_bytes_used;
1445 m_nBytesPlayed += src_bytes_used;
1446 uncompressed_bytes_written = convert_len;
1448 // Successful read, keep running total of number of data bytes read
1452 // Successful read, keep running total of number of data bytes read
1453 // Adjust number of bytes left
1454 m_data_bytes_left -= num_bytes_read;
1455 m_nBytesPlayed += num_bytes_read;
1456 uncompressed_bytes_written = num_bytes_read;
1462 uncompressed_bytes_written = 0;
1465 m_total_uncompressed_bytes_read += uncompressed_bytes_written;
1466 // nprintf(("Alan","Read: %d\n", uncompressed_bytes_written));
1467 return (uncompressed_bytes_written);
1473 // Returns 8 bits of data representing silence for the Wave file format.
1475 // Since we are dealing only with PCM format, we can fudge a bit and take
1476 // advantage of the fact that for all PCM formats, silence can be represented
1477 // by a single byte, repeated to make up the proper word size. The actual size
1478 // of a word of wave data depends on the format:
1480 // PCM Format Word Size Silence Data
1481 // 8-bit mono 1 byte 0x80
1482 // 8-bit stereo 2 bytes 0x8080
1483 // 16-bit mono 2 bytes 0x0000
1484 // 16-bit stereo 4 bytes 0x00000000
1486 BYTE WaveFile::GetSilenceData (void)
1488 BYTE bSilenceData = 0;
1490 // Silence data depends on format of Wave file
1491 if (m_pwfmt_original) {
1492 if (m_wfmt.wBitsPerSample == 8) {
1493 // For 8-bit formats (unsigned, 0 to 255)
1494 // Packed DWORD = 0x80808080;
1495 bSilenceData = 0x80;
1497 else if (m_wfmt.wBitsPerSample == 16) {
1498 // For 16-bit formats (signed, -32768 to 32767)
1499 // Packed DWORD = 0x00000000;
1500 bSilenceData = 0x00;
1510 return (bSilenceData);
1513 int Audiostream_inited = 0;
1514 AudioStreamServices * m_pass = NULL; // ptr to AudioStreamServices object
1516 #define MAX_AUDIO_STREAMS 30
1517 AudioStream Audio_streams[MAX_AUDIO_STREAMS];
1519 void audiostream_init()
1523 if ( Audiostream_inited == 1 )
1526 if ( !ACM_is_inited() ) {
1530 // Create and initialize AudioStreamServices object.
1531 // This must be done once and only once for each window that uses
1532 // streaming services.
1533 m_pass = (AudioStreamServices *)malloc(sizeof(AudioStreamServices));
1536 m_pass->Constructor();
1537 m_pass->Initialize();
1539 if ( !pDirectSound ) {
1544 // Allocate memory for the buffer which holds the uncompressed wave data that is streamed from the
1545 // disk during a load/cue
1546 if ( Wavedata_load_buffer == NULL ) {
1547 Wavedata_load_buffer = (unsigned char*)malloc(BIGBUF_SIZE);
1548 Assert(Wavedata_load_buffer != NULL);
1551 // Allocate memory for the buffer which holds the uncompressed wave data that is streamed from the
1552 // disk during a service interval
1553 if ( Wavedata_service_buffer == NULL ) {
1554 Wavedata_service_buffer = (unsigned char*)malloc(BIGBUF_SIZE);
1555 Assert(Wavedata_service_buffer != NULL);
1558 // Allocate memory for the buffer which holds the compressed wave data that is read from the hard disk
1559 if ( Compressed_buffer == NULL ) {
1560 Compressed_buffer = (unsigned char*)malloc(COMPRESSED_BUFFER_SIZE);
1561 Assert(Compressed_buffer != NULL);
1564 if ( Compressed_service_buffer == NULL ) {
1565 Compressed_service_buffer = (unsigned char*)malloc(COMPRESSED_BUFFER_SIZE);
1566 Assert(Compressed_service_buffer != NULL);
1569 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1570 Audio_streams[i].Init_Data();
1571 Audio_streams[i].status = ASF_FREE;
1572 Audio_streams[i].type = ASF_NONE;
1575 InitializeCriticalSection( &Global_service_lock );
1577 Audiostream_inited = 1;
1580 // Close down the audiostream system. Must call audiostream_init() before any audiostream functions can
1582 void audiostream_close()
1585 if ( Audiostream_inited == 0 )
1588 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1589 if ( Audio_streams[i].status == ASF_USED ) {
1590 Audio_streams[i].status = ASF_FREE;
1591 Audio_streams[i].Destroy();
1595 // Destroy AudioStreamServices object
1601 // free global buffers
1602 if ( Wavedata_load_buffer ) {
1603 free(Wavedata_load_buffer);
1604 Wavedata_load_buffer = NULL;
1607 if ( Wavedata_service_buffer ) {
1608 free(Wavedata_service_buffer);
1609 Wavedata_service_buffer = NULL;
1612 if ( Compressed_buffer ) {
1613 free(Compressed_buffer);
1614 Compressed_buffer = NULL;
1617 if ( Compressed_service_buffer ) {
1618 free(Compressed_service_buffer);
1619 Compressed_service_buffer = NULL;
1622 DeleteCriticalSection( &Global_service_lock );
1624 Audiostream_inited = 0;
1627 // Open a digital sound file for streaming
1629 // input: filename => disk filename of sound file
1630 // type => what type of audio stream do we want to open:
1635 // returns: success => handle to identify streaming sound
1637 int audiostream_open( char * filename, int type )
1640 if (!Audiostream_inited || !snd_is_inited())
1643 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1644 if ( Audio_streams[i].status == ASF_FREE ) {
1645 Audio_streams[i].status = ASF_USED;
1646 Audio_streams[i].type = type;
1651 if ( i == MAX_AUDIO_STREAMS ) {
1652 nprintf(("Sound", "SOUND => No more audio streams available!\n"));
1659 Audio_streams[i].m_bits_per_sample_uncompressed = 8;
1661 case ASF_EVENTMUSIC:
1662 Audio_streams[i].m_bits_per_sample_uncompressed = 16;
1669 rc = Audio_streams[i].Create(filename, m_pass);
1671 Audio_streams[i].status = ASF_FREE;
1679 void audiostream_close_file(int i, int fade)
1681 if (!Audiostream_inited)
1687 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1689 if ( Audio_streams[i].status == ASF_USED ) {
1690 if ( fade == TRUE ) {
1691 Audio_streams[i].Fade_and_Destroy();
1694 Audio_streams[i].Destroy();
1699 void audiostream_close_all(int fade)
1703 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1704 if ( Audio_streams[i].status == ASF_FREE )
1707 audiostream_close_file(i, fade);
1711 extern int ds_convert_volume(float volume);
1713 void audiostream_play(int i, float volume, int looping)
1715 if (!Audiostream_inited)
1721 Assert(looping >= 0);
1722 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1724 // convert from 0->1 to -10000->0 for volume
1725 int converted_volume;
1726 if ( volume == -1 ) {
1727 converted_volume = Audio_streams[i].Get_Default_Volume();
1730 Assert(volume >= 0.0f && volume <= 1.0f );
1731 converted_volume = ds_convert_volume(volume);
1734 Assert( Audio_streams[i].status == ASF_USED );
1735 Audio_streams[i].Set_Default_Volume(converted_volume);
1736 Audio_streams[i].Play(converted_volume, looping);
1739 void audiostream_stop(int i, int rewind, int paused)
1741 if (!Audiostream_inited) return;
1746 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1747 Assert( Audio_streams[i].status == ASF_USED );
1750 Audio_streams[i].Stop_and_Rewind();
1752 Audio_streams[i].Stop(paused);
1755 int audiostream_is_playing(int i)
1760 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1761 if ( Audio_streams[i].status != ASF_USED )
1764 return Audio_streams[i].Is_Playing();
1768 void audiostream_set_volume_all(float volume, int type)
1772 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1773 if ( Audio_streams[i].status == ASF_FREE )
1776 if ( Audio_streams[i].type == type ) {
1777 int converted_volume;
1778 converted_volume = ds_convert_volume(volume);
1779 Audio_streams[i].Set_Volume(converted_volume);
1785 void audiostream_set_volume(int i, float volume)
1790 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1791 Assert( volume >= 0 && volume <= 1);
1793 if ( Audio_streams[i].status == ASF_FREE )
1796 int converted_volume;
1797 converted_volume = ds_convert_volume(volume);
1798 Audio_streams[i].Set_Volume(converted_volume);
1802 int audiostream_is_paused(int i)
1807 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1808 if ( Audio_streams[i].status == ASF_FREE )
1812 is_paused = Audio_streams[i].Is_Paused();
1817 void audiostream_set_byte_cutoff(int i, unsigned int cutoff)
1822 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1823 Assert( cutoff > 0 );
1825 if ( Audio_streams[i].status == ASF_FREE )
1828 Audio_streams[i].Set_Byte_Cutoff(cutoff);
1832 unsigned int audiostream_get_bytes_committed(int i)
1837 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1839 if ( Audio_streams[i].status == ASF_FREE )
1842 unsigned int num_bytes_committed;
1843 num_bytes_committed = Audio_streams[i].Get_Bytes_Committed();
1844 return num_bytes_committed;
1847 int audiostream_done_reading(int i)
1852 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1854 if ( Audio_streams[i].status == ASF_FREE )
1858 done_reading = Audio_streams[i].Is_Past_Limit();
1859 return done_reading;
1863 int audiostream_is_inited()
1865 return Audiostream_inited;
1868 // pause a single audio stream, indentified by handle i.
1869 void audiostream_pause(int i)
1874 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1875 if ( Audio_streams[i].status == ASF_FREE )
1878 if ( audiostream_is_playing(i) == TRUE ) {
1879 audiostream_stop(i, 0, 1);
1883 // pause all audio streams that are currently playing.
1884 void audiostream_pause_all()
1888 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1889 if ( Audio_streams[i].status == ASF_FREE )
1892 audiostream_pause(i);
1896 // unpause the audio stream identified by handle i.
1897 void audiostream_unpause(int i)
1904 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1905 if ( Audio_streams[i].status == ASF_FREE )
1908 if ( audiostream_is_paused(i) == TRUE ) {
1909 is_looping = Audio_streams[i].Is_looping();
1910 audiostream_play(i, -1.0f, is_looping);
1914 // unpause all audio streams that are currently paused
1915 void audiostream_unpause_all()
1919 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1920 if ( Audio_streams[i].status == ASF_FREE )
1923 audiostream_unpause(i);