LMMS
Loading...
Searching...
No Matches
woodyopl.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2002-2015 The DOSBox Team
3 * OPL2/OPL3 emulation library
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20
21/*
22 * Originally based on ADLIBEMU.C, an AdLib/OPL2 emulation library by Ken Silverman
23 * Copyright (C) 1998-2001 Ken Silverman
24 * Ken Silverman's official web site: "http://www.advsys.net/ken"
25 */
26
27/*
28 * Modified for AdPlug
29 */
30
31#ifndef H_ADPLUG_WOODYOPL
32#define H_ADPLUG_WOODYOPL
33
34#define OPLTYPE_IS_OPL3
35
36#define fltype double
37
38/*
39 define Bits, Bitu, Bit32s, Bit32u, Bit16s, Bit16u, Bit8s, Bit8u here
40*/
41#include <stdint.h>
42typedef uintptr_t Bitu;
43typedef intptr_t Bits;
44typedef uint32_t Bit32u;
45typedef int32_t Bit32s;
46typedef uint16_t Bit16u;
47typedef int16_t Bit16s;
48typedef uint8_t Bit8u;
49typedef int8_t Bit8s;
50
51
52/*
53 define attribution that inlines/forces inlining of a function (optional)
54*/
55#define OPL_INLINE __inline
56
57
58#undef NUM_CHANNELS
59#if defined(OPLTYPE_IS_OPL3)
60#define NUM_CHANNELS 18
61#else
62#define NUM_CHANNELS 9
63#endif
64
65#define MAXOPERATORS (NUM_CHANNELS*2)
66
67
68#define FL05 ((fltype)0.5)
69#define FL2 ((fltype)2.0)
70#define PI ((fltype)3.1415926535897932384626433832795)
71
72
73#define FIXEDPT 0x10000 // fixed-point calculations using 16+16
74#define FIXEDPT_LFO 0x1000000 // fixed-point calculations using 8+24
75
76#define WAVEPREC 1024 // waveform precision (10 bits)
77
78#define INTFREQU ((fltype)(14318180.0 / 288.0)) // clocking of the chip
79
80
81#define OF_TYPE_ATT 0
82#define OF_TYPE_DEC 1
83#define OF_TYPE_REL 2
84#define OF_TYPE_SUS 3
85#define OF_TYPE_SUS_NOKEEP 4
86#define OF_TYPE_OFF 5
87
88#define ARC_CONTROL 0x00
89#define ARC_TVS_KSR_MUL 0x20
90#define ARC_KSL_OUTLEV 0x40
91#define ARC_ATTR_DECR 0x60
92#define ARC_SUSL_RELR 0x80
93#define ARC_FREQ_NUM 0xa0
94#define ARC_KON_BNUM 0xb0
95#define ARC_PERC_MODE 0xbd
96#define ARC_FEEDBACK 0xc0
97#define ARC_WAVE_SEL 0xe0
98
99#define ARC_SECONDSET 0x100 // second operator set for OPL3
100
101
102#define OP_ACT_OFF 0x00
103#define OP_ACT_NORMAL 0x01 // regular channel activated (bitmasked)
104#define OP_ACT_PERC 0x02 // percussion channel activated (bitmasked)
105
106#define BLOCKBUF_SIZE 512
107
108
109// vibrato constants
110#define VIBTAB_SIZE 8
111#define VIBFAC 70/50000 // no braces, integer mul/div
112
113// tremolo constants and table
114#define TREMTAB_SIZE 53
115#define TREM_FREQ ((fltype)(3.7)) // tremolo at 3.7hz
116
117
118/* operator struct definition
119 For OPL2 all 9 channels consist of two operators each, carrier and modulator.
120 Channel x has operators x as modulator and operators (9+x) as carrier.
121 For OPL3 all 18 channels consist either of two operators (2op mode) or four
122 operators (4op mode) which is determined through register4 of the second
123 adlib register set.
124 Only the channels 0,1,2 (first set) and 9,10,11 (second set) can act as
125 4op channels. The two additional operators for a channel y come from the
126 2op channel y+3 so the operatorss y, (9+y), y+3, (9+y)+3 make up a 4op
127 channel.
128*/
129typedef struct operator_struct {
130 Bit32s cval, lastcval; // current output/last output (used for feedback)
131 Bit32u tcount, wfpos, tinc; // time (position in waveform) and time increment
132 fltype amp, step_amp; // and amplification (envelope)
133 fltype vol; // volume
134 fltype sustain_level; // sustain level
135 Bit32s mfbi; // feedback amount
136 fltype a0, a1, a2, a3; // attack rate function coefficients
137 fltype decaymul, releasemul; // decay/release rate functions
138 Bit32u op_state; // current state of operator (attack/decay/sustain/release/off)
140 Bit32s freq_high; // highest three bits of the frequency, used for vibrato calculations
141 Bit16s* cur_wform; // start of selected waveform
142 Bit32u cur_wmask; // mask for selected waveform
143 Bit32u act_state; // activity state (regular, percussion)
144 bool sus_keep; // keep sustain level when decay finished
145 bool vibrato,tremolo; // vibrato/tremolo enable bits
146
147 // variables used to provide non-continuous envelopes
148 Bit32u generator_pos; // for non-standard sample rates we need to determine how many samples have passed
149 Bits cur_env_step; // current (standardized) sample position
150 Bits env_step_a,env_step_d,env_step_r; // number of std samples of one step (for attack/decay/release mode)
151 Bit8u step_skip_pos_a; // position of 8-cyclic step skipping (always 2^x to check against mask)
152 Bits env_step_skip_a; // bitmask that determines if a step is skipped (respective bit is zero then)
153
154#if defined(OPLTYPE_IS_OPL3)
155 bool is_4op,is_4op_attached; // base of a 4op channel/part of a 4op channel
156 Bit32s left_pan,right_pan; // opl3 stereo panning amount
157#endif
159
161public:
162
163 // per-chip variables
166
170
173 #if defined(OPLTYPE_IS_OPL3)
174 Bit8u adlibreg[512]; // adlib register set (including second set)
175 Bit8u wave_sel[44]; // waveform selection
176 #else
177 Bit8u adlibreg[256]; // adlib register set
178 Bit8u wave_sel[22]; // waveform selection
179 #endif
180
181
182 // vibrato/tremolo increment/counter
187
188
189 // enable an operator
190 void enable_operator(Bitu regbase, op_type* op_pt, Bit32u act_type);
191
192 // functions to change parameters of an operator
193 void change_frequency(Bitu chanbase, Bitu regbase, op_type* op_pt);
194
195 void change_attackrate(Bitu regbase, op_type* op_pt);
196 void change_decayrate(Bitu regbase, op_type* op_pt);
197 void change_releaserate(Bitu regbase, op_type* op_pt);
198 void change_sustainlevel(Bitu regbase, op_type* op_pt);
199 void change_waveform(Bitu regbase, op_type* op_pt);
200 void change_keepsustain(Bitu regbase, op_type* op_pt);
201 void change_vibrato(Bitu regbase, op_type* op_pt);
202 void change_feedback(Bitu chanbase, op_type* op_pt);
203
204 // general functions
205 void adlib_init(Bit32u samplerate, Bit32u numchannels, Bit32u bytespersample);
206 void adlib_write(Bitu idx, Bit8u val);
207 void adlib_getsample(Bit16s* sndptr, Bits numsamples);
208
210 void adlib_write_index(Bitu port, Bit8u val);
211};
212
213static Bit32u generator_add; // should be a chip parameter
214
215#endif
static long bytespersample
Definition adlibemu.c:87
Definition woodyopl.h:160
void enable_operator(Bitu regbase, op_type *op_pt, Bit32u act_type)
Definition woodyopl.cpp:455
void change_waveform(Bitu regbase, op_type *op_pt)
Definition woodyopl.cpp:395
op_type op[MAXOPERATORS]
Definition woodyopl.h:165
void adlib_init(Bit32u samplerate, Bit32u numchannels, Bit32u bytespersample)
Definition woodyopl.cpp:479
void adlib_getsample(Bit16s *sndptr, Bits numsamples)
Definition woodyopl.cpp:971
Bit8u wave_sel[44]
Definition woodyopl.h:175
Bit32u vibtab_add
Definition woodyopl.h:184
Bit32u opl_index
Definition woodyopl.h:172
void change_decayrate(Bitu regbase, op_type *op_pt)
Definition woodyopl.cpp:357
void change_frequency(Bitu chanbase, Bitu regbase, op_type *op_pt)
Definition woodyopl.cpp:427
Bitu chip_num
Definition woodyopl.h:164
Bit32u tremtab_pos
Definition woodyopl.h:185
Bits int_numsamplechannels
Definition woodyopl.h:168
Bit32u tremtab_add
Definition woodyopl.h:186
void change_sustainlevel(Bitu regbase, op_type *op_pt)
Definition woodyopl.cpp:385
Bits int_bytespersample
Definition woodyopl.h:169
void change_attackrate(Bitu regbase, op_type *op_pt)
Definition woodyopl.cpp:318
void change_keepsustain(Bitu regbase, op_type *op_pt)
Definition woodyopl.cpp:405
void change_releaserate(Bitu regbase, op_type *op_pt)
Definition woodyopl.cpp:371
Bitu adlib_reg_read(Bitu port)
Definition woodyopl.cpp:900
void adlib_write_index(Bitu port, Bit8u val)
Definition woodyopl.cpp:916
Bit32u vibtab_pos
Definition woodyopl.h:183
void change_feedback(Bitu chanbase, op_type *op_pt)
Definition woodyopl.cpp:421
Bit8u adlibreg[512]
Definition woodyopl.h:174
void adlib_write(Bitu idx, Bit8u val)
Definition woodyopl.cpp:604
Bit8u status
Definition woodyopl.h:171
Bits int_samplerate
Definition woodyopl.h:167
void change_vibrato(Bitu regbase, op_type *op_pt)
Definition woodyopl.cpp:415
int val
Definition jpeglib.h:956
unsigned short uint16_t
Definition mid.cpp:99
int int32_t
Definition mid.cpp:97
unsigned int uint32_t
Definition mid.cpp:100
short int16_t
Definition mid.cpp:96
unsigned char uint8_t
Definition mid.cpp:98
signed char int8_t
Definition mid.cpp:95
int8_t Bit8s
Definition nukedopl.h:47
uint16_t Bit16u
Definition nukedopl.h:44
int32_t Bit32s
Definition nukedopl.h:43
intptr_t Bits
Definition nukedopl.h:39
int16_t Bit16s
Definition nukedopl.h:45
uint32_t Bit32u
Definition nukedopl.h:42
uint8_t Bit8u
Definition nukedopl.h:46
uintptr_t Bitu
Definition nukedopl.h:38
Definition woodyopl.h:129
Bit32s lastcval
Definition woodyopl.h:130
Bit32s cval
Definition woodyopl.h:130
bool tremolo
Definition woodyopl.h:145
Bit32u generator_pos
Definition woodyopl.h:148
Bit32u toff
Definition woodyopl.h:139
fltype sustain_level
Definition woodyopl.h:134
Bit32s mfbi
Definition woodyopl.h:135
Bit32s left_pan
Definition woodyopl.h:156
fltype a0
Definition woodyopl.h:136
Bits cur_env_step
Definition woodyopl.h:149
fltype a1
Definition woodyopl.h:136
fltype amp
Definition woodyopl.h:132
bool is_4op_attached
Definition woodyopl.h:155
Bits env_step_r
Definition woodyopl.h:150
Bit32s freq_high
Definition woodyopl.h:140
Bit8u step_skip_pos_a
Definition woodyopl.h:151
Bit32u op_state
Definition woodyopl.h:138
Bits env_step_a
Definition woodyopl.h:150
bool vibrato
Definition woodyopl.h:145
Bit32u wfpos
Definition woodyopl.h:131
Bit32u cur_wmask
Definition woodyopl.h:142
Bit32u tinc
Definition woodyopl.h:131
Bit32s right_pan
Definition woodyopl.h:156
bool is_4op
Definition woodyopl.h:155
fltype a3
Definition woodyopl.h:136
bool sus_keep
Definition woodyopl.h:144
fltype releasemul
Definition woodyopl.h:137
Bits env_step_skip_a
Definition woodyopl.h:152
fltype decaymul
Definition woodyopl.h:137
fltype a2
Definition woodyopl.h:136
Bit32u tcount
Definition woodyopl.h:131
fltype vol
Definition woodyopl.h:133
Bits env_step_d
Definition woodyopl.h:150
Bit32u act_state
Definition woodyopl.h:143
fltype step_amp
Definition woodyopl.h:132
Bit16s * cur_wform
Definition woodyopl.h:141
struct operator_struct op_type
static Bit32u generator_add
Definition woodyopl.h:213
#define fltype
Definition woodyopl.h:36
#define MAXOPERATORS
Definition woodyopl.h:65