1From 962ee8fc46ca51691bde1c8c1022dacbe8a037ed Mon Sep 17 00:00:00 2001
2From: Carl Reinke <carlreinke@users.noreply.github.com>
3Date: Sun, 14 Jun 2020 14:11:00 -0600
4Subject: [PATCH] Move definitions that don't need to be exposed from opl.h to
5 opl.c
6
7[Retrieved from:
8https://github.com/opentyrian/opentyrian/commit/962ee8fc46ca51691bde1c8c1022dacbe8a037ed]
9Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
10---
11 src/opl.c | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++-
12 src/opl.h | 157 ++----------------------------------------------------
13 2 files changed, 154 insertions(+), 156 deletions(-)
14
15diff --git a/src/opl.c b/src/opl.c
16index a4071c5..f15474c 100644
17--- a/src/opl.c
18+++ b/src/opl.c
19@@ -23,12 +23,161 @@
20  * Copyright (C) 1998-2001 Ken Silverman
21  * Ken Silverman's official web site: "http://www.advsys.net/ken"
22  */
23-
24+#include "opl.h"
25
26 #include <math.h>
27+#include <stdbool.h>
28 #include <stdlib.h> // rand()
29 #include <string.h> // memset()
30-#include "opl.h"
31+
32+#define fltype double
33+
34+ /*
35+ define attribution that inlines/forces inlining of a function (optional)
36+ */
37+#define OPL_INLINE inline
38+
39+
40+#undef NUM_CHANNELS
41+#if defined(OPLTYPE_IS_OPL3)
42+#define NUM_CHANNELS	18
43+#else
44+#define NUM_CHANNELS	9
45+#endif
46+
47+#define MAXOPERATORS	(NUM_CHANNELS*2)
48+
49+
50+#define FL05	((fltype)0.5)
51+#define FL2		((fltype)2.0)
52+#define PI		((fltype)3.1415926535897932384626433832795)
53+
54+
55+#define FIXEDPT			0x10000		// fixed-point calculations using 16+16
56+#define FIXEDPT_LFO		0x1000000	// fixed-point calculations using 8+24
57+
58+#define WAVEPREC		1024		// waveform precision (10 bits)
59+
60+#define INTFREQU		((fltype)(14318180.0 / 288.0))		// clocking of the chip
61+
62+
63+#define OF_TYPE_ATT			0
64+#define OF_TYPE_DEC			1
65+#define OF_TYPE_REL			2
66+#define OF_TYPE_SUS			3
67+#define OF_TYPE_SUS_NOKEEP	4
68+#define OF_TYPE_OFF			5
69+
70+#define ARC_CONTROL			0x00
71+#define ARC_TVS_KSR_MUL		0x20
72+#define ARC_KSL_OUTLEV		0x40
73+#define ARC_ATTR_DECR		0x60
74+#define ARC_SUSL_RELR		0x80
75+#define ARC_FREQ_NUM		0xa0
76+#define ARC_KON_BNUM		0xb0
77+#define ARC_PERC_MODE		0xbd
78+#define ARC_FEEDBACK		0xc0
79+#define ARC_WAVE_SEL		0xe0
80+
81+#define ARC_SECONDSET		0x100	// second operator set for OPL3
82+
83+
84+#define OP_ACT_OFF			0x00
85+#define OP_ACT_NORMAL		0x01	// regular channel activated (bitmasked)
86+#define OP_ACT_PERC			0x02	// percussion channel activated (bitmasked)
87+
88+#define BLOCKBUF_SIZE		512
89+
90+
91+ // vibrato constants
92+#define VIBTAB_SIZE			8
93+#define VIBFAC				70/50000		// no braces, integer mul/div
94+
95+ // tremolo constants and table
96+#define TREMTAB_SIZE		53
97+#define TREM_FREQ			((fltype)(3.7))			// tremolo at 3.7hz
98+
99+
100+ /* operator struct definition
101+ For OPL2 all 9 channels consist of two operators each, carrier and modulator.
102+ Channel x has operators x as modulator and operators (9+x) as carrier.
103+ For OPL3 all 18 channels consist either of two operators (2op mode) or four
104+ operators (4op mode) which is determined through register4 of the second
105+ adlib register set.
106+ Only the channels 0,1,2 (first set) and 9,10,11 (second set) can act as
107+ 4op channels. The two additional operators for a channel y come from the
108+ 2op channel y+3 so the operatorss y, (9+y), y+3, (9+y)+3 make up a 4op
109+ channel.
110+ */
111+typedef struct operator_struct {
112+	Bit32s cval, lastcval;			// current output/last output (used for feedback)
113+	Bit32u tcount, wfpos, tinc;		// time (position in waveform) and time increment
114+	fltype amp, step_amp;			// and amplification (envelope)
115+	fltype vol;						// volume
116+	fltype sustain_level;			// sustain level
117+	Bit32s mfbi;					// feedback amount
118+	fltype a0, a1, a2, a3;			// attack rate function coefficients
119+	fltype decaymul, releasemul;	// decay/release rate functions
120+	Bit32u op_state;				// current state of operator (attack/decay/sustain/release/off)
121+	Bit32u toff;
122+	Bit32s freq_high;				// highest three bits of the frequency, used for vibrato calculations
123+	Bit16s* cur_wform;				// start of selected waveform
124+	Bit32u cur_wmask;				// mask for selected waveform
125+	Bit32u act_state;				// activity state (regular, percussion)
126+	bool sus_keep;					// keep sustain level when decay finished
127+	bool vibrato,tremolo;			// vibrato/tremolo enable bits
128+
129+									// variables used to provide non-continuous envelopes
130+	Bit32u generator_pos;			// for non-standard sample rates we need to determine how many samples have passed
131+	Bits cur_env_step;				// current (standardized) sample position
132+	Bits env_step_a,env_step_d,env_step_r;	// number of std samples of one step (for attack/decay/release mode)
133+	Bit8u step_skip_pos_a;			// position of 8-cyclic step skipping (always 2^x to check against mask)
134+	Bits env_step_skip_a;			// bitmask that determines if a step is skipped (respective bit is zero then)
135+
136+#if defined(OPLTYPE_IS_OPL3)
137+	bool is_4op,is_4op_attached;	// base of a 4op channel/part of a 4op channel
138+	Bit32s left_pan,right_pan;		// opl3 stereo panning amount
139+#endif
140+} op_type;
141+
142+// per-chip variables
143+static op_type op[MAXOPERATORS];
144+
145+static Bits int_samplerate;
146+
147+static Bit8u status;
148+static Bit32u opl_index;
149+#if defined(OPLTYPE_IS_OPL3)
150+static Bit8u adlibreg[512];	// adlib register set (including second set)
151+static Bit8u wave_sel[44];		// waveform selection
152+#else
153+static Bit8u adlibreg[256];	// adlib register set
154+static Bit8u wave_sel[22];		// waveform selection
155+#endif
156+
157+
158+						// vibrato/tremolo increment/counter
159+static Bit32u vibtab_pos;
160+static Bit32u vibtab_add;
161+static Bit32u tremtab_pos;
162+static Bit32u tremtab_add;
163+
164+
165+// enable an operator
166+void enable_operator(Bitu regbase, op_type* op_pt, Bit32u act_type);
167+
168+// functions to change parameters of an operator
169+void change_frequency(Bitu chanbase, Bitu regbase, op_type* op_pt);
170+
171+void change_attackrate(Bitu regbase, op_type* op_pt);
172+void change_decayrate(Bitu regbase, op_type* op_pt);
173+void change_releaserate(Bitu regbase, op_type* op_pt);
174+void change_sustainlevel(Bitu regbase, op_type* op_pt);
175+void change_waveform(Bitu regbase, op_type* op_pt);
176+void change_keepsustain(Bitu regbase, op_type* op_pt);
177+void change_vibrato(Bitu regbase, op_type* op_pt);
178+void change_feedback(Bitu chanbase, op_type* op_pt);
179+
180
181 static Bit32u generator_add;	// should be a chip parameter
182
183diff --git a/src/opl.h b/src/opl.h
184index c8e643b..cbb56ad 100644
185--- a/src/opl.h
186+++ b/src/opl.h
187@@ -25,11 +25,8 @@
188  * Ken Silverman's official web site: "http://www.advsys.net/ken"
189  */
190
191-
192-#define fltype double
193-
194-#include <stdbool.h>
195 #include <stdint.h>
196+
197 typedef uintptr_t	Bitu;
198 typedef intptr_t	Bits;
199 typedef uint32_t	Bit32u;
200@@ -39,154 +36,6 @@ typedef int16_t		Bit16s;
201 typedef uint8_t		Bit8u;
202 typedef int8_t		Bit8s;
203
204-
205-/*
206-	define attribution that inlines/forces inlining of a function (optional)
207-*/
208-#define OPL_INLINE inline
209-
210-
211-#undef NUM_CHANNELS
212-#if defined(OPLTYPE_IS_OPL3)
213-#define NUM_CHANNELS	18
214-#else
215-#define NUM_CHANNELS	9
216-#endif
217-
218-#define MAXOPERATORS	(NUM_CHANNELS*2)
219-
220-
221-#define FL05	((fltype)0.5)
222-#define FL2		((fltype)2.0)
223-#define PI		((fltype)3.1415926535897932384626433832795)
224-
225-
226-#define FIXEDPT			0x10000		// fixed-point calculations using 16+16
227-#define FIXEDPT_LFO		0x1000000	// fixed-point calculations using 8+24
228-
229-#define WAVEPREC		1024		// waveform precision (10 bits)
230-
231-#define INTFREQU		((fltype)(14318180.0 / 288.0))		// clocking of the chip
232-
233-
234-#define OF_TYPE_ATT			0
235-#define OF_TYPE_DEC			1
236-#define OF_TYPE_REL			2
237-#define OF_TYPE_SUS			3
238-#define OF_TYPE_SUS_NOKEEP	4
239-#define OF_TYPE_OFF			5
240-
241-#define ARC_CONTROL			0x00
242-#define ARC_TVS_KSR_MUL		0x20
243-#define ARC_KSL_OUTLEV		0x40
244-#define ARC_ATTR_DECR		0x60
245-#define ARC_SUSL_RELR		0x80
246-#define ARC_FREQ_NUM		0xa0
247-#define ARC_KON_BNUM		0xb0
248-#define ARC_PERC_MODE		0xbd
249-#define ARC_FEEDBACK		0xc0
250-#define ARC_WAVE_SEL		0xe0
251-
252-#define ARC_SECONDSET		0x100	// second operator set for OPL3
253-
254-
255-#define OP_ACT_OFF			0x00
256-#define OP_ACT_NORMAL		0x01	// regular channel activated (bitmasked)
257-#define OP_ACT_PERC			0x02	// percussion channel activated (bitmasked)
258-
259-#define BLOCKBUF_SIZE		512
260-
261-
262-// vibrato constants
263-#define VIBTAB_SIZE			8
264-#define VIBFAC				70/50000		// no braces, integer mul/div
265-
266-// tremolo constants and table
267-#define TREMTAB_SIZE		53
268-#define TREM_FREQ			((fltype)(3.7))			// tremolo at 3.7hz
269-
270-
271-/* operator struct definition
272-     For OPL2 all 9 channels consist of two operators each, carrier and modulator.
273-     Channel x has operators x as modulator and operators (9+x) as carrier.
274-     For OPL3 all 18 channels consist either of two operators (2op mode) or four
275-     operators (4op mode) which is determined through register4 of the second
276-     adlib register set.
277-     Only the channels 0,1,2 (first set) and 9,10,11 (second set) can act as
278-     4op channels. The two additional operators for a channel y come from the
279-     2op channel y+3 so the operatorss y, (9+y), y+3, (9+y)+3 make up a 4op
280-     channel.
281-*/
282-typedef struct operator_struct {
283-	Bit32s cval, lastcval;			// current output/last output (used for feedback)
284-	Bit32u tcount, wfpos, tinc;		// time (position in waveform) and time increment
285-	fltype amp, step_amp;			// and amplification (envelope)
286-	fltype vol;						// volume
287-	fltype sustain_level;			// sustain level
288-	Bit32s mfbi;					// feedback amount
289-	fltype a0, a1, a2, a3;			// attack rate function coefficients
290-	fltype decaymul, releasemul;	// decay/release rate functions
291-	Bit32u op_state;				// current state of operator (attack/decay/sustain/release/off)
292-	Bit32u toff;
293-	Bit32s freq_high;				// highest three bits of the frequency, used for vibrato calculations
294-	Bit16s* cur_wform;				// start of selected waveform
295-	Bit32u cur_wmask;				// mask for selected waveform
296-	Bit32u act_state;				// activity state (regular, percussion)
297-	bool sus_keep;					// keep sustain level when decay finished
298-	bool vibrato,tremolo;			// vibrato/tremolo enable bits
299-
300-	// variables used to provide non-continuous envelopes
301-	Bit32u generator_pos;			// for non-standard sample rates we need to determine how many samples have passed
302-	Bits cur_env_step;				// current (standardized) sample position
303-	Bits env_step_a,env_step_d,env_step_r;	// number of std samples of one step (for attack/decay/release mode)
304-	Bit8u step_skip_pos_a;			// position of 8-cyclic step skipping (always 2^x to check against mask)
305-	Bits env_step_skip_a;			// bitmask that determines if a step is skipped (respective bit is zero then)
306-
307-#if defined(OPLTYPE_IS_OPL3)
308-	bool is_4op,is_4op_attached;	// base of a 4op channel/part of a 4op channel
309-	Bit32s left_pan,right_pan;		// opl3 stereo panning amount
310-#endif
311-} op_type;
312-
313-// per-chip variables
314-Bitu chip_num;
315-op_type op[MAXOPERATORS];
316-
317-Bits int_samplerate;
318-
319-Bit8u status;
320-Bit32u opl_index;
321-#if defined(OPLTYPE_IS_OPL3)
322-Bit8u adlibreg[512];	// adlib register set (including second set)
323-Bit8u wave_sel[44];		// waveform selection
324-#else
325-Bit8u adlibreg[256];	// adlib register set
326-Bit8u wave_sel[22];		// waveform selection
327-#endif
328-
329-
330-// vibrato/tremolo increment/counter
331-Bit32u vibtab_pos;
332-Bit32u vibtab_add;
333-Bit32u tremtab_pos;
334-Bit32u tremtab_add;
335-
336-
337-// enable an operator
338-void enable_operator(Bitu regbase, op_type* op_pt, Bit32u act_type);
339-
340-// functions to change parameters of an operator
341-void change_frequency(Bitu chanbase, Bitu regbase, op_type* op_pt);
342-
343-void change_attackrate(Bitu regbase, op_type* op_pt);
344-void change_decayrate(Bitu regbase, op_type* op_pt);
345-void change_releaserate(Bitu regbase, op_type* op_pt);
346-void change_sustainlevel(Bitu regbase, op_type* op_pt);
347-void change_waveform(Bitu regbase, op_type* op_pt);
348-void change_keepsustain(Bitu regbase, op_type* op_pt);
349-void change_vibrato(Bitu regbase, op_type* op_pt);
350-void change_feedback(Bitu chanbase, op_type* op_pt);
351-
352 // general functions
353 void adlib_init(Bit32u samplerate);
354 void adlib_write(Bitu idx, Bit8u val);
355@@ -195,8 +44,8 @@ void adlib_getsample(Bit16s* sndptr, Bits numsamples);
356 Bitu adlib_reg_read(Bitu port);
357 void adlib_write_index(Bitu port, Bit8u val);
358
359-#endif /* OPL_H */
360-
361 #define opl_init() adlib_init(OUTPUT_QUALITY * 11025)
362 #define opl_write(reg, val) adlib_write(reg, val)
363 #define opl_update(buf, num) adlib_getsample(buf, num)
364+
365+#endif /* OPL_H */
366