-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynth.h
252 lines (209 loc) · 6.1 KB
/
Synth.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#pragma once
#include <string>
#include <string_view>
#include <vector>
#ifndef FTYPE
#define FTYPE double
#endif
namespace Synth
{
enum WaveType
{
OSC_SINE
, OSC_SQUARE
, OSC_TRIANGLE
, OSC_SAW_ANA
, OSC_SAW_DIG
, OSC_NOISE
};
enum HarmonicDecayType
{
LINEAR,
EXPONENTIAL,
};
constexpr auto BaseNoteID = 64;
WaveType strToWaveType(const std::string_view str);
std::string waveTypeToStr(const WaveType wt);
HarmonicDecayType strToHarmonicDecayType(const std::string_view str);
std::string harmonicDecayTypeToStr(const HarmonicDecayType wt);
// Converts frequency (Hz) to angular velocity
inline FTYPE f2w(const FTYPE dHertz);
FTYPE oscillator(const FTYPE dTime, const FTYPE dHertz, const WaveType nType);
FTYPE oscillator(const FTYPE dTime, const FTYPE dHertz, const WaveType nType, const FTYPE dLFOHertz, const FTYPE dLFOAmplitude);
FTYPE oscillator(const FTYPE dTime, const FTYPE dHertz, const WaveType nType, const FTYPE dLFOHertz, const FTYPE dLFOAmplitude, FTYPE dCustom);
//////////////////////////////////////////////////////////////////////////////
// Scale to Frequency conversion
constexpr int SCALE_DEFAULT = 0;
FTYPE scale(const int nNoteID, const int /*nScaleID*/ = SCALE_DEFAULT);
//////////////////////////////////////////////////////////////////////////////
// Envelopes
// Don't need a base class (yet)
//struct Envelope { virtual FTYPE amplitude(const FTYPE dTime, const FTYPE dTimeOn, const FTYPE dTimeOff) const = 0;};
//struct EnvelopeADSR : public Envelope
struct Envelope
{
FTYPE dAttackTime = 0;
FTYPE dDecayTime = 0;
FTYPE dSustainAmplitude = 0;
FTYPE dReleaseTime = 0;
FTYPE dStartAmplitude = 1.0;
FTYPE amplitude(const FTYPE dTime, const FTYPE dTimeOn, const FTYPE dTimeOff) const;
};
FTYPE env(const FTYPE dTime, const Envelope& envel, const FTYPE dTimeOn, const FTYPE dTimeOff);
// A basic note
struct Note
{
int id = 0; // Position in scale
FTYPE on = 0; // Time note was activated
FTYPE off = 0; // Time note was deactivated
FTYPE velocity = 1.0; // how lound is this note, relative to the base loudness of the instrument
bool active = false;
};
struct Instrument
{
FTYPE dVolume;
Envelope envADSR;
FTYPE fMaxLifeTime;
std::string name;
virtual FTYPE sound(const FTYPE dTime, Note note, bool& bNoteFinished) const = 0;
};
struct NoteInstrumentPtr
{
Note m_Note;
Instrument* m_pInstrument;
};
struct CustomInstrument : public Instrument
{
struct Sound
{
/*
"Amp": 1.0,
"Freq": 0,
"Type": "SawA",
"LFreq": 5.0,
"LAmp": 0.001,
"Cust": 100
"Harmonics": 1,
"DecayType": "Exp",
"Decay": 50,
"EvenOddBalance": 50
*/
FTYPE amp = 1;
int freq = 0;
WaveType type;
FTYPE lFreq = 0;
FTYPE lAmp = 0;
FTYPE custom = 0;
int harmonics = 0;
HarmonicDecayType decayType = LINEAR;
FTYPE decay = 0;
int evenOddBal = 50;
};
CustomInstrument();
FTYPE sound(const FTYPE dTime, Note note, bool& bNoteFinished) const override;
std::vector<Sound> sounds;
};
struct Instrument_harmonica : public Instrument
{
Instrument_harmonica();
FTYPE sound(const FTYPE dTime, Note note, bool& bNoteFinished) const override;
};
/* Currently not in use
struct Instrument_bell : public Instrument
{
Instrument_bell()
{
envADSR.dAttackTime = 0.01;
envADSR.dDecayTime = 1.0;
envADSR.dSustainAmplitude = 0.0;
envADSR.dReleaseTime = 1.0;
fMaxLifeTime = 3.0;
dVolume = 1.0;
name = "Bell";
}
FTYPE sound(const FTYPE dTime, Note note, bool& bNoteFinished) const override
{
FTYPE dAmplitude = Synth::env(dTime, envADSR, note.on, note.off);
if (dAmplitude <= 0.0)
bNoteFinished = true;
FTYPE dSound =
+ 1.00 * Synth::oscillator(dTime - note.on, Synth::scale(note.id + 12), Synth::OSC_SINE, 5.0, 0.001)
+ 0.50 * Synth::oscillator(dTime - note.on, Synth::scale(note.id + 24), Synth::OSC_SINE)
+ 0.25 * Synth::oscillator(dTime - note.on, Synth::scale(note.id + 36), Synth::OSC_SINE);
return dAmplitude * dSound * dVolume;
}
};
*/
/* Currently not in use
struct Instrument_bell8 : public Instrument
{
Instrument_bell8()
{
envADSR.dAttackTime = 0.01;
envADSR.dDecayTime = 0.5;
envADSR.dSustainAmplitude = 0.8;
envADSR.dReleaseTime = 1.0;
fMaxLifeTime = 3.0;
dVolume = 1.0;
name = "8-Bit Bell";
}
FTYPE sound(const FTYPE dTime, Note note, bool& bNoteFinished) const override
{
FTYPE dAmplitude = Synth::env(dTime, envADSR, note.on, note.off);
if (dAmplitude <= 0.0)
bNoteFinished = true;
FTYPE dSound =
+1.00 * Synth::oscillator(dTime - note.on, Synth::scale(note.id), Synth::OSC_SQUARE, 5.0, 0.001)
+ 0.50 * Synth::oscillator(dTime - note.on, Synth::scale(note.id + 12), Synth::OSC_SINE)
+ 0.25 * Synth::oscillator(dTime - note.on, Synth::scale(note.id + 24), Synth::OSC_SINE);
return dAmplitude * dSound * dVolume;
}
};
*/
struct Instrument_drumkick : public Instrument
{
Instrument_drumkick();
FTYPE sound(const FTYPE dTime, Note note, bool& bNoteFinished) const override;
};
struct Instrument_drumsnare : public Instrument
{
Instrument_drumsnare();
FTYPE sound(const FTYPE dTime, Note note, bool& bNoteFinished) const override;
};
struct Instrument_drumhihat : public Instrument
{
Instrument_drumhihat();
FTYPE sound(const FTYPE dTime, Note note, bool& bNoteFinished) const override;
};
struct Sequencer
{
public:
struct Channel
{
Instrument* instrument;
std::vector<int> sBeat;
bool bMuted = false;
};
public:
Sequencer(float tempo, int beats, int subbeats);
void Update(FTYPE fElapsedTime);
/** Returns index of instrument */
size_t AddInstrument(Instrument* inst);
public:
int nBeats;
int nSubBeats;
FTYPE fTempo;
FTYPE fBeatTime;
FTYPE fAccumulate;
int nCurrentBeat;
int nTotalBeats;
bool bMuted = false;
public:
std::vector<Channel> vecChannel;
std::vector<NoteInstrumentPtr> vecNotes;
bool muted() const { return bMuted; }
bool muted(const Channel& v) const { return bMuted || v.bMuted; }
private:
};
std::vector<CustomInstrument> loadInstruments();
}