1 ////////////////////////////////////////////////////////////////////////////////
3 ////////////////////////////////////////////////////////////////////////////////
5 #include "include/jaguar.h"
6 #include "include/dsnd.h"
12 void dsound_reset(void);
16 #define SOUND_BUFFER (0.16) //Seconds
17 #define UNDEFINED 0xFFFFFF
19 int chipBufferLength; //Number of bytes to hold SOUND_BUFFER seconds.
22 int chipWrite =UNDEFINED; //Write Cursor
26 ////////////////////////////////////////////////////////////////////////////////
28 ////////////////////////////////////////////////////////////////////////////////
30 void ws_write_state(int fp)
35 void ws_read_state(int fp)
42 ////////////////////////////////////////////////////////////////////////////////
44 ////////////////////////////////////////////////////////////////////////////////
45 void ws_audio_init(void)
48 dsound_init(GetForegroundWindow(),1,FREQUENCE);
52 ////////////////////////////////////////////////////////////////////////////////
54 ////////////////////////////////////////////////////////////////////////////////
55 void ws_audio_reset(void)
65 ws_audio_stop_channel(i);
66 ws_audio_play_channel(i);
67 ws_audio_clear_channel(i);
70 IDirectSoundBuffer_SetPan(chipBuffer,0);
71 IDirectSoundBuffer_SetVolume(chipBuffer,0);
76 ////////////////////////////////////////////////////////////////////////////////
78 ////////////////////////////////////////////////////////////////////////////////
79 void ws_audio_done(void)
82 system_sound_shutdown();
87 ////////////////////////////////////////////////////////////////////////////////
89 ////////////////////////////////////////////////////////////////////////////////
90 void ws_audio_clear_channel(int Channel)
98 memset(buf1,0,sizeof(buf1));
99 chipWrite = UNDEFINED;
112 ////////////////////////////////////////////////////////////////////////////////
113 // start playing a channel
114 ////////////////////////////////////////////////////////////////////////////////
115 int ws_audio_play_channel(int Channel)
117 #ifdef _EMULATE_SOUND
123 IDirectSoundBuffer_Play(chipBuffer,0,0, DSBPLAY_LOOPING);
134 ////////////////////////////////////////////////////////////////////////////////
135 // stop playing a channel
136 ////////////////////////////////////////////////////////////////////////////////
137 int ws_audio_stop_channel(int Channel)
139 #ifdef _EMULATE_SOUND
144 IDirectSoundBuffer_Stop(chipBuffer);
158 void dsound_reset(void)
160 #ifdef _EMULATE_SOUND
161 BYTE *ppvAudioPtr1, *ppvAudioPtr2;
162 DWORD pdwAudioBytes1, pdwAudioBytes2;
165 chipWrite = UNDEFINED;
168 IDirectSoundBuffer_Stop(chipBuffer);
169 // Fill the sound buffer
170 if SUCCEEDED(IDirectSoundBuffer_Lock(chipBuffer,0,0,
171 (LPVOID*)&ppvAudioPtr1, &pdwAudioBytes1, (LPVOID*)&ppvAudioPtr2, &pdwAudioBytes2, DSBLOCK_ENTIREBUFFER))
173 if (ppvAudioPtr1 && pdwAudioBytes1)
174 memset(ppvAudioPtr1, 0, pdwAudioBytes1);
176 if (ppvAudioPtr2 && pdwAudioBytes2)
177 memset(ppvAudioPtr2, 0, pdwAudioBytes2);
179 IDirectSoundBuffer_Unlock(chipBuffer,
180 ppvAudioPtr1, pdwAudioBytes1, ppvAudioPtr2, pdwAudioBytes2);
185 IDirectSoundBuffer_Play(chipBuffer, 0,0, DSBPLAY_LOOPING );
187 IDirectSoundBuffer_Play(chipBuffer, 0,0, DSBPLAY_LOOPING );
193 void system_sound_shutdown(void)
195 #ifdef _EMULATE_SOUND
196 if (chipBuffer) IDirectSoundBuffer_Stop(chipBuffer);
197 if (chipBuffer) IDirectSoundBuffer_Release(chipBuffer);
199 if (primaryBuffer) IDirectSoundBuffer_Stop(primaryBuffer);
200 if (primaryBuffer) IDirectSoundBuffer_Release(primaryBuffer);
201 if (ds) IDirectSound_Release(ds);
202 if (buf1) free(buf1);
207 void system_sound_update(void)
209 #ifdef _EMULATE_SOUND
210 LPDWORD null_ptr=NULL;
211 int pdwAudioBytes1, pdwAudioBytes2;
212 int Write, LengthSamples;
213 UINT16 *chipPtr1, *chipPtr2;
215 // UNDEFINED write cursors
216 IDirectSoundBuffer_GetCurrentPosition(chipBuffer, 0, (LPDWORD)&Write);
217 if ((chipWrite == UNDEFINED) )
219 lastChipWrite = chipWrite= Write - 128;
220 return; //Wait a frame to accumulate length.
224 //SN76496 Sound Chips
226 if (Write < lastChipWrite) //Wrap?
227 lastChipWrite -= chipBufferLength;
229 LengthSamples = (Write - lastChipWrite) / 2;
230 lastChipWrite = Write;
233 pcm_updateOne(0,buf1,LengthSamples>>1);
235 if SUCCEEDED(IDirectSoundBuffer_Lock(
236 chipBuffer, chipWrite, LengthSamples*2,
237 (LPVOID*)&chipPtr1, (LPDWORD)&pdwAudioBytes1,
238 (LPVOID*)&chipPtr2, (LPDWORD)&pdwAudioBytes2, 0))
245 mov ecx,pdwAudioBytes1
259 mov ecx,pdwAudioBytes2
274 IDirectSoundBuffer_Unlock(chipBuffer,
275 chipPtr1, pdwAudioBytes1, chipPtr2, pdwAudioBytes2);
277 chipWrite+= (LengthSamples * 2);
278 if (chipWrite> chipBufferLength)
279 chipWrite-= chipBufferLength;
284 chipWrite= UNDEFINED;
285 IDirectSoundBuffer_GetStatus(chipBuffer, &status);
286 if (status & DSBSTATUS_BUFFERLOST)
288 if (IDirectSoundBuffer_Restore(chipBuffer) != DS_OK) return;
289 /*if (psg_on)*/ IDirectSoundBuffer_Play(chipBuffer, 0, 0, DSBPLAY_LOOPING);