annotate Plugins/Input/console/Gb_Oscs.cpp @ 574:80a301d11c77 trunk

[svn] Add -I../.. here to satisfy OSS header dependency (O_o)
author nenolod
date Thu, 02 Feb 2006 15:42:11 -0800
parents c04dff121e1d
children f12d7e208b43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
493
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
1
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
2 // Gb_Snd_Emu 0.1.4. http://www.slack.net/~ant/
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
3
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
4 #include "Gb_Apu.h"
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
5
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
6 #include <string.h>
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
7
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
8 /* Copyright (C) 2003-2006 Shay Green. This module is free software; you
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
9 can redistribute it and/or modify it under the terms of the GNU Lesser
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
10 General Public License as published by the Free Software Foundation; either
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
11 version 2.1 of the License, or (at your option) any later version. This
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
12 module is distributed in the hope that it will be useful, but WITHOUT ANY
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
15 more details. You should have received a copy of the GNU Lesser General
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
16 Public License along with this module; if not, write to the Free Software
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
18
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
19 #include BLARGG_SOURCE_BEGIN
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
20
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
21 // Gb_Osc
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
22
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
23 void Gb_Osc::reset()
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
24 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
25 delay = 0;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
26 last_amp = 0;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
27 length = 0;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
28 output_select = 3;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
29 output = outputs [output_select];
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
30 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
31
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
32 void Gb_Osc::clock_length()
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
33 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
34 if ( (regs [4] & len_enabled_mask) && length )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
35 length--;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
36 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
37
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
38 // Gb_Env
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
39
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
40 void Gb_Env::clock_envelope()
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
41 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
42 if ( env_delay && !--env_delay )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
43 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
44 env_delay = regs [2] & 7;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
45 int v = volume - 1 + (regs [2] >> 2 & 2);
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
46 if ( (unsigned) v < 15 )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
47 volume = v;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
48 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
49 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
50
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
51 bool Gb_Env::write_register( int reg, int data )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
52 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
53 switch ( reg )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
54 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
55 case 1:
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
56 length = 64 - (regs [1] & 0x3f);
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
57 break;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
58
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
59 case 2:
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
60 if ( !(data >> 4) )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
61 enabled = false;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
62 break;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
63
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
64 case 4:
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
65 if ( data & trigger )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
66 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
67 env_delay = regs [2] & 7;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
68 volume = regs [2] >> 4;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
69 enabled = true;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
70 if ( length == 0 )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
71 length = 64;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
72 return true;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
73 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
74 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
75 return false;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
76 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
77
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
78 // Gb_Square
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
79
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
80 void Gb_Square::reset()
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
81 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
82 phase = 0;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
83 sweep_freq = 0;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
84 sweep_delay = 0;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
85 Gb_Env::reset();
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
86 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
87
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
88 void Gb_Square::clock_sweep()
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
89 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
90 int sweep_period = (regs [0] & period_mask) >> 4;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
91 if ( sweep_period && sweep_delay && !--sweep_delay )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
92 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
93 sweep_delay = sweep_period;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
94 regs [3] = sweep_freq & 0xFF;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
95 regs [4] = (regs [4] & ~0x07) | (sweep_freq >> 8 & 0x07);
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
96
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
97 int offset = sweep_freq >> (regs [0] & shift_mask);
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
98 if ( regs [0] & 0x08 )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
99 offset = -offset;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
100 sweep_freq += offset;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
101
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
102 if ( sweep_freq < 0 )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
103 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
104 sweep_freq = 0;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
105 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
106 else if ( sweep_freq >= 2048 )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
107 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
108 sweep_delay = 0; // don't modify channel frequency any further
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
109 sweep_freq = 2048; // silence sound immediately
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
110 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
111 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
112 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
113
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
114 void Gb_Square::run( gb_time_t time, gb_time_t end_time, int playing )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
115 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
116 if ( sweep_freq == 2048 )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
117 playing = false;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
118
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
119 static unsigned char const table [4] = { 1, 2, 4, 6 };
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
120 int const duty = table [regs [1] >> 6];
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
121 int amp = volume & playing;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
122 if ( phase >= duty )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
123 amp = -amp;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
124
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
125 int frequency = this->frequency();
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
126 if ( unsigned (frequency - 1) > 2040 ) // frequency < 1 || frequency > 2041
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
127 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
128 // really high frequency results in DC at half volume
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
129 amp = volume >> 1;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
130 playing = false;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
131 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
132
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
133 int delta = amp - last_amp;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
134 if ( delta )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
135 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
136 last_amp = amp;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
137 synth->offset( time, delta, output );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
138 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
139
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
140 time += delay;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
141 if ( !playing )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
142 time = end_time;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
143
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
144 if ( time < end_time )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
145 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
146 int const period = (2048 - frequency) * 4;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
147 Blip_Buffer* const output = this->output;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
148 int phase = this->phase;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
149 int delta = amp * 2;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
150 do
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
151 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
152 phase = (phase + 1) & 7;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
153 if ( phase == 0 || phase == duty )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
154 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
155 delta = -delta;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
156 synth->offset_inline( time, delta, output );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
157 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
158 time += period;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
159 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
160 while ( time < end_time );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
161
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
162 this->phase = phase;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
163 last_amp = delta >> 1;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
164 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
165 delay = time - end_time;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
166 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
167
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
168 // Gb_Noise
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
169
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
170 #include BLARGG_ENABLE_OPTIMIZER
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
171
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
172 void Gb_Noise::run( gb_time_t time, gb_time_t end_time, int playing )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
173 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
174 int amp = volume & playing;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
175 int tap = 13 - (regs [3] & 8);
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
176 if ( bits >> tap & 2 )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
177 amp = -amp;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
178
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
179 int delta = amp - last_amp;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
180 if ( delta )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
181 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
182 last_amp = amp;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
183 synth->offset( time, delta, output );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
184 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
185
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
186 time += delay;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
187 if ( !playing )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
188 time = end_time;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
189
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
190 if ( time < end_time )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
191 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
192 static unsigned char const table [8] = { 8, 16, 32, 48, 64, 80, 96, 112 };
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
193 int period = table [regs [3] & 7] << (regs [3] >> 4);
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
194
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
195 // keep parallel resampled time to eliminate time conversion in the loop
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
196 Blip_Buffer* const output = this->output;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
197 const blip_resampled_time_t resampled_period =
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
198 output->resampled_duration( period );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
199 blip_resampled_time_t resampled_time = output->resampled_time( time );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
200 unsigned bits = this->bits;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
201 int delta = amp * 2;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
202
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
203 do
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
204 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
205 unsigned changed = (bits >> tap) + 1;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
206 time += period;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
207 bits <<= 1;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
208 if ( changed & 2 )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
209 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
210 delta = -delta;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
211 bits |= 1;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
212 synth->offset_resampled( resampled_time, delta, output );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
213 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
214 resampled_time += resampled_period;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
215 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
216 while ( time < end_time );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
217
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
218 this->bits = bits;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
219 last_amp = delta >> 1;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
220 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
221 delay = time - end_time;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
222 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
223
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
224 // Gb_Wave
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
225
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
226 inline void Gb_Wave::write_register( int reg, int data )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
227 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
228 switch ( reg )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
229 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
230 case 0:
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
231 if ( !(data & 0x80) )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
232 enabled = false;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
233 break;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
234
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
235 case 1:
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
236 length = 256 - regs [1];
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
237 break;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
238
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
239 case 2:
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
240 volume = data >> 5 & 3;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
241 break;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
242
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
243 case 4:
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
244 if ( data & trigger & regs [0] )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
245 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
246 wave_pos = 0;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
247 enabled = true;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
248 if ( length == 0 )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
249 length = 256;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
250 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
251 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
252 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
253
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
254 void Gb_Wave::run( gb_time_t time, gb_time_t end_time, int playing )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
255 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
256 int volume_shift = (volume - 1) & 7; // volume = 0 causes shift = 7
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
257 int amp = (wave [wave_pos] >> volume_shift & playing) * 2;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
258
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
259 int frequency = this->frequency();
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
260 if ( unsigned (frequency - 1) > 2044 ) // frequency < 1 || frequency > 2045
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
261 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
262 amp = 30 >> volume_shift & playing;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
263 playing = false;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
264 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
265
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
266 int delta = amp - last_amp;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
267 if ( delta )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
268 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
269 last_amp = amp;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
270 synth->offset( time, delta, output );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
271 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
272
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
273 time += delay;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
274 if ( !playing )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
275 time = end_time;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
276
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
277 if ( time < end_time )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
278 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
279 Blip_Buffer* const output = this->output;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
280 int const period = (2048 - frequency) * 2;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
281 int wave_pos = (this->wave_pos + 1) & (wave_size - 1);
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
282
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
283 do
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
284 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
285 int amp = (wave [wave_pos] >> volume_shift) * 2;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
286 wave_pos = (wave_pos + 1) & (wave_size - 1);
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
287 int delta = amp - last_amp;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
288 if ( delta )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
289 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
290 last_amp = amp;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
291 synth->offset_inline( time, delta, output );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
292 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
293 time += period;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
294 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
295 while ( time < end_time );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
296
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
297 this->wave_pos = (wave_pos - 1) & (wave_size - 1);
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
298 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
299 delay = time - end_time;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
300 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
301
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
302 // Gb_Apu::write_osc
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
303
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
304 void Gb_Apu::write_osc( int index, int reg, int data )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
305 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
306 reg -= index * 5;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
307 Gb_Square* sq = &square2;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
308 switch ( index )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
309 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
310 case 0:
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
311 sq = &square1;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
312 case 1:
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
313 if ( sq->write_register( reg, data ) && index == 0 )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
314 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
315 square1.sweep_freq = square1.frequency();
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
316 if ( (regs [0] & sq->period_mask) && (regs [0] & sq->shift_mask) )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
317 {
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
318 square1.sweep_delay = 1; // cause sweep to recalculate now
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
319 square1.clock_sweep();
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
320 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
321 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
322 break;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
323
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
324 case 2:
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
325 wave.write_register( reg, data );
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
326 break;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
327
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
328 case 3:
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
329 if ( noise.write_register( reg, data ) )
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
330 noise.bits = 0x7FFF;
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
331 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
332 }
c04dff121e1d [svn] hostile merge, phase 2: reimport based on new plugin code
nenolod
parents:
diff changeset
333