dosbox_opl.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C) 2002-2013 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. * Originally based on ADLIBEMU.C, an AdLib/OPL2 emulation library by Ken Silverman
  21. * Copyright (C) 1998-2001 Ken Silverman
  22. * Ken Silverman's official web site: "http://www.advsys.net/ken"
  23. */
  24. #define fltype double
  25. /*
  26. define Bits, Bitu, Bit32s, Bit32u, Bit16s, Bit16u, Bit8s, Bit8u here
  27. */
  28. #if defined(_MSC_VER) && _MSC_VER <= 1600
  29. #include <windows.h>
  30. #define uintptr_t ULONG_PTR
  31. #define intptr_t LONG_PTR
  32. #define uint32_t DWORD
  33. #define int32_t INT
  34. #define uint16_t WORD
  35. #define int16_t SHORT
  36. #define uint8_t BYTE
  37. #define int8_t CHAR
  38. #else
  39. #include <stdint.h>
  40. #endif
  41. typedef uintptr_t Bitu;
  42. typedef intptr_t Bits;
  43. typedef uint32_t Bit32u;
  44. typedef int32_t Bit32s;
  45. typedef uint16_t Bit16u;
  46. typedef int16_t Bit16s;
  47. typedef uint8_t Bit8u;
  48. typedef int8_t Bit8s;
  49. /*
  50. define attribution that inlines/forces inlining of a function (optional)
  51. */
  52. #define OPL_INLINE inline
  53. #undef NUM_CHANNELS
  54. #if defined(OPLTYPE_IS_OPL3)
  55. #define NUM_CHANNELS 18
  56. #else
  57. #define NUM_CHANNELS 9
  58. #endif
  59. #define MAXOPERATORS (NUM_CHANNELS*2)
  60. #define FL05 ((fltype)0.5)
  61. #define FL2 ((fltype)2.0)
  62. #define PI ((fltype)3.1415926535897932384626433832795)
  63. #define FIXEDPT 0x10000 // fixed-point calculations using 16+16
  64. #define FIXEDPT_LFO 0x1000000 // fixed-point calculations using 8+24
  65. #define WAVEPREC 1024 // waveform precision (10 bits)
  66. #define INTFREQU ((fltype)(14318180.0 / 288.0)) // clocking of the chip
  67. #define OF_TYPE_ATT 0
  68. #define OF_TYPE_DEC 1
  69. #define OF_TYPE_REL 2
  70. #define OF_TYPE_SUS 3
  71. #define OF_TYPE_SUS_NOKEEP 4
  72. #define OF_TYPE_OFF 5
  73. #define ARC_CONTROL 0x00
  74. #define ARC_TVS_KSR_MUL 0x20
  75. #define ARC_KSL_OUTLEV 0x40
  76. #define ARC_ATTR_DECR 0x60
  77. #define ARC_SUSL_RELR 0x80
  78. #define ARC_FREQ_NUM 0xa0
  79. #define ARC_KON_BNUM 0xb0
  80. #define ARC_PERC_MODE 0xbd
  81. #define ARC_FEEDBACK 0xc0
  82. #define ARC_WAVE_SEL 0xe0
  83. #define ARC_SECONDSET 0x100 // second operator set for OPL3
  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. #define BLOCKBUF_SIZE 512
  88. // vibrato constants
  89. #define VIBTAB_SIZE 8
  90. #define VIBFAC 70/50000 // no braces, integer mul/div
  91. // tremolo constants and table
  92. #define TREMTAB_SIZE 53
  93. #define TREM_FREQ ((fltype)(3.7)) // tremolo at 3.7hz
  94. typedef struct opl_chip_struct opl_chip;
  95. /* operator struct definition
  96. For OPL2 all 9 channels consist of two operators each, carrier and modulator.
  97. Channel x has operators x as modulator and operators (9+x) as carrier.
  98. For OPL3 all 18 channels consist either of two operators (2op mode) or four
  99. operators (4op mode) which is determined through register4 of the second
  100. adlib register set.
  101. Only the channels 0,1,2 (first set) and 9,10,11 (second set) can act as
  102. 4op channels. The two additional operators for a channel y come from the
  103. 2op channel y+3 so the operatorss y, (9+y), y+3, (9+y)+3 make up a 4op
  104. channel.
  105. */
  106. typedef struct operator_struct {
  107. opl_chip* chip; // the opl chip
  108. Bit32s cval, lastcval; // current output/last output (used for feedback)
  109. Bit32u tcount, wfpos, tinc; // time (position in waveform) and time increment
  110. fltype amp, step_amp; // and amplification (envelope)
  111. fltype vol; // volume
  112. fltype sustain_level; // sustain level
  113. Bit32s mfbi; // feedback amount
  114. fltype a0, a1, a2, a3; // attack rate function coefficients
  115. fltype decaymul, releasemul; // decay/release rate functions
  116. Bit32u op_state; // current state of operator (attack/decay/sustain/release/off)
  117. Bit32u toff;
  118. Bit32s freq_high; // highest three bits of the frequency, used for vibrato calculations
  119. Bit16s* cur_wform; // start of selected waveform
  120. Bit32u cur_wmask; // mask for selected waveform
  121. Bit32u act_state; // activity state (regular, percussion)
  122. bool sus_keep; // keep sustain level when decay finished
  123. bool vibrato,tremolo; // vibrato/tremolo enable bits
  124. // variables used to provide non-continuous envelopes
  125. Bit32u generator_pos; // for non-standard sample rates we need to determine how many samples have passed
  126. Bits cur_env_step; // current (standardized) sample position
  127. Bits env_step_a,env_step_d,env_step_r; // number of std samples of one step (for attack/decay/release mode)
  128. Bit8u step_skip_pos_a; // position of 8-cyclic step skipping (always 2^x to check against mask)
  129. Bits env_step_skip_a; // bitmask that determines if a step is skipped (respective bit is zero then)
  130. #if defined(OPLTYPE_IS_OPL3)
  131. bool is_4op,is_4op_attached; // base of a 4op channel/part of a 4op channel
  132. Bit32s left_pan,right_pan; // opl3 stereo panning amount
  133. #endif
  134. } op_type;
  135. // enable an operator
  136. void enable_operator(Bitu regbase, op_type* op_pt);
  137. // functions to change parameters of an operator
  138. void change_frequency(Bitu chanbase, Bitu regbase, op_type* op_pt);
  139. void change_attackrate(Bitu regbase, op_type* op_pt);
  140. void change_decayrate(Bitu regbase, op_type* op_pt);
  141. void change_releaserate(Bitu regbase, op_type* op_pt);
  142. void change_sustainlevel(Bitu regbase, op_type* op_pt);
  143. void change_waveform(Bitu regbase, op_type* op_pt);
  144. void change_keepsustain(Bitu regbase, op_type* op_pt);
  145. void change_vibrato(Bitu regbase, op_type* op_pt);
  146. void change_feedback(Bitu chanbase, op_type* op_pt);
  147. // general functions
  148. opl_chip* adlib_init(Bit32u samplerate);
  149. void adlib_release(opl_chip* opl);
  150. void adlib_write(opl_chip* opl, Bitu idx, Bit8u val);
  151. void adlib_getsample(opl_chip* opl, Bit16s* sndptr, Bits numsamples);
  152. Bitu adlib_reg_read(opl_chip* opl, Bitu port);
  153. void adlib_write_index(opl_chip* opl, Bitu port, Bit8u val);