5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups by James L. Hammons
8 // We really need to convert this to SDL... DirectSound won't cut it here!
16 int FREQUENCE = 44100;
18 void dsound_reset(void);
22 #define SOUND_BUFFER (0.16) //Seconds
23 #define UNDEFINED 0xFFFFFF
25 int chipBufferLength; //Number of bytes to hold SOUND_BUFFER seconds.
27 int lastChipWrite = 0;
28 int chipWrite = UNDEFINED; //Write Cursor
33 void ws_write_state(int fp)
37 void ws_read_state(int fp)
41 void ws_audio_init(void)
44 dsound_init(GetForegroundWindow(),1,FREQUENCE);
49 void ws_audio_reset(void)
59 ws_audio_stop_channel(i);
60 ws_audio_play_channel(i);
61 ws_audio_clear_channel(i);
64 IDirectSoundBuffer_SetPan(chipBuffer,0);
65 IDirectSoundBuffer_SetVolume(chipBuffer,0);
69 void ws_audio_done(void)
72 system_sound_shutdown();
76 void ws_audio_clear_channel(int Channel)
84 memset(buf1,0,sizeof(buf1));
85 chipWrite = UNDEFINED;
97 // start playing a channel
100 int ws_audio_play_channel(int Channel)
102 #ifdef _EMULATE_SOUND
108 IDirectSoundBuffer_Play(chipBuffer,0,0, DSBPLAY_LOOPING);
119 // stop playing a channel
122 int ws_audio_stop_channel(int Channel)
124 #ifdef _EMULATE_SOUND
129 IDirectSoundBuffer_Stop(chipBuffer);
141 void dsound_reset(void)
143 #ifdef _EMULATE_SOUND
144 BYTE *ppvAudioPtr1, *ppvAudioPtr2;
145 DWORD pdwAudioBytes1, pdwAudioBytes2;
148 chipWrite = UNDEFINED;
151 IDirectSoundBuffer_Stop(chipBuffer);
152 // Fill the sound buffer
153 if SUCCEEDED(IDirectSoundBuffer_Lock(chipBuffer,0,0,
154 (LPVOID*)&ppvAudioPtr1, &pdwAudioBytes1, (LPVOID*)&ppvAudioPtr2, &pdwAudioBytes2, DSBLOCK_ENTIREBUFFER))
156 if (ppvAudioPtr1 && pdwAudioBytes1)
157 memset(ppvAudioPtr1, 0, pdwAudioBytes1);
159 if (ppvAudioPtr2 && pdwAudioBytes2)
160 memset(ppvAudioPtr2, 0, pdwAudioBytes2);
162 IDirectSoundBuffer_Unlock(chipBuffer,
163 ppvAudioPtr1, pdwAudioBytes1, ppvAudioPtr2, pdwAudioBytes2);
168 IDirectSoundBuffer_Play(chipBuffer, 0,0, DSBPLAY_LOOPING );
170 IDirectSoundBuffer_Play(chipBuffer, 0,0, DSBPLAY_LOOPING );
174 void system_sound_shutdown(void)
176 #ifdef _EMULATE_SOUND
177 if (chipBuffer) IDirectSoundBuffer_Stop(chipBuffer);
178 if (chipBuffer) IDirectSoundBuffer_Release(chipBuffer);
180 if (primaryBuffer) IDirectSoundBuffer_Stop(primaryBuffer);
181 if (primaryBuffer) IDirectSoundBuffer_Release(primaryBuffer);
182 if (ds) IDirectSound_Release(ds);
183 if (buf1) free(buf1);
187 void system_sound_update(void)
189 #ifdef _EMULATE_SOUND
190 LPDWORD null_ptr=NULL;
191 int pdwAudioBytes1, pdwAudioBytes2;
192 int Write, LengthSamples;
193 UINT16 *chipPtr1, *chipPtr2;
195 // UNDEFINED write cursors
196 IDirectSoundBuffer_GetCurrentPosition(chipBuffer, 0, (LPDWORD)&Write);
197 if ((chipWrite == UNDEFINED) )
199 lastChipWrite = chipWrite= Write - 128;
200 return; //Wait a frame to accumulate length.
204 //SN76496 Sound Chips
206 if (Write < lastChipWrite) //Wrap?
207 lastChipWrite -= chipBufferLength;
209 LengthSamples = (Write - lastChipWrite) / 2;
210 lastChipWrite = Write;
213 pcm_updateOne(0,buf1,LengthSamples>>1);
215 if SUCCEEDED(IDirectSoundBuffer_Lock(
216 chipBuffer, chipWrite, LengthSamples*2,
217 (LPVOID*)&chipPtr1, (LPDWORD)&pdwAudioBytes1,
218 (LPVOID*)&chipPtr2, (LPDWORD)&pdwAudioBytes2, 0))
225 mov ecx,pdwAudioBytes1
239 mov ecx,pdwAudioBytes2
254 IDirectSoundBuffer_Unlock(chipBuffer,
255 chipPtr1, pdwAudioBytes1, chipPtr2, pdwAudioBytes2);
257 chipWrite+= (LengthSamples * 2);
258 if (chipWrite> chipBufferLength)
259 chipWrite-= chipBufferLength;
264 chipWrite= UNDEFINED;
265 IDirectSoundBuffer_GetStatus(chipBuffer, &status);
266 if (status & DSBSTATUS_BUFFERLOST)
268 if (IDirectSoundBuffer_Restore(chipBuffer) != DS_OK) return;
269 /*if (psg_on)*/ IDirectSoundBuffer_Play(chipBuffer, 0, 0, DSBPLAY_LOOPING);