2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Sound/AudioStr.cpp $
15 * Routines to stream large WAV files from disk
18 * Revision 1.5 2002/06/09 04:41:26 relnev
19 * added copyright header
21 * Revision 1.4 2002/06/05 04:03:33 relnev
22 * finished cfilesystem.
24 * removed some old code.
26 * fixed mouse save off-by-one.
30 * Revision 1.3 2002/05/27 18:42:50 theoddone33
31 * Fix missing audiostr_* symbols
33 * Revision 1.2 2002/05/07 03:16:52 theoddone33
34 * The Great Newline Fix
36 * Revision 1.1.1.1 2002/05/03 03:28:10 root
40 * 5 9/14/99 1:32a Jimb
41 * Commented out Int3() that was hanging Jim's machine. Happens before
42 * sm2-07 command brief.
44 * 4 7/14/99 12:09p Jefff
45 * Make sure we're not servicing a bogus audiostream. Check for "used"
46 * after the critical section lock.
48 * 3 12/17/98 4:01p Andsager
49 * up wavedata buffer size to 180000 to allow stereo 16b/22KHz streaming
51 * 2 10/07/98 10:53a Dave
54 * 1 10/07/98 10:51a Dave
56 * 53 6/28/98 6:35p Lawrance
57 * move re-entrancy semaphore into audiostream class
59 * 52 5/24/98 4:42p Dan
60 * AL: Fix several bugs related to pausing and enabling/disabling event
63 * 51 5/21/98 11:57a Lawrance
64 * fix potential bug with transitions for music when in packfiles
66 * 50 5/15/98 9:09p Lawrance
67 * The last of the multi-threading fixes
69 * 49 5/15/98 7:57p Duncan
70 * AL: Fix race condition with music streaming
72 * 48 5/15/98 10:13a Lawrance
73 * remove unused audiostream member
75 * 47 5/14/98 5:45p Lawrance2
76 * Put critical section around audiostream destroying
78 * 46 5/12/98 5:40p Lawrance
79 * Add critical section code to the service buffer call.. since it is
80 * possible to release buffers while in this call
82 * 45 5/10/98 3:49p Sandeep
83 * Fix problem with having the audio streaming while trying to close down
86 * 44 4/30/98 4:53p John
87 * Restructured and cleaned up cfile code. Added capability to read off
88 * of CD-ROM drive and out of multiple pack files.
90 * 43 4/26/98 3:30a Lawrance
91 * Fix a couple of potential bugs
93 * 42 4/21/98 10:18a Dan
95 * 41 4/17/98 6:59a John
96 * Changed code the used 'new' and 'delete' to use 'malloc' and 'free'
97 * instead. Had to manually can constructors/destructors.
99 * 40 4/13/98 10:18a John
102 * 39 4/13/98 10:16a John
103 * Switched gettime back to timer_get_milliseconds, which is now thread
106 * 38 4/12/98 11:08p Lawrance
107 * switch back to using gettime() in separate threads
109 * 37 4/12/98 5:31p Lawrance
110 * use timer_get_milliseconds() instead of gettime()
112 * 36 4/06/98 12:36a Lawrance
113 * Ensure all non-music ADPCM files get decompressed to 8 bit.
115 * 35 4/03/98 4:56p Lawrance
116 * Upu the max audio streams to 30
118 * 34 3/31/98 4:50p Dan
119 * AL: Clean up all audio streams if necessary in
120 * event_music_level_close()
122 * 33 3/23/98 4:12p Lawrance
123 * Fix subtle bug with looping and fading out songs
125 * 32 2/18/98 5:49p Lawrance
126 * Even if the ADPCM codec is unavailable, allow game to continue.
128 * 31 2/15/98 4:43p Lawrance
129 * work on real-time voice
131 * 30 1/19/98 11:37p Lawrance
132 * Fixing Optimization build warnings
134 * 29 1/17/98 4:41p Lawrance
135 * Fix problem with multiple audio streams using the same buffers
137 * 28 1/16/98 11:49a Lawrance
138 * Use own internal timer for fading.
140 * 27 12/28/97 12:43p John
141 * Put in support for reading archive files; Made missionload use the
142 * cf_get_file_list function. Moved demos directory out of data tree.
144 * 26 12/27/97 8:08p Lawrance
145 * If an audiostream doesn't exists, it can't be playing
147 * 25 12/18/97 3:30p Lawrance
148 * Fix bug that sometimes caused music with no volume to not get stopped
151 * 24 12/17/97 10:17p Allender
152 * redid streadming code to use mmio* functions instead of cf* functions.
153 * Our functions are not reentrant!
155 * 23 12/10/97 10:04p Lawrance
156 * modify what happens in Audio_stream constructor
158 * 22 12/09/97 6:14p Lawrance
161 * 21 12/08/97 6:21p Lawrance
162 * fix problems with signaling that end-of-file has been reached
164 * 20 12/05/97 10:50a Lawrance
165 * improve how silence bytes are written on transitions
167 * 19 12/04/97 5:35p Lawrance
168 * fix bug that may have caused errors when writing silence
170 * 18 11/28/97 2:09p Lawrance
171 * Overhaul how ADPCM conversion works... use much less memory... safer
174 * 17 10/03/97 8:24a Lawrance
175 * When unpausing, be sure to retain looping status
177 * 16 9/24/97 5:30p Lawrance
178 * fix bug that was messing up streaming of 8 bit audio
180 * 15 9/18/97 10:31p Lawrance
181 * add functions to pause and unpause all audio streams
183 * 14 9/09/97 3:39p Sandeep
184 * warning level 4 bugs
196 #include <mmsystem.h>
201 #include "audiostr.h"
202 #include "cfile.h" // needed for cf_get_path
204 #include "sound.h" /* for Snd_sram */
210 #define SUCCESS TRUE // Error returns for all member functions
211 #define FAILURE FALSE
216 typedef BOOL (*TIMERCALLBACK)(DWORD);
218 #define BIGBUF_SIZE 180000 // This can be reduced to 88200 once we don't use any stereo
219 //#define BIGBUF_SIZE 88300 // This can be reduced to 88200 once we don't use any stereo
220 unsigned char *Wavedata_load_buffer = NULL; // buffer used for cueing audiostreams
221 unsigned char *Wavedata_service_buffer = NULL; // buffer used for servicing audiostreams
223 CRITICAL_SECTION Global_service_lock;
225 #define COMPRESSED_BUFFER_SIZE 88300
226 unsigned char *Compressed_buffer = NULL; // Used to load in compressed data during a cueing interval
227 unsigned char *Compressed_service_buffer = NULL; // Used to read in compressed data during a service interval
229 #define AS_HIGHEST_MAX 999999999 // max uncompressed filesize supported is 999 meg
235 // Wrapper class for Windows multimedia timer services. Provides
236 // both periodic and one-shot events. User must supply callback
237 // for periodic events.
243 void constructor(void);
244 void destructor(void);
245 BOOL Create (UINT nPeriod, UINT nRes, DWORD dwUser, TIMERCALLBACK pfnCallback);
247 static void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);
248 TIMERCALLBACK m_pfnCallback;
260 // WAV file class (read-only).
273 BOOL Open (LPSTR pszFilename);
275 int Read (BYTE * pbDest, UINT cbSize, int service=1);
276 UINT GetNumBytesRemaining (void) { return (m_nDataSize - m_nBytesPlayed); }
277 UINT GetUncompressedAvgDataRate (void) { return (m_nUncompressedAvgDataRate); }
278 UINT GetDataSize (void) { return (m_nDataSize); }
279 UINT GetNumBytesPlayed (void) { return (m_nBytesPlayed); }
280 BYTE GetSilenceData (void);
281 WAVEFORMATEX m_wfmt; // format of wave file used by Direct Sound
282 WAVEFORMATEX * m_pwfmt_original; // foramt of wave file from actual wave source
283 UINT m_total_uncompressed_bytes_read;
284 UINT m_max_uncompressed_bytes_to_read;
285 UINT m_bits_per_sample_uncompressed;
288 UINT m_data_offset; // number of bytes to actual wave data
289 int m_data_bytes_left;
292 UINT m_wave_format; // format of wave source (ie WAVE_FORMAT_PCM, WAVE_FORMAT_ADPCM)
293 UINT m_nBlockAlign; // wave data block alignment spec
294 UINT m_nUncompressedAvgDataRate; // average wave data rate
295 UINT m_nDataSize; // size of data chunk
296 UINT m_nBytesPlayed; // offset into data chunk
297 BOOL m_abort_next_read;
299 HACMSTREAM m_hStream;
301 WAVEFORMATEX m_wfxDest;
306 // AudioStreamServices
308 // DirectSound apportions services on a per-window basis to allow
309 // sound from background windows to be muted. The AudioStreamServices
310 // class encapsulates the initialization of DirectSound services.
312 // Each window that wants to create AudioStream objects must
313 // first create and initialize an AudioStreamServices object.
314 // All AudioStream objects must be destroyed before the associated
315 // AudioStreamServices object is destroyed.
316 class AudioStreamServices
319 void Constructor(void);
321 LPDIRECTSOUND GetPDS (void) { return m_pds; }
329 // Audio stream interface class for playing WAV files using DirectSound.
330 // Users of this class must create AudioStreamServices object before
331 // creating an AudioStream object.
347 BOOL Create (LPSTR pszFilename, AudioStreamServices * pass);
349 void Play (long volume, int looping);
350 int Is_Playing(){ return(m_fPlaying); }
351 int Is_Paused(){ return(m_bIsPaused); }
352 int Is_Past_Limit() { return m_bPastLimit; }
353 void Stop (int paused=0);
354 void Stop_and_Rewind (void);
355 void Fade_and_Destroy (void);
356 void Fade_and_Stop(void);
357 void Set_Volume(long vol);
360 void Set_Byte_Cutoff(unsigned int num_bytes_cutoff);
361 void Set_Default_Volume(long converted_volume) { m_lDefaultVolume = converted_volume; }
362 long Get_Default_Volume() { return m_lDefaultVolume; }
363 unsigned int Get_Bytes_Committed(void);
364 int Is_looping() { return m_bLooping; }
367 UINT m_bits_per_sample_uncompressed;
371 BOOL WriteWaveData (UINT cbSize, UINT* num_bytes_written,int service=1);
372 BOOL WriteSilence (UINT cbSize);
373 DWORD GetMaxWriteSize (void);
374 BOOL ServiceBuffer (void);
375 static BOOL TimerCallback (DWORD dwUser);
377 AudioStreamServices * m_pass; // ptr to AudioStreamServices object
378 LPDIRECTSOUNDBUFFER m_pdsb; // ptr to Direct Sound buffer
379 WaveFile * m_pwavefile; // ptr to WaveFile object
380 Timer m_timer; // ptr to Timer object
381 BOOL m_fCued; // semaphore (stream cued)
382 BOOL m_fPlaying; // semaphore (stream playing)
383 DSBUFFERDESC m_dsbd; // Direct Sound buffer description
384 LONG m_lInService; // reentrancy semaphore
385 UINT m_cbBufOffset; // last write position
386 UINT m_nBufLength; // length of sound buffer in msec
387 UINT m_cbBufSize; // size of sound buffer in bytes
388 UINT m_nBufService; // service interval in msec
389 UINT m_nTimeStarted; // time (in system time) playback started
391 BOOL m_bLooping; // whether or not to loop playback
392 BOOL m_bFade; // fade out music
393 BOOL m_bDestroy_when_faded;
394 LONG m_lVolume; // volume of stream ( 0 -> -10 000 )
395 LONG m_lCutoffVolume;
396 BOOL m_bIsPaused; // stream is stopped, but not rewinded
397 UINT m_silence_written; // number of bytes of silence written to buffer
398 UINT m_bReadingDone; // no more bytes to be read from disk, still have remaining buffer to play
399 DWORD m_fade_timer_id; // timestamp so we know when to start fade
400 DWORD m_finished_id; // timestamp so we know when we've played #bytes required
401 BOOL m_bPastLimit; // flag to show we've played past the number of bytes requred
402 LONG m_lDefaultVolume;
405 CRITICAL_SECTION write_lock;
409 // AudioStreamServices class implementation
411 ////////////////////////////////////////////////////////////
414 void AudioStreamServices::Constructor(void)
416 // Initialize member data
419 // It would seem to make sense to initialize DirectSound here,
420 // but because there could be an error, it's best done in a
421 // separate member function, ::Initialize.
425 extern LPDIRECTSOUND pDirectSound; // From Sound.cpp
429 BOOL AudioStreamServices::Initialize ()
432 BOOL fRtn = SUCCESS; // assume success
435 m_pds = pDirectSound;
444 // AudioStream class implementation
446 ////////////////////////////////////////////////////////////
448 // The following constants are the defaults for our streaming buffer operation.
449 const UINT DefBufferLength = 2000; // default buffer length in msec
450 const UINT DefBufferServiceInterval = 250; // default buffer service interval in msec
453 AudioStream::AudioStream (void)
455 InitializeCriticalSection( &write_lock );
460 AudioStream::~AudioStream (void)
462 DeleteCriticalSection( &write_lock );
466 void AudioStream::Init_Data ()
472 m_bPastLimit = FALSE;
474 m_bDestroy_when_faded = FALSE;
476 m_lCutoffVolume = -10000;
478 m_silence_written = 0;
479 m_bReadingDone = FALSE;
483 m_fPlaying = m_fCued = FALSE;
484 m_lInService = FALSE;
486 m_nBufLength = DefBufferLength;
488 m_nBufService = DefBufferServiceInterval;
493 BOOL AudioStream::Create (LPSTR pszFilename, AudioStreamServices * pass)
495 BOOL fRtn = SUCCESS; // assume success
503 if (pszFilename && m_pass) {
504 // Create a new WaveFile object
506 m_pwavefile = (WaveFile *)malloc(sizeof(WaveFile));
513 m_pwavefile->m_bits_per_sample_uncompressed = m_bits_per_sample_uncompressed;
514 if (m_pwavefile->Open (pszFilename)) {
515 // Calculate sound buffer size in bytes
516 // Buffer size is average data rate times length of buffer
517 // No need for buffer to be larger than wave data though
518 m_cbBufSize = (m_pwavefile->GetUncompressedAvgDataRate () * m_nBufLength) / 1000;
519 nprintf(("SOUND", "SOUND => Stream buffer created using %d bytes\n", m_cbBufSize));
520 // m_cbBufSize = (m_cbBufSize > m_pwavefile->GetDataSize ()) ? m_pwavefile->GetDataSize () : m_cbBufSize;
522 //nprintf(("Sound", "SOUND => average data rate = %d\n\r", m_pwavefile->GetUncompressedAvgDataRate ()));
523 //nprintf(("Sound", "SOUND => m_cbBufSize = %d\n\r", m_cbBufSize));
525 // Create sound buffer
527 memset (&m_dsbd, 0, sizeof (DSBUFFERDESC));
528 m_dsbd.dwSize = sizeof (DSBUFFERDESC);
529 m_dsbd.dwBufferBytes = m_cbBufSize;
530 m_dsbd.lpwfxFormat = &m_pwavefile->m_wfmt;
531 m_dsbd.dwFlags = DSBCAPS_STATIC | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_LOCSOFTWARE;
533 hr = (m_pass->GetPDS ())->CreateSoundBuffer (&m_dsbd, &m_pdsb, NULL);
537 Snd_sram += m_cbBufSize;
540 // Error, unable to create DirectSound buffer
541 nprintf(("Sound", "SOUND => Error, unable to create DirectSound buffer\n\r"));
542 if (hr == DSERR_BADFORMAT) {
543 nprintf(("Sound", "SOUND => Bad format (probably ADPCM)\n\r"));
550 // Error opening file
551 nprintf(("SOUND", "SOUND => Failed to open wave file: %s\n\r", pszFilename));
552 m_pwavefile->Close();
559 // Error, unable to create WaveFile object
560 nprintf(("Sound", "SOUND => Failed to create WaveFile object %s\n\r", pszFilename));
565 // Error, passed invalid parms
574 BOOL AudioStream::Destroy (void)
578 EnterCriticalSection(&write_lock);
583 // Release DirectSound buffer
587 Snd_sram -= m_cbBufSize;
590 // Delete WaveFile object
592 m_pwavefile->Close();
599 LeaveCriticalSection(&write_lock);
606 // Writes wave data to sound buffer. This is a helper method used by Create and
607 // ServiceBuffer; it's not exposed to users of the AudioStream class.
608 BOOL AudioStream::WriteWaveData (UINT size, UINT *num_bytes_written, int service)
611 LPBYTE lpbuf1 = NULL;
612 LPBYTE lpbuf2 = NULL;
615 DWORD dwbyteswritten1 = 0;
616 DWORD dwbyteswritten2 = 0;
618 unsigned char *uncompressed_wave_data;
620 *num_bytes_written = 0;
622 if ( size == 0 || m_bReadingDone ) {
626 if ( !m_pdsb || !m_pwavefile ) {
631 EnterCriticalSection(&Global_service_lock);
635 uncompressed_wave_data = Wavedata_service_buffer;
637 uncompressed_wave_data = Wavedata_load_buffer;
640 int num_bytes_read = 0;
642 // Lock the sound buffer
643 hr = m_pdsb->Lock (m_cbBufOffset, size, (void**)(&lpbuf1), &dwsize1, (void**)(&lpbuf2), &dwsize2, 0);
645 // Write data to sound buffer. Because the sound buffer is circular, we may have to
646 // do two write operations if locked portion of buffer wraps around to start of buffer.
649 num_bytes_read = m_pwavefile->Read(uncompressed_wave_data, dwsize1+dwsize2,service);
650 if ( num_bytes_read == -1 ) {
651 // means nothing left to read!
656 if ( num_bytes_read > 0 ) {
657 if ( (unsigned int)num_bytes_read > dwsize1 ) {
658 dwbyteswritten1 = dwsize1;
659 dwbyteswritten2 = num_bytes_read - dwsize1;
661 memcpy(lpbuf1, uncompressed_wave_data, dwsize1);
663 memcpy(lpbuf2, uncompressed_wave_data+dwsize1, num_bytes_read-dwsize1);
665 dwbyteswritten1 = num_bytes_read;
667 memcpy(lpbuf1, uncompressed_wave_data, num_bytes_read);
671 // Update our buffer offset and unlock sound buffer
672 m_cbBufOffset = (m_cbBufOffset + dwbyteswritten1 + dwbyteswritten2) % m_cbBufSize;
673 *num_bytes_written = dwbyteswritten1 + dwbyteswritten2;
674 m_pdsb->Unlock (lpbuf1, dwsize1, lpbuf2, dwsize2);
677 // Error locking sound buffer
678 nprintf(("SOUND", "SOUND ==> Error, unable to lock sound buffer in AudioStr\n"));
683 LeaveCriticalSection(&Global_service_lock);
692 // Writes silence to sound buffer. This is a helper method used by
693 // ServiceBuffer; it's not exposed to users of the AudioStream class.
694 BOOL AudioStream::WriteSilence (UINT size)
697 LPBYTE lpbuf1 = NULL;
698 LPBYTE lpbuf2 = NULL;
701 DWORD dwbyteswritten1 = 0;
702 DWORD dwbyteswritten2 = 0;
705 // Lock the sound buffer
706 hr = m_pdsb->Lock (m_cbBufOffset, size, (void**)(&lpbuf1), &dwsize1, (void**)(&lpbuf2), &dwsize2, 0);
709 // Get silence data for this file format. Although word sizes vary for different
710 // wave file formats, ::Lock will always return pointers on word boundaries.
711 // Because silence data for 16-bit PCM formats is 0x0000 or 0x00000000, we can
712 // get away with writing bytes and ignoring word size here.
713 BYTE bSilence = m_pwavefile->GetSilenceData ();
715 // Write silence to sound buffer. Because the sound buffer is circular, we may have to
716 // do two write operations if locked portion of buffer wraps around to start of buffer.
717 memset (lpbuf1, bSilence, dwsize1);
718 dwbyteswritten1 = dwsize1;
720 // Second write required?
722 memset (lpbuf2, bSilence, dwsize2);
723 dwbyteswritten2 = dwsize2;
726 // Update our buffer offset and unlock sound buffer
727 m_cbBufOffset = (m_cbBufOffset + dwbyteswritten1 + dwbyteswritten2) % m_cbBufSize;
728 // m_pdsb->Unlock (lpbuf1, dwbyteswritten1, lpbuf2, dwbyteswritten2);
729 m_pdsb->Unlock (lpbuf1, dwsize1, lpbuf2, dwsize2);
732 // Error locking sound buffer
733 nprintf(("SOUND", "SOUND ==> Error, unable to lock sound buffer in AudioStr\n"));
743 // Helper function to calculate max size of sound buffer write operation, i.e. how much
744 // free space there is in buffer.
745 DWORD AudioStream::GetMaxWriteSize (void)
747 DWORD dwWriteCursor, dwPlayCursor, dwMaxSize;
749 // Get current play position
750 if (m_pdsb->GetCurrentPosition (&dwPlayCursor, &dwWriteCursor) == DS_OK) {
751 if (m_cbBufOffset <= dwPlayCursor) {
752 // Our write position trails play cursor
753 dwMaxSize = dwPlayCursor - m_cbBufOffset;
756 else {// (m_cbBufOffset > dw7Cursor)
757 // Play cursor has wrapped
758 dwMaxSize = m_cbBufSize - m_cbBufOffset + dwPlayCursor;
762 // GetCurrentPosition call failed
767 // nprintf(("Alan","Max write size: %d\n", dwMaxSize));
774 // Routine to service buffer requests initiated by periodic timer.
776 // Returns TRUE if buffer serviced normally; otherwise returns FALSE.
777 #define FADE_VOLUME_INTERVAL 400 // 100 == 1db
778 #define VOLUME_ATTENUATION_BEFORE_CUTOFF 3000 // 12db
779 BOOL AudioStream::ServiceBuffer (void)
784 if ( status != ASF_USED )
787 EnterCriticalSection(&write_lock);
789 // status may have changed, so lets check once again
790 if ( status != ASF_USED ){
791 LeaveCriticalSection(&write_lock);
795 // Check for reentrance
796 if (InterlockedExchange (&m_lInService, TRUE) == FALSE) {
797 if ( m_bFade == TRUE ) {
798 if ( m_lCutoffVolume == -10000 ) {
800 // nprintf(("Alan","Volume is: %d\n",vol));
801 m_lCutoffVolume = max(vol - VOLUME_ATTENUATION_BEFORE_CUTOFF, -10000);
805 vol = vol - FADE_VOLUME_INTERVAL; // decrease by 1db
806 // nprintf(("Alan","Volume is now: %d\n",vol));
809 // nprintf(("Sound","SOUND => Volume for stream sound is %d\n",vol));
810 // nprintf(("Alan","Cuttoff Volume is: %d\n",m_lCutoffVolume));
811 if ( vol < m_lCutoffVolume ) {
813 m_lCutoffVolume = -10000;
814 if ( m_bDestroy_when_faded == TRUE ) {
815 LeaveCriticalSection(&write_lock);
817 // Reset reentrancy semaphore
818 InterlockedExchange (&m_lInService, FALSE);
823 // Reset reentrancy semaphore
824 LeaveCriticalSection(&write_lock);
825 InterlockedExchange (&m_lInService, FALSE);
831 // All of sound not played yet, send more data to buffer
832 DWORD dwFreeSpace = GetMaxWriteSize ();
834 // Determine free space in sound buffer
837 // Some wave data remains, but not enough to fill free space
838 // Send wave data to buffer, fill remainder of free space with silence
839 uint num_bytes_written;
841 if (WriteWaveData (dwFreeSpace, &num_bytes_written) == SUCCESS) {
842 // nprintf(("Alan","Num bytes written: %d\n", num_bytes_written));
844 if ( m_pwavefile->m_total_uncompressed_bytes_read >= m_pwavefile->m_max_uncompressed_bytes_to_read ) {
845 m_fade_timer_id = timer_get_milliseconds() + 1700; // start fading 1.7 seconds from now
846 m_finished_id = timer_get_milliseconds() + 2000; // 2 seconds left to play out buffer
847 m_pwavefile->m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
850 if ( (m_fade_timer_id>0) && ((uint)timer_get_milliseconds() > m_fade_timer_id) ) {
855 if ( (m_finished_id>0) && ((uint)timer_get_milliseconds() > m_finished_id) ) {
860 if ( (num_bytes_written < dwFreeSpace) && m_bReadingDone ) {
861 int num_bytes_silence;
862 num_bytes_silence = dwFreeSpace - num_bytes_written;
864 if ( num_bytes_silence > 0 ) {
866 m_silence_written += num_bytes_silence;
867 if (WriteSilence (num_bytes_silence) == FAILURE) {
872 if ( m_silence_written >= m_cbBufSize ) {
873 m_silence_written = 0;
875 if ( m_bDestroy_when_faded == TRUE ) {
876 LeaveCriticalSection(&write_lock);
878 // Reset reentrancy semaphore
879 InterlockedExchange (&m_lInService, FALSE);
883 // All of sound has played, stop playback or loop again
884 if ( m_bLooping && !m_bFade) {
885 Play(m_lVolume, m_bLooping);
895 // Error writing wave data
901 // Reset reentrancy semaphore
902 InterlockedExchange (&m_lInService, FALSE);
904 // Service routine reentered. Do nothing, just return
908 LeaveCriticalSection(&write_lock);
913 void AudioStream::Cue (void)
915 UINT num_bytes_written;
921 m_bPastLimit = FALSE;
923 m_lCutoffVolume = -10000;
925 m_bDestroy_when_faded = FALSE;
930 // Reset file ptr, etc
933 // Reset DirectSound buffer
934 m_pdsb->SetCurrentPosition (0);
936 // Fill buffer with wave data
937 WriteWaveData (m_cbBufSize, &num_bytes_written,0);
945 void AudioStream::Play (long volume, int looping)
950 if ( m_bIsPaused == FALSE)
954 // Cue for playback if necessary
964 // Begin DirectSound playback
965 HRESULT hr = m_pdsb->Play (0, 0, DSBPLAY_LOOPING);
967 m_nTimeStarted = timer_get_milliseconds();
969 // Kick off timer to service buffer
970 m_timer.constructor();
972 m_timer.Create (m_nBufService, m_nBufService, DWORD (this), TimerCallback);
974 // Playback begun, no longer cued
979 // If the buffer was lost, try to restore it
980 if ( hr == DSERR_BUFFERLOST ) {
981 hr = m_pdsb->Restore();
983 hr = m_pdsb->Play (0, 0, DSBPLAY_LOOPING);
986 nprintf(("Sound", "Sound => Lost a buffer, tried restoring but got %s\n", get_DSERR_text(hr) ));
987 Int3(); // get Alan, he wants to see this
992 nprintf(("Sound", "Sound => Play failed with return value %s\n", get_DSERR_text(hr) ));
998 // Timer callback for Timer object created by ::Play method.
999 BOOL AudioStream::TimerCallback (DWORD dwUser)
1001 // dwUser contains ptr to AudioStream object
1002 AudioStream * pas = (AudioStream *) dwUser;
1004 return (pas->ServiceBuffer ());
1007 void AudioStream::Set_Byte_Cutoff(unsigned int byte_cutoff)
1009 if ( m_pwavefile == NULL )
1012 m_pwavefile->m_max_uncompressed_bytes_to_read = byte_cutoff;
1015 unsigned int AudioStream::Get_Bytes_Committed(void)
1017 if ( m_pwavefile == NULL )
1020 return m_pwavefile->m_total_uncompressed_bytes_read;
1025 void AudioStream::Fade_and_Destroy (void)
1028 m_bDestroy_when_faded = TRUE;
1032 void AudioStream::Fade_and_Stop (void)
1035 m_bDestroy_when_faded = FALSE;
1040 void AudioStream::Stop(int paused)
1043 // Stop DirectSound playback
1046 m_bIsPaused = paused;
1048 // Delete Timer object
1049 m_timer.destructor();
1054 void AudioStream::Stop_and_Rewind (void)
1057 // Stop DirectSound playback
1060 // Delete Timer object
1061 m_timer.destructor();
1066 m_fCued = FALSE; // this will cause wave file to start from beginning
1067 m_bReadingDone = FALSE;
1071 void AudioStream::Set_Volume(long vol)
1079 Assert( vol >= -10000 && vol <= 0 );
1080 h_result = m_pdsb->SetVolume(vol);
1082 if ( h_result != DS_OK )
1083 nprintf(("Sound","SOUND => SetVolume() failed with code '%s'\n", get_DSERR_text(h_result) ));
1088 long AudioStream::Get_Volume()
1094 void Timer::constructor(void)
1101 void Timer::destructor(void)
1104 timeKillEvent (m_nIDTimer);
1111 BOOL Timer::Create (UINT nPeriod, UINT nRes, DWORD dwUser, TIMERCALLBACK pfnCallback)
1113 BOOL bRtn = SUCCESS; // assume success
1115 Assert(pfnCallback);
1116 Assert(nPeriod > 10);
1117 Assert(nPeriod >= nRes);
1119 m_nPeriod = nPeriod;
1122 m_pfnCallback = pfnCallback;
1124 if ((m_nIDTimer = timeSetEvent (m_nPeriod, m_nRes, TimeProc, (DWORD) this, TIME_PERIODIC)) == NULL) {
1132 // Timer proc for multimedia timer callback set with timeSetTime().
1134 // Calls procedure specified when Timer object was created. The
1135 // dwUser parameter contains "this" pointer for associated Timer object.
1137 void CALLBACK Timer::TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
1139 // dwUser contains ptr to Timer object
1140 Timer * ptimer = (Timer *) dwUser;
1142 // Call user-specified callback and pass back user specified data
1143 (ptimer->m_pfnCallback) (ptimer->m_dwUser);
1147 // WaveFile class implementation
1149 ////////////////////////////////////////////////////////////
1152 void WaveFile::Init(void)
1154 // Init data members
1157 m_pwfmt_original = NULL;
1159 m_nUncompressedAvgDataRate = 0;
1162 m_total_uncompressed_bytes_read = 0;
1163 m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
1166 m_abort_next_read = FALSE;
1170 void WaveFile::Close(void)
1173 if (m_pwfmt_original) {
1174 free(m_pwfmt_original);
1175 m_pwfmt_original = NULL;
1178 if ( m_hStream_open ) {
1179 ACM_stream_close((void*)m_hStream);
1186 mmioClose( cfp, 0 );
1193 BOOL WaveFile::Open (LPSTR pszFilename)
1197 BOOL fRtn = SUCCESS; // assume success
1198 PCMWAVEFORMAT pcmwf;
1199 char fullpath[_MAX_PATH];
1201 m_total_uncompressed_bytes_read = 0;
1202 m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
1204 int FileSize, FileOffset;
1206 if ( !cf_find_file_location(pszFilename, CF_TYPE_ANY, fullpath, &FileSize, &FileOffset )) {
1210 cfp = mmioOpen(fullpath, NULL, MMIO_ALLOCBUF | MMIO_READ);
1211 if ( cfp == NULL ) {
1215 // Skip the "RIFF" tag and file size (8 bytes)
1216 // Skip the "WAVE" tag (4 bytes)
1217 mmioSeek( cfp, 12+FileOffset, SEEK_SET );
1219 // Now read RIFF tags until the end of file
1220 uint tag, size, next_chunk;
1222 while(done == FALSE) {
1223 if ( mmioRead(cfp, (char *)&tag, sizeof(uint)) != sizeof(uint) )
1226 if ( mmioRead(cfp, (char *)&size, sizeof(uint)) != sizeof(uint) )
1229 next_chunk = mmioSeek( cfp, 0, SEEK_CUR );
1233 case 0x20746d66: // The 'fmt ' tag
1234 mmioRead( cfp, (char *)&pcmwf, sizeof(PCMWAVEFORMAT) );
1235 if ( pcmwf.wf.wFormatTag != WAVE_FORMAT_PCM ) {
1236 mmioRead( cfp, (char *)&cbExtra, sizeof(short) );
1239 // Allocate memory for WAVEFORMATEX structure + extra bytes
1240 if ( (m_pwfmt_original = (WAVEFORMATEX *) malloc ( sizeof(WAVEFORMATEX)+cbExtra )) != NULL ){
1241 Assert(m_pwfmt_original != NULL);
1242 // Copy bytes from temporary format structure
1243 memcpy (m_pwfmt_original, &pcmwf, sizeof(pcmwf));
1244 m_pwfmt_original->cbSize = cbExtra;
1246 // Read those extra bytes, append to WAVEFORMATEX structure
1248 mmioRead( cfp, (char *)((ubyte *)(m_pwfmt_original) + sizeof(WAVEFORMATEX)), cbExtra );
1252 Int3(); // malloc failed
1257 case 0x61746164: // the 'data' tag
1258 m_nDataSize = size; // This is size of data chunk. Compressed if ADPCM.
1259 m_data_bytes_left = size;
1260 m_data_offset = mmioSeek( cfp, 0, SEEK_CUR);
1264 default: // unknown, skip it
1268 mmioSeek( cfp, next_chunk, SEEK_SET );
1271 // At this stage, examine source format, and set up WAVEFORATEX structure for DirectSound.
1272 // Since DirectSound only supports PCM, force this structure to be PCM compliant. We will
1273 // need to convert data on the fly later if our souce is not PCM
1274 switch ( m_pwfmt_original->wFormatTag ) {
1275 case WAVE_FORMAT_PCM:
1276 m_wave_format = WAVE_FORMAT_PCM;
1277 m_wfmt.wBitsPerSample = m_pwfmt_original->wBitsPerSample;
1280 case WAVE_FORMAT_ADPCM:
1281 m_wave_format = WAVE_FORMAT_ADPCM;
1282 m_wfmt.wBitsPerSample = 16;
1286 nprintf(("SOUND", "SOUND => Not supporting %d format for playing wave files\n"));
1293 // Set up the WAVEFORMATEX structure to have the right PCM characteristics
1294 m_wfmt.wFormatTag = WAVE_FORMAT_PCM;
1295 m_wfmt.nChannels = m_pwfmt_original->nChannels;
1296 m_wfmt.nSamplesPerSec = m_pwfmt_original->nSamplesPerSec;
1298 m_wfmt.nBlockAlign = (unsigned short)(( m_wfmt.nChannels * m_wfmt.wBitsPerSample ) / 8);
1299 m_wfmt.nAvgBytesPerSec = m_wfmt.nBlockAlign * m_wfmt.nSamplesPerSec;
1301 // Init some member data from format chunk
1302 m_nBlockAlign = m_pwfmt_original->nBlockAlign;
1303 m_nUncompressedAvgDataRate = m_wfmt.nAvgBytesPerSec;
1305 // Cue for streaming
1312 // Handle all errors here
1313 nprintf(("SOUND","SOUND ==> Could not open wave file %s for streaming\n",pszFilename));
1318 mmioClose( cfp, 0 );
1321 if (m_pwfmt_original)
1323 free(m_pwfmt_original);
1324 m_pwfmt_original = NULL;
1334 // Set the file pointer to the start of wave data
1336 BOOL WaveFile::Cue (void)
1338 BOOL fRtn = SUCCESS; // assume success
1341 m_total_uncompressed_bytes_read = 0;
1342 m_max_uncompressed_bytes_to_read = AS_HIGHEST_MAX;
1344 rval = mmioSeek( cfp, m_data_offset, SEEK_SET );
1349 m_data_bytes_left = m_nDataSize;
1350 m_abort_next_read = FALSE;
1358 // Returns number of bytes actually read.
1360 // Returns -1 if there is nothing more to be read. This function can return 0, since
1361 // sometimes the amount of bytes requested is too small for the ACM decompression to
1362 // locate a suitable block
1363 int WaveFile::Read(BYTE *pbDest, UINT cbSize, int service)
1365 unsigned char *dest_buf=NULL, *uncompressed_wave_data;
1366 int rc, uncompressed_bytes_written;
1367 unsigned int src_bytes_used, convert_len, num_bytes_desired=0, num_bytes_read;
1369 // nprintf(("Alan","Reqeusted: %d\n", cbSize));
1373 uncompressed_wave_data = Wavedata_service_buffer;
1375 uncompressed_wave_data = Wavedata_load_buffer;
1378 switch ( m_wave_format ) {
1379 case WAVE_FORMAT_PCM:
1380 num_bytes_desired = cbSize;
1384 case WAVE_FORMAT_ADPCM:
1385 if ( !m_hStream_open ) {
1386 if ( !ACM_stream_open(m_pwfmt_original, &m_wfxDest, (void**)&m_hStream), m_bits_per_sample_uncompressed ) {
1393 num_bytes_desired = cbSize;
1396 dest_buf = Compressed_service_buffer;
1398 dest_buf = Compressed_buffer;
1401 if ( num_bytes_desired <= 0 ) {
1402 num_bytes_desired = 0;
1403 // nprintf(("Alan","No bytes required for ADPCM time interval\n"));
1405 num_bytes_desired = ACM_query_source_size((void*)m_hStream, cbSize);
1406 // nprintf(("Alan","Num bytes desired: %d\n", num_bytes_desired));
1411 nprintf(("SOUND", "SOUND => Not supporting %d format for playing wave files\n"));
1421 // read data from disk
1422 if ( m_data_bytes_left <= 0 ) {
1424 uncompressed_bytes_written = 0;
1428 if ( m_data_bytes_left > 0 && num_bytes_desired > 0 ) {
1431 if ( num_bytes_desired <= (unsigned int)m_data_bytes_left ) {
1432 num_bytes_read = num_bytes_desired;
1435 num_bytes_read = m_data_bytes_left;
1438 actual_read = mmioRead( cfp, (char *)dest_buf, num_bytes_read );
1439 if ( (actual_read <= 0) || (m_abort_next_read) ) {
1441 uncompressed_bytes_written = 0;
1445 if ( num_bytes_desired >= (unsigned int)m_data_bytes_left ) {
1446 m_abort_next_read = 1;
1449 num_bytes_read = actual_read;
1452 // convert data if necessary, to PCM
1453 if ( m_wave_format == WAVE_FORMAT_ADPCM ) {
1454 if ( num_bytes_read > 0 ) {
1455 rc = ACM_convert((void*)m_hStream, dest_buf, num_bytes_read, uncompressed_wave_data, BIGBUF_SIZE, &convert_len, &src_bytes_used);
1459 if ( convert_len == 0 ) {
1464 Assert(src_bytes_used <= num_bytes_read);
1465 if ( src_bytes_used < num_bytes_read ) {
1466 // seek back file pointer to reposition before unused source data
1467 mmioSeek(cfp, src_bytes_used - num_bytes_read, SEEK_CUR);
1470 // Adjust number of bytes left
1471 m_data_bytes_left -= src_bytes_used;
1472 m_nBytesPlayed += src_bytes_used;
1473 uncompressed_bytes_written = convert_len;
1475 // Successful read, keep running total of number of data bytes read
1479 // Successful read, keep running total of number of data bytes read
1480 // Adjust number of bytes left
1481 m_data_bytes_left -= num_bytes_read;
1482 m_nBytesPlayed += num_bytes_read;
1483 uncompressed_bytes_written = num_bytes_read;
1489 uncompressed_bytes_written = 0;
1492 m_total_uncompressed_bytes_read += uncompressed_bytes_written;
1493 // nprintf(("Alan","Read: %d\n", uncompressed_bytes_written));
1494 return (uncompressed_bytes_written);
1500 // Returns 8 bits of data representing silence for the Wave file format.
1502 // Since we are dealing only with PCM format, we can fudge a bit and take
1503 // advantage of the fact that for all PCM formats, silence can be represented
1504 // by a single byte, repeated to make up the proper word size. The actual size
1505 // of a word of wave data depends on the format:
1507 // PCM Format Word Size Silence Data
1508 // 8-bit mono 1 byte 0x80
1509 // 8-bit stereo 2 bytes 0x8080
1510 // 16-bit mono 2 bytes 0x0000
1511 // 16-bit stereo 4 bytes 0x00000000
1513 BYTE WaveFile::GetSilenceData (void)
1515 BYTE bSilenceData = 0;
1517 // Silence data depends on format of Wave file
1518 if (m_pwfmt_original) {
1519 if (m_wfmt.wBitsPerSample == 8) {
1520 // For 8-bit formats (unsigned, 0 to 255)
1521 // Packed DWORD = 0x80808080;
1522 bSilenceData = 0x80;
1524 else if (m_wfmt.wBitsPerSample == 16) {
1525 // For 16-bit formats (signed, -32768 to 32767)
1526 // Packed DWORD = 0x00000000;
1527 bSilenceData = 0x00;
1537 return (bSilenceData);
1540 AudioStreamServices * m_pass = NULL; // ptr to AudioStreamServices object
1542 #define MAX_AUDIO_STREAMS 30
1543 AudioStream Audio_streams[MAX_AUDIO_STREAMS];
1544 #endif // !PLAT_UNIX
1546 int Audiostream_inited = 0;
1548 void audiostream_init()
1552 if ( Audiostream_inited == 1 )
1558 if ( !ACM_is_inited() ) {
1562 // Create and initialize AudioStreamServices object.
1563 // This must be done once and only once for each window that uses
1564 // streaming services.
1565 m_pass = (AudioStreamServices *)malloc(sizeof(AudioStreamServices));
1568 m_pass->Constructor();
1569 m_pass->Initialize();
1571 if ( !pDirectSound ) {
1576 // Allocate memory for the buffer which holds the uncompressed wave data that is streamed from the
1577 // disk during a load/cue
1578 if ( Wavedata_load_buffer == NULL ) {
1579 Wavedata_load_buffer = (unsigned char*)malloc(BIGBUF_SIZE);
1580 Assert(Wavedata_load_buffer != NULL);
1583 // Allocate memory for the buffer which holds the uncompressed wave data that is streamed from the
1584 // disk during a service interval
1585 if ( Wavedata_service_buffer == NULL ) {
1586 Wavedata_service_buffer = (unsigned char*)malloc(BIGBUF_SIZE);
1587 Assert(Wavedata_service_buffer != NULL);
1590 // Allocate memory for the buffer which holds the compressed wave data that is read from the hard disk
1591 if ( Compressed_buffer == NULL ) {
1592 Compressed_buffer = (unsigned char*)malloc(COMPRESSED_BUFFER_SIZE);
1593 Assert(Compressed_buffer != NULL);
1596 if ( Compressed_service_buffer == NULL ) {
1597 Compressed_service_buffer = (unsigned char*)malloc(COMPRESSED_BUFFER_SIZE);
1598 Assert(Compressed_service_buffer != NULL);
1601 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1602 Audio_streams[i].Init_Data();
1603 Audio_streams[i].status = ASF_FREE;
1604 Audio_streams[i].type = ASF_NONE;
1607 InitializeCriticalSection( &Global_service_lock );
1610 Audiostream_inited = 1;
1613 // Close down the audiostream system. Must call audiostream_init() before any audiostream functions can
1615 void audiostream_close()
1618 if ( Audiostream_inited == 0 )
1624 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1625 if ( Audio_streams[i].status == ASF_USED ) {
1626 Audio_streams[i].status = ASF_FREE;
1627 Audio_streams[i].Destroy();
1631 // Destroy AudioStreamServices object
1637 // free global buffers
1638 if ( Wavedata_load_buffer ) {
1639 free(Wavedata_load_buffer);
1640 Wavedata_load_buffer = NULL;
1643 if ( Wavedata_service_buffer ) {
1644 free(Wavedata_service_buffer);
1645 Wavedata_service_buffer = NULL;
1648 if ( Compressed_buffer ) {
1649 free(Compressed_buffer);
1650 Compressed_buffer = NULL;
1653 if ( Compressed_service_buffer ) {
1654 free(Compressed_service_buffer);
1655 Compressed_service_buffer = NULL;
1658 DeleteCriticalSection( &Global_service_lock );
1660 Audiostream_inited = 0;
1663 // Open a digital sound file for streaming
1665 // input: filename => disk filename of sound file
1666 // type => what type of audio stream do we want to open:
1671 // returns: success => handle to identify streaming sound
1673 int audiostream_open( char * filename, int type )
1680 if (!Audiostream_inited || !snd_is_inited())
1683 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1684 if ( Audio_streams[i].status == ASF_FREE ) {
1685 Audio_streams[i].status = ASF_USED;
1686 Audio_streams[i].type = type;
1691 if ( i == MAX_AUDIO_STREAMS ) {
1692 nprintf(("Sound", "SOUND => No more audio streams available!\n"));
1699 Audio_streams[i].m_bits_per_sample_uncompressed = 8;
1701 case ASF_EVENTMUSIC:
1702 Audio_streams[i].m_bits_per_sample_uncompressed = 16;
1709 rc = Audio_streams[i].Create(filename, m_pass);
1711 Audio_streams[i].status = ASF_FREE;
1720 void audiostream_close_file(int i, int fade)
1722 if (!Audiostream_inited)
1731 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1733 if ( Audio_streams[i].status == ASF_USED ) {
1734 if ( fade == TRUE ) {
1735 Audio_streams[i].Fade_and_Destroy();
1738 Audio_streams[i].Destroy();
1744 void audiostream_close_all(int fade)
1751 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1752 if ( Audio_streams[i].status == ASF_FREE )
1755 audiostream_close_file(i, fade);
1760 extern int ds_convert_volume(float volume);
1762 void audiostream_play(int i, float volume, int looping)
1764 if (!Audiostream_inited)
1773 Assert(looping >= 0);
1774 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1776 // convert from 0->1 to -10000->0 for volume
1777 int converted_volume;
1778 if ( volume == -1 ) {
1779 converted_volume = Audio_streams[i].Get_Default_Volume();
1782 Assert(volume >= 0.0f && volume <= 1.0f );
1783 converted_volume = ds_convert_volume(volume);
1786 Assert( Audio_streams[i].status == ASF_USED );
1787 Audio_streams[i].Set_Default_Volume(converted_volume);
1788 Audio_streams[i].Play(converted_volume, looping);
1792 void audiostream_stop(int i, int rewind, int paused)
1794 if (!Audiostream_inited) return;
1802 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1803 Assert( Audio_streams[i].status == ASF_USED );
1806 Audio_streams[i].Stop_and_Rewind();
1808 Audio_streams[i].Stop(paused);
1812 int audiostream_is_playing(int i)
1821 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1822 if ( Audio_streams[i].status != ASF_USED )
1825 return Audio_streams[i].Is_Playing();
1830 void audiostream_set_volume_all(float volume, int type)
1837 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1838 if ( Audio_streams[i].status == ASF_FREE )
1841 if ( Audio_streams[i].type == type ) {
1842 int converted_volume;
1843 converted_volume = ds_convert_volume(volume);
1844 Audio_streams[i].Set_Volume(converted_volume);
1851 void audiostream_set_volume(int i, float volume)
1859 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1860 Assert( volume >= 0 && volume <= 1);
1862 if ( Audio_streams[i].status == ASF_FREE )
1865 int converted_volume;
1866 converted_volume = ds_convert_volume(volume);
1867 Audio_streams[i].Set_Volume(converted_volume);
1872 int audiostream_is_paused(int i)
1881 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1882 if ( Audio_streams[i].status == ASF_FREE )
1886 is_paused = Audio_streams[i].Is_Paused();
1892 void audiostream_set_byte_cutoff(int i, unsigned int cutoff)
1900 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1901 Assert( cutoff > 0 );
1903 if ( Audio_streams[i].status == ASF_FREE )
1906 Audio_streams[i].Set_Byte_Cutoff(cutoff);
1911 unsigned int audiostream_get_bytes_committed(int i)
1920 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1922 if ( Audio_streams[i].status == ASF_FREE )
1925 unsigned int num_bytes_committed;
1926 num_bytes_committed = Audio_streams[i].Get_Bytes_Committed();
1927 return num_bytes_committed;
1931 int audiostream_done_reading(int i)
1940 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1942 if ( Audio_streams[i].status == ASF_FREE )
1946 done_reading = Audio_streams[i].Is_Past_Limit();
1947 return done_reading;
1952 int audiostream_is_inited()
1954 return Audiostream_inited;
1957 // pause a single audio stream, indentified by handle i.
1958 void audiostream_pause(int i)
1966 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
1967 if ( Audio_streams[i].status == ASF_FREE )
1970 if ( audiostream_is_playing(i) == TRUE ) {
1971 audiostream_stop(i, 0, 1);
1976 // pause all audio streams that are currently playing.
1977 void audiostream_pause_all()
1984 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
1985 if ( Audio_streams[i].status == ASF_FREE )
1988 audiostream_pause(i);
1993 // unpause the audio stream identified by handle i.
1994 void audiostream_unpause(int i)
2004 Assert( i >= 0 && i < MAX_AUDIO_STREAMS );
2005 if ( Audio_streams[i].status == ASF_FREE )
2008 if ( audiostream_is_paused(i) == TRUE ) {
2009 is_looping = Audio_streams[i].Is_looping();
2010 audiostream_play(i, -1.0f, is_looping);
2015 // unpause all audio streams that are currently paused
2016 void audiostream_unpause_all()
2023 for ( i = 0; i < MAX_AUDIO_STREAMS; i++ ) {
2024 if ( Audio_streams[i].status == ASF_FREE )
2027 audiostream_unpause(i);