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