-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathmain1.cpp
126 lines (102 loc) · 3.74 KB
/
main1.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
/*
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
~~~~~~~~
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"
// Global synthesizer variables
atomic<double> dFrequencyOutput = 0.0; // dominant output frequency of instrument, i.e. the note
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)
{
double dOutput = sin(dFrequencyOutput * 2.0 * 3.14159 * dTime);
return dOutput * 0.5; // Master Volume
}
int main()
{
// Shameless self-promotion
wcout << "www.OneLoneCoder.com - Synthesizer Part 1" << endl << "Single Sine Wave Oscillator, 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);
wcout << "\rNote On : " << sound.GetTime() << "s " << dFrequencyOutput << "Hz";
nCurrentKey = k;
}
bKeyPressed = true;
}
}
if (!bKeyPressed)
{
if (nCurrentKey != -1)
{
wcout << "\rNote Off: " << sound.GetTime() << "s ";
nCurrentKey = -1;
}
dFrequencyOutput = 0.0;
}
}
return 0;
}