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_mutex *sound_mutex;
31 static SDL_cond *sound_cv;
34 u32 audio_buffer_size_number = 1;
36 u32 audio_buffer_size_number = 8;
40 static u32 audio_buffer_size;
41 static s16 sound_buffer[BUFFER_SIZE];
42 static u32 sound_buffer_base;
44 static u32 sound_last_cpu_ticks;
45 static fixed16_16 gbc_sound_tick_step;
47 static u32 sound_exit_flag;
49 // Queue 1, 2, or 4 samples to the top of the DS FIFO, wrap around circularly
51 #define sound_timer_queue(size, value) \
52 *((s##size *)(ds->fifo + ds->fifo_top)) = value; \
53 ds->fifo_top = (ds->fifo_top + 1) % 32; \
55 void sound_timer_queue8(u32 channel, u8 value)
57 direct_sound_struct *ds = direct_sound_channel + channel;
58 sound_timer_queue(8, value);
61 void sound_timer_queue16(u32 channel, u16 value)
63 direct_sound_struct *ds = direct_sound_channel + channel;
64 sound_timer_queue(8, value & 0xFF);
65 sound_timer_queue(8, value >> 8);
68 void sound_timer_queue32(u32 channel, u32 value)
70 direct_sound_struct *ds = direct_sound_channel + channel;
72 sound_timer_queue(8, value & 0xFF);
73 sound_timer_queue(8, (value >> 8) & 0xFF);
74 sound_timer_queue(8, (value >> 16) & 0xFF);
75 sound_timer_queue(8, value >> 24);
78 // Unqueue 1 sample from the base of the DS FIFO and place it on the audio
79 // buffer for as many samples as necessary. If the DS FIFO is 16 bytes or
80 // smaller and if DMA is enabled for the sound channel initiate a DMA transfer
83 #define render_sample_null() \
85 #define render_sample_left() \
86 sound_buffer[buffer_index] += current_sample + \
87 fp16_16_to_u32((next_sample - current_sample) * (fifo_fractional >> 8)) \
89 #define render_sample_right() \
90 sound_buffer[buffer_index + 1] += current_sample + \
91 fp16_16_to_u32((next_sample - current_sample) * (fifo_fractional >> 8)) \
93 #define render_sample_both() \
94 dest_sample = current_sample + \
95 fp16_16_to_u32((next_sample - current_sample) * (fifo_fractional >> 8)); \
96 sound_buffer[buffer_index] += dest_sample; \
97 sound_buffer[buffer_index + 1] += dest_sample \
99 #define render_samples(type) \
100 while(fifo_fractional <= 0xFFFFFF) \
102 render_sample_##type(); \
103 fifo_fractional += frequency_step; \
104 buffer_index = (buffer_index + 2) % BUFFER_SIZE; \
107 void sound_timer(fixed8_24 frequency_step, u32 channel)
109 direct_sound_struct *ds = direct_sound_channel + channel;
111 fixed8_24 fifo_fractional = ds->fifo_fractional;
112 u32 buffer_index = ds->buffer_index;
113 s16 current_sample, next_sample, dest_sample;
115 current_sample = ds->fifo[ds->fifo_base] << 4;
116 ds->fifo_base = (ds->fifo_base + 1) % 32;
117 next_sample = ds->fifo[ds->fifo_base] << 4;
121 if(ds->volume == DIRECT_SOUND_VOLUME_50)
123 current_sample >>= 1;
129 case DIRECT_SOUND_INACTIVE:
130 render_samples(null);
133 case DIRECT_SOUND_RIGHT:
134 render_samples(right);
137 case DIRECT_SOUND_LEFT:
138 render_samples(left);
141 case DIRECT_SOUND_LEFTRIGHT:
142 render_samples(both);
148 render_samples(null);
151 ds->buffer_index = buffer_index;
152 ds->fifo_fractional = fp8_24_fractional_part(fifo_fractional);
154 if(((ds->fifo_top - ds->fifo_base) % 32) <= 16)
156 if(dma[1].direct_sound_channel == channel)
157 dma_transfer(dma + 1);
159 if(dma[2].direct_sound_channel == channel)
160 dma_transfer(dma + 2);
164 void sound_reset_fifo(u32 channel)
166 direct_sound_struct *ds = direct_sound_channel;
168 memset(ds->fifo, 0, 32);
171 // Initial pattern data = 4bits (signed)
172 // Channel volume = 12bits
173 // Envelope volume = 14bits
174 // Master volume = 2bits
176 // Recalculate left and right volume as volume changes.
177 // To calculate the current sample, use (sample * volume) >> 16
179 // Square waves range from -8 (low) to 7 (high)
181 s8 square_pattern_duty[4][8] =
183 { 0xF8, 0xF8, 0xF8, 0xF8, 0x07, 0xF8, 0xF8, 0xF8 },
184 { 0xF8, 0xF8, 0xF8, 0xF8, 0x07, 0x07, 0xF8, 0xF8 },
185 { 0xF8, 0xF8, 0x07, 0x07, 0x07, 0x07, 0xF8, 0xF8 },
186 { 0x07, 0x07, 0x07, 0x07, 0xF8, 0xF8, 0x07, 0x07 },
191 u32 noise_table15[1024];
194 u32 gbc_sound_master_volume_table[4] = { 1, 2, 4, 0 };
196 u32 gbc_sound_channel_volume_table[8] =
208 u32 gbc_sound_envelope_volume_table[16] =
210 fixed_div(0, 15, 14),
211 fixed_div(1, 15, 14),
212 fixed_div(2, 15, 14),
213 fixed_div(3, 15, 14),
214 fixed_div(4, 15, 14),
215 fixed_div(5, 15, 14),
216 fixed_div(6, 15, 14),
217 fixed_div(7, 15, 14),
218 fixed_div(8, 15, 14),
219 fixed_div(9, 15, 14),
220 fixed_div(10, 15, 14),
221 fixed_div(11, 15, 14),
222 fixed_div(12, 15, 14),
223 fixed_div(13, 15, 14),
224 fixed_div(14, 15, 14),
225 fixed_div(15, 15, 14)
228 u32 gbc_sound_buffer_index = 0;
229 u32 gbc_sound_last_cpu_ticks = 0;
230 u32 gbc_sound_partial_ticks = 0;
232 u32 gbc_sound_master_volume_left;
233 u32 gbc_sound_master_volume_right;
234 u32 gbc_sound_master_volume;
236 #define update_volume_channel_envelope(channel) \
237 volume_##channel = gbc_sound_envelope_volume_table[envelope_volume] * \
238 gbc_sound_channel_volume_table[gbc_sound_master_volume_##channel] * \
239 gbc_sound_master_volume_table[gbc_sound_master_volume] \
241 #define update_volume_channel_noenvelope(channel) \
242 volume_##channel = gs->wave_volume * \
243 gbc_sound_channel_volume_table[gbc_sound_master_volume_##channel] * \
244 gbc_sound_master_volume_table[gbc_sound_master_volume] \
246 #define update_volume(type) \
247 update_volume_channel_##type(left); \
248 update_volume_channel_##type(right) \
250 #define update_tone_sweep() \
251 if(gs->sweep_status) \
253 u32 sweep_ticks = gs->sweep_ticks - 1; \
255 if(sweep_ticks == 0) \
257 u32 rate = gs->rate; \
259 if(gs->sweep_direction) \
260 rate = rate - (rate >> gs->sweep_shift); \
262 rate = rate + (rate >> gs->sweep_shift); \
267 frequency_step = float_to_fp16_16(((131072.0f / (2048 - rate)) * 8.0f) \
268 / sound_frequency); \
270 gs->frequency_step = frequency_step; \
273 sweep_ticks = gs->sweep_initial_ticks; \
275 gs->sweep_ticks = sweep_ticks; \
278 #define update_tone_nosweep() \
280 #define update_tone_envelope() \
281 if(gs->envelope_status) \
283 u32 envelope_ticks = gs->envelope_ticks - 1; \
284 envelope_volume = gs->envelope_volume; \
286 if(envelope_ticks == 0) \
288 if(gs->envelope_direction) \
290 if(envelope_volume != 15) \
291 envelope_volume = gs->envelope_volume + 1; \
295 if(envelope_volume != 0) \
296 envelope_volume = gs->envelope_volume - 1; \
299 update_volume(envelope); \
301 gs->envelope_volume = envelope_volume; \
302 gs->envelope_ticks = gs->envelope_initial_ticks; \
306 gs->envelope_ticks = envelope_ticks; \
310 #define update_tone_noenvelope() \
312 #define update_tone_counters(envelope_op, sweep_op) \
313 tick_counter += gbc_sound_tick_step; \
314 if(tick_counter > 0xFFFF) \
316 if(gs->length_status) \
318 u32 length_ticks = gs->length_ticks - 1; \
319 gs->length_ticks = length_ticks; \
321 if(length_ticks == 0) \
323 gs->active_flag = 0; \
328 update_tone_##envelope_op(); \
329 update_tone_##sweep_op(); \
331 tick_counter &= 0xFFFF; \
334 #define gbc_sound_render_sample_right() \
335 sound_buffer[buffer_index + 1] += (current_sample * volume_right) >> 22 \
337 #define gbc_sound_render_sample_left() \
338 sound_buffer[buffer_index] += (current_sample * volume_left) >> 22 \
340 #define gbc_sound_render_sample_both() \
341 gbc_sound_render_sample_right(); \
342 gbc_sound_render_sample_left() \
344 #define gbc_sound_render_samples(type, sample_length, envelope_op, sweep_op) \
345 for(i = 0; i < buffer_ticks; i++) \
348 sample_data[fp16_16_to_u32(sample_index) % sample_length]; \
349 gbc_sound_render_sample_##type(); \
351 sample_index += frequency_step; \
352 buffer_index = (buffer_index + 2) % BUFFER_SIZE; \
354 update_tone_counters(envelope_op, sweep_op); \
357 #define gbc_noise_wrap_full 32767
359 #define gbc_noise_wrap_half 126
361 #define get_noise_sample_full() \
363 ((s32)(noise_table15[fp16_16_to_u32(sample_index) >> 5] << \
364 (fp16_16_to_u32(sample_index) & 0x1F)) >> 31) & 0x0F \
366 #define get_noise_sample_half() \
368 ((s32)(noise_table7[fp16_16_to_u32(sample_index) >> 5] << \
369 (fp16_16_to_u32(sample_index) & 0x1F)) >> 31) & 0x0F \
371 #define gbc_sound_render_noise(type, noise_type, envelope_op, sweep_op) \
372 for(i = 0; i < buffer_ticks; i++) \
374 get_noise_sample_##noise_type(); \
375 gbc_sound_render_sample_##type(); \
377 sample_index += frequency_step; \
379 if(sample_index >= u32_to_fp16_16(gbc_noise_wrap_##noise_type)) \
380 sample_index -= u32_to_fp16_16(gbc_noise_wrap_##noise_type); \
382 buffer_index = (buffer_index + 2) % BUFFER_SIZE; \
383 update_tone_counters(envelope_op, sweep_op); \
386 #define gbc_sound_render_channel(type, sample_length, envelope_op, sweep_op) \
387 buffer_index = gbc_sound_buffer_index; \
388 sample_index = gs->sample_index; \
389 frequency_step = gs->frequency_step; \
390 tick_counter = gs->tick_counter; \
392 update_volume(envelope_op); \
396 case GBC_SOUND_INACTIVE: \
399 case GBC_SOUND_LEFT: \
400 gbc_sound_render_##type(left, sample_length, envelope_op, sweep_op); \
403 case GBC_SOUND_RIGHT: \
404 gbc_sound_render_##type(right, sample_length, envelope_op, sweep_op); \
407 case GBC_SOUND_LEFTRIGHT: \
408 gbc_sound_render_##type(both, sample_length, envelope_op, sweep_op); \
412 gs->sample_index = sample_index; \
413 gs->tick_counter = tick_counter; \
415 #define gbc_sound_load_wave_ram(bank) \
416 wave_bank = wave_samples + (bank * 32); \
417 for(i = 0, i2 = 0; i < 16; i++, i2 += 2) \
419 current_sample = wave_ram[i]; \
420 wave_bank[i2] = (((current_sample >> 4) & 0x0F) - 8); \
421 wave_bank[i2 + 1] = ((current_sample & 0x0F) - 8); \
424 void update_gbc_sound(u32 cpu_ticks)
426 fixed16_16 buffer_ticks = float_to_fp16_16((float)(cpu_ticks -
427 gbc_sound_last_cpu_ticks) * sound_frequency / GBC_BASE_RATE);
429 gbc_sound_struct *gs = gbc_sound_channel;
430 fixed16_16 sample_index, frequency_step;
431 fixed16_16 tick_counter;
433 s32 volume_left, volume_right;
436 u32 sound_status = address16(io_registers, 0x84) & 0xFFF0;
439 u8 *wave_ram = ((u8 *)io_registers) + 0x90;
441 gbc_sound_partial_ticks += fp16_16_fractional_part(buffer_ticks);
442 buffer_ticks = fp16_16_to_u32(buffer_ticks);
444 if(gbc_sound_partial_ticks > 0xFFFF)
447 gbc_sound_partial_ticks &= 0xFFFF;
450 SDL_LockMutex(sound_mutex);
453 if(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) >=
454 (audio_buffer_size * 2))
456 while(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) >
457 (audio_buffer_size * 3 / 2))
459 SDL_CondWait(sound_cv, sound_mutex);
463 if(current_frameskip_type == auto_frameskip)
465 sceDisplayWaitVblankStart();
466 real_frame_count = 0;
467 virtual_frame_count = 0;
470 if(current_frameskip_type == auto_frameskip)
475 get_ticks_us(¤t_ticks);
477 next_ticks = ((current_ticks + 16666) / 16667) * 16667;
478 delay_us(next_ticks - current_ticks);
480 get_ticks_us(&frame_count_initial_timestamp);
482 /* prevent frameskip, or it will cause more audio,
483 * then more waiting here, then frame skip again, ... */
484 num_skipped_frames = 100;
492 gs = gbc_sound_channel + 0;
495 sound_status |= 0x01;
496 sample_data = gs->sample_data;
497 envelope_volume = gs->envelope_volume;
498 gbc_sound_render_channel(samples, 8, envelope, sweep);
501 gs = gbc_sound_channel + 1;
504 sound_status |= 0x02;
505 sample_data = gs->sample_data;
506 envelope_volume = gs->envelope_volume;
507 gbc_sound_render_channel(samples, 8, envelope, nosweep);
510 gs = gbc_sound_channel + 2;
511 if(gbc_sound_wave_update)
513 if(gs->wave_bank == 1)
515 gbc_sound_load_wave_ram(1);
519 gbc_sound_load_wave_ram(0);
522 gbc_sound_wave_update = 0;
525 if((gs->active_flag) && (gs->master_enable))
527 sound_status |= 0x04;
528 sample_data = wave_samples;
529 if(gs->wave_type == 0)
531 if(gs->wave_bank == 1)
534 gbc_sound_render_channel(samples, 32, noenvelope, nosweep);
538 gbc_sound_render_channel(samples, 64, noenvelope, nosweep);
542 gs = gbc_sound_channel + 3;
545 sound_status |= 0x08;
546 envelope_volume = gs->envelope_volume;
548 if(gs->noise_type == 1)
550 gbc_sound_render_channel(noise, half, envelope, nosweep);
554 gbc_sound_render_channel(noise, full, envelope, nosweep);
559 address16(io_registers, 0x84) = sound_status;
561 gbc_sound_last_cpu_ticks = cpu_ticks;
562 gbc_sound_buffer_index =
563 (gbc_sound_buffer_index + (buffer_ticks * 2)) % BUFFER_SIZE;
565 SDL_UnlockMutex(sound_mutex);
567 SDL_CondSignal(sound_cv);
570 #define sound_copy_normal() \
571 current_sample = source[i] \
573 #define sound_copy(source_offset, length, render_type) \
574 _length = (length) / 2; \
575 source = (s16 *)(sound_buffer + source_offset); \
576 for(i = 0; i < _length; i++) \
578 sound_copy_##render_type(); \
579 if(current_sample > 2047) \
580 current_sample = 2047; \
581 if(current_sample < -2048) \
582 current_sample = -2048; \
584 stream_base[i] = current_sample << 4; \
588 #define sound_copy_null(source_offset, length) \
589 _length = (length) / 2; \
590 source = (s16 *)(sound_buffer + source_offset); \
591 for(i = 0; i < _length; i++) \
593 stream_base[i] = 0; \
598 void sound_callback(void *userdata, Uint8 *stream, int length)
600 u32 sample_length = length / 2;
603 s16 *stream_base = (s16 *)stream;
607 SDL_LockMutex(sound_mutex);
609 while(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) <
610 length && !sound_exit_flag)
612 SDL_CondWait(sound_cv, sound_mutex);
615 if(global_enable_audio)
617 if((sound_buffer_base + sample_length) >= BUFFER_SIZE)
619 u32 partial_length = (BUFFER_SIZE - sound_buffer_base) * 2;
620 sound_copy(sound_buffer_base, partial_length, normal);
621 source = (s16 *)sound_buffer;
622 sound_copy(0, length - partial_length, normal);
623 sound_buffer_base = (length - partial_length) / 2;
627 sound_copy(sound_buffer_base, length, normal);
628 sound_buffer_base += sample_length;
633 if((sound_buffer_base + sample_length) >= BUFFER_SIZE)
635 u32 partial_length = (BUFFER_SIZE - sound_buffer_base) * 2;
636 sound_copy_null(sound_buffer_base, partial_length);
637 source = (s16 *)sound_buffer;
638 sound_copy(0, length - partial_length, normal);
639 sound_buffer_base = (length - partial_length) / 2;
643 sound_copy_null(sound_buffer_base, length);
644 sound_buffer_base += sample_length;
648 SDL_CondSignal(sound_cv);
650 SDL_UnlockMutex(sound_mutex);
653 // Special thanks to blarrg for the LSFR frequency used in Meridian, as posted
654 // on the forum at http://meridian.overclocked.org:
655 // http://meridian.overclocked.org/cgi-bin/wwwthreads/showpost.pl?Board=merid
656 // angeneraldiscussion&Number=2069&page=0&view=expanded&mode=threaded&sb=4
657 // Hope you don't mind me borrowing it ^_-
659 void init_noise_table(u32 *table, u32 period, u32 bit_length)
661 u32 shift_register = 0xFF;
662 u32 mask = ~(1 << bit_length);
663 s32 table_pos, bit_pos;
665 u32 table_period = (period + 31) / 32;
667 // Bits are stored in reverse order so they can be more easily moved to
668 // bit 31, for sign extended shift down.
670 for(table_pos = 0; table_pos < table_period; table_pos++)
673 for(bit_pos = 31; bit_pos >= 0; bit_pos--)
675 current_entry |= (shift_register & 0x01) << bit_pos;
678 ((1 & (shift_register ^ (shift_register >> 1))) << bit_length) |
679 ((shift_register >> 1) & mask);
682 table[table_pos] = current_entry;
688 direct_sound_struct *ds = direct_sound_channel;
689 gbc_sound_struct *gs = gbc_sound_channel;
692 SDL_LockMutex(sound_mutex);
695 sound_buffer_base = 0;
696 sound_last_cpu_ticks = 0;
697 memset(sound_buffer, 0, sizeof(sound_buffer));
699 for(i = 0; i < 2; i++, ds++)
701 ds->buffer_index = 0;
702 ds->status = DIRECT_SOUND_INACTIVE;
705 ds->fifo_fractional = 0;
706 ds->last_cpu_ticks = 0;
707 memset(ds->fifo, 0, 32);
710 gbc_sound_buffer_index = 0;
711 gbc_sound_last_cpu_ticks = 0;
712 gbc_sound_partial_ticks = 0;
714 gbc_sound_master_volume_left = 0;
715 gbc_sound_master_volume_right = 0;
716 gbc_sound_master_volume = 0;
717 memset(wave_samples, 0, 64);
719 for(i = 0; i < 4; i++, gs++)
721 gs->status = GBC_SOUND_INACTIVE;
722 gs->sample_data = square_pattern_duty[2];
726 SDL_UnlockMutex(sound_mutex);
731 gbc_sound_buffer_index =
732 (sound_buffer_base + audio_buffer_size) % BUFFER_SIZE;
735 SDL_CondSignal(sound_cv);
741 SDL_AudioSpec sound_settings;
744 audio_buffer_size = (audio_buffer_size_number * 1024) + 3072;
746 audio_buffer_size = 16 << audio_buffer_size_number;
747 // audio_buffer_size = 16384;
750 SDL_AudioSpec desired_spec =
756 audio_buffer_size / 4,
763 sound_mutex = SDL_CreateMutex();
764 sound_cv = SDL_CreateCond();
766 SDL_OpenAudio(&desired_spec, &sound_settings);
767 sound_frequency = sound_settings.freq;
768 audio_buffer_size = sound_settings.size;
769 u32 i = audio_buffer_size / 16;
770 for (audio_buffer_size_number = 0; i && (i & 1) == 0; i >>= 1)
771 audio_buffer_size_number++;
773 printf("audio: freq %d, size %d\n", sound_frequency, audio_buffer_size);
776 gbc_sound_tick_step =
777 float_to_fp16_16(256.0f / sound_frequency);
779 init_noise_table(noise_table15, 32767, 14);
780 init_noise_table(noise_table7, 127, 6);
787 #define sound_savestate_builder(type) \
788 void sound_##type##_savestate(file_tag_type savestate_file) \
790 file_##type##_variable(savestate_file, sound_on); \
791 file_##type##_variable(savestate_file, sound_buffer_base); \
792 file_##type##_variable(savestate_file, sound_last_cpu_ticks); \
793 file_##type##_variable(savestate_file, gbc_sound_buffer_index); \
794 file_##type##_variable(savestate_file, gbc_sound_last_cpu_ticks); \
795 file_##type##_variable(savestate_file, gbc_sound_partial_ticks); \
796 file_##type##_variable(savestate_file, gbc_sound_master_volume_left); \
797 file_##type##_variable(savestate_file, gbc_sound_master_volume_right); \
798 file_##type##_variable(savestate_file, gbc_sound_master_volume); \
799 file_##type##_array(savestate_file, wave_samples); \
800 file_##type##_array(savestate_file, direct_sound_channel); \
801 file_##type##_array(savestate_file, gbc_sound_channel); \
804 sound_savestate_builder(read);
805 sound_savestate_builder(write_mem);