3 * Copyright (C) 2006 Exophase <exophase@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 u32 global_enable_audio = 1;
25 direct_sound_struct direct_sound_channel[2];
26 gbc_sound_struct gbc_sound_channel[4];
28 u32 sound_frequency = 44100;
30 SDL_AudioSpec sound_settings;
31 SDL_mutex *sound_mutex;
35 u32 audio_buffer_size_number = 7;
37 u32 audio_buffer_size_number = 1;
40 u32 audio_buffer_size;
42 s16 sound_buffer[BUFFER_SIZE];
43 u32 sound_buffer_base = 0;
45 u32 sound_last_cpu_ticks = 0;
46 fixed16_16 gbc_sound_tick_step;
48 // Queue 1, 2, or 4 samples to the top of the DS FIFO, wrap around circularly
50 #define sound_timer_queue(size, value) \
51 *((s##size *)(ds->fifo + ds->fifo_top)) = value; \
52 ds->fifo_top = (ds->fifo_top + 1) % 32; \
54 void sound_timer_queue8(u32 channel, u8 value)
56 direct_sound_struct *ds = direct_sound_channel + channel;
57 sound_timer_queue(8, value);
60 void sound_timer_queue16(u32 channel, u16 value)
62 direct_sound_struct *ds = direct_sound_channel + channel;
63 sound_timer_queue(8, value & 0xFF);
64 sound_timer_queue(8, value >> 8);
67 void sound_timer_queue32(u32 channel, u32 value)
69 direct_sound_struct *ds = direct_sound_channel + channel;
71 sound_timer_queue(8, value & 0xFF);
72 sound_timer_queue(8, (value >> 8) & 0xFF);
73 sound_timer_queue(8, (value >> 16) & 0xFF);
74 sound_timer_queue(8, value >> 24);
77 // Unqueue 1 sample from the base of the DS FIFO and place it on the audio
78 // buffer for as many samples as necessary. If the DS FIFO is 16 bytes or
79 // smaller and if DMA is enabled for the sound channel initiate a DMA transfer
82 #define render_sample_null() \
84 #define render_sample_left() \
85 sound_buffer[buffer_index] += current_sample + \
86 fp16_16_to_u32((next_sample - current_sample) * fifo_fractional) \
88 #define render_sample_right() \
89 sound_buffer[buffer_index + 1] += current_sample + \
90 fp16_16_to_u32((next_sample - current_sample) * fifo_fractional) \
92 #define render_sample_both() \
93 dest_sample = current_sample + \
94 fp16_16_to_u32((next_sample - current_sample) * fifo_fractional); \
95 sound_buffer[buffer_index] += dest_sample; \
96 sound_buffer[buffer_index + 1] += dest_sample \
98 #define render_samples(type) \
99 while(fifo_fractional <= 0xFFFF) \
101 render_sample_##type(); \
102 fifo_fractional += frequency_step; \
103 buffer_index = (buffer_index + 2) % BUFFER_SIZE; \
106 void sound_timer(fixed16_16 frequency_step, u32 channel)
108 direct_sound_struct *ds = direct_sound_channel + channel;
110 fixed16_16 fifo_fractional = ds->fifo_fractional;
111 u32 buffer_index = ds->buffer_index;
112 s16 current_sample, next_sample, dest_sample;
114 current_sample = ds->fifo[ds->fifo_base] << 4;
115 ds->fifo_base = (ds->fifo_base + 1) % 32;
116 next_sample = ds->fifo[ds->fifo_base] << 4;
120 if(ds->volume == DIRECT_SOUND_VOLUME_50)
122 current_sample >>= 1;
128 case DIRECT_SOUND_INACTIVE:
129 render_samples(null);
132 case DIRECT_SOUND_RIGHT:
133 render_samples(right);
136 case DIRECT_SOUND_LEFT:
137 render_samples(left);
140 case DIRECT_SOUND_LEFTRIGHT:
141 render_samples(both);
147 render_samples(null);
150 ds->buffer_index = buffer_index;
151 ds->fifo_fractional = fp16_16_fractional_part(fifo_fractional);
153 if(((ds->fifo_top - ds->fifo_base) % 32) <= 16)
155 if(dma[1].direct_sound_channel == channel)
156 dma_transfer(dma + 1);
158 if(dma[2].direct_sound_channel == channel)
159 dma_transfer(dma + 2);
163 void sound_reset_fifo(u32 channel)
165 direct_sound_struct *ds = direct_sound_channel;
167 memset(ds->fifo, 0, 32);
170 // Initial pattern data = 4bits (signed)
171 // Channel volume = 12bits
172 // Envelope volume = 14bits
173 // Master volume = 2bits
175 // Recalculate left and right volume as volume changes.
176 // To calculate the current sample, use (sample * volume) >> 16
178 // Square waves range from -8 (low) to 7 (high)
180 s8 square_pattern_duty[4][8] =
182 { 0xF8, 0xF8, 0xF8, 0xF8, 0x07, 0xF8, 0xF8, 0xF8 },
183 { 0xF8, 0xF8, 0xF8, 0xF8, 0x07, 0x07, 0xF8, 0xF8 },
184 { 0xF8, 0xF8, 0x07, 0x07, 0x07, 0x07, 0xF8, 0xF8 },
185 { 0x07, 0x07, 0x07, 0x07, 0xF8, 0xF8, 0x07, 0x07 },
190 u32 noise_table15[1024];
193 u32 gbc_sound_master_volume_table[4] = { 1, 2, 4, 0 };
195 u32 gbc_sound_channel_volume_table[8] =
207 u32 gbc_sound_envelope_volume_table[16] =
209 fixed_div(0, 15, 14),
210 fixed_div(1, 15, 14),
211 fixed_div(2, 15, 14),
212 fixed_div(3, 15, 14),
213 fixed_div(4, 15, 14),
214 fixed_div(5, 15, 14),
215 fixed_div(6, 15, 14),
216 fixed_div(7, 15, 14),
217 fixed_div(8, 15, 14),
218 fixed_div(9, 15, 14),
219 fixed_div(10, 15, 14),
220 fixed_div(11, 15, 14),
221 fixed_div(12, 15, 14),
222 fixed_div(13, 15, 14),
223 fixed_div(14, 15, 14),
224 fixed_div(15, 15, 14)
227 u32 gbc_sound_buffer_index = 0;
228 u32 gbc_sound_last_cpu_ticks = 0;
229 u32 gbc_sound_partial_ticks = 0;
231 u32 gbc_sound_master_volume_left;
232 u32 gbc_sound_master_volume_right;
233 u32 gbc_sound_master_volume;
235 #define update_volume_channel_envelope(channel) \
236 volume_##channel = gbc_sound_envelope_volume_table[envelope_volume] * \
237 gbc_sound_channel_volume_table[gbc_sound_master_volume_##channel] * \
238 gbc_sound_master_volume_table[gbc_sound_master_volume] \
240 #define update_volume_channel_noenvelope(channel) \
241 volume_##channel = gs->wave_volume * \
242 gbc_sound_channel_volume_table[gbc_sound_master_volume_##channel] * \
243 gbc_sound_master_volume_table[gbc_sound_master_volume] \
245 #define update_volume(type) \
246 update_volume_channel_##type(left); \
247 update_volume_channel_##type(right) \
249 #define update_tone_sweep() \
250 if(gs->sweep_status) \
252 u32 sweep_ticks = gs->sweep_ticks - 1; \
254 if(sweep_ticks == 0) \
256 u32 rate = gs->rate; \
258 if(gs->sweep_direction) \
259 rate = rate - (rate >> gs->sweep_shift); \
261 rate = rate + (rate >> gs->sweep_shift); \
266 frequency_step = float_to_fp16_16(((131072.0 / (2048 - rate)) * 8.0) / \
269 gs->frequency_step = frequency_step; \
272 sweep_ticks = gs->sweep_initial_ticks; \
274 gs->sweep_ticks = sweep_ticks; \
277 #define update_tone_nosweep() \
279 #define update_tone_envelope() \
280 if(gs->envelope_status) \
282 u32 envelope_ticks = gs->envelope_ticks - 1; \
283 envelope_volume = gs->envelope_volume; \
285 if(envelope_ticks == 0) \
287 if(gs->envelope_direction) \
289 if(envelope_volume != 15) \
290 envelope_volume = gs->envelope_volume + 1; \
294 if(envelope_volume != 0) \
295 envelope_volume = gs->envelope_volume - 1; \
298 update_volume(envelope); \
300 gs->envelope_volume = envelope_volume; \
301 gs->envelope_ticks = gs->envelope_initial_ticks; \
305 gs->envelope_ticks = envelope_ticks; \
309 #define update_tone_noenvelope() \
311 #define gbc_sound_synchronize() \
312 while(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) > \
313 (audio_buffer_size * 2)) \
315 SDL_CondWait(sound_cv, sound_mutex); \
318 #define update_tone_counters(envelope_op, sweep_op) \
319 tick_counter += gbc_sound_tick_step; \
320 if(tick_counter > 0xFFFF) \
322 if(gs->length_status) \
324 u32 length_ticks = gs->length_ticks - 1; \
325 gs->length_ticks = length_ticks; \
327 if(length_ticks == 0) \
329 gs->active_flag = 0; \
334 update_tone_##envelope_op(); \
335 update_tone_##sweep_op(); \
337 tick_counter &= 0xFFFF; \
340 #define gbc_sound_render_sample_right() \
341 sound_buffer[buffer_index + 1] += (current_sample * volume_right) >> 22 \
343 #define gbc_sound_render_sample_left() \
344 sound_buffer[buffer_index] += (current_sample * volume_left) >> 22 \
346 #define gbc_sound_render_sample_both() \
347 gbc_sound_render_sample_right(); \
348 gbc_sound_render_sample_left() \
350 #define gbc_sound_render_samples(type, sample_length, envelope_op, sweep_op) \
351 for(i = 0; i < buffer_ticks; i++) \
354 sample_data[fp16_16_to_u32(sample_index) % sample_length]; \
355 gbc_sound_render_sample_##type(); \
357 sample_index += frequency_step; \
358 buffer_index = (buffer_index + 2) % BUFFER_SIZE; \
360 update_tone_counters(envelope_op, sweep_op); \
363 #define gbc_noise_wrap_full 32767
365 #define gbc_noise_wrap_half 126
367 #define get_noise_sample_full() \
369 ((s32)(noise_table15[fp16_16_to_u32(sample_index) >> 5] << \
370 (fp16_16_to_u32(sample_index) & 0x1F)) >> 31) & 0x0F \
372 #define get_noise_sample_half() \
374 ((s32)(noise_table7[fp16_16_to_u32(sample_index) >> 5] << \
375 (fp16_16_to_u32(sample_index) & 0x1F)) >> 31) & 0x0F \
377 #define gbc_sound_render_noise(type, noise_type, envelope_op, sweep_op) \
378 for(i = 0; i < buffer_ticks; i++) \
380 get_noise_sample_##noise_type(); \
381 gbc_sound_render_sample_##type(); \
383 sample_index += frequency_step; \
385 if(sample_index >= u32_to_fp16_16(gbc_noise_wrap_##noise_type)) \
386 sample_index -= u32_to_fp16_16(gbc_noise_wrap_##noise_type); \
388 buffer_index = (buffer_index + 2) % BUFFER_SIZE; \
389 update_tone_counters(envelope_op, sweep_op); \
392 #define gbc_sound_render_channel(type, sample_length, envelope_op, sweep_op) \
393 buffer_index = gbc_sound_buffer_index; \
394 sample_index = gs->sample_index; \
395 frequency_step = gs->frequency_step; \
396 tick_counter = gs->tick_counter; \
398 update_volume(envelope_op); \
402 case GBC_SOUND_INACTIVE: \
405 case GBC_SOUND_LEFT: \
406 gbc_sound_render_##type(left, sample_length, envelope_op, sweep_op); \
409 case GBC_SOUND_RIGHT: \
410 gbc_sound_render_##type(right, sample_length, envelope_op, sweep_op); \
413 case GBC_SOUND_LEFTRIGHT: \
414 gbc_sound_render_##type(both, sample_length, envelope_op, sweep_op); \
418 gs->sample_index = sample_index; \
419 gs->tick_counter = tick_counter; \
421 #define gbc_sound_load_wave_ram(bank) \
422 wave_bank = wave_samples + (bank * 32); \
423 for(i = 0, i2 = 0; i < 16; i++, i2 += 2) \
425 current_sample = wave_ram[i]; \
426 wave_bank[i2] = (((current_sample >> 4) & 0x0F) - 8); \
427 wave_bank[i2 + 1] = ((current_sample & 0x0F) - 8); \
430 void synchronize_sound()
432 SDL_LockMutex(sound_mutex);
434 gbc_sound_synchronize();
436 SDL_UnlockMutex(sound_mutex);
439 void update_gbc_sound(u32 cpu_ticks)
441 fixed16_16 buffer_ticks = float_to_fp16_16(((float)(cpu_ticks -
442 gbc_sound_last_cpu_ticks) * sound_frequency) / 16777216.0);
444 gbc_sound_struct *gs = gbc_sound_channel;
445 fixed16_16 sample_index, frequency_step;
446 fixed16_16 tick_counter;
448 s32 volume_left, volume_right;
451 u32 sound_status = address16(io_registers, 0x84) & 0xFFF0;
454 u8 *wave_ram = ((u8 *)io_registers) + 0x90;
456 gbc_sound_partial_ticks += fp16_16_fractional_part(buffer_ticks);
457 buffer_ticks = fp16_16_to_u32(buffer_ticks);
459 if(gbc_sound_partial_ticks > 0xFFFF)
462 gbc_sound_partial_ticks &= 0xFFFF;
465 SDL_LockMutex(sound_mutex);
468 if(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) >
469 (audio_buffer_size * 3 / 2))
471 while(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) >
472 (audio_buffer_size * 3 / 2))
474 SDL_CondWait(sound_cv, sound_mutex);
478 if(current_frameskip_type == auto_frameskip)
480 sceDisplayWaitVblankStart();
481 real_frame_count = 0;
482 virtual_frame_count = 0;
489 if(current_frameskip_type == auto_frameskip)
493 get_ticks_us(¤t_ticks);
495 next_ticks = ((current_ticks + 16666) / 16667) * 16667;
496 delay_us(next_ticks - current_ticks);
498 get_ticks_us(&frame_count_initial_timestamp);
499 real_frame_count = 0;
500 virtual_frame_count = 0;
509 gs = gbc_sound_channel + 0;
512 sound_status |= 0x01;
513 sample_data = gs->sample_data;
514 envelope_volume = gs->envelope_volume;
515 gbc_sound_render_channel(samples, 8, envelope, sweep);
518 gs = gbc_sound_channel + 1;
521 sound_status |= 0x02;
522 sample_data = gs->sample_data;
523 envelope_volume = gs->envelope_volume;
524 gbc_sound_render_channel(samples, 8, envelope, nosweep);
527 gs = gbc_sound_channel + 2;
528 if(gbc_sound_wave_update)
530 if(gs->wave_bank == 1)
532 gbc_sound_load_wave_ram(1);
536 gbc_sound_load_wave_ram(0);
539 gbc_sound_wave_update = 0;
542 if((gs->active_flag) && (gs->master_enable))
544 sound_status |= 0x04;
545 sample_data = wave_samples;
546 if(gs->wave_type == 0)
548 if(gs->wave_bank == 1)
551 gbc_sound_render_channel(samples, 32, noenvelope, nosweep);
555 gbc_sound_render_channel(samples, 64, noenvelope, nosweep);
559 gs = gbc_sound_channel + 3;
562 sound_status |= 0x08;
563 envelope_volume = gs->envelope_volume;
565 if(gs->noise_type == 1)
567 gbc_sound_render_channel(noise, half, envelope, nosweep);
571 gbc_sound_render_channel(noise, full, envelope, nosweep);
576 address16(io_registers, 0x84) = sound_status;
578 gbc_sound_last_cpu_ticks = cpu_ticks;
579 gbc_sound_buffer_index =
580 (gbc_sound_buffer_index + (buffer_ticks * 2)) % BUFFER_SIZE;
582 SDL_UnlockMutex(sound_mutex);
584 SDL_CondSignal(sound_cv);
587 #define sound_copy_normal() \
588 current_sample = source[i] \
590 #define sound_copy(source_offset, length, render_type) \
591 _length = (length) / 2; \
592 source = (s16 *)(sound_buffer + source_offset); \
593 for(i = 0; i < _length; i++) \
595 sound_copy_##render_type(); \
596 if(current_sample > 2047) \
597 current_sample = 2047; \
598 if(current_sample < -2048) \
599 current_sample = -2048; \
601 stream_base[i] = current_sample << 4; \
605 #define sound_copy_null(source_offset, length) \
606 _length = (length) / 2; \
607 source = (s16 *)(sound_buffer + source_offset); \
608 for(i = 0; i < _length; i++) \
610 stream_base[i] = 0; \
615 void sound_callback(void *userdata, Uint8 *stream, int length)
617 u32 sample_length = length / 2;
620 s16 *stream_base = (s16 *)stream;
624 SDL_LockMutex(sound_mutex);
626 while(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) <
629 SDL_CondWait(sound_cv, sound_mutex);
632 if(global_enable_audio)
634 if((sound_buffer_base + sample_length) >= BUFFER_SIZE)
636 u32 partial_length = (BUFFER_SIZE - sound_buffer_base) * 2;
637 sound_copy(sound_buffer_base, partial_length, normal);
638 source = (s16 *)sound_buffer;
639 sound_copy(0, length - partial_length, normal);
640 sound_buffer_base = (length - partial_length) / 2;
644 sound_copy(sound_buffer_base, length, normal);
645 sound_buffer_base += sample_length;
650 if((sound_buffer_base + sample_length) >= BUFFER_SIZE)
652 u32 partial_length = (BUFFER_SIZE - sound_buffer_base) * 2;
653 sound_copy_null(sound_buffer_base, partial_length);
654 source = (s16 *)sound_buffer;
655 sound_copy(0, length - partial_length, normal);
656 sound_buffer_base = (length - partial_length) / 2;
660 sound_copy_null(sound_buffer_base, length);
661 sound_buffer_base += sample_length;
665 SDL_CondSignal(sound_cv);
667 SDL_UnlockMutex(sound_mutex);
670 // Special thanks to blarrg for the LSFR frequency used in Meridian, as posted
671 // on the forum at http://meridian.overclocked.org:
672 // http://meridian.overclocked.org/cgi-bin/wwwthreads/showpost.pl?Board=merid
673 // angeneraldiscussion&Number=2069&page=0&view=expanded&mode=threaded&sb=4
674 // Hope you don't mind me borrowing it ^_-
676 void init_noise_table(u32 *table, u32 period, u32 bit_length)
678 u32 shift_register = 0xFF;
679 u32 mask = ~(1 << bit_length);
680 s32 table_pos, bit_pos;
682 u32 table_period = (period + 31) / 32;
684 // Bits are stored in reverse order so they can be more easily moved to
685 // bit 31, for sign extended shift down.
687 for(table_pos = 0; table_pos < table_period; table_pos++)
690 for(bit_pos = 31; bit_pos >= 0; bit_pos--)
692 current_entry |= (shift_register & 0x01) << bit_pos;
695 ((1 & (shift_register ^ (shift_register >> 1))) << bit_length) |
696 ((shift_register >> 1) & mask);
699 table[table_pos] = current_entry;
705 direct_sound_struct *ds = direct_sound_channel;
706 gbc_sound_struct *gs = gbc_sound_channel;
710 sound_buffer_base = 0;
711 sound_last_cpu_ticks = 0;
712 memset(sound_buffer, 0, audio_buffer_size);
714 for(i = 0; i < 2; i++, ds++)
716 ds->buffer_index = 0;
717 ds->status = DIRECT_SOUND_INACTIVE;
720 ds->fifo_fractional = 0;
721 ds->last_cpu_ticks = 0;
722 memset(ds->fifo, 0, 32);
725 gbc_sound_buffer_index = 0;
726 gbc_sound_last_cpu_ticks = 0;
727 gbc_sound_partial_ticks = 0;
729 gbc_sound_master_volume_left = 0;
730 gbc_sound_master_volume_right = 0;
731 gbc_sound_master_volume = 0;
732 memset(wave_samples, 0, 64);
734 for(i = 0; i < 4; i++, gs++)
736 gs->status = GBC_SOUND_INACTIVE;
737 gs->sample_data = square_pattern_duty[2];
744 gbc_sound_buffer_index =
745 (sound_buffer_base + audio_buffer_size) % BUFFER_SIZE;
747 SDL_CondSignal(sound_cv);
753 audio_buffer_size = (audio_buffer_size_number * 1024) + 3072;
754 #elif defined(TAVI_BUILD) || defined(ARM_ARCH)
755 audio_buffer_size = 16 << audio_buffer_size_number;
756 // audio_buffer_size = 16384;
758 audio_buffer_size = 16384;
761 SDL_AudioSpec desired_spec =
767 audio_buffer_size / 4,
774 gbc_sound_tick_step =
775 float_to_fp16_16(256.0 / sound_frequency);
777 init_noise_table(noise_table15, 32767, 14);
778 init_noise_table(noise_table7, 127, 6);
782 SDL_OpenAudio(&desired_spec, &sound_settings);
783 sound_frequency = sound_settings.freq;
784 sound_mutex = SDL_CreateMutex();
785 sound_cv = SDL_CreateCond();
789 #define sound_savestate_builder(type) \
790 void sound_##type##_savestate(file_tag_type savestate_file) \
792 file_##type##_variable(savestate_file, sound_on); \
793 file_##type##_variable(savestate_file, sound_buffer_base); \
794 file_##type##_variable(savestate_file, sound_last_cpu_ticks); \
795 file_##type##_variable(savestate_file, gbc_sound_buffer_index); \
796 file_##type##_variable(savestate_file, gbc_sound_last_cpu_ticks); \
797 file_##type##_variable(savestate_file, gbc_sound_partial_ticks); \
798 file_##type##_variable(savestate_file, gbc_sound_master_volume_left); \
799 file_##type##_variable(savestate_file, gbc_sound_master_volume_right); \
800 file_##type##_variable(savestate_file, gbc_sound_master_volume); \
801 file_##type##_array(savestate_file, wave_samples); \
802 file_##type##_array(savestate_file, direct_sound_channel); \
803 file_##type##_array(savestate_file, gbc_sound_channel); \
806 sound_savestate_builder(read);
807 sound_savestate_builder(write_mem);