]> Shamusworld >> Repos - virtualjaguar/blob - src/dsound_dummy.cpp
f81f4c66ab8495967ed44ca18f71e9f5b31cce57
[virtualjaguar] / src / dsound_dummy.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2 // Audio
3 ////////////////////////////////////////////////////////////////////////////////
4
5 #include "include/jaguar.h"
6 #include "include/dsnd.h"
7
8 int ym2413_enable;
9
10 int FREQUENCE=44100;
11
12 void dsound_reset(void);
13
14 #define LEFT 0
15 #define RIGHT 1
16 #define SOUND_BUFFER    (0.16)          //Seconds
17 #define UNDEFINED               0xFFFFFF
18
19 int chipBufferLength;   //Number of bytes to hold SOUND_BUFFER seconds.
20
21 int lastChipWrite  =0;
22 int chipWrite =UNDEFINED;       //Write Cursor
23
24 INT16* buf1;
25
26 ////////////////////////////////////////////////////////////////////////////////
27 //
28 ////////////////////////////////////////////////////////////////////////////////
29
30 void ws_write_state(int fp)
31 {
32 }
33
34
35 void ws_read_state(int fp)
36 {
37 }
38
39
40
41
42 ////////////////////////////////////////////////////////////////////////////////
43 //
44 ////////////////////////////////////////////////////////////////////////////////
45 void ws_audio_init(void)
46 {
47 #ifdef _EMULATE_SOUND
48         dsound_init(GetForegroundWindow(),1,FREQUENCE);
49 //      ws_audio_reset();
50 #endif
51 }
52 ////////////////////////////////////////////////////////////////////////////////
53 //
54 ////////////////////////////////////////////////////////////////////////////////
55 void ws_audio_reset(void)
56 {
57 #ifdef _EMULATE_SOUND
58  int i;
59  dsound_reset();
60
61          chipWrite=UNDEFINED;
62
63         for (i=0;i<4;i++)
64         {
65          ws_audio_stop_channel(i);
66          ws_audio_play_channel(i);
67          ws_audio_clear_channel(i);
68         }
69
70          IDirectSoundBuffer_SetPan(chipBuffer,0);
71          IDirectSoundBuffer_SetVolume(chipBuffer,0);
72 #endif
73 }
74
75
76 ////////////////////////////////////////////////////////////////////////////////
77 //
78 ////////////////////////////////////////////////////////////////////////////////
79 void ws_audio_done(void)
80 {
81 #ifdef _EMULATE_SOUND
82         system_sound_shutdown();
83 #endif
84 }
85
86
87 ////////////////////////////////////////////////////////////////////////////////
88 //
89 ////////////////////////////////////////////////////////////////////////////////
90 void ws_audio_clear_channel(int Channel)
91 {
92 #ifdef _EMULATE_SOUND
93
94  switch (Channel)
95  {
96  case 0:
97  case 1:
98           memset(buf1,0,sizeof(buf1));
99           chipWrite = UNDEFINED;
100           break;
101
102  case 2:
103  case 3:
104             break;
105
106  }
107 #endif
108 }
109
110
111
112 ////////////////////////////////////////////////////////////////////////////////
113 // start playing a channel
114 ////////////////////////////////////////////////////////////////////////////////
115 int ws_audio_play_channel(int Channel)
116 {
117 #ifdef _EMULATE_SOUND
118         switch (Channel)
119         {
120         case 0:
121         case 1:
122 //              if (psg_on)
123                 IDirectSoundBuffer_Play(chipBuffer,0,0, DSBPLAY_LOOPING);
124                 break;
125         case 2:
126         case 3:
127                 break;
128         }
129 #endif
130         return 0;
131 }
132
133
134 ////////////////////////////////////////////////////////////////////////////////
135 // stop playing a channel
136 ////////////////////////////////////////////////////////////////////////////////
137 int ws_audio_stop_channel(int Channel)
138 {
139 #ifdef _EMULATE_SOUND
140         switch (Channel)
141         {
142         case 0:
143         case 1:
144                 IDirectSoundBuffer_Stop(chipBuffer);
145                 chipWrite=UNDEFINED;
146                 break;
147
148         case 2:
149         case 3:
150                 break;
151         }
152 #endif
153         return(0);
154 }
155
156
157
158 void dsound_reset(void)
159 {
160 #ifdef _EMULATE_SOUND
161         BYTE    *ppvAudioPtr1, *ppvAudioPtr2;
162         DWORD   pdwAudioBytes1, pdwAudioBytes2;
163
164
165          chipWrite = UNDEFINED;
166          if (chipBuffer)
167          {
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))
172                 {
173                         if (ppvAudioPtr1 && pdwAudioBytes1)
174                                 memset(ppvAudioPtr1, 0, pdwAudioBytes1);
175
176                         if (ppvAudioPtr2 && pdwAudioBytes2)
177                                 memset(ppvAudioPtr2, 0, pdwAudioBytes2);
178                         
179                         IDirectSoundBuffer_Unlock(chipBuffer, 
180                                 ppvAudioPtr1, pdwAudioBytes1, ppvAudioPtr2, pdwAudioBytes2);
181                 }
182
183                 //Start playing
184 //              if (psg_on)
185                         IDirectSoundBuffer_Play(chipBuffer, 0,0, DSBPLAY_LOOPING );
186          }
187 IDirectSoundBuffer_Play(chipBuffer, 0,0, DSBPLAY_LOOPING );
188 #endif
189 }
190
191
192
193 void system_sound_shutdown(void)
194 {
195 #ifdef _EMULATE_SOUND
196          if (chipBuffer)        IDirectSoundBuffer_Stop(chipBuffer);
197          if (chipBuffer)        IDirectSoundBuffer_Release(chipBuffer);
198
199         if (primaryBuffer)      IDirectSoundBuffer_Stop(primaryBuffer);
200         if (primaryBuffer)      IDirectSoundBuffer_Release(primaryBuffer);
201         if (ds) IDirectSound_Release(ds);
202         if (buf1) free(buf1);
203 #endif
204 }
205
206
207 void system_sound_update(void)
208 {
209 #ifdef _EMULATE_SOUND
210         LPDWORD null_ptr=NULL;
211         int             pdwAudioBytes1, pdwAudioBytes2;
212         int             Write, LengthSamples;
213         UINT16  *chipPtr1, *chipPtr2;
214
215          // UNDEFINED write cursors
216          IDirectSoundBuffer_GetCurrentPosition(chipBuffer, 0, (LPDWORD)&Write);
217          if ((chipWrite == UNDEFINED) )
218          {
219                 lastChipWrite = chipWrite= Write - 128;
220                 return; //Wait a frame to accumulate length.
221          }
222
223         
224                 //SN76496 Sound Chips
225
226                  if (Write < lastChipWrite)     //Wrap?
227                         lastChipWrite -= chipBufferLength;
228
229                 LengthSamples = (Write - lastChipWrite) / 2; 
230                 lastChipWrite = Write;
231
232 /*              if (psg_on)*/ {
233                 pcm_updateOne(0,buf1,LengthSamples>>1);
234
235                 if SUCCEEDED(IDirectSoundBuffer_Lock(
236                         chipBuffer, chipWrite, LengthSamples*2, 
237                         (LPVOID*)&chipPtr1, (LPDWORD)&pdwAudioBytes1,
238                         (LPVOID*)&chipPtr2, (LPDWORD)&pdwAudioBytes2, 0))
239                  {
240                          __asm {
241                                         cld
242                                         mov eax,buf1
243                                         mov ebx,buf1
244                                         mov edi,chipPtr1
245                                         mov ecx,pdwAudioBytes1
246                                         shr ecx,2
247                                         
248 Copie1:                         mov esi,eax
249                                         movsw
250                                         add eax,2
251                                         mov esi,ebx
252                                         movsw
253                                         add ebx,2
254                                         loop Copie1
255                         
256                                         mov edi,chipPtr2
257                                         cmp edi,0
258                                         je End
259                                         mov ecx,pdwAudioBytes2
260                                         shr ecx,2
261
262 Copie2:                         
263                                         mov esi,eax
264                                         movsw
265                                         add eax,2
266                                         mov esi,ebx
267                                         movsw
268                                         add ebx,2
269                                         loop Copie2
270
271 End:                            }
272                                 
273
274                          IDirectSoundBuffer_Unlock(chipBuffer,
275                                 chipPtr1, pdwAudioBytes1, chipPtr2, pdwAudioBytes2);
276
277                         chipWrite+= (LengthSamples * 2);
278                         if (chipWrite> chipBufferLength)
279                                 chipWrite-= chipBufferLength;
280                 }
281                 else
282                 {
283                         DWORD status;
284                         chipWrite= UNDEFINED;
285                         IDirectSoundBuffer_GetStatus(chipBuffer, &status);
286                         if (status & DSBSTATUS_BUFFERLOST)
287                         {
288                                 if (IDirectSoundBuffer_Restore(chipBuffer) != DS_OK) return;
289                                 /*if (psg_on)*/ IDirectSoundBuffer_Play(chipBuffer, 0, 0, DSBPLAY_LOOPING);
290                         }
291           }
292          }
293 #endif
294 }