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 #define BUFFER_SIZE 65536
25 // A lot of sound cards on PC can't handle such small buffers but this
26 // seems to work well on PSP.
30 #define SOUND_BUFFER_SIZE 4096
34 #define SOUND_BUFFER_SIZE 16384
40 DIRECT_SOUND_INACTIVE,
43 DIRECT_SOUND_LEFTRIGHT
44 } direct_sound_status_type;
48 DIRECT_SOUND_VOLUME_50,
49 DIRECT_SOUND_VOLUME_100
50 } direct_sound_volume_type;
57 fixed16_16 fifo_fractional;
58 // The + 1 is to give some extra room for linear interpolation
59 // when wrapping around.
61 direct_sound_status_type status;
62 direct_sound_volume_type volume;
64 } direct_sound_struct;
72 } gbc_sound_status_type;
78 fixed16_16 frequency_step;
79 fixed16_16 sample_index;
80 fixed16_16 tick_counter;
82 u32 envelope_initial_volume;
84 u32 envelope_direction;
88 u32 envelope_initial_ticks;
92 u32 sweep_initial_ticks;
100 gbc_sound_status_type status;
106 extern direct_sound_struct direct_sound_channel[2];
107 extern gbc_sound_struct gbc_sound_channel[4];
108 extern s8 square_pattern_duty[4][8];
109 extern u32 gbc_sound_master_volume_left;
110 extern u32 gbc_sound_master_volume_right;
111 extern u32 gbc_sound_master_volume;
113 extern u32 sound_frequency;
116 extern u32 global_enable_audio;
117 extern u32 enable_low_pass_filter;
118 extern u32 audio_buffer_size_number;
120 extern SDL_mutex *sound_mutex;
121 extern SDL_cond *sound_cv;
123 void sound_timer_queue8(u32 channel, u8 value);
124 void sound_timer_queue16(u32 channel, u16 value);
125 void sound_timer_queue32(u32 channel, u32 value);
126 void sound_timer(fixed16_16 frequency_step, u32 channel);
127 void sound_reset_fifo(u32 channel);
128 void update_gbc_sound(u32 cpu_ticks);
130 void sound_write_mem_savestate(file_tag_type savestate_file);
131 void sound_read_savestate(file_tag_type savestate_file);
133 #define gbc_sound_tone_control_low(channel, address) \
135 u32 initial_volume = (value >> 12) & 0x0F; \
136 u32 envelope_ticks = ((value >> 8) & 0x07) * 4; \
137 gbc_sound_channel[channel].length_ticks = 64 - (value & 0x3F); \
138 gbc_sound_channel[channel].sample_data = \
139 square_pattern_duty[(value >> 6) & 0x03]; \
140 gbc_sound_channel[channel].envelope_direction = (value >> 11) & 0x01; \
141 gbc_sound_channel[channel].envelope_initial_volume = initial_volume; \
142 gbc_sound_channel[channel].envelope_volume = initial_volume; \
143 gbc_sound_channel[channel].envelope_initial_ticks = envelope_ticks; \
144 gbc_sound_channel[channel].envelope_ticks = envelope_ticks; \
145 gbc_sound_channel[channel].envelope_status = (envelope_ticks != 0); \
146 gbc_sound_channel[channel].envelope_volume = initial_volume; \
147 gbc_sound_update = 1; \
148 address16(io_registers, address) = value; \
151 #define gbc_sound_tone_control_high(channel, address) \
153 u32 rate = value & 0x7FF; \
154 gbc_sound_channel[channel].rate = rate; \
155 gbc_sound_channel[channel].frequency_step = \
156 float_to_fp16_16(((131072.0 / (2048 - rate)) * 8.0) / sound_frequency); \
157 gbc_sound_channel[channel].length_status = (value >> 14) & 0x01; \
160 gbc_sound_channel[channel].active_flag = 1; \
161 gbc_sound_channel[channel].sample_index -= float_to_fp16_16(1.0 / 12.0); \
162 gbc_sound_channel[channel].envelope_ticks = \
163 gbc_sound_channel[channel].envelope_initial_ticks; \
164 gbc_sound_channel[channel].envelope_volume = \
165 gbc_sound_channel[channel].envelope_initial_volume; \
168 gbc_sound_update = 1; \
169 address16(io_registers, address) = value; \
172 #define gbc_sound_tone_control_sweep() \
174 u32 sweep_ticks = ((value >> 4) & 0x07) * 2; \
175 gbc_sound_channel[0].sweep_shift = value & 0x07; \
176 gbc_sound_channel[0].sweep_direction = (value >> 3) & 0x01; \
177 gbc_sound_channel[0].sweep_status = (value != 8); \
178 gbc_sound_channel[0].sweep_ticks = sweep_ticks; \
179 gbc_sound_channel[0].sweep_initial_ticks = sweep_ticks; \
180 gbc_sound_update = 1; \
181 address16(io_registers, 0x60) = value; \
184 #define gbc_sound_wave_control() \
186 gbc_sound_channel[2].wave_type = (value >> 5) & 0x01; \
187 gbc_sound_channel[2].wave_bank = (value >> 6) & 0x01; \
190 gbc_sound_channel[2].master_enable = 1; \
194 gbc_sound_channel[2].master_enable = 0; \
197 gbc_sound_update = 1; \
198 address16(io_registers, 0x70) = value; \
201 static u32 gbc_sound_wave_volume[4] = { 0, 16384, 8192, 4096 };
203 #define gbc_sound_tone_control_low_wave() \
205 gbc_sound_channel[2].length_ticks = 256 - (value & 0xFF); \
206 if((value >> 15) & 0x01) \
208 gbc_sound_channel[2].wave_volume = 12288; \
212 gbc_sound_channel[2].wave_volume = \
213 gbc_sound_wave_volume[(value >> 13) & 0x03]; \
215 gbc_sound_update = 1; \
216 address16(io_registers, 0x72) = value; \
219 #define gbc_sound_tone_control_high_wave() \
221 u32 rate = value & 0x7FF; \
222 gbc_sound_channel[2].rate = rate; \
223 gbc_sound_channel[2].frequency_step = \
224 float_to_fp16_16((2097152.0 / (2048 - rate)) / sound_frequency); \
225 gbc_sound_channel[2].length_status = (value >> 14) & 0x01; \
228 gbc_sound_channel[2].sample_index = 0; \
229 gbc_sound_channel[2].active_flag = 1; \
231 gbc_sound_update = 1; \
232 address16(io_registers, 0x74) = value; \
235 #define gbc_sound_noise_control() \
237 u32 dividing_ratio = value & 0x07; \
238 u32 frequency_shift = (value >> 4) & 0x0F; \
239 if(dividing_ratio == 0) \
241 gbc_sound_channel[3].frequency_step = \
242 float_to_fp16_16(1048576.0 / (1 << (frequency_shift + 1)) / \
247 gbc_sound_channel[3].frequency_step = \
248 float_to_fp16_16(524288.0 / (dividing_ratio * \
249 (1 << (frequency_shift + 1))) / sound_frequency); \
251 gbc_sound_channel[3].noise_type = (value >> 3) & 0x01; \
252 gbc_sound_channel[3].length_status = (value >> 14) & 0x01; \
255 gbc_sound_channel[3].sample_index = 0; \
256 gbc_sound_channel[3].active_flag = 1; \
257 gbc_sound_channel[3].envelope_ticks = \
258 gbc_sound_channel[3].envelope_initial_ticks; \
259 gbc_sound_channel[3].envelope_volume = \
260 gbc_sound_channel[3].envelope_initial_volume; \
262 gbc_sound_update = 1; \
263 address16(io_registers, 0x7C) = value; \
266 #define gbc_trigger_sound_channel(channel) \
267 gbc_sound_master_volume_right = value & 0x07; \
268 gbc_sound_master_volume_left = (value >> 4) & 0x07; \
269 gbc_sound_channel[channel].status = ((value >> (channel + 8)) & 0x01) | \
270 ((value >> (channel + 11)) & 0x03) \
272 #define gbc_trigger_sound() \
274 gbc_trigger_sound_channel(0); \
275 gbc_trigger_sound_channel(1); \
276 gbc_trigger_sound_channel(2); \
277 gbc_trigger_sound_channel(3); \
278 address16(io_registers, 0x80) = value; \
281 #define trigger_sound() \
283 timer[0].direct_sound_channels = (((value >> 10) & 0x01) == 0) | \
284 ((((value >> 14) & 0x01) == 0) << 1); \
285 timer[1].direct_sound_channels = (((value >> 10) & 0x01) == 1) | \
286 ((((value >> 14) & 0x01) == 1) << 1); \
287 direct_sound_channel[0].volume = (value >> 2) & 0x01; \
288 direct_sound_channel[0].status = (value >> 8) & 0x03; \
289 direct_sound_channel[1].volume = (value >> 3) & 0x01; \
290 direct_sound_channel[1].status = (value >> 12) & 0x03; \
291 gbc_sound_master_volume = value & 0x03; \
293 if((value >> 11) & 0x01) \
294 sound_reset_fifo(0); \
295 if((value >> 15) & 0x01) \
296 sound_reset_fifo(1); \
297 address16(io_registers, 0x82) = value; \
311 for(i = 0; i < 4; i++) \
313 gbc_sound_channel[i].active_flag = 0; \
317 address16(io_registers, 0x84) = \
318 (address16(io_registers, 0x84) & 0x000F) | (value & 0xFFF0); \
320 #define sound_update_frequency_step(timer_number) \
321 timer[timer_number].frequency_step = \
322 float_to_fp16_16(16777216.0 / (timer_reload * sound_frequency)) \