69c1be0397b7ea2527f44d0f92487cbb30469016
[picodrive.git] / pico / sound / sound.c
1 /*\r
2  * PicoDrive\r
3  * (c) Copyright Dave, 2004\r
4  * (C) notaz, 2006-2009\r
5  *\r
6  * This work is licensed under the terms of MAME license.\r
7  * See COPYING file in the top-level directory.\r
8  */\r
9 \r
10 #include <string.h>\r
11 #include "ym2612.h"\r
12 #include "sn76496.h"\r
13 #include "../pico_int.h"\r
14 #include "../cd/cue.h"\r
15 #include "mix.h"\r
16 \r
17 #define SIMPLE_WRITE_SOUND 0\r
18 \r
19 void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;\r
20 \r
21 // master int buffer to mix to\r
22 static int PsndBuffer[2*(44100+100)/50];\r
23 \r
24 // dac\r
25 static unsigned short dac_info[312+4]; // pppppppp ppppllll, p - pos in buff, l - length to write for this sample\r
26 \r
27 // cdda output buffer\r
28 short cdda_out_buffer[2*1152];\r
29 \r
30 // for Pico\r
31 int PsndRate=0;\r
32 int PsndLen=0; // number of mono samples, multiply by 2 for stereo\r
33 int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22050/60\r
34 int PsndLen_exc_cnt=0;\r
35 int PsndDacLine=0;\r
36 short *PsndOut=NULL; // PCM data buffer\r
37 \r
38 // timers\r
39 int timer_a_next_oflow, timer_a_step; // in z80 cycles\r
40 int timer_b_next_oflow, timer_b_step;\r
41 \r
42 // sn76496\r
43 extern int *sn76496_regs;\r
44 \r
45 \r
46 static void dac_recalculate(void)\r
47 {\r
48   int i, dac_cnt, pos, len, lines = Pico.m.pal ? 312 : 262, mid = Pico.m.pal ? 68 : 93;\r
49 \r
50   if (PsndLen <= lines)\r
51   {\r
52     // shrinking algo\r
53     dac_cnt = -PsndLen;\r
54     len=1; pos=0;\r
55     dac_info[225] = 1;\r
56 \r
57     for(i=226; i != 225; i++)\r
58     {\r
59       if (i >= lines) i = 0;\r
60       len = 0;\r
61       if(dac_cnt < 0) {\r
62         len=1;\r
63         pos++;\r
64         dac_cnt += lines;\r
65       }\r
66       dac_cnt -= PsndLen;\r
67       dac_info[i] = (pos<<4)|len;\r
68     }\r
69   }\r
70   else\r
71   {\r
72     // stretching\r
73     dac_cnt = PsndLen;\r
74     pos=0;\r
75     for(i = 225; i != 224; i++)\r
76     {\r
77       if (i >= lines) i = 0;\r
78       len=0;\r
79       while(dac_cnt >= 0) {\r
80         dac_cnt -= lines;\r
81         len++;\r
82       }\r
83       if (i == mid) // midpoint\r
84         while(pos+len < PsndLen/2) {\r
85           dac_cnt -= lines;\r
86           len++;\r
87         }\r
88       dac_cnt += PsndLen;\r
89       dac_info[i] = (pos<<4)|len;\r
90       pos+=len;\r
91     }\r
92     // last sample\r
93     for(len = 0, i = pos; i < PsndLen; i++) len++;\r
94     if (PsndLen_exc_add) len++;\r
95     dac_info[224] = (pos<<4)|len;\r
96   }\r
97   mid = (dac_info[lines-1] & 0xfff0) + ((dac_info[lines-1] & 0xf) << 4);\r
98   for (i = lines; i < sizeof(dac_info) / sizeof(dac_info[0]); i++)\r
99     dac_info[i] = mid;\r
100   //for(i=len=0; i < lines; i++) {\r
101   //  printf("%03i : %03i : %i\n", i, dac_info[i]>>4, dac_info[i]&0xf);\r
102   //  len+=dac_info[i]&0xf;\r
103   //}\r
104   //printf("rate is %i, len %f\n", PsndRate, (double)PsndRate/(Pico.m.pal ? 50.0 : 60.0));\r
105   //printf("len total: %i, last pos: %i\n", len, pos);\r
106   //exit(8);\r
107 }\r
108 \r
109 \r
110 PICO_INTERNAL void PsndReset(void)\r
111 {\r
112   // PsndRerate calls YM2612Init, which also resets\r
113   PsndRerate(0);\r
114   timers_reset();\r
115 }\r
116 \r
117 \r
118 // to be called after changing sound rate or chips\r
119 void PsndRerate(int preserve_state)\r
120 {\r
121   void *state = NULL;\r
122   int target_fps = Pico.m.pal ? 50 : 60;\r
123 \r
124   if (preserve_state) {\r
125     state = malloc(0x204);\r
126     if (state == NULL) return;\r
127     ym2612_pack_state();\r
128     memcpy(state, YM2612GetRegs(), 0x204);\r
129   }\r
130   YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate);\r
131   if (preserve_state) {\r
132     // feed it back it's own registers, just like after loading state\r
133     memcpy(YM2612GetRegs(), state, 0x204);\r
134     ym2612_unpack_state();\r
135   }\r
136 \r
137   if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state\r
138   SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PsndRate);\r
139   if (preserve_state) memcpy(sn76496_regs, state, 28*4); // restore old state\r
140 \r
141   if (state)\r
142     free(state);\r
143 \r
144   // calculate PsndLen\r
145   PsndLen=PsndRate / target_fps;\r
146   PsndLen_exc_add=((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
147   PsndLen_exc_cnt=0;\r
148 \r
149   // recalculate dac info\r
150   dac_recalculate();\r
151 \r
152   // clear all buffers\r
153   memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);\r
154   memset(cdda_out_buffer, 0, sizeof(cdda_out_buffer));\r
155   if (PsndOut)\r
156     PsndClear();\r
157 \r
158   // set mixer\r
159   PsndMix_32_to_16l = (PicoOpt & POPT_EN_STEREO) ? mix_32_to_16l_stereo : mix_32_to_16_mono;\r
160 \r
161   if (PicoAHW & PAHW_PICO)\r
162     PicoReratePico();\r
163 }\r
164 \r
165 \r
166 PICO_INTERNAL void PsndDoDAC(int line_to)\r
167 {\r
168   int pos, pos1, len;\r
169   int dout = ym2612.dacout;\r
170   int line_from = PsndDacLine;\r
171 \r
172   if (line_to >= 312)\r
173     line_to = 311;\r
174 \r
175   PsndDacLine = line_to + 1;\r
176 \r
177   pos =dac_info[line_from]>>4;\r
178   pos1=dac_info[line_to];\r
179   len = ((pos1>>4)-pos) + (pos1&0xf);\r
180   if (!len) return;\r
181 \r
182   if (PicoOpt & POPT_EN_STEREO) {\r
183     short *d = PsndOut + pos*2;\r
184     for (; len > 0; len--, d+=2) *d = dout;\r
185   } else {\r
186     short *d = PsndOut + pos;\r
187     for (; len > 0; len--, d++)  *d = dout;\r
188   }\r
189 }\r
190 \r
191 // cdda\r
192 static void cdda_raw_update(int *buffer, int length)\r
193 {\r
194   int ret, cdda_bytes, mult = 1;\r
195 \r
196   cdda_bytes = length*4;\r
197   if (PsndRate <= 22050 + 100) mult = 2;\r
198   if (PsndRate <  22050 - 100) mult = 4;\r
199   cdda_bytes *= mult;\r
200 \r
201   ret = pm_read(cdda_out_buffer, cdda_bytes, Pico_mcd->cdda_stream);\r
202   if (ret < cdda_bytes) {\r
203     memset((char *)cdda_out_buffer + ret, 0, cdda_bytes - ret);\r
204     Pico_mcd->cdda_stream = NULL;\r
205     return;\r
206   }\r
207 \r
208   // now mix\r
209   switch (mult) {\r
210     case 1: mix_16h_to_32(buffer, cdda_out_buffer, length*2); break;\r
211     case 2: mix_16h_to_32_s1(buffer, cdda_out_buffer, length*2); break;\r
212     case 4: mix_16h_to_32_s2(buffer, cdda_out_buffer, length*2); break;\r
213   }\r
214 }\r
215 \r
216 void cdda_start_play(int lba_base, int lba_offset, int lb_len)\r
217 {\r
218   if (Pico_mcd->cdda_type == CT_MP3)\r
219   {\r
220     int pos1024 = 0;\r
221 \r
222     if (lba_offset)\r
223       pos1024 = lba_offset * 1024 / lb_len;\r
224 \r
225     mp3_start_play(Pico_mcd->cdda_stream, pos1024);\r
226     return;\r
227   }\r
228 \r
229   pm_seek(Pico_mcd->cdda_stream, (lba_base + lba_offset) * 2352, SEEK_SET);\r
230   if (Pico_mcd->cdda_type == CT_WAV)\r
231   {\r
232     // skip headers, assume it's 44kHz stereo uncompressed\r
233     pm_seek(Pico_mcd->cdda_stream, 44, SEEK_CUR);\r
234   }\r
235 }\r
236 \r
237 \r
238 PICO_INTERNAL void PsndClear(void)\r
239 {\r
240   int len = PsndLen;\r
241   if (PsndLen_exc_add) len++;\r
242   if (PicoOpt & POPT_EN_STEREO)\r
243     memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned\r
244   else {\r
245     short *out = PsndOut;\r
246     if ((long)out & 2) { *out++ = 0; len--; }\r
247     memset32((int *) out, 0, len/2);\r
248     if (len & 1) out[len-1] = 0;\r
249   }\r
250 }\r
251 \r
252 \r
253 static int PsndRender(int offset, int length)\r
254 {\r
255   int  buf32_updated = 0;\r
256   int *buf32 = PsndBuffer+offset;\r
257   int stereo = (PicoOpt & 8) >> 3;\r
258 \r
259   offset <<= stereo;\r
260 \r
261   pprof_start(sound);\r
262 \r
263 #if !SIMPLE_WRITE_SOUND\r
264   if (offset == 0) { // should happen once per frame\r
265     // compensate for float part of PsndLen\r
266     PsndLen_exc_cnt += PsndLen_exc_add;\r
267     if (PsndLen_exc_cnt >= 0x10000) {\r
268       PsndLen_exc_cnt -= 0x10000;\r
269       length++;\r
270     }\r
271   }\r
272 #endif\r
273 \r
274   // PSG\r
275   if (PicoOpt & POPT_EN_PSG)\r
276     SN76496Update(PsndOut+offset, length, stereo);\r
277 \r
278   if (PicoAHW & PAHW_PICO) {\r
279     PicoPicoPCMUpdate(PsndOut+offset, length, stereo);\r
280     return length;\r
281   }\r
282 \r
283   // Add in the stereo FM buffer\r
284   if (PicoOpt & POPT_EN_FM) {\r
285     buf32_updated = YM2612UpdateOne(buf32, length, stereo, 1);\r
286   } else\r
287     memset32(buf32, 0, length<<stereo);\r
288 \r
289 //printf("active_chs: %02x\n", buf32_updated);\r
290   (void)buf32_updated;\r
291 \r
292   // CD: PCM sound\r
293   if (PicoAHW & PAHW_MCD) {\r
294     pcd_pcm_update(buf32, length, stereo);\r
295     //buf32_updated = 1;\r
296   }\r
297 \r
298   // CD: CDDA audio\r
299   // CD mode, cdda enabled, not data track, CDC is reading\r
300   if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA)\r
301       && Pico_mcd->cdda_stream != NULL\r
302       && !(Pico_mcd->s68k_regs[0x36] & 1))\r
303   {\r
304     // note: only 44, 22 and 11 kHz supported, with forced stereo\r
305     if (Pico_mcd->cdda_type == CT_MP3)\r
306       mp3_update(buf32, length, stereo);\r
307     else\r
308       cdda_raw_update(buf32, length);\r
309   }\r
310 \r
311   if ((PicoAHW & PAHW_32X) && (PicoOpt & POPT_EN_PWM))\r
312     p32x_pwm_update(buf32, length, stereo);\r
313 \r
314   // convert + limit to normal 16bit output\r
315   PsndMix_32_to_16l(PsndOut+offset, buf32, length);\r
316 \r
317   pprof_end(sound);\r
318 \r
319   return length;\r
320 }\r
321 \r
322 // to be called on 224 or line_sample scanlines only\r
323 PICO_INTERNAL void PsndGetSamples(int y)\r
324 {\r
325 #if SIMPLE_WRITE_SOUND\r
326   if (y != 224) return;\r
327   PsndRender(0, PsndLen);\r
328   if (PicoWriteSound)\r
329     PicoWriteSound(PsndLen * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
330   PsndClear();\r
331 #else\r
332   static int curr_pos = 0;\r
333 \r
334   if (y == 224)\r
335   {\r
336     if (emustatus & 2)\r
337          curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);\r
338     else curr_pos  = PsndRender(0, PsndLen);\r
339     if (emustatus & 1)\r
340          emustatus |=  2;\r
341     else emustatus &= ~2;\r
342     if (PicoWriteSound)\r
343       PicoWriteSound(curr_pos * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
344     // clear sound buffer\r
345     PsndClear();\r
346   }\r
347   else if (emustatus & 3) {\r
348     emustatus|= 2;\r
349     emustatus&=~1;\r
350     curr_pos = PsndRender(0, PsndLen/2);\r
351   }\r
352 #endif\r
353 }\r
354 \r
355 PICO_INTERNAL void PsndGetSamplesMS(void)\r
356 {\r
357   int stereo = (PicoOpt & 8) >> 3;\r
358   int length = PsndLen;\r
359 \r
360 #if !SIMPLE_WRITE_SOUND\r
361   // compensate for float part of PsndLen\r
362   PsndLen_exc_cnt += PsndLen_exc_add;\r
363   if (PsndLen_exc_cnt >= 0x10000) {\r
364     PsndLen_exc_cnt -= 0x10000;\r
365     length++;\r
366   }\r
367 #endif\r
368 \r
369   // PSG\r
370   if (PicoOpt & POPT_EN_PSG)\r
371     SN76496Update(PsndOut, length, stereo);\r
372 \r
373   // upmix to "stereo" if needed\r
374   if (stereo) {\r
375     int i, *p;\r
376     for (i = length, p = (void *)PsndOut; i > 0; i--, p++)\r
377       *p |= *p << 16;\r
378   }\r
379 \r
380   if (PicoWriteSound != NULL)\r
381     PicoWriteSound(length * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
382   PsndClear();\r
383 }\r
384 \r