-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathmain3a.cpp
More file actions
467 lines (370 loc) · 11.6 KB
/
main3a.cpp
File metadata and controls
467 lines (370 loc) · 11.6 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
OneLoneCoder.com - Simple Audio Noisy Thing
"Allows you to simply listen to that waveform!" - @Javidx9
License
~~~~~~~
Copyright (C) 2018 Javidx9
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; See license for details.
Original works located at:
https://www.github.com/onelonecoder
https://www.onelonecoder.com
https://www.youtube.com/javidx9
GNU GPLv3
https://github.com/OneLoneCoder/videos/blob/master/LICENSE
From Javidx9 :)
~~~~~~~~~~~~~~~
Hello! Ultimately I don't care what you use this for. It's intended to be
educational, and perhaps to the oddly minded - a little bit of fun.
Please hack this, change it and use it in any way you see fit. You acknowledge
that I am not responsible for anything bad that happens as a result of
your actions. However this code is protected by GNU GPLv3, see the license in the
github repo. This means you must attribute me if you use it. You can view this
license here: https://github.com/OneLoneCoder/videos/blob/master/LICENSE
Cheers!
Author
~~~~~~
Twitter: @javidx9
Blog: www.onelonecoder.com
Versions
~~~~~~~~
main3a.cpp
This adds polyphony, frequency modulation and instruments.
See Video: https://youtu.be/kDuvruJTjOs
main2.cpp
This version expands on oscillators to include other waveforms
and introduces envelopes
See Video: https://youtu.be/OSCzKOqtgcA
main1.cpp
This is the first version of the software. It presents a simple
keyboard and a sine wave oscillator.
See video: https://youtu.be/tgamhuQnOkM
*/
#include <list>
#include <iostream>
#include <algorithm>
using namespace std;
#define FTYPE double
#include "olcNoiseMaker.h"
namespace synth
{
//////////////////////////////////////////////////////////////////////////////
// Utilities
// Converts frequency (Hz) to angular velocity
FTYPE w(const FTYPE dHertz)
{
return dHertz * 2.0 * PI;
}
// A basic note
struct note
{
int id; // Position in scale
FTYPE on; // Time note was activated
FTYPE off; // Time note was deactivated
bool active;
int channel;
note()
{
id = 0;
on = 0.0;
off = 0.0;
active = false;
channel = 0;
}
};
//////////////////////////////////////////////////////////////////////////////
// Multi-Function Oscillator
const int OSC_SINE = 0;
const int OSC_SQUARE = 1;
const int OSC_TRIANGLE = 2;
const int OSC_SAW_ANA = 3;
const int OSC_SAW_DIG = 4;
const int OSC_NOISE = 5;
FTYPE osc(const FTYPE dTime, const FTYPE dHertz, const int nType = OSC_SINE,
const FTYPE dLFOHertz = 0.0, const FTYPE dLFOAmplitude = 0.0, FTYPE dCustom = 50.0)
{
FTYPE dFreq = w(dHertz) * dTime + dLFOAmplitude * dHertz * (sin(w(dLFOHertz) * dTime));// osc(dTime, dLFOHertz, OSC_SINE);
switch (nType)
{
case OSC_SINE: // Sine wave bewteen -1 and +1
return sin(dFreq);
case OSC_SQUARE: // Square wave between -1 and +1
return sin(dFreq) > 0 ? 1.0 : -1.0;
case OSC_TRIANGLE: // Triangle wave between -1 and +1
return asin(sin(dFreq)) * (2.0 / PI);
case OSC_SAW_ANA: // Saw wave (analogue / warm / slow)
{
FTYPE dOutput = 0.0;
for (FTYPE n = 1.0; n < dCustom; n++)
dOutput += (sin(n*dFreq)) / n;
return dOutput * (2.0 / PI);
}
case OSC_SAW_DIG:
return (2.0 / PI) * (dHertz * PI * fmod(dTime, 1.0 / dHertz) - (PI / 2.0));
case OSC_NOISE:
return 2.0 * ((FTYPE)rand() / (FTYPE)RAND_MAX) - 1.0;
default:
return 0.0;
}
}
//////////////////////////////////////////////////////////////////////////////
// Scale to Frequency conversion
const int SCALE_DEFAULT = 0;
FTYPE scale(const int nNoteID, const int nScaleID = SCALE_DEFAULT)
{
switch (nScaleID)
{
case SCALE_DEFAULT: default:
return 256 * pow(1.0594630943592952645618252949463, nNoteID);
}
}
//////////////////////////////////////////////////////////////////////////////
// Envelopes
struct envelope
{
virtual FTYPE amplitude(const FTYPE dTime, const FTYPE dTimeOn, const FTYPE dTimeOff) = 0;
};
struct envelope_adsr : public envelope
{
FTYPE dAttackTime;
FTYPE dDecayTime;
FTYPE dSustainAmplitude;
FTYPE dReleaseTime;
FTYPE dStartAmplitude;
envelope_adsr()
{
dAttackTime = 0.1;
dDecayTime = 0.1;
dSustainAmplitude = 1.0;
dReleaseTime = 0.2;
dStartAmplitude = 1.0;
}
virtual FTYPE amplitude(const FTYPE dTime, const FTYPE dTimeOn, const FTYPE dTimeOff)
{
FTYPE dAmplitude = 0.0;
FTYPE dReleaseAmplitude = 0.0;
if (dTimeOn > dTimeOff) // Note is on
{
FTYPE dLifeTime = dTime - dTimeOn;
if (dLifeTime <= dAttackTime)
dAmplitude = (dLifeTime / dAttackTime) * dStartAmplitude;
if (dLifeTime > dAttackTime && dLifeTime <= (dAttackTime + dDecayTime))
dAmplitude = ((dLifeTime - dAttackTime) / dDecayTime) * (dSustainAmplitude - dStartAmplitude) + dStartAmplitude;
if (dLifeTime > (dAttackTime + dDecayTime))
dAmplitude = dSustainAmplitude;
}
else // Note is off
{
FTYPE dLifeTime = dTimeOff - dTimeOn;
if (dLifeTime <= dAttackTime)
dReleaseAmplitude = (dLifeTime / dAttackTime) * dStartAmplitude;
if (dLifeTime > dAttackTime && dLifeTime <= (dAttackTime + dDecayTime))
dReleaseAmplitude = ((dLifeTime - dAttackTime) / dDecayTime) * (dSustainAmplitude - dStartAmplitude) + dStartAmplitude;
if (dLifeTime > (dAttackTime + dDecayTime))
dReleaseAmplitude = dSustainAmplitude;
dAmplitude = ((dTime - dTimeOff) / dReleaseTime) * (0.0 - dReleaseAmplitude) + dReleaseAmplitude;
}
// Amplitude should not be negative
if (dAmplitude <= 0.000)
dAmplitude = 0.0;
return dAmplitude;
}
};
FTYPE env(const FTYPE dTime, envelope &env, const FTYPE dTimeOn, const FTYPE dTimeOff)
{
return env.amplitude(dTime, dTimeOn, dTimeOff);
}
struct instrument_base
{
FTYPE dVolume;
synth::envelope_adsr env;
virtual FTYPE sound(const FTYPE dTime, synth::note n, bool &bNoteFinished) = 0;
};
struct instrument_bell : public instrument_base
{
instrument_bell()
{
env.dAttackTime = 0.01;
env.dDecayTime = 1.0;
env.dSustainAmplitude = 0.0;
env.dReleaseTime = 1.0;
dVolume = 1.0;
}
virtual FTYPE sound(const FTYPE dTime, synth::note n, bool &bNoteFinished)
{
FTYPE dAmplitude = synth::env(dTime, env, n.on, n.off);
if (dAmplitude <= 0.0) bNoteFinished = true;
FTYPE dSound =
+ 1.00 * synth::osc(n.on - dTime, synth::scale(n.id + 12), synth::OSC_SINE, 5.0, 0.001)
+ 0.50 * synth::osc(n.on - dTime, synth::scale(n.id + 24))
+ 0.25 * synth::osc(n.on - dTime, synth::scale(n.id + 36));
return dAmplitude * dSound * dVolume;
}
};
struct instrument_bell8 : public instrument_base
{
instrument_bell8()
{
env.dAttackTime = 0.01;
env.dDecayTime = 0.5;
env.dSustainAmplitude = 0.8;
env.dReleaseTime = 1.0;
dVolume = 1.0;
}
virtual FTYPE sound(const FTYPE dTime, synth::note n, bool &bNoteFinished)
{
FTYPE dAmplitude = synth::env(dTime, env, n.on, n.off);
if (dAmplitude <= 0.0) bNoteFinished = true;
FTYPE dSound =
+1.00 * synth::osc(n.on - dTime, synth::scale(n.id), synth::OSC_SQUARE, 5.0, 0.001)
+ 0.50 * synth::osc(n.on - dTime, synth::scale(n.id + 12))
+ 0.25 * synth::osc(n.on - dTime, synth::scale(n.id + 24));
return dAmplitude * dSound * dVolume;
}
};
struct instrument_harmonica : public instrument_base
{
instrument_harmonica()
{
env.dAttackTime = 0.05;
env.dDecayTime = 1.0;
env.dSustainAmplitude = 0.95;
env.dReleaseTime = 0.1;
dVolume = 1.0;
}
virtual FTYPE sound(const FTYPE dTime, synth::note n, bool &bNoteFinished)
{
FTYPE dAmplitude = synth::env(dTime, env, n.on, n.off);
if (dAmplitude <= 0.0) bNoteFinished = true;
FTYPE dSound =
//+ 1.0 * synth::osc(n.on - dTime, synth::scale(n.id-12), synth::OSC_SAW_ANA, 5.0, 0.001, 100)
+ 1.00 * synth::osc(n.on - dTime, synth::scale(n.id), synth::OSC_SQUARE, 5.0, 0.001)
+ 0.50 * synth::osc(n.on - dTime, synth::scale(n.id + 12), synth::OSC_SQUARE)
+ 0.05 * synth::osc(n.on - dTime, synth::scale(n.id + 24), synth::OSC_NOISE);
return dAmplitude * dSound * dVolume;
}
};
}
vector<synth::note> vecNotes;
mutex muxNotes;
synth::instrument_bell instBell;
synth::instrument_harmonica instHarm;
typedef bool(*lambda)(synth::note const& item);
template<class T>
void safe_remove(T &v, lambda f)
{
auto n = v.begin();
while (n != v.end())
if (!f(*n))
n = v.erase(n);
else
++n;
}
// Function used by olcNoiseMaker to generate sound waves
// Returns amplitude (-1.0 to +1.0) as a function of time
FTYPE MakeNoise(int nChannel, FTYPE dTime)
{
unique_lock<mutex> lm(muxNotes);
FTYPE dMixedOutput = 0.0;
for (auto &n : vecNotes)
{
bool bNoteFinished = false;
FTYPE dSound = 0;
if(n.channel == 2)
dSound = instBell.sound(dTime, n, bNoteFinished);
if (n.channel == 1)
dSound = instHarm.sound(dTime, n, bNoteFinished) * 0.5;
dMixedOutput += dSound;
if (bNoteFinished && n.off > n.on)
n.active = false;
}
// Woah! Modern C++ Overload!!!
safe_remove<vector<synth::note>>(vecNotes, [](synth::note const& item) { return item.active; });
return dMixedOutput * 0.2;
}
int main()
{
// Shameless self-promotion
wcout << "www.OneLoneCoder.com - Synthesizer Part 3" << endl << "Multiple Oscillators with Polyphony" << endl << endl;
// Get all sound hardware
vector<wstring> devices = olcNoiseMaker<short>::Enumerate();
// Display findings
for (auto d : devices) wcout << "Found Output Device: " << d << endl;
wcout << "Using Device: " << devices[0] << endl;
// Display a keyboard
wcout << endl <<
"| | | | | | | | | | | | | | | | |" << endl <<
"| | S | | | F | | G | | | J | | K | | L | | |" << endl <<
"| |___| | |___| |___| | |___| |___| |___| | |__" << endl <<
"| | | | | | | | | | |" << endl <<
"| Z | X | C | V | B | N | M | , | . | / |" << endl <<
"|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|" << endl << endl;
// Create sound machine!!
olcNoiseMaker<short> sound(devices[0], 44100, 1, 8, 512);
// Link noise function with sound machine
sound.SetUserFunction(MakeNoise);
char keyboard[129];
memset(keyboard, ' ', 127);
keyboard[128] = '\0';
auto clock_old_time = chrono::high_resolution_clock::now();
auto clock_real_time = chrono::high_resolution_clock::now();
double dElapsedTime = 0.0;
while (1)
{
for (int k = 0; k < 16; k++)
{
short nKeyState = GetAsyncKeyState((unsigned char)("ZSXCFVGBNJMK\xbcL\xbe\xbf"[k]));
double dTimeNow = sound.GetTime();
// Check if note already exists in currently playing notes
muxNotes.lock();
auto noteFound = find_if(vecNotes.begin(), vecNotes.end(), [&k](synth::note const& item) { return item.id == k; });
if (noteFound == vecNotes.end())
{
// Note not found in vector
if (nKeyState & 0x8000)
{
// Key has been pressed so create a new note
synth::note n;
n.id = k;
n.on = dTimeNow;
n.channel = 1;
n.active = true;
// Add note to vector
vecNotes.emplace_back(n);
}
else
{
// Note not in vector, but key has been released...
// ...nothing to do
}
}
else
{
// Note exists in vector
if (nKeyState & 0x8000)
{
// Key is still held, so do nothing
if (noteFound->off > noteFound->on)
{
// Key has been pressed again during release phase
noteFound->on = dTimeNow;
noteFound->active = true;
}
}
else
{
// Key has been released, so switch off
if (noteFound->off < noteFound->on)
{
noteFound->off = dTimeNow;
}
}
}
muxNotes.unlock();
}
wcout << "\rNotes: " << vecNotes.size() << " ";
//this_thread::sleep_for(5ms);
}
return 0;
}