0001-Move-definitions-that-don-t-need-to-be-exposed-from-opl-h-to-opl-c.patch 12 KB

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