1
// Game_Music_Emu 0.5.2. http://www.slack.net/~ant/
3
#include "Kss_Scc_Apu.h"
5
/* Copyright (C) 2006 Shay Green. This module is free software; you
6
can redistribute it and/or modify it under the terms of the GNU Lesser
7
General Public License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version. This
9
module is distributed in the hope that it will be useful, but WITHOUT ANY
10
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12
details. You should have received a copy of the GNU Lesser General Public
13
License along with this module; if not, write to the Free Software Foundation,
14
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
16
#include "blargg_source.h"
18
// Tones above this frequency are treated as disabled tone at half volume.
19
// Power of two is more efficient (avoids division).
20
unsigned const inaudible_freq = 16384;
22
int const wave_size = 0x20;
24
void Scc_Apu::run_until( blip_time_t end_time )
26
for ( int index = 0; index < osc_count; index++ )
28
osc_t& osc = oscs [index];
30
Blip_Buffer* const output = osc.output;
33
output->set_modified();
35
blip_time_t period = (regs [0x80 + index * 2 + 1] & 0x0F) * 0x100 +
36
regs [0x80 + index * 2] + 1;
38
if ( regs [0x8F] & (1 << index) )
40
blip_time_t inaudible_period = (blargg_ulong) (output->clock_rate() +
41
inaudible_freq * 32) / (inaudible_freq * 16);
42
if ( period > inaudible_period )
43
volume = (regs [0x8A + index] & 0x0F) * (amp_range / 256 / 15);
46
BOOST::int8_t const* wave = (BOOST::int8_t*) regs + index * wave_size;
47
if ( index == osc_count - 1 )
48
wave -= wave_size; // last two oscs share wave
50
int amp = wave [osc.phase] * volume;
51
int delta = amp - osc.last_amp;
55
synth.offset( last_time, delta, output );
59
blip_time_t time = last_time + osc.delay;
60
if ( time < end_time )
65
blargg_long count = (end_time - time + period - 1) / period;
66
osc.phase = (osc.phase + count) & (wave_size - 1);
67
time += count * period;
72
int phase = osc.phase;
73
int last_wave = wave [phase];
74
phase = (phase + 1) & (wave_size - 1); // pre-advance for optimal inner loop
78
int amp = wave [phase];
79
phase = (phase + 1) & (wave_size - 1);
80
int delta = amp - last_wave;
84
synth.offset( time, delta * volume, output );
88
while ( time < end_time );
90
osc.phase = phase = (phase - 1) & (wave_size - 1); // undo pre-advance
91
osc.last_amp = wave [phase] * volume;
94
osc.delay = time - end_time;