1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
#include "opl_apu.h"
#include "blargg_source.h"
/* NOTE: Removed unused chips ~ gama */
blargg_err_t Opl_init( struct Opl_Apu* this, long clock, long rate, blip_time_t period, enum opl_type_t type )
{
Synth_init( &this->synth );
this->type_ = type;
this->clock_ = clock;
this->rate_ = rate;
this->period_ = period;
Opl_set_output( this, 0 );
Opl_volume( this, (int)FP_ONE_VOLUME );
switch (type)
{
case type_opll:
case type_msxmusic:
case type_smsfmunit:
OPLL_new ( &this->opll, clock, rate );
OPLL_reset_patch( &this->opll, OPLL_2413_TONE );
break;
case type_vrc7:
OPLL_new ( &this->opll, clock, rate );
OPLL_reset_patch( &this->opll, OPLL_VRC7_TONE );
break;
case type_msxaudio:
OPL_init( &this->opl, this->opl_memory, sizeof this->opl_memory );
OPL_setSampleRate( &this->opl, rate, clock );
OPL_setInternalVolume(&this->opl, 1 << 13);
break;
}
Opl_reset( this );
return 0;
}
void Opl_shutdown( struct Opl_Apu* this )
{
switch (this->type_)
{
case type_opll:
case type_msxmusic:
case type_smsfmunit:
case type_vrc7:
OPLL_delete( &this->opll );
break;
case type_msxaudio: break;
}
}
void Opl_reset( struct Opl_Apu* this )
{
this->addr = 0;
this->next_time = 0;
this->last_amp = 0;
switch (this->type_)
{
case type_opll:
case type_msxmusic:
case type_smsfmunit:
case type_vrc7:
OPLL_reset( &this->opll );
OPLL_setMask( &this->opll, 0 );
break;
case type_msxaudio:
OPL_reset( &this->opl );
break;
}
}
static void run_until( struct Opl_Apu* this, blip_time_t end_time );
void Opl_write_data( struct Opl_Apu* this, blip_time_t time, int data )
{
run_until( this, time );
switch (this->type_)
{
case type_opll:
case type_msxmusic:
case type_smsfmunit:
case type_vrc7:
OPLL_writeIO( &this->opll, 0, this->addr );
OPLL_writeIO( &this->opll, 1, data );
break;
case type_msxaudio:
OPL_writeReg( &this->opl, this->addr, data );
break;
}
}
int Opl_read( struct Opl_Apu* this, blip_time_t time, int port )
{
run_until( this, time );
switch (this->type_)
{
case type_opll:
case type_msxmusic:
case type_smsfmunit:
case type_vrc7:
return OPLL_read( &this->opll, port );
case type_msxaudio:
return OPL_readStatus( &this->opl );
}
return 0;
}
void Opl_end_frame( struct Opl_Apu* this, blip_time_t time )
{
run_until( this, time );
this->next_time -= time;
if ( this->output_ )
Blip_set_modified( this->output_ );
}
static void run_until( struct Opl_Apu* this, blip_time_t end_time )
{
if ( end_time > this->next_time )
{
blip_time_t time_delta = end_time - this->next_time;
blip_time_t time = this->next_time;
unsigned count = time_delta / this->period_ + 1;
switch (this->type_)
{
case type_opll:
case type_msxmusic:
case type_smsfmunit:
case type_vrc7:
{
OPLL* opll = &this->opll; // cache
struct Blip_Buffer* const output = this->output_;
while ( count > 0 )
{
unsigned todo = count;
if ( todo > 1024 ) todo = 1024;
short *buffer = OPLL_update_buffer(opll, todo);
if ( output && buffer )
{
int last_amp = this->last_amp;
unsigned i;
for ( i = 0; i < todo; i++ )
{
int amp = buffer [i];
int delta = amp - last_amp;
if ( delta )
{
last_amp = amp;
Synth_offset_inline( &this->synth, time, delta, output );
}
time += this->period_;
}
this->last_amp = last_amp;
}
count -= todo;
}
}
break;
case type_msxaudio:
{
struct Y8950* opl = &this->opl;
struct Blip_Buffer* const output = this->output_;
while ( count > 0 )
{
unsigned todo = count;
if ( todo > 1024 ) todo = 1024;
int *buffer = OPL_updateBuffer(opl, todo);
if ( output && buffer )
{
int last_amp = this->last_amp;
unsigned i;
for ( i = 0; i < todo; i++ )
{
int amp = buffer [i];
int delta = amp - last_amp;
if ( delta )
{
last_amp = amp;
Synth_offset_inline( &this->synth, time, delta, output );
}
time += this->period_;
}
this->last_amp = last_amp;
}
count -= todo;
}
}
break;
}
this->next_time = time;
}
}
|