/src/vlc/contrib/contrib-build/game-music-emu/gme/Hes_Apu.cpp
Line | Count | Source |
1 | | // Game_Music_Emu https://bitbucket.org/mpyne/game-music-emu/ |
2 | | |
3 | | #include "Hes_Apu.h" |
4 | | |
5 | | #include <string.h> |
6 | | |
7 | | /* Copyright (C) 2006 Shay Green. This module is free software; you |
8 | | can redistribute it and/or modify it under the terms of the GNU Lesser |
9 | | General Public License as published by the Free Software Foundation; either |
10 | | version 2.1 of the License, or (at your option) any later version. This |
11 | | module is distributed in the hope that it will be useful, but WITHOUT ANY |
12 | | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
13 | | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
14 | | details. You should have received a copy of the GNU Lesser General Public |
15 | | License along with this module; if not, write to the Free Software Foundation, |
16 | | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
17 | | |
18 | | #include "blargg_source.h" |
19 | | |
20 | | static bool const center_waves = true; // reduces asymmetry and clamping when starting notes |
21 | | |
22 | | Hes_Apu::Hes_Apu() |
23 | 3 | { |
24 | 3 | Hes_Osc* osc = &oscs [osc_count]; |
25 | 3 | do |
26 | 18 | { |
27 | 18 | osc--; |
28 | 18 | osc->outputs [0] = 0; |
29 | 18 | osc->outputs [1] = 0; |
30 | 18 | osc->chans [0] = 0; |
31 | 18 | osc->chans [1] = 0; |
32 | 18 | osc->chans [2] = 0; |
33 | 18 | } |
34 | 18 | while ( osc != oscs ); |
35 | | |
36 | 3 | reset(); |
37 | 3 | } |
38 | | |
39 | | void Hes_Apu::reset() |
40 | 515 | { |
41 | 515 | latch = 0; |
42 | 515 | balance = 0xFF; |
43 | | |
44 | 515 | Hes_Osc* osc = &oscs [osc_count]; |
45 | 515 | do |
46 | 3.09k | { |
47 | 3.09k | osc--; |
48 | 3.09k | memset( osc, 0, offsetof (Hes_Osc,outputs) ); |
49 | 3.09k | osc->noise_lfsr = 1; |
50 | 3.09k | osc->control = 0x40; |
51 | 3.09k | osc->balance = 0xFF; |
52 | 3.09k | } |
53 | 3.09k | while ( osc != oscs ); |
54 | 515 | } |
55 | | |
56 | | void Hes_Apu::osc_output( int index, Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right ) |
57 | 12 | { |
58 | 12 | require( (unsigned) index < osc_count ); |
59 | 12 | oscs [index].chans [0] = center; |
60 | 12 | oscs [index].chans [1] = left; |
61 | 12 | oscs [index].chans [2] = right; |
62 | | |
63 | 12 | Hes_Osc* osc = &oscs [osc_count]; |
64 | 12 | do |
65 | 72 | { |
66 | 72 | osc--; |
67 | 72 | balance_changed( *osc ); |
68 | 72 | } |
69 | 72 | while ( osc != oscs ); |
70 | 12 | } |
71 | | |
72 | | void Hes_Osc::run_until( synth_t& synth_, blip_time_t end_time ) |
73 | 494k | { |
74 | 494k | Blip_Buffer* const osc_outputs_0 = outputs [0]; // cache often-used values |
75 | 494k | if ( osc_outputs_0 && control & 0x80 ) |
76 | 0 | { |
77 | 0 | int dac = this->dac; |
78 | |
|
79 | 0 | int const volume_0 = volume [0]; |
80 | 0 | { |
81 | 0 | int delta = dac * volume_0 - last_amp [0]; |
82 | 0 | if ( delta ) |
83 | 0 | synth_.offset( last_time, delta, osc_outputs_0 ); |
84 | 0 | osc_outputs_0->set_modified(); |
85 | 0 | } |
86 | |
|
87 | 0 | Blip_Buffer* const osc_outputs_1 = outputs [1]; |
88 | 0 | int const volume_1 = volume [1]; |
89 | 0 | if ( osc_outputs_1 ) |
90 | 0 | { |
91 | 0 | int delta = dac * volume_1 - last_amp [1]; |
92 | 0 | if ( delta ) |
93 | 0 | synth_.offset( last_time, delta, osc_outputs_1 ); |
94 | 0 | osc_outputs_1->set_modified(); |
95 | 0 | } |
96 | |
|
97 | 0 | blip_time_t time = last_time + delay; |
98 | 0 | if ( time < end_time ) |
99 | 0 | { |
100 | 0 | if ( noise & 0x80 ) |
101 | 0 | { |
102 | 0 | if ( volume_0 | volume_1 ) |
103 | 0 | { |
104 | | // noise |
105 | 0 | int const period = (32 - (noise & 0x1F)) * 64; // TODO: correct? |
106 | 0 | unsigned noise_lfsr = this->noise_lfsr; |
107 | 0 | do |
108 | 0 | { |
109 | 0 | int new_dac = 0x1F & uMinus(noise_lfsr >> 1 & 1); |
110 | | // Implemented using "Galios configuration" |
111 | | // TODO: find correct LFSR algorithm |
112 | 0 | noise_lfsr = (noise_lfsr >> 1) ^ (0xE008 & uMinus(noise_lfsr & 1)); |
113 | | //noise_lfsr = (noise_lfsr >> 1) ^ (0x6000 & -(noise_lfsr & 1)); |
114 | 0 | int delta = new_dac - dac; |
115 | 0 | if ( delta ) |
116 | 0 | { |
117 | 0 | dac = new_dac; |
118 | 0 | synth_.offset( time, delta * volume_0, osc_outputs_0 ); |
119 | 0 | if ( osc_outputs_1 ) |
120 | 0 | synth_.offset( time, delta * volume_1, osc_outputs_1 ); |
121 | 0 | } |
122 | 0 | time += period; |
123 | 0 | } |
124 | 0 | while ( time < end_time ); |
125 | |
|
126 | 0 | this->noise_lfsr = noise_lfsr; |
127 | 0 | assert( noise_lfsr ); |
128 | 0 | } |
129 | 0 | } |
130 | 0 | else if ( !(control & 0x40) ) |
131 | 0 | { |
132 | | // wave |
133 | 0 | int phase = (this->phase + 1) & 0x1F; // pre-advance for optimal inner loop |
134 | 0 | int period = this->period * 2; |
135 | 0 | if ( period >= 14 && (volume_0 | volume_1) ) |
136 | 0 | { |
137 | 0 | do |
138 | 0 | { |
139 | 0 | int new_dac = wave [phase]; |
140 | 0 | phase = (phase + 1) & 0x1F; |
141 | 0 | int delta = new_dac - dac; |
142 | 0 | if ( delta ) |
143 | 0 | { |
144 | 0 | dac = new_dac; |
145 | 0 | synth_.offset( time, delta * volume_0, osc_outputs_0 ); |
146 | 0 | if ( osc_outputs_1 ) |
147 | 0 | synth_.offset( time, delta * volume_1, osc_outputs_1 ); |
148 | 0 | } |
149 | 0 | time += period; |
150 | 0 | } |
151 | 0 | while ( time < end_time ); |
152 | 0 | } |
153 | 0 | else |
154 | 0 | { |
155 | 0 | if ( !period ) |
156 | 0 | { |
157 | | // TODO: Gekisha Boy assumes that period = 0 silences wave |
158 | | //period = 0x1000 * 2; |
159 | 0 | period = 1; |
160 | | //if ( !(volume_0 | volume_1) ) |
161 | | // debug_printf( "Used period 0\n" ); |
162 | 0 | } |
163 | | // maintain phase when silent |
164 | 0 | int32_t count = (end_time - time + period - 1) / period; |
165 | 0 | phase += count; // phase will be masked below |
166 | 0 | time += count * period; |
167 | 0 | } |
168 | 0 | this->phase = (phase - 1) & 0x1F; // undo pre-advance |
169 | 0 | } |
170 | 0 | } |
171 | 0 | time -= end_time; |
172 | 0 | if ( time < 0 ) |
173 | 0 | time = 0; |
174 | 0 | delay = time; |
175 | |
|
176 | 0 | this->dac = dac; |
177 | 0 | last_amp [0] = dac * volume_0; |
178 | 0 | last_amp [1] = dac * volume_1; |
179 | 0 | } |
180 | 494k | last_time = end_time; |
181 | 494k | } |
182 | | |
183 | | void Hes_Apu::balance_changed( Hes_Osc& osc ) |
184 | 72 | { |
185 | 72 | static short const log_table [32] = { // ~1.5 db per step |
186 | 2.30k | #define ENTRY( factor ) short (factor * Hes_Osc::amp_range / 31.0 + 0.5) |
187 | 72 | ENTRY( 0.000000 ),ENTRY( 0.005524 ),ENTRY( 0.006570 ),ENTRY( 0.007813 ), |
188 | 72 | ENTRY( 0.009291 ),ENTRY( 0.011049 ),ENTRY( 0.013139 ),ENTRY( 0.015625 ), |
189 | 72 | ENTRY( 0.018581 ),ENTRY( 0.022097 ),ENTRY( 0.026278 ),ENTRY( 0.031250 ), |
190 | 72 | ENTRY( 0.037163 ),ENTRY( 0.044194 ),ENTRY( 0.052556 ),ENTRY( 0.062500 ), |
191 | 72 | ENTRY( 0.074325 ),ENTRY( 0.088388 ),ENTRY( 0.105112 ),ENTRY( 0.125000 ), |
192 | 72 | ENTRY( 0.148651 ),ENTRY( 0.176777 ),ENTRY( 0.210224 ),ENTRY( 0.250000 ), |
193 | 72 | ENTRY( 0.297302 ),ENTRY( 0.353553 ),ENTRY( 0.420448 ),ENTRY( 0.500000 ), |
194 | 72 | ENTRY( 0.594604 ),ENTRY( 0.707107 ),ENTRY( 0.840896 ),ENTRY( 1.000000 ), |
195 | 72 | #undef ENTRY |
196 | 72 | }; |
197 | | |
198 | 72 | int vol = (osc.control & 0x1F) - 0x1E * 2; |
199 | | |
200 | 72 | int left = vol + (osc.balance >> 3 & 0x1E) + (balance >> 3 & 0x1E); |
201 | 72 | if ( left < 0 ) left = 0; |
202 | | |
203 | 72 | int right = vol + (osc.balance << 1 & 0x1E) + (balance << 1 & 0x1E); |
204 | 72 | if ( right < 0 ) right = 0; |
205 | | |
206 | 72 | left = log_table [left ]; |
207 | 72 | right = log_table [right]; |
208 | | |
209 | | // optimizing for the common case of being centered also allows easy |
210 | | // panning using Effects_Buffer |
211 | 72 | osc.outputs [0] = osc.chans [0]; // center |
212 | 72 | osc.outputs [1] = 0; |
213 | 72 | if ( left != right ) |
214 | 0 | { |
215 | 0 | osc.outputs [0] = osc.chans [1]; // left |
216 | 0 | osc.outputs [1] = osc.chans [2]; // right |
217 | 0 | } |
218 | | |
219 | 72 | if ( center_waves ) |
220 | 72 | { |
221 | 72 | osc.last_amp [0] += (left - osc.volume [0]) * 16; |
222 | 72 | osc.last_amp [1] += (right - osc.volume [1]) * 16; |
223 | 72 | } |
224 | | |
225 | 72 | osc.volume [0] = left; |
226 | 72 | osc.volume [1] = right; |
227 | 72 | } |
228 | | |
229 | | void Hes_Apu::write_data( blip_time_t time, int addr, int data ) |
230 | 0 | { |
231 | 0 | if ( addr == 0x800 ) |
232 | 0 | { |
233 | 0 | latch = data & 7; |
234 | 0 | } |
235 | 0 | else if ( addr == 0x801 ) |
236 | 0 | { |
237 | 0 | if ( balance != data ) |
238 | 0 | { |
239 | 0 | balance = data; |
240 | |
|
241 | 0 | Hes_Osc* osc = &oscs [osc_count]; |
242 | 0 | do |
243 | 0 | { |
244 | 0 | osc--; |
245 | 0 | osc->run_until( synth, time ); |
246 | 0 | balance_changed( *oscs ); |
247 | 0 | } |
248 | 0 | while ( osc != oscs ); |
249 | 0 | } |
250 | 0 | } |
251 | 0 | else if ( latch < osc_count ) |
252 | 0 | { |
253 | 0 | Hes_Osc& osc = oscs [latch]; |
254 | 0 | osc.run_until( synth, time ); |
255 | 0 | switch ( addr ) |
256 | 0 | { |
257 | 0 | case 0x802: |
258 | 0 | osc.period = (osc.period & 0xF00) | data; |
259 | 0 | break; |
260 | | |
261 | 0 | case 0x803: |
262 | 0 | osc.period = (osc.period & 0x0FF) | ((data & 0x0F) << 8); |
263 | 0 | break; |
264 | | |
265 | 0 | case 0x804: |
266 | 0 | if ( osc.control & 0x40 & ~data ) |
267 | 0 | osc.phase = 0; |
268 | 0 | osc.control = data; |
269 | 0 | balance_changed( osc ); |
270 | 0 | break; |
271 | | |
272 | 0 | case 0x805: |
273 | 0 | osc.balance = data; |
274 | 0 | balance_changed( osc ); |
275 | 0 | break; |
276 | | |
277 | 0 | case 0x806: |
278 | 0 | data &= 0x1F; |
279 | 0 | if ( !(osc.control & 0x40) ) |
280 | 0 | { |
281 | 0 | osc.wave [osc.phase] = data; |
282 | 0 | osc.phase = (osc.phase + 1) & 0x1F; |
283 | 0 | } |
284 | 0 | else if ( osc.control & 0x80 ) |
285 | 0 | { |
286 | 0 | osc.dac = data; |
287 | 0 | } |
288 | 0 | break; |
289 | | |
290 | 0 | case 0x807: |
291 | 0 | if ( &osc >= &oscs [4] ) |
292 | 0 | osc.noise = data; |
293 | 0 | break; |
294 | | |
295 | 0 | case 0x809: |
296 | 0 | if ( !(data & 0x80) && (data & 0x03) != 0 ) |
297 | 0 | debug_printf( "HES LFO not supported\n" ); |
298 | 0 | } |
299 | 0 | } |
300 | 0 | } |
301 | | |
302 | | void Hes_Apu::end_frame( blip_time_t end_time ) |
303 | 82.4k | { |
304 | 82.4k | Hes_Osc* osc = &oscs [osc_count]; |
305 | 82.4k | do |
306 | 494k | { |
307 | 494k | osc--; |
308 | 494k | if ( end_time > osc->last_time ) |
309 | 494k | osc->run_until( synth, end_time ); |
310 | 494k | assert( osc->last_time >= end_time ); |
311 | 494k | osc->last_time -= end_time; |
312 | 494k | } |
313 | 494k | while ( osc != oscs ); |
314 | 82.4k | } |