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 #if defined(GP2X_BUILD) || defined(TAVI_BUILD)
29 u32 sound_frequency = 44100;
31 u32 sound_frequency = 44100;
34 SDL_AudioSpec sound_settings;
35 SDL_mutex *sound_mutex;
39 u32 audio_buffer_size_number = 7;
41 u32 audio_buffer_size_number = 1;
44 u32 audio_buffer_size;
46 s16 sound_buffer[BUFFER_SIZE];
47 u32 sound_buffer_base = 0;
49 u32 sound_last_cpu_ticks = 0;
50 fixed16_16 gbc_sound_tick_step;
52 // Queue 1, 2, or 4 samples to the top of the DS FIFO, wrap around circularly
54 #define sound_timer_queue(size, value) \
55 *((s##size *)(ds->fifo + ds->fifo_top)) = value; \
56 ds->fifo_top = (ds->fifo_top + 1) % 32; \
58 void sound_timer_queue8(u32 channel, u8 value)
60 direct_sound_struct *ds = direct_sound_channel + channel;
61 sound_timer_queue(8, value);
64 void sound_timer_queue16(u32 channel, u16 value)
66 direct_sound_struct *ds = direct_sound_channel + channel;
67 sound_timer_queue(8, value & 0xFF);
68 sound_timer_queue(8, value >> 8);
71 void sound_timer_queue32(u32 channel, u32 value)
73 direct_sound_struct *ds = direct_sound_channel + channel;
75 sound_timer_queue(8, value & 0xFF);
76 sound_timer_queue(8, (value >> 8) & 0xFF);
77 sound_timer_queue(8, (value >> 16) & 0xFF);
78 sound_timer_queue(8, value >> 24);
81 // Unqueue 1 sample from the base of the DS FIFO and place it on the audio
82 // buffer for as many samples as necessary. If the DS FIFO is 16 bytes or
83 // smaller and if DMA is enabled for the sound channel initiate a DMA transfer
86 #define render_sample_null() \
88 #define render_sample_left() \
89 sound_buffer[buffer_index] += current_sample + \
90 fp16_16_to_u32((next_sample - current_sample) * fifo_fractional) \
92 #define render_sample_right() \
93 sound_buffer[buffer_index + 1] += current_sample + \
94 fp16_16_to_u32((next_sample - current_sample) * fifo_fractional) \
96 #define render_sample_both() \
97 dest_sample = current_sample + \
98 fp16_16_to_u32((next_sample - current_sample) * fifo_fractional); \
99 sound_buffer[buffer_index] += dest_sample; \
100 sound_buffer[buffer_index + 1] += dest_sample \
102 #define render_samples(type) \
103 while(fifo_fractional <= 0xFFFF) \
105 render_sample_##type(); \
106 fifo_fractional += frequency_step; \
107 buffer_index = (buffer_index + 2) % BUFFER_SIZE; \
110 void sound_timer(fixed16_16 frequency_step, u32 channel)
112 direct_sound_struct *ds = direct_sound_channel + channel;
114 fixed16_16 fifo_fractional = ds->fifo_fractional;
115 u32 buffer_index = ds->buffer_index;
116 s16 current_sample, next_sample, dest_sample;
118 current_sample = ds->fifo[ds->fifo_base] << 4;
119 ds->fifo_base = (ds->fifo_base + 1) % 32;
120 next_sample = ds->fifo[ds->fifo_base] << 4;
124 if(ds->volume == DIRECT_SOUND_VOLUME_50)
126 current_sample >>= 1;
132 case DIRECT_SOUND_INACTIVE:
133 render_samples(null);
136 case DIRECT_SOUND_RIGHT:
137 render_samples(right);
140 case DIRECT_SOUND_LEFT:
141 render_samples(left);
144 case DIRECT_SOUND_LEFTRIGHT:
145 render_samples(both);
151 render_samples(null);
154 ds->buffer_index = buffer_index;
155 ds->fifo_fractional = fp16_16_fractional_part(fifo_fractional);
157 if(((ds->fifo_top - ds->fifo_base) % 32) <= 16)
159 if(dma[1].direct_sound_channel == channel)
160 dma_transfer(dma + 1);
162 if(dma[2].direct_sound_channel == channel)
163 dma_transfer(dma + 2);
167 void sound_reset_fifo(u32 channel)
169 direct_sound_struct *ds = direct_sound_channel;
171 memset(ds->fifo, 0, 32);
174 // Initial pattern data = 4bits (signed)
175 // Channel volume = 12bits
176 // Envelope volume = 14bits
177 // Master volume = 2bits
179 // Recalculate left and right volume as volume changes.
180 // To calculate the current sample, use (sample * volume) >> 16
182 // Square waves range from -8 (low) to 7 (high)
184 s8 square_pattern_duty[4][8] =
186 { 0xF8, 0xF8, 0xF8, 0xF8, 0x07, 0xF8, 0xF8, 0xF8 },
187 { 0xF8, 0xF8, 0xF8, 0xF8, 0x07, 0x07, 0xF8, 0xF8 },
188 { 0xF8, 0xF8, 0x07, 0x07, 0x07, 0x07, 0xF8, 0xF8 },
189 { 0x07, 0x07, 0x07, 0x07, 0xF8, 0xF8, 0x07, 0x07 },
194 u32 noise_table15[1024];
197 u32 gbc_sound_master_volume_table[4] = { 1, 2, 4, 0 };
199 u32 gbc_sound_channel_volume_table[8] =
211 u32 gbc_sound_envelope_volume_table[16] =
213 fixed_div(0, 15, 14),
214 fixed_div(1, 15, 14),
215 fixed_div(2, 15, 14),
216 fixed_div(3, 15, 14),
217 fixed_div(4, 15, 14),
218 fixed_div(5, 15, 14),
219 fixed_div(6, 15, 14),
220 fixed_div(7, 15, 14),
221 fixed_div(8, 15, 14),
222 fixed_div(9, 15, 14),
223 fixed_div(10, 15, 14),
224 fixed_div(11, 15, 14),
225 fixed_div(12, 15, 14),
226 fixed_div(13, 15, 14),
227 fixed_div(14, 15, 14),
228 fixed_div(15, 15, 14)
231 u32 gbc_sound_buffer_index = 0;
232 u32 gbc_sound_last_cpu_ticks = 0;
233 u32 gbc_sound_partial_ticks = 0;
235 u32 gbc_sound_master_volume_left;
236 u32 gbc_sound_master_volume_right;
237 u32 gbc_sound_master_volume;
239 #define update_volume_channel_envelope(channel) \
240 volume_##channel = gbc_sound_envelope_volume_table[envelope_volume] * \
241 gbc_sound_channel_volume_table[gbc_sound_master_volume_##channel] * \
242 gbc_sound_master_volume_table[gbc_sound_master_volume] \
244 #define update_volume_channel_noenvelope(channel) \
245 volume_##channel = gs->wave_volume * \
246 gbc_sound_channel_volume_table[gbc_sound_master_volume_##channel] * \
247 gbc_sound_master_volume_table[gbc_sound_master_volume] \
249 #define update_volume(type) \
250 update_volume_channel_##type(left); \
251 update_volume_channel_##type(right) \
253 #define update_tone_sweep() \
254 if(gs->sweep_status) \
256 u32 sweep_ticks = gs->sweep_ticks - 1; \
258 if(sweep_ticks == 0) \
260 u32 rate = gs->rate; \
262 if(gs->sweep_direction) \
263 rate = rate - (rate >> gs->sweep_shift); \
265 rate = rate + (rate >> gs->sweep_shift); \
270 frequency_step = float_to_fp16_16(((131072.0 / (2048 - rate)) * 8.0) / \
273 gs->frequency_step = frequency_step; \
276 sweep_ticks = gs->sweep_initial_ticks; \
278 gs->sweep_ticks = sweep_ticks; \
281 #define update_tone_nosweep() \
283 #define update_tone_envelope() \
284 if(gs->envelope_status) \
286 u32 envelope_ticks = gs->envelope_ticks - 1; \
287 envelope_volume = gs->envelope_volume; \
289 if(envelope_ticks == 0) \
291 if(gs->envelope_direction) \
293 if(envelope_volume != 15) \
294 envelope_volume = gs->envelope_volume + 1; \
298 if(envelope_volume != 0) \
299 envelope_volume = gs->envelope_volume - 1; \
302 update_volume(envelope); \
304 gs->envelope_volume = envelope_volume; \
305 gs->envelope_ticks = gs->envelope_initial_ticks; \
309 gs->envelope_ticks = envelope_ticks; \
313 #define update_tone_noenvelope() \
315 #define gbc_sound_synchronize() \
316 while(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) > \
317 (audio_buffer_size * 2)) \
319 SDL_CondWait(sound_cv, sound_mutex); \
322 #define update_tone_counters(envelope_op, sweep_op) \
323 tick_counter += gbc_sound_tick_step; \
324 if(tick_counter > 0xFFFF) \
326 if(gs->length_status) \
328 u32 length_ticks = gs->length_ticks - 1; \
329 gs->length_ticks = length_ticks; \
331 if(length_ticks == 0) \
333 gs->active_flag = 0; \
338 update_tone_##envelope_op(); \
339 update_tone_##sweep_op(); \
341 tick_counter &= 0xFFFF; \
344 #define gbc_sound_render_sample_right() \
345 sound_buffer[buffer_index + 1] += (current_sample * volume_right) >> 22 \
347 #define gbc_sound_render_sample_left() \
348 sound_buffer[buffer_index] += (current_sample * volume_left) >> 22 \
350 #define gbc_sound_render_sample_both() \
351 gbc_sound_render_sample_right(); \
352 gbc_sound_render_sample_left() \
354 #define gbc_sound_render_samples(type, sample_length, envelope_op, sweep_op) \
355 for(i = 0; i < buffer_ticks; i++) \
358 sample_data[fp16_16_to_u32(sample_index) % sample_length]; \
359 gbc_sound_render_sample_##type(); \
361 sample_index += frequency_step; \
362 buffer_index = (buffer_index + 2) % BUFFER_SIZE; \
364 update_tone_counters(envelope_op, sweep_op); \
367 #define gbc_noise_wrap_full 32767
369 #define gbc_noise_wrap_half 126
371 #define get_noise_sample_full() \
373 ((s32)(noise_table15[fp16_16_to_u32(sample_index) >> 5] << \
374 (fp16_16_to_u32(sample_index) & 0x1F)) >> 31) & 0x0F \
376 #define get_noise_sample_half() \
378 ((s32)(noise_table7[fp16_16_to_u32(sample_index) >> 5] << \
379 (fp16_16_to_u32(sample_index) & 0x1F)) >> 31) & 0x0F \
381 #define gbc_sound_render_noise(type, noise_type, envelope_op, sweep_op) \
382 for(i = 0; i < buffer_ticks; i++) \
384 get_noise_sample_##noise_type(); \
385 gbc_sound_render_sample_##type(); \
387 sample_index += frequency_step; \
389 if(sample_index >= u32_to_fp16_16(gbc_noise_wrap_##noise_type)) \
390 sample_index -= u32_to_fp16_16(gbc_noise_wrap_##noise_type); \
392 buffer_index = (buffer_index + 2) % BUFFER_SIZE; \
393 update_tone_counters(envelope_op, sweep_op); \
396 #define gbc_sound_render_channel(type, sample_length, envelope_op, sweep_op) \
397 buffer_index = gbc_sound_buffer_index; \
398 sample_index = gs->sample_index; \
399 frequency_step = gs->frequency_step; \
400 tick_counter = gs->tick_counter; \
402 update_volume(envelope_op); \
406 case GBC_SOUND_INACTIVE: \
409 case GBC_SOUND_LEFT: \
410 gbc_sound_render_##type(left, sample_length, envelope_op, sweep_op); \
413 case GBC_SOUND_RIGHT: \
414 gbc_sound_render_##type(right, sample_length, envelope_op, sweep_op); \
417 case GBC_SOUND_LEFTRIGHT: \
418 gbc_sound_render_##type(both, sample_length, envelope_op, sweep_op); \
422 gs->sample_index = sample_index; \
423 gs->tick_counter = tick_counter; \
425 #define gbc_sound_load_wave_ram(bank) \
426 wave_bank = wave_samples + (bank * 32); \
427 for(i = 0, i2 = 0; i < 16; i++, i2 += 2) \
429 current_sample = wave_ram[i]; \
430 wave_bank[i2] = (((current_sample >> 4) & 0x0F) - 8); \
431 wave_bank[i2 + 1] = ((current_sample & 0x0F) - 8); \
434 void synchronize_sound()
436 SDL_LockMutex(sound_mutex);
438 gbc_sound_synchronize();
440 SDL_UnlockMutex(sound_mutex);
443 void update_gbc_sound(u32 cpu_ticks)
445 fixed16_16 buffer_ticks = float_to_fp16_16(((float)(cpu_ticks -
446 gbc_sound_last_cpu_ticks) * sound_frequency) / 16777216.0);
448 gbc_sound_struct *gs = gbc_sound_channel;
449 fixed16_16 sample_index, frequency_step;
450 fixed16_16 tick_counter;
452 s32 volume_left, volume_right;
455 u32 sound_status = address16(io_registers, 0x84) & 0xFFF0;
458 u8 *wave_ram = ((u8 *)io_registers) + 0x90;
460 gbc_sound_partial_ticks += fp16_16_fractional_part(buffer_ticks);
461 buffer_ticks = fp16_16_to_u32(buffer_ticks);
463 if(gbc_sound_partial_ticks > 0xFFFF)
466 gbc_sound_partial_ticks &= 0xFFFF;
469 SDL_LockMutex(sound_mutex);
472 if(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) >
473 (audio_buffer_size * 3 / 2))
475 while(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) >
476 (audio_buffer_size * 3 / 2))
478 SDL_CondWait(sound_cv, sound_mutex);
482 if(current_frameskip_type == auto_frameskip)
484 sceDisplayWaitVblankStart();
485 real_frame_count = 0;
486 virtual_frame_count = 0;
493 if(current_frameskip_type == auto_frameskip)
497 get_ticks_us(¤t_ticks);
499 next_ticks = ((current_ticks + 16666) / 16667) * 16667;
500 delay_us(next_ticks - current_ticks);
502 get_ticks_us(&frame_count_initial_timestamp);
503 real_frame_count = 0;
504 virtual_frame_count = 0;
513 gs = gbc_sound_channel + 0;
516 sound_status |= 0x01;
517 sample_data = gs->sample_data;
518 envelope_volume = gs->envelope_volume;
519 gbc_sound_render_channel(samples, 8, envelope, sweep);
522 gs = gbc_sound_channel + 1;
525 sound_status |= 0x02;
526 sample_data = gs->sample_data;
527 envelope_volume = gs->envelope_volume;
528 gbc_sound_render_channel(samples, 8, envelope, nosweep);
531 gs = gbc_sound_channel + 2;
532 if(gbc_sound_wave_update)
534 if(gs->wave_bank == 1)
536 gbc_sound_load_wave_ram(1);
540 gbc_sound_load_wave_ram(0);
543 gbc_sound_wave_update = 0;
546 if((gs->active_flag) && (gs->master_enable))
548 sound_status |= 0x04;
549 sample_data = wave_samples;
550 if(gs->wave_type == 0)
552 if(gs->wave_bank == 1)
555 gbc_sound_render_channel(samples, 32, noenvelope, nosweep);
559 gbc_sound_render_channel(samples, 64, noenvelope, nosweep);
563 gs = gbc_sound_channel + 3;
566 sound_status |= 0x08;
567 envelope_volume = gs->envelope_volume;
569 if(gs->noise_type == 1)
571 gbc_sound_render_channel(noise, half, envelope, nosweep);
575 gbc_sound_render_channel(noise, full, envelope, nosweep);
580 address16(io_registers, 0x84) = sound_status;
582 SDL_CondSignal(sound_cv);
584 SDL_UnlockMutex(sound_mutex);
586 gbc_sound_last_cpu_ticks = cpu_ticks;
587 gbc_sound_buffer_index =
588 (gbc_sound_buffer_index + (buffer_ticks * 2)) % BUFFER_SIZE;
591 #define sound_copy_normal() \
592 current_sample = source[i] \
594 #define sound_copy(source_offset, length, render_type) \
595 _length = (length) / 2; \
596 source = (s16 *)(sound_buffer + source_offset); \
597 for(i = 0; i < _length; i++) \
599 sound_copy_##render_type(); \
600 if(current_sample > 2047) \
601 current_sample = 2047; \
602 if(current_sample < -2048) \
603 current_sample = -2048; \
605 stream_base[i] = current_sample << 4; \
609 #define sound_copy_null(source_offset, length) \
610 _length = (length) / 2; \
611 source = (s16 *)(sound_buffer + source_offset); \
612 for(i = 0; i < _length; i++) \
614 stream_base[i] = 0; \
619 void sound_callback(void *userdata, Uint8 *stream, int length)
621 u32 sample_length = length / 2;
624 s16 *stream_base = (s16 *)stream;
628 SDL_LockMutex(sound_mutex);
630 while(((gbc_sound_buffer_index - sound_buffer_base) % BUFFER_SIZE) <
633 SDL_CondWait(sound_cv, sound_mutex);
636 if(global_enable_audio)
638 if((sound_buffer_base + sample_length) >= BUFFER_SIZE)
640 u32 partial_length = (BUFFER_SIZE - sound_buffer_base) * 2;
641 sound_copy(sound_buffer_base, partial_length, normal);
642 source = (s16 *)sound_buffer;
643 sound_copy(0, length - partial_length, normal);
644 sound_buffer_base = (length - partial_length) / 2;
648 sound_copy(sound_buffer_base, length, normal);
649 sound_buffer_base += sample_length;
654 if((sound_buffer_base + sample_length) >= BUFFER_SIZE)
656 u32 partial_length = (BUFFER_SIZE - sound_buffer_base) * 2;
657 sound_copy_null(sound_buffer_base, partial_length);
658 source = (s16 *)sound_buffer;
659 sound_copy(0, length - partial_length, normal);
660 sound_buffer_base = (length - partial_length) / 2;
664 sound_copy_null(sound_buffer_base, length);
665 sound_buffer_base += sample_length;
669 SDL_CondSignal(sound_cv);
671 SDL_UnlockMutex(sound_mutex);
674 // Special thanks to blarrg for the LSFR frequency used in Meridian, as posted
675 // on the forum at http://meridian.overclocked.org:
676 // http://meridian.overclocked.org/cgi-bin/wwwthreads/showpost.pl?Board=merid
677 // angeneraldiscussion&Number=2069&page=0&view=expanded&mode=threaded&sb=4
678 // Hope you don't mind me borrowing it ^_-
680 void init_noise_table(u32 *table, u32 period, u32 bit_length)
682 u32 shift_register = 0xFF;
683 u32 mask = ~(1 << bit_length);
684 s32 table_pos, bit_pos;
686 u32 table_period = (period + 31) / 32;
688 // Bits are stored in reverse order so they can be more easily moved to
689 // bit 31, for sign extended shift down.
691 for(table_pos = 0; table_pos < table_period; table_pos++)
694 for(bit_pos = 31; bit_pos >= 0; bit_pos--)
696 current_entry |= (shift_register & 0x01) << bit_pos;
699 ((1 & (shift_register ^ (shift_register >> 1))) << bit_length) |
700 ((shift_register >> 1) & mask);
703 table[table_pos] = current_entry;
709 direct_sound_struct *ds = direct_sound_channel;
710 gbc_sound_struct *gs = gbc_sound_channel;
714 sound_buffer_base = 0;
715 sound_last_cpu_ticks = 0;
716 memset(sound_buffer, 0, audio_buffer_size);
718 for(i = 0; i < 2; i++, ds++)
720 ds->buffer_index = 0;
721 ds->status = DIRECT_SOUND_INACTIVE;
724 ds->fifo_fractional = 0;
725 ds->last_cpu_ticks = 0;
726 memset(ds->fifo, 0, 32);
729 gbc_sound_buffer_index = 0;
730 gbc_sound_last_cpu_ticks = 0;
731 gbc_sound_partial_ticks = 0;
733 gbc_sound_master_volume_left = 0;
734 gbc_sound_master_volume_right = 0;
735 gbc_sound_master_volume = 0;
736 memset(wave_samples, 0, 64);
738 for(i = 0; i < 4; i++, gs++)
740 gs->status = GBC_SOUND_INACTIVE;
741 gs->sample_data = square_pattern_duty[2];
748 gbc_sound_buffer_index =
749 (sound_buffer_base + audio_buffer_size) % BUFFER_SIZE;
751 SDL_CondSignal(sound_cv);
757 audio_buffer_size = (audio_buffer_size_number * 1024) + 3072;
758 #elif defined(TAVI_BUILD) || defined(ARM_ARCH)
759 audio_buffer_size = 16 << audio_buffer_size_number;
760 // audio_buffer_size = 16384;
762 audio_buffer_size = 16384;
765 SDL_AudioSpec desired_spec =
771 audio_buffer_size / 4,
778 gbc_sound_tick_step =
779 float_to_fp16_16(256.0 / sound_frequency);
781 init_noise_table(noise_table15, 32767, 14);
782 init_noise_table(noise_table7, 127, 6);
786 SDL_OpenAudio(&desired_spec, &sound_settings);
787 sound_frequency = sound_settings.freq;
788 sound_mutex = SDL_CreateMutex();
789 sound_cv = SDL_CreateCond();
793 #define sound_savestate_builder(type) \
794 void sound_##type##_savestate(file_tag_type savestate_file) \
796 file_##type##_variable(savestate_file, sound_on); \
797 file_##type##_variable(savestate_file, sound_buffer_base); \
798 file_##type##_variable(savestate_file, sound_last_cpu_ticks); \
799 file_##type##_variable(savestate_file, gbc_sound_buffer_index); \
800 file_##type##_variable(savestate_file, gbc_sound_last_cpu_ticks); \
801 file_##type##_variable(savestate_file, gbc_sound_partial_ticks); \
802 file_##type##_variable(savestate_file, gbc_sound_master_volume_left); \
803 file_##type##_variable(savestate_file, gbc_sound_master_volume_right); \
804 file_##type##_variable(savestate_file, gbc_sound_master_volume); \
805 file_##type##_array(savestate_file, wave_samples); \
806 file_##type##_array(savestate_file, direct_sound_channel); \
807 file_##type##_array(savestate_file, gbc_sound_channel); \
810 sound_savestate_builder(read);
811 sound_savestate_builder(write_mem);