Streamtools: Support multichannel FM
[jpcrr.git] / streamtools / opl.h
blob2a5183dd8d0c832e0487deb2af34e7ae467121ee
1 /*
2 * Copyright (C) 2002-2009 The DOSBox Team
3 * Copyright (C) 2011 H. Ilari Liusvaara
4 * OPL2/OPL3 emulation library
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Originally based on ADLIBEMU.C, an AdLib/OPL2 emulation library by Ken Silverman
24 * Copyright (C) 1998-2001 Ken Silverman
25 * Ken Silverman's official web site: "http://www.advsys.net/ken"
29 * Modified 20th April 2011 by Ilari:
30 * - Replace static state with context.
31 * - Remove OPL2 support (OPL3 is superset anyway)
32 * - Add include guards.
35 #ifndef OPL__H__INCLUDED__
36 #define OPL__H__INCLUDED__
38 #define fltype double
41 define Bits, Bitu, Bit32s, Bit32u, Bit16s, Bit16u, Bit8s, Bit8u here
44 #include <stdint.h>
45 typedef uintptr_t Bitu;
46 typedef intptr_t Bits;
47 typedef uint32_t Bit32u;
48 typedef int32_t Bit32s;
49 typedef uint16_t Bit16u;
50 typedef int16_t Bit16s;
51 typedef uint8_t Bit8u;
52 typedef int8_t Bit8s;
54 #undef NUM_CHANNELS
55 #define NUM_CHANNELS 18
57 #define MAXOPERATORS (NUM_CHANNELS*2)
60 #define FL05 ((fltype)0.5)
61 #define FL2 ((fltype)2.0)
62 #define PI ((fltype)3.1415926535897932384626433832795)
65 #define FIXEDPT 0x10000 // fixed-point calculations using 16+16
66 #define FIXEDPT_LFO 0x1000000 // fixed-point calculations using 8+24
68 #define WAVEPREC 1024 // waveform precision (10 bits)
70 #define INTFREQU ((fltype)(14318180.0 / 288.0)) // clocking of the chip
73 #define OF_TYPE_ATT 0
74 #define OF_TYPE_DEC 1
75 #define OF_TYPE_REL 2
76 #define OF_TYPE_SUS 3
77 #define OF_TYPE_SUS_NOKEEP 4
78 #define OF_TYPE_OFF 5
80 #define ARC_CONTROL 0x00
81 #define ARC_TVS_KSR_MUL 0x20
82 #define ARC_KSL_OUTLEV 0x40
83 #define ARC_ATTR_DECR 0x60
84 #define ARC_SUSL_RELR 0x80
85 #define ARC_FREQ_NUM 0xa0
86 #define ARC_KON_BNUM 0xb0
87 #define ARC_PERC_MODE 0xbd
88 #define ARC_FEEDBACK 0xc0
89 #define ARC_WAVE_SEL 0xe0
91 #define ARC_SECONDSET 0x100 // second operator set for OPL3
94 #define OP_ACT_OFF 0x00
95 #define OP_ACT_NORMAL 0x01 // regular channel activated (bitmasked)
96 #define OP_ACT_PERC 0x02 // percussion channel activated (bitmasked)
98 #define BLOCKBUF_SIZE 512
101 // vibrato constants
102 #define VIBTAB_SIZE 8
103 #define VIBFAC 70/50000 // no braces, integer mul/div
105 // tremolo constants and table
106 #define TREMTAB_SIZE 53
107 #define TREM_FREQ ((fltype)(3.7)) // tremolo at 3.7hz
110 /* operator struct definition
111 For OPL2 all 9 channels consist of two operators each, carrier and modulator.
112 Channel x has operators x as modulator and operators (9+x) as carrier.
113 For OPL3 all 18 channels consist either of two operators (2op mode) or four
114 operators (4op mode) which is determined through register4 of the second
115 adlib register set.
116 Only the channels 0,1,2 (first set) and 9,10,11 (second set) can act as
117 4op channels. The two additional operators for a channel y come from the
118 2op channel y+3 so the operatorss y, (9+y), y+3, (9+y)+3 make up a 4op
119 channel.
121 typedef struct operator_struct {
122 Bit32s cval, lastcval; // current output/last output (used for feedback)
123 Bit32u tcount, wfpos, tinc; // time (position in waveform) and time increment
124 fltype amp, step_amp; // and amplification (envelope)
125 fltype vol; // volume
126 fltype sustain_level; // sustain level
127 Bit32s mfbi; // feedback amount
128 fltype a0, a1, a2, a3; // attack rate function coefficients
129 fltype decaymul, releasemul; // decay/release rate functions
130 Bit32u op_state; // current state of operator (attack/decay/sustain/release/off)
131 Bit32u toff;
132 Bit32s freq_high; // highest three bits of the frequency, used for vibrato calculations
133 Bit16s* cur_wform; // start of selected waveform
134 Bit32u cur_wmask; // mask for selected waveform
135 Bit32u act_state; // activity state (regular, percussion)
136 bool sus_keep; // keep sustain level when decay finished
137 bool vibrato,tremolo; // vibrato/tremolo enable bits
139 // variables used to provide non-continuous envelopes
140 Bit32u generator_pos; // for non-standard sample rates we need to determine how many samples have passed
141 Bits cur_env_step; // current (standardized) sample position
142 Bits env_step_a,env_step_d,env_step_r; // number of std samples of one step (for attack/decay/release mode)
143 Bit8u step_skip_pos_a; // position of 8-cyclic step skipping (always 2^x to check against mask)
144 Bits env_step_skip_a; // bitmask that determines if a step is skipped (respective bit is zero then)
146 bool is_4op,is_4op_attached; // base of a 4op channel/part of a 4op channel
147 Bit32s left_pan,right_pan; // opl3 stereo panning amount
148 } op_type;
150 /* OPL context defintion. */
151 typedef struct opl_struct {
152 // per-chip variables
153 Bitu chip_num;
154 op_type op[MAXOPERATORS];
156 Bits int_samplerate;
158 Bit8u status;
159 Bit32u opl_index;
160 Bit8u adlibreg[512]; // adlib register set (including second set)
161 Bit8u wave_sel[44]; // waveform selection
163 // vibrato/tremolo increment/counter
164 Bit32u vibtab_pos;
165 Bit32u vibtab_add;
166 Bit32u tremtab_pos;
167 Bit32u tremtab_add;
168 Bit32u generator_add; // should be a chip parameter
170 fltype recipsamp; // inverse of sampling rate
171 Bit16s wavtable[WAVEPREC*3]; // wave form table
173 // vibrato/tremolo tables
174 Bit32s vib_table[VIBTAB_SIZE];
175 Bit32s trem_table[TREMTAB_SIZE*2];
177 Bit32s vibval_const[BLOCKBUF_SIZE];
178 Bit32s tremval_const[BLOCKBUF_SIZE];
180 // vibrato value tables (used per-operator)
181 Bit32s vibval_var1[BLOCKBUF_SIZE];
182 Bit32s vibval_var2[BLOCKBUF_SIZE];
184 // calculated frequency multiplication values (depend on sampling rate)
185 float frqmul[16];
187 // key scale levels
188 Bit8u kslev[8][16];
190 // vibrato/trmolo value table pointers
191 Bit32s *vibval1, *vibval2, *vibval3, *vibval4;
192 Bit32s *tremval1, *tremval2, *tremval3, *tremval4;
194 } opl_context;
197 // enable an operator
198 void enable_operator(opl_context* ctx, Bitu regbase, op_type* op_pt);
200 // functions to change parameters of an operator
201 void change_frequency(opl_context* ctx, Bitu chanbase, Bitu regbase, op_type* op_pt);
203 void change_attackrate(opl_context* ctx, Bitu regbase, op_type* op_pt);
204 void change_decayrate(opl_context* ctx, Bitu regbase, op_type* op_pt);
205 void change_releaserate(opl_context* ctx, Bitu regbase, op_type* op_pt);
206 void change_sustainlevel(opl_context* ctx, Bitu regbase, op_type* op_pt);
207 void change_waveform(opl_context* ctx, Bitu regbase, op_type* op_pt);
208 void change_keepsustain(opl_context* ctx, Bitu regbase, op_type* op_pt);
209 void change_vibrato(opl_context* ctx, Bitu regbase, op_type* op_pt);
210 void change_feedback(opl_context* ctx, Bitu chanbase, op_type* op_pt);
212 // general functions
213 void adlib_init(opl_context* ctx, Bit32u samplerate);
214 void adlib_write(opl_context* ctx, Bitu idx, Bit8u val);
215 void adlib_getsample(opl_context* ctx, Bit16s* sndptr, Bits numsamples);
217 Bitu adlib_reg_read(opl_context* ctx, Bitu port);
218 void adlib_write_index(opl_context* ctx, Bitu port, Bit8u val);
220 #endif