forked from OneLoneCoder/synth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
269 lines (219 loc) · 6.96 KB
/
main2.cpp
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
/*
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
~~~~~~~~
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 <iostream>
using namespace std;
#include "olcNoiseMaker.h"
// Converts frequency (Hz) to angular velocity
double w(double dHertz)
{
return dHertz * 2.0 * PI;
}
// General purpose oscillator
#define OSC_SINE 0
#define OSC_SQUARE 1
#define OSC_TRIANGLE 2
#define OSC_SAW_ANA 3
#define OSC_SAW_DIG 4
#define OSC_NOISE 5
double osc(double dHertz, double dTime, int nType = OSC_SINE)
{
switch (nType)
{
case OSC_SINE: // Sine wave bewteen -1 and +1
return sin(w(dHertz) * dTime);
case OSC_SQUARE: // Square wave between -1 and +1
return sin(w(dHertz) * dTime) > 0 ? 1.0 : -1.0;
case OSC_TRIANGLE: // Triangle wave between -1 and +1
return asin(sin(w(dHertz) * dTime)) * (2.0 / PI);
case OSC_SAW_ANA: // Saw wave (analogue / warm / slow)
{
double dOutput = 0.0;
for (double n = 1.0; n < 40.0; n++)
dOutput += (sin(n * w(dHertz) * dTime)) / n;
return dOutput * (2.0 / PI);
}
case OSC_SAW_DIG: // Saw Wave (optimised / harsh / fast)
return (2.0 / PI) * (dHertz * PI * fmod(dTime, 1.0 / dHertz) - (PI / 2.0));
case OSC_NOISE: // Pseudorandom noise
return 2.0 * ((double)rand() / (double)RAND_MAX) - 1.0;
default:
return 0.0;
}
}
// Amplitude (Attack, Decay, Sustain, Release) Envelope
struct sEnvelopeADSR
{
double dAttackTime;
double dDecayTime;
double dSustainAmplitude;
double dReleaseTime;
double dStartAmplitude;
double dTriggerOffTime;
double dTriggerOnTime;
bool bNoteOn;
sEnvelopeADSR()
{
dAttackTime = 0.10;
dDecayTime = 0.01;
dStartAmplitude = 1.0;
dSustainAmplitude = 0.8;
dReleaseTime = 0.20;
bNoteOn = false;
dTriggerOffTime = 0.0;
dTriggerOnTime = 0.0;
}
// Call when key is pressed
void NoteOn(double dTimeOn)
{
dTriggerOnTime = dTimeOn;
bNoteOn = true;
}
// Call when key is released
void NoteOff(double dTimeOff)
{
dTriggerOffTime = dTimeOff;
bNoteOn = false;
}
// Get the correct amplitude at the requested point in time
double GetAmplitude(double dTime)
{
double dAmplitude = 0.0;
double dLifeTime = dTime - dTriggerOnTime;
if (bNoteOn)
{
if (dLifeTime <= dAttackTime)
{
// In attack Phase - approach max amplitude
dAmplitude = (dLifeTime / dAttackTime) * dStartAmplitude;
}
if (dLifeTime > dAttackTime && dLifeTime <= (dAttackTime + dDecayTime))
{
// In decay phase - reduce to sustained amplitude
dAmplitude = ((dLifeTime - dAttackTime) / dDecayTime) * (dSustainAmplitude - dStartAmplitude) + dStartAmplitude;
}
if (dLifeTime > (dAttackTime + dDecayTime))
{
// In sustain phase - dont change until note released
dAmplitude = dSustainAmplitude;
}
}
else
{
// Note has been released, so in release phase
dAmplitude = ((dTime - dTriggerOffTime) / dReleaseTime) * (0.0 - dSustainAmplitude) + dSustainAmplitude;
}
// Amplitude should not be negative
if (dAmplitude <= 0.0001)
dAmplitude = 0.0;
return dAmplitude;
}
};
// Global synthesizer variables
atomic<double> dFrequencyOutput = 0.0; // dominant output frequency of instrument, i.e. the note
sEnvelopeADSR envelope; // amplitude modulation of output to give texture, i.e. the timbre
double dOctaveBaseFrequency = 110.0; // A2 // frequency of octave represented by keyboard
double d12thRootOf2 = pow(2.0, 1.0 / 12.0); // assuming western 12 notes per ocatve
// Function used by olcNoiseMaker to generate sound waves
// Returns amplitude (-1.0 to +1.0) as a function of time
double MakeNoise(double dTime)
{
// Mix together a little sine and square waves
double dOutput = envelope.GetAmplitude(dTime) *
(
+ 1.0 * osc(dFrequencyOutput * 0.5, dTime, OSC_SINE)
+ 1.0 * osc(dFrequencyOutput, dTime, OSC_SAW_ANA)
);
return dOutput * 0.4; // Master Volume
}
int main()
{
// Shameless self-promotion
wcout << "www.OneLoneCoder.com - Synthesizer Part 2" << endl << "Multiple Oscillators with Single Amplitude Envelope, No 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);
// Sit in loop, capturing keyboard state changes and modify
// synthesizer output accordingly
int nCurrentKey = -1;
bool bKeyPressed = false;
while (1)
{
bKeyPressed = false;
for (int k = 0; k < 16; k++)
{
if (GetAsyncKeyState((unsigned char)("ZSXCFVGBNJMK\xbcL\xbe\xbf"[k])) & 0x8000)
{
if (nCurrentKey != k)
{
dFrequencyOutput = dOctaveBaseFrequency * pow(d12thRootOf2, k);
envelope.NoteOn(sound.GetTime());
wcout << "\rNote On : " << sound.GetTime() << "s " << dFrequencyOutput << "Hz";
nCurrentKey = k;
}
bKeyPressed = true;
}
}
if (!bKeyPressed)
{
if (nCurrentKey != -1)
{
wcout << "\rNote Off: " << sound.GetTime() << "s ";
envelope.NoteOff(sound.GetTime());
nCurrentKey = -1;
}
}
}
return 0;
}