comparison fmgen/op.h @ 0:c55ea9478c80

Hello Gensokyo!
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 21 May 2013 10:29:21 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c55ea9478c80
1 #ifndef FM_OP_H
2 #define FM_OP_H
3
4 #include <stdint.h>
5 #define false 0
6 #define true 1
7 #define Max(a,b) ((a>b)?a:b)
8 #define Min(a,b) ((a<b)?a:b)
9
10 // Types ----------------------------------------------------------------
11 typedef int32_t Sample;
12 typedef int32_t ISample;
13
14 // ---------------------------------------------------------------------------
15 // Various implementation-specific constants.
16 // Most are used to either define the bit-width of counters, or the size
17 // of various constant tables.
18 //
19 #define FM_LFOBITS 8
20 #define FM_TLBITS 7
21 #define FM_TLENTS (1 << FM_TLBITS)
22 #define FM_LFOENTS (1 << FM_LFOBITS)
23 #define FM_TLPOS (FM_TLENTS/4)
24 #define FM_CLENTS (0xc00 << 2)
25 #define FM_OPSINBITS 10
26 #define FM_OPSINENTS (1 << FM_OPSINBITS)
27 #define FM_EGBITS 16
28 #define FM_EGCBITS 18
29 #define FM_LFOCBITS 14
30 #define FM_PGBITS 9
31 #define FM_RATIOBITS 12
32
33 typedef uint32_t uint;
34 typedef enum _EGPhase { next, attack, decay, sustain, release, off } EGPhase;
35
36 static inline int Limit(int v, int max, int min)
37 {
38 return v > max ? max : (v < min ? min : v);
39 }
40
41 // Operator ----------------------------------------------------------------
42 typedef struct _FMOperator
43 {
44 int32_t out, out2;
45
46 // Phase Generator -----------------------------------------------------
47 uint32_t dp; // Octave (used to define note in conjunction with bn, below).
48 uint8_t detune; // Detune
49 uint8_t multiple; // Multiple
50 uint32_t pgcount; // Phase generator sweep value. Only the top 9 bits are relevant/used.
51 uint32_t pgdcount; // Phase generator increment-per-clock value. Hopefully self-explanatory.
52 uint32_t pgdcountl; // Phase generator detune increment value. Used in the implementation of vibrato.
53 // Equal to pgdcount >> 11.
54
55 // Envelope Generator --------------------------------------------------
56 uint32_t bn; // Block/Note
57 uint32_t egout;
58 int eglevel; // EG ¤Î½ÐÎÏÃÍ
59 int eglvnext; // ¼¡¤Î phase ¤Ë°Ü¤ëÃÍ
60 int32_t egstep; // EG ¤Î¼¡¤ÎÊѰܤޤǤλþ´Ö
61 int32_t egstepd; // egstep ¤Î»þ´Öº¹Ê¬
62 uint8_t egtransa; // EG ÊѲ½¤Î³ä¹ç (for attack)
63 uint8_t egtransd; // EG ÊѲ½¤Î³ä¹ç (for decay)
64
65 uint32_t ksr; // key scale rate
66 EGPhase phase;
67 uint8_t ams;
68 uint8_t ms;
69
70 uint8_t keyon; // current key state
71
72 uint8_t tl; // Total Level (0-127)
73 uint8_t tll; // Total Level Latch (for CSM mode)
74 uint8_t ar; // Attack Rate (0-63)
75 uint8_t dr; // Decay Rate (0-63)
76 uint8_t sr; // Sustain Rate (0-63)
77 uint8_t sl; // Sustain Level (0-127)
78 uint8_t rr; // Release Rate (0-63)
79 uint8_t ks; // Keyscale (0-3)
80 uint8_t ssgtype; // SSG-Type Envelope Control
81
82 uint8_t amon; // enable Amplitude Modulation
83 uint8_t paramchanged; // Set whenever f-number or any ADSR constants
84 // are set in OPNASetReg(), as well as upon
85 // chip reset and chip "DAC" samplerate change.
86 // Causes the envelope generator to reset its
87 // internal state, also sets correct increments
88 // for the phase generator.
89 uint8_t mute;
90 } FMOperator;
91
92 // 4-op Channel ------------------------------------------------------------
93 typedef struct Channel4
94 {
95 uint32_t fb;
96 int buf[4];
97 uint8_t idx[6];
98 int *pms;
99 FMOperator op[4];
100 } Channel4;
101
102 // OPNA Rhythm Generator ---------------------------------------------------
103 typedef struct Rhythm {
104 uint8_t pan;
105 int8_t level;
106 int8_t volume;
107 int8_t* sample; // Rhythm sample data
108 uint32_t size; // Rhythm sample data size
109 uint32_t pos; // Current index into rhytm sample data array
110 uint32_t step; // Amount to increment the above by every time
111 // RhythmMix() gets called.
112 uint32_t rate; // Samplerate of rhythm sample data
113 // (44100Hz in this implementation).
114 } Rhythm;
115
116 #ifdef __cplusplus
117 extern "C" {
118 #endif
119
120 // --------------------------------------------------------------------------
121 // Miscellaneous and probably irrelevant function prototypes.
122 void MakeTable(void);
123 void OperatorInit(FMOperator *op);
124 void OperatorReset(FMOperator *op);
125 void OperatorPrepare(FMOperator *op);
126
127 static inline uint32_t IsOn(FMOperator *op) {
128 return (op->phase - off);
129 }
130
131 #ifdef __cplusplus
132 };
133 #endif
134
135 #endif