1 /* gameplaySP
2  *
3  * Copyright (C) 2006 Exophase <exophase@gmail.com>
4  *
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.
9  *
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.
14  *
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
18  */
19 
20 #ifndef SOUND_H
21 #define SOUND_H
22 
23 #define BUFFER_SIZE        (1 << 16)
24 #define BUFFER_SIZE_MASK   (BUFFER_SIZE - 1)
25 
26 #define GBA_SOUND_FREQUENCY   (64 * 1024)
27 
28 #define GBC_BASE_RATE ((float)(16 * 1024 * 1024))
29 
30 typedef enum
31 {
32    DIRECT_SOUND_INACTIVE,
33    DIRECT_SOUND_RIGHT,
34    DIRECT_SOUND_LEFT,
35    DIRECT_SOUND_LEFTRIGHT
36 } direct_sound_status_type;
37 
38 typedef enum
39 {
40    DIRECT_SOUND_VOLUME_50,
41    DIRECT_SOUND_VOLUME_100
42 } direct_sound_volume_type;
43 
44 typedef struct
45 {
46    s8 fifo[32];
47    u32 fifo_base;
48    u32 fifo_top;
49    fixed8_24 fifo_fractional;
50    // The + 1 is to give some extra room for linear interpolation
51    // when wrapping around.
52    u32 buffer_index;
53    direct_sound_status_type status;
54    direct_sound_volume_type volume;
55    u32 last_cpu_ticks;
56 } direct_sound_struct;
57 
58 typedef enum
59 {
60    GBC_SOUND_INACTIVE,
61    GBC_SOUND_RIGHT,
62    GBC_SOUND_LEFT,
63    GBC_SOUND_LEFTRIGHT
64 } gbc_sound_status_type;
65 
66 
67 typedef struct
68 {
69    u32 rate;
70    fixed16_16 frequency_step;
71    fixed16_16 sample_index;
72    fixed16_16 tick_counter;
73    u32 total_volume;
74    u32 envelope_initial_volume;
75    u32 envelope_volume;
76    u32 envelope_direction;
77    u32 envelope_status;
78    u32 envelope_step;
79    u32 envelope_ticks;
80    u32 envelope_initial_ticks;
81    u32 sweep_status;
82    u32 sweep_direction;
83    u32 sweep_ticks;
84    u32 sweep_initial_ticks;
85    u32 sweep_shift;
86    u32 length_status;
87    u32 length_ticks;
88    u32 noise_type;
89    u32 wave_type;
90    u32 wave_bank;
91    u32 wave_volume;
92    gbc_sound_status_type status;
93    u32 active_flag;
94    u32 master_enable;
95    s8* sample_data;
96 } gbc_sound_struct;
97 
98 extern direct_sound_struct direct_sound_channel[2];
99 extern gbc_sound_struct gbc_sound_channel[4];
100 extern s8 square_pattern_duty[4][8];
101 extern u32 gbc_sound_master_volume_left;
102 extern u32 gbc_sound_master_volume_right;
103 extern u32 gbc_sound_master_volume;
104 extern u32 gbc_sound_buffer_index;
105 extern u32 gbc_sound_last_cpu_ticks;
106 
107 extern u32 sound_frequency;
108 extern u32 sound_on;
109 
110 extern u32 global_enable_audio;
111 extern u32 enable_low_pass_filter;
112 
113 void sound_timer_queue8(u32 channel, u8 value);
114 void sound_timer_queue16(u32 channel, u16 value);
115 void sound_timer_queue32(u32 channel, u32 value);
116 void sound_timer(fixed8_24 frequency_step, u32 channel);
117 void sound_reset_fifo(u32 channel);
118 void update_gbc_sound(u32 cpu_ticks);
119 void init_sound(int need_reset);
120 void sound_write_savestate(void);
121 void sound_read_savestate(void);
122 
123 void render_audio(void);
124 
125 void reset_sound(void);
126 
127 #endif
128