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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/*
* Schism Tracker - a cross-platform Impulse Tracker clone
* copyright (c) 2003-2005 Storlek <storlek@rigelseven.com>
* copyright (c) 2005-2008 Mrs. Brisby <mrs.brisby@nimh.org>
* copyright (c) 2009 Storlek & Mrs. Brisby
* copyright (c) 2010-2012 Storlek
* URL: http://schismtracker.org/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "fmopl.h"
#include "snd_fm.h"
#define MAX_VOICES 256 /* Must not be less than the setting in sndfile.h */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define OPLNew(x,r) ym3812_init(x, r)
#define OPLResetChip ym3812_reset_chip
#define OPLWrite ym3812_write
#define OPLUpdateOne ym3812_update_one
#define OPLClose ym3812_shutdown
/* Mostly pulled from my posterior. Original value was 2000, but Manwe says that's too quiet.
It'd help if this was at all connected to the song's mixing volume...
*/
#define OPL_VOLUME 5000
/*
The documentation in this file regarding the output ports,
including the comment "Don't ask me why", are attributed
to Jeffrey S. Lee's article:
Programming the AdLib/Sound Blaster
FM Music Chips
Version 2.0 (24 Feb 1992)
*/
static const int oplbase = 0x388;
// OPL info
static struct OPL* opl = NULL;
static UINT32 oplretval = 0,
oplregno = 0;
static UINT32 fm_active = 0;
extern int fnumToMilliHertz(unsigned int fnum, unsigned int block,
unsigned int conversionFactor);
extern void milliHertzToFnum(unsigned int milliHertz,
unsigned int *fnum, unsigned int *block, unsigned int conversionFactor);
static void Fmdrv_Outportb(unsigned port, unsigned value)
{
if (opl == NULL ||
((int) port) < oplbase ||
((int) port) >= oplbase + 4)
return;
unsigned ind = port - oplbase;
OPLWrite(opl, ind, value);
if (ind & 1) {
if (oplregno == 4) {
if (value == 0x80)
oplretval = 0x02;
else if (value == 0x21)
oplretval = 0xC0;
}
}
else
oplregno = value;
}
static unsigned char Fmdrv_Inportb(unsigned port)
{
return (((int) port) >= oplbase &&
((int) port) < oplbase + 4) ? oplretval : 0;
}
void Fmdrv_Init(int mixfreq)
{
if (opl != NULL) {
OPLClose(opl);
opl = NULL;
}
//Clock for frequency 49716Hz. Mixfreq is used for output mix frequency.
opl = OPLNew(1789776 * 2, mixfreq);
OPLResetChip(opl);
OPL_Detect();
}
void Fmdrv_MixTo(int *target, int count)
{
static short *buf = NULL;
static int buf_size = 0;
if (!fm_active)
return;
if (buf_size != count * 2) {
int before = buf_size;
buf_size = sizeof(short) * count;
if (before) {
buf = (short *) realloc(buf, buf_size);
}
else {
buf = (short *) malloc(buf_size);
}
}
memset(buf, 0, count * 2);
OPLUpdateOne(opl, buf, count);
/*
static int counter = 0;
for(int a = 0; a < count; ++a)
buf[a] = ((counter++) & 0x100) ? -10000 : 10000;
*/
for (int a = 0; a < count; ++a) {
target[a * 2 + 0] += buf[a] * OPL_VOLUME;
target[a * 2 + 1] += buf[a] * OPL_VOLUME;
}
}
/***************************************/
static const char PortBases[9] = {0, 1, 2, 8, 9, 10, 16, 17, 18};
static signed char Pans[MAX_VOICES];
static const unsigned char *Dtab[MAX_VOICES] = {NULL};
static int SetBase(int c)
{
return c % 9;
}
static void OPL_Byte(unsigned char idx, unsigned char data)
{
//register int a;
Fmdrv_Outportb(oplbase, idx); // for(a = 0; a < 6; a++) Fmdrv_Inportb(oplbase);
Fmdrv_Outportb(oplbase + 1, data); // for(a = 0; a < 35; a++) Fmdrv_Inportb(oplbase);
}
void OPL_NoteOff(int c)
{
c = SetBase(c);
if (c<9) {
/* KEYON_BLOCK+c seems to not work alone?? */
OPL_Byte(KEYON_BLOCK + c, 0);
//OPL_Byte(KSL_LEVEL + Ope, 0xFF);
//OPL_Byte(KSL_LEVEL + 3 + Ope, 0xFF);
}
}
/* OPL_NoteOn changes the frequency on specified
channel and guarantees the key is on. (Doesn't
retrig, just turns the note on and sets freq.)
If keyoff is nonzero, doesn't even set the note on.
Could be used for pitch bending also. */
void OPL_HertzTouch(int c, int milliHertz, int keyoff)
{
c = SetBase(c);
if (c >= 9)
return;
fm_active = 1;
/*
Bytes A0-B8 - Octave / F-Number / Key-On
7 6 5 4 3 2 1 0
+-----+-----+-----+-----+-----+-----+-----+-----+
| F-Number (least significant byte) | (A0-A8)
+-----+-----+-----+-----+-----+-----+-----+-----+
| Unused | Key | Octave | F-Number | (B0-B8)
| | On | | most sig. |
+-----+-----+-----+-----+-----+-----+-----+-----+
*/
unsigned int outfnum;
unsigned int outblock;
const int conversion_factor = 49716; // Frequency of OPL.
milliHertzToFnum(milliHertz, &outfnum, &outblock, conversion_factor);
OPL_Byte(0xA0 + c, outfnum & 255); // F-Number low 8 bits
OPL_Byte(0xB0 + c, (keyoff ? 0 : 0x20) // Key on
| ((outfnum >> 8) & 3) // F-number high 2 bits
| (outblock << 2)
);
}
void OPL_Touch(int c, const unsigned char *D, unsigned vol)
{
if (!D) {
if (c < MAX_VOICES)
D = Dtab[c];
if (!D)
return;
}
//fprintf(stderr, "OPL_Touch(%d, %p:%02X.%02X.%02X.%02X-%02X.%02X.%02X.%02X-%02X.%02X.%02X, %d)\n",
// c, D,D[0],D[1],D[2],D[3],D[4],D[5],D[6],D[7],D[8],D[9],D[10], Vol);
Dtab[c] = D;
c = SetBase(c);
if (c >= 9)
return;
int Ope = PortBases[c];
/*
Bytes 40-55 - Level Key Scaling / Total Level
7 6 5 4 3 2 1 0
+-----+-----+-----+-----+-----+-----+-----+-----+
| Scaling | Total Level |
| Level | 24 12 6 3 1.5 .75 | <-- dB
+-----+-----+-----+-----+-----+-----+-----+-----+
bits 7-6 - causes output levels to decrease as the frequency
rises:
00 - no change
10 - 1.5 dB/8ve
01 - 3 dB/8ve
11 - 6 dB/8ve
bits 5-0 - controls the total output level of the operator.
all bits CLEAR is loudest; all bits SET is the
softest. Don't ask me why.
*/
OPL_Byte(KSL_LEVEL + Ope, (D[2] & KSL_MASK) |
// (63 + (d[2] & 63) * vol / 63 - vol) - old formula
// (63 - ((63 - (d[2] & 63)) * vol ) / 63) - older formula
// (63 - ((63 - (d[2] & 63)) * vol + 32) / 64) - revised formula, like ST3
(((int)(D[2] & 63) - 63) * vol + 63 * 64 - 32) / 64 // - optimized revised formula
);
OPL_Byte(KSL_LEVEL + 3 + Ope, (D[3] & KSL_MASK) |
(((int)(D[3] & 63) - 63) * vol + 63 * 64 - 32) / 64
);
/* 2008-09-27 Bisqwit:
* Did tests in ST3: The value poked
* to 0x43, minus from 63, is:
*
* OplVol 63 47 31
* SmpVol
* 64 63 47 31
* 32 32 24 15
* 16 16 12 8
*
* This seems to clearly indicate that the value
* poked is calculated with 63 - round(oplvol*smpvol/64.0).
*
* Also, from the documentation we can deduce that
* the maximum volume to be set is 47.25 dB and that
* each increase by 1 corresponds to 0.75 dB.
*
* Since we know that 6 dB is equivalent to a doubling
* of the volume, we can deduce that an increase or
* decrease by 8 will double / halve the volume.
*
*/
}
void OPL_Pan(int c, signed char val)
{
Pans[c] = val;
/* Doesn't happen immediately! */
}
void OPL_Patch(int c, const unsigned char *D)
{
//fprintf(stderr, "OPL_Patch(%d, %p:%02X.%02X.%02X.%02X-%02X.%02X.%02X.%02X-%02X.%02X.%02X)\n",
// c, D,D[0],D[1],D[2],D[3],D[4],D[5],D[6],D[7],D[8],D[9],D[10]);
Dtab[c] = D;
c = SetBase(c);
if(c >= 9)return;
int Ope = PortBases[c];
OPL_Byte(AM_VIB+ Ope, D[0]);
OPL_Byte(ATTACK_DECAY+ Ope, D[4]);
OPL_Byte(SUSTAIN_RELEASE+ Ope, D[6]);
OPL_Byte(WAVE_SELECT+ Ope, D[8]&3);// 6 high bits used elsewhere
OPL_Byte(AM_VIB+ 3+Ope, D[1]);
OPL_Byte(ATTACK_DECAY+ 3+Ope, D[5]);
OPL_Byte(SUSTAIN_RELEASE+3+Ope, D[7]);
OPL_Byte(WAVE_SELECT+ 3+Ope, D[9]&3);// 6 high bits used elsewhere
/* feedback, additive synthesis and Panning... */
OPL_Byte(FEEDBACK_CONNECTION+c,
(D[10] & ~STEREO_BITS)
| (Pans[c]<-32 ? VOICE_TO_LEFT
: Pans[c]>32 ? VOICE_TO_RIGHT
: (VOICE_TO_LEFT | VOICE_TO_RIGHT)
));
}
void OPL_Reset(void)
{
//fprintf(stderr, "OPL_Reset\n");
int a;
for(a = 0; a < 244; a++)
OPL_Byte(a, 0);
for(a = 0; a < MAX_VOICES; ++a)
Dtab[a] = NULL;
OPL_Byte(TEST_REGISTER, ENABLE_WAVE_SELECT);
fm_active = 0;
}
int OPL_Detect(void)
{
SetBase(0);
/* Reset timers 1 and 2 */
OPL_Byte(TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
/* Reset the IRQ of the FM chip */
OPL_Byte(TIMER_CONTROL_REGISTER, IRQ_RESET);
unsigned char ST1 = Fmdrv_Inportb(oplbase); /* Status register */
OPL_Byte(TIMER1_REGISTER, 255);
OPL_Byte(TIMER_CONTROL_REGISTER, TIMER2_MASK | TIMER1_START);
/*_asm xor cx,cx;P1:_asm loop P1*/
unsigned char ST2 = Fmdrv_Inportb(oplbase);
OPL_Byte(TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
OPL_Byte(TIMER_CONTROL_REGISTER, IRQ_RESET);
int OPLMode = (ST2 & 0xE0) == 0xC0 && !(ST1 & 0xE0);
if (!OPLMode)
return -1;
return 0;
}
void OPL_Close(void)
{
OPL_Reset();
}
|