| 1 |
#ifndef _EMU2413_H_ |
| 2 |
#define _EMU2413_H_ |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
|
| 6 |
#ifdef __cplusplus |
| 7 |
extern "C" { |
| 8 |
#endif |
| 9 |
|
| 10 |
#define OPLL_DEBUG 0 |
| 11 |
|
| 12 |
enum OPLL_TONE_ENUM { OPLL_2413_TONE = 0, OPLL_VRC7_TONE = 1, OPLL_281B_TONE = 2, OPLL_2423_TONE = 3 }; |
| 13 |
|
| 14 |
/* voice data */ |
| 15 |
typedef struct __OPLL_PATCH { |
| 16 |
uint32_t TL, FB, EG, ML, AR, DR, SL, RR, KR, KL, AM, PM, WS; |
| 17 |
} OPLL_PATCH; |
| 18 |
|
| 19 |
/* slot */ |
| 20 |
typedef struct __OPLL_SLOT { |
| 21 |
uint8_t number; |
| 22 |
|
| 23 |
/* type flags: |
| 24 |
* 000000SM |
| 25 |
* |+-- M: 0:modulator 1:carrier |
| 26 |
* +--- S: 0:normal 1:single slot mode (sd, tom, hh or cym) |
| 27 |
*/ |
| 28 |
uint8_t type; |
| 29 |
|
| 30 |
OPLL_PATCH *patch; /* voice parameter */ |
| 31 |
|
| 32 |
/* slot output */ |
| 33 |
int32_t output[2]; /* output value, latest and previous. */ |
| 34 |
|
| 35 |
/* phase generator (pg) */ |
| 36 |
uint16_t *wave_table; /* wave table */ |
| 37 |
uint32_t pg_phase; /* pg phase */ |
| 38 |
uint32_t pg_out; /* pg output, as index of wave table */ |
| 39 |
uint8_t pg_keep; /* if 1, pg_phase is preserved when key-on */ |
| 40 |
uint16_t blk_fnum; /* (block << 9) | f-number */ |
| 41 |
uint16_t fnum; /* f-number (9 bits) */ |
| 42 |
uint8_t blk; /* block (3 bits) */ |
| 43 |
|
| 44 |
/* envelope generator (eg) */ |
| 45 |
uint8_t eg_state; /* current state */ |
| 46 |
int32_t volume; /* current volume */ |
| 47 |
uint8_t key_flag; /* key-on flag 1:on 0:off */ |
| 48 |
uint8_t sus_flag; /* key-sus option 1:on 0:off */ |
| 49 |
uint16_t tll; /* total level + key scale level*/ |
| 50 |
uint8_t rks; /* key scale offset (rks) for eg speed */ |
| 51 |
uint8_t eg_rate_h; /* eg speed rate high 4bits */ |
| 52 |
uint8_t eg_rate_l; /* eg speed rate low 2bits */ |
| 53 |
uint32_t eg_shift; /* shift for eg global counter, controls envelope speed */ |
| 54 |
uint32_t eg_out; /* eg output */ |
| 55 |
|
| 56 |
uint32_t update_requests; /* flags to debounce update */ |
| 57 |
|
| 58 |
#if OPLL_DEBUG |
| 59 |
uint8_t last_eg_state; |
| 60 |
#endif |
| 61 |
} OPLL_SLOT; |
| 62 |
|
| 63 |
/* mask */ |
| 64 |
#define OPLL_MASK_CH(x) (1 << (x)) |
| 65 |
#define OPLL_MASK_HH (1 << (9)) |
| 66 |
#define OPLL_MASK_CYM (1 << (10)) |
| 67 |
#define OPLL_MASK_TOM (1 << (11)) |
| 68 |
#define OPLL_MASK_SD (1 << (12)) |
| 69 |
#define OPLL_MASK_BD (1 << (13)) |
| 70 |
#define OPLL_MASK_RHYTHM (OPLL_MASK_HH | OPLL_MASK_CYM | OPLL_MASK_TOM | OPLL_MASK_SD | OPLL_MASK_BD) |
| 71 |
|
| 72 |
/* rate conveter */ |
| 73 |
typedef struct __OPLL_RateConv { |
| 74 |
int ch; |
| 75 |
double timer; |
| 76 |
double f_ratio; |
| 77 |
int16_t *sinc_table; |
| 78 |
int16_t **buf; |
| 79 |
} OPLL_RateConv; |
| 80 |
|
| 81 |
OPLL_RateConv *OPLL_RateConv_new(double f_inp, double f_out, int ch); |
| 82 |
void OPLL_RateConv_reset(OPLL_RateConv *conv); |
| 83 |
void OPLL_RateConv_putData(OPLL_RateConv *conv, int ch, int16_t data); |
| 84 |
int16_t OPLL_RateConv_getData(OPLL_RateConv *conv, int ch); |
| 85 |
void OPLL_RateConv_delete(OPLL_RateConv *conv); |
| 86 |
|
| 87 |
typedef struct __OPLL { |
| 88 |
uint32_t clk; |
| 89 |
uint32_t rate; |
| 90 |
|
| 91 |
uint8_t chip_type; |
| 92 |
|
| 93 |
uint32_t adr; |
| 94 |
|
| 95 |
double inp_step; |
| 96 |
double out_step; |
| 97 |
double out_time; |
| 98 |
|
| 99 |
uint8_t reg[64]; |
| 100 |
uint8_t test_flag; |
| 101 |
uint32_t slot_key_status; |
| 102 |
uint8_t rhythm_mode; |
| 103 |
|
| 104 |
uint32_t eg_counter; |
| 105 |
|
| 106 |
uint32_t pm_phase; |
| 107 |
int32_t am_phase; |
| 108 |
|
| 109 |
uint8_t lfo_am; |
| 110 |
|
| 111 |
uint32_t noise; |
| 112 |
uint8_t short_noise; |
| 113 |
|
| 114 |
int32_t patch_number[9]; |
| 115 |
OPLL_SLOT slot[18]; |
| 116 |
OPLL_PATCH patch[19 * 2]; |
| 117 |
|
| 118 |
uint8_t pan[16]; |
| 119 |
float pan_fine[16][2]; |
| 120 |
|
| 121 |
uint32_t mask; |
| 122 |
|
| 123 |
/* channel output */ |
| 124 |
/* 0..8:tone 9:bd 10:hh 11:sd 12:tom 13:cym */ |
| 125 |
int16_t ch_out[14]; |
| 126 |
|
| 127 |
int16_t mix_out[2]; |
| 128 |
|
| 129 |
OPLL_RateConv *conv; |
| 130 |
} OPLL; |
| 131 |
|
| 132 |
OPLL *OPLL_new(uint32_t clk, uint32_t rate); |
| 133 |
void OPLL_delete(OPLL *); |
| 134 |
|
| 135 |
void OPLL_reset(OPLL *); |
| 136 |
void OPLL_resetPatch(OPLL *, uint8_t); |
| 137 |
|
| 138 |
/** |
| 139 |
* Set output wave sampling rate. |
| 140 |
* @param rate sampling rate. If clock / 72 (typically 49716 or 49715 at 3.58MHz) is set, the internal rate converter is |
| 141 |
* disabled. |
| 142 |
*/ |
| 143 |
void OPLL_setRate(OPLL *opll, uint32_t rate); |
| 144 |
|
| 145 |
/** |
| 146 |
* Set internal calcuration quality. Currently no effects, just for compatibility. |
| 147 |
* >= v1.0.0 always synthesizes internal output at clock/72 Hz. |
| 148 |
*/ |
| 149 |
void OPLL_setQuality(OPLL *opll, uint8_t q); |
| 150 |
|
| 151 |
/** |
| 152 |
* Set pan pot (extra function - not YM2413 chip feature) |
| 153 |
* @param ch 0..8:tone 9:bd 10:hh 11:sd 12:tom 13:cym 14,15:reserved |
| 154 |
* @param pan 0:mute 1:right 2:left 3:center |
| 155 |
* ``` |
| 156 |
* pan: 76543210 |
| 157 |
* |+- bit 1: enable Left output |
| 158 |
* +-- bit 0: enable Right output |
| 159 |
* ``` |
| 160 |
*/ |
| 161 |
void OPLL_setPan(OPLL *opll, uint32_t ch, uint8_t pan); |
| 162 |
|
| 163 |
/** |
| 164 |
* Set fine-grained panning |
| 165 |
* @param ch 0..8:tone 9:bd 10:hh 11:sd 12:tom 13:cym 14,15:reserved |
| 166 |
* @param pan output strength of left/right channel. |
| 167 |
* pan[0]: left, pan[1]: right. pan[0]=pan[1]=1.0f for center. |
| 168 |
*/ |
| 169 |
void OPLL_setPanFine(OPLL *opll, uint32_t ch, float pan[2]); |
| 170 |
|
| 171 |
/** |
| 172 |
* Set chip type. If vrc7 is selected, r#14 is ignored. |
| 173 |
* This method not change the current ROM patch set. |
| 174 |
* To change ROM patch set, use OPLL_resetPatch. |
| 175 |
* @param type 0:YM2413 1:VRC7 |
| 176 |
*/ |
| 177 |
void OPLL_setChipType(OPLL *opll, uint8_t type); |
| 178 |
|
| 179 |
void OPLL_writeIO(OPLL *opll, uint32_t reg, uint8_t val); |
| 180 |
void OPLL_writeReg(OPLL *opll, uint32_t reg, uint8_t val); |
| 181 |
|
| 182 |
/** |
| 183 |
* Calculate one sample |
| 184 |
*/ |
| 185 |
int16_t OPLL_calc(OPLL *opll); |
| 186 |
|
| 187 |
/** |
| 188 |
* Calulate stereo sample |
| 189 |
*/ |
| 190 |
void OPLL_calcStereo(OPLL *opll, int32_t out[2]); |
| 191 |
|
| 192 |
void OPLL_setPatch(OPLL *, const uint8_t *dump); |
| 193 |
void OPLL_copyPatch(OPLL *, int32_t, OPLL_PATCH *); |
| 194 |
|
| 195 |
/** |
| 196 |
* Force to refresh. |
| 197 |
* External program should call this function after updating patch parameters. |
| 198 |
*/ |
| 199 |
void OPLL_forceRefresh(OPLL *); |
| 200 |
|
| 201 |
void OPLL_dumpToPatch(const uint8_t *dump, OPLL_PATCH *patch); |
| 202 |
void OPLL_patchToDump(const OPLL_PATCH *patch, uint8_t *dump); |
| 203 |
void OPLL_getDefaultPatch(int32_t type, int32_t num, OPLL_PATCH *); |
| 204 |
|
| 205 |
/** |
| 206 |
* Set channel mask |
| 207 |
* @param mask mask flag: OPLL_MASK_* can be used. |
| 208 |
* - bit 0..8: mask for ch 1 to 9 (OPLL_MASK_CH(i)) |
| 209 |
* - bit 9: mask for Hi-Hat (OPLL_MASK_HH) |
| 210 |
* - bit 10: mask for Top-Cym (OPLL_MASK_CYM) |
| 211 |
* - bit 11: mask for Tom (OPLL_MASK_TOM) |
| 212 |
* - bit 12: mask for Snare Drum (OPLL_MASK_SD) |
| 213 |
* - bit 13: mask for Bass Drum (OPLL_MASK_BD) |
| 214 |
*/ |
| 215 |
uint32_t OPLL_setMask(OPLL *, uint32_t mask); |
| 216 |
|
| 217 |
/** |
| 218 |
* Toggler channel mask flag |
| 219 |
*/ |
| 220 |
uint32_t OPLL_toggleMask(OPLL *, uint32_t mask); |
| 221 |
|
| 222 |
/* for compatibility */ |
| 223 |
#define OPLL_set_rate OPLL_setRate |
| 224 |
#define OPLL_set_quality OPLL_setQuality |
| 225 |
#define OPLL_set_pan OPLL_setPan |
| 226 |
#define OPLL_set_pan_fine OPLL_setPanFine |
| 227 |
#define OPLL_calc_stereo OPLL_calcStereo |
| 228 |
#define OPLL_reset_patch OPLL_resetPatch |
| 229 |
#define OPLL_dump2patch OPLL_dumpToPatch |
| 230 |
#define OPLL_patch2dump OPLL_patchToDump |
| 231 |
#define OPLL_setChipMode OPLL_setChipType |
| 232 |
|
| 233 |
#ifdef __cplusplus |
| 234 |
} |
| 235 |
#endif |
| 236 |
|
| 237 |
#endif |