diff options
Diffstat (limited to 'apps/codecs/libgme/nes_namco_apu.c')
-rw-r--r-- | apps/codecs/libgme/nes_namco_apu.c | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/apps/codecs/libgme/nes_namco_apu.c b/apps/codecs/libgme/nes_namco_apu.c new file mode 100644 index 0000000000..0fca501eff --- /dev/null +++ b/apps/codecs/libgme/nes_namco_apu.c | |||
@@ -0,0 +1,133 @@ | |||
1 | // Nes_Snd_Emu 0.1.8. http://www.slack.net/~ant/ | ||
2 | |||
3 | #include "nes_namco_apu.h" | ||
4 | |||
5 | /* Copyright (C) 2003-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 */ | ||
15 | |||
16 | #include "blargg_source.h" | ||
17 | |||
18 | void Namco_init( struct Nes_Namco_Apu* this ) | ||
19 | { | ||
20 | Synth_init( &this->synth ); | ||
21 | |||
22 | Namco_output( this, NULL ); | ||
23 | Namco_volume( this, 1.0 ); | ||
24 | Namco_reset( this ); | ||
25 | } | ||
26 | |||
27 | void Namco_reset( struct Nes_Namco_Apu* this ) | ||
28 | { | ||
29 | this->last_time = 0; | ||
30 | this->addr_reg = 0; | ||
31 | |||
32 | int i; | ||
33 | for ( i = 0; i < namco_reg_count; i++ ) | ||
34 | this->reg [i] = 0; | ||
35 | |||
36 | for ( i = 0; i < namco_osc_count; i++ ) | ||
37 | { | ||
38 | struct Namco_Osc* osc = &this->oscs [i]; | ||
39 | osc->delay = 0; | ||
40 | osc->last_amp = 0; | ||
41 | osc->wave_pos = 0; | ||
42 | } | ||
43 | } | ||
44 | |||
45 | void Namco_output( struct Nes_Namco_Apu* this, struct Blip_Buffer* buf ) | ||
46 | { | ||
47 | int i; | ||
48 | for ( i = 0; i < namco_osc_count; i++ ) | ||
49 | Namco_osc_output( this, i, buf ); | ||
50 | } | ||
51 | |||
52 | void Namco_end_frame( struct Nes_Namco_Apu* this, blip_time_t time ) | ||
53 | { | ||
54 | if ( time > this->last_time ) | ||
55 | Namco_run_until( this, time ); | ||
56 | |||
57 | assert( this->last_time >= time ); | ||
58 | this->last_time -= time; | ||
59 | } | ||
60 | |||
61 | void Namco_run_until( struct Nes_Namco_Apu* this, blip_time_t nes_end_time ) | ||
62 | { | ||
63 | int active_oscs = (this->reg [0x7F] >> 4 & 7) + 1; | ||
64 | int i; | ||
65 | for ( i = namco_osc_count - active_oscs; i < namco_osc_count; i++ ) | ||
66 | { | ||
67 | struct Namco_Osc* osc = &this->oscs [i]; | ||
68 | struct Blip_Buffer* output = osc->output; | ||
69 | if ( !output ) | ||
70 | continue; | ||
71 | /* output->set_modified(); */ | ||
72 | Blip_set_modified( output ); | ||
73 | |||
74 | blip_resampled_time_t time = | ||
75 | Blip_resampled_time( output, this->last_time ) + osc->delay; | ||
76 | blip_resampled_time_t end_time = Blip_resampled_time( output, nes_end_time ); | ||
77 | osc->delay = 0; | ||
78 | if ( time < end_time ) | ||
79 | { | ||
80 | const uint8_t* osc_reg = &this->reg [i * 8 + 0x40]; | ||
81 | if ( !(osc_reg [4] & 0xE0) ) | ||
82 | continue; | ||
83 | |||
84 | int volume = osc_reg [7] & 15; | ||
85 | if ( !volume ) | ||
86 | continue; | ||
87 | |||
88 | blargg_long freq = (osc_reg [4] & 3) * 0x10000 + osc_reg [2] * 0x100L + osc_reg [0]; | ||
89 | if ( freq < 64 * active_oscs ) | ||
90 | continue; // prevent low frequencies from excessively delaying freq changes | ||
91 | blip_resampled_time_t period = | ||
92 | /* output->resampled_duration( 983040 ) / freq * active_oscs; */ | ||
93 | Blip_resampled_duration( output, 983040 ) / freq * active_oscs; | ||
94 | |||
95 | int wave_size = 32 - (osc_reg [4] >> 2 & 7) * 4; | ||
96 | if ( !wave_size ) | ||
97 | continue; | ||
98 | |||
99 | int last_amp = osc->last_amp; | ||
100 | int wave_pos = osc->wave_pos; | ||
101 | |||
102 | do | ||
103 | { | ||
104 | // read wave sample | ||
105 | int addr = wave_pos + osc_reg [6]; | ||
106 | int sample = this->reg [addr >> 1] >> (addr << 2 & 4); | ||
107 | wave_pos++; | ||
108 | sample = (sample & 15) * volume; | ||
109 | |||
110 | // output impulse if amplitude changed | ||
111 | int delta = sample - last_amp; | ||
112 | if ( delta ) | ||
113 | { | ||
114 | last_amp = sample; | ||
115 | Synth_offset_resampled( &this->synth, time, delta, output ); | ||
116 | } | ||
117 | |||
118 | // next sample | ||
119 | time += period; | ||
120 | if ( wave_pos >= wave_size ) | ||
121 | wave_pos = 0; | ||
122 | } | ||
123 | while ( time < end_time ); | ||
124 | |||
125 | osc->wave_pos = wave_pos; | ||
126 | osc->last_amp = last_amp; | ||
127 | } | ||
128 | osc->delay = time - end_time; | ||
129 | } | ||
130 | |||
131 | this->last_time = nes_end_time; | ||
132 | } | ||
133 | |||