forked from OneLoneCoder/olcPixelGameEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneLoneCoder_PGE_SoundTest.cpp
272 lines (210 loc) · 7.65 KB
/
OneLoneCoder_PGE_SoundTest.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
270
271
272
/*
Simple example code for olcPGEX_Sound.h - Mind your speakers!
You will need SampleA.wav, SampleB.wav and SampleC.wav for this demo.
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2018 OneLoneCoder.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions or derivations of source code must retain the above
copyright notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce
the above copyright notice. This list of conditions and the following
disclaimer must be reproduced in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Links
~~~~~
YouTube: https://www.youtube.com/javidx9
Discord: https://discord.gg/WhwHUMV
Twitter: https://www.twitter.com/javidx9
Twitch: https://www.twitch.tv/javidx9
GitHub: https://www.github.com/onelonecoder
Homepage: https://www.onelonecoder.com
Patreon: https://www.patreon.com/javidx9
Author
~~~~~~
David Barr, aka javidx9, ©OneLoneCoder 2018
*/
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#define OLC_PGEX_SOUND
#include "olcPGEX_Sound.h"
#include <list>
class SoundTest : public olc::PixelGameEngine
{
public:
SoundTest()
{
sAppName = "Sound Test";
}
private:
int sndSampleA;
int sndSampleB;
int sndSampleC;
bool bToggle = false;
static bool bSynthPlaying;
static float fSynthFrequency;
static float fFilterVolume;
const olc::Key keys[12] = { olc::Key::Z, olc::Key::S, olc::Key::X, olc::Key::D, olc::Key::C,
olc::Key::V, olc::Key::G, olc::Key::B, olc::Key::H, olc::Key::N, olc::Key::J, olc::Key::M};
static float fPreviousSamples[128];
static int nSamplePos;
private:
// This is an optional function that allows the user to generate or synthesize sounds
// in a custom way, it is fed into the output mixer bu the extension
static float MyCustomSynthFunction(int nChannel, float fGlobalTime, float fTimeStep)
{
// Just generate a sine wave of the appropriate frequency
if (bSynthPlaying)
return sin(fSynthFrequency * 2.0f * 3.14159f * fGlobalTime);
else
return 0.0f;
}
// This is an optional function that allows the user to filter the output from
// the internal mixer of the extension. Here you could add effects or just
// control volume. I also like to use it to extract information about
// the currently playing output waveform
static float MyCustomFilterFunction(int nChannel, float fGlobalTime, float fSample)
{
// Fundamentally just control volume
float fOutput = fSample * fFilterVolume;
// But also add sample to list of previous samples for visualisation
fPreviousSamples[nSamplePos] = fOutput;
nSamplePos++;
nSamplePos %= 128;
return fOutput;
}
bool OnUserCreate()
{
olc::SOUND::InitialiseAudio(44100, 1, 8, 512);
sndSampleA = olc::SOUND::LoadAudioSample("SampleA.wav");
sndSampleB = olc::SOUND::LoadAudioSample("SampleB.wav");
sndSampleC = olc::SOUND::LoadAudioSample("SampleC.wav");
// Give the sound engine a hook to a custom generation function
olc::SOUND::SetUserSynthFunction(MyCustomSynthFunction);
// Give the sound engine a hook to a custom filtering function
olc::SOUND::SetUserFilterFunction(MyCustomFilterFunction);
return true;
}
bool OnUserUpdate(float fElapsedTime)
{
//olc::SOUND::PlaySample(sndTest);
auto PointInRect = [&](int x, int y, int rx, int ry, int rw, int rh)
{
return x >= rx && x < (rx + rw) && y >= ry && y < (ry + rh);
};
int nMouseX = GetMouseX();
int nMouseY = GetMouseY();
if(GetMouse(0).bPressed && PointInRect(nMouseX, nMouseY, 16, 16, 128, 24))
olc::SOUND::PlaySample(sndSampleA); // Plays the sample once
if (GetMouse(0).bPressed && PointInRect(nMouseX, nMouseY, 16, 48, 128, 24))
olc::SOUND::PlaySample(sndSampleB);
if (GetMouse(0).bPressed && PointInRect(nMouseX, nMouseY, 16, 80, 128, 24))
{
bToggle = !bToggle;
if (bToggle)
{
olc::SOUND::PlaySample(sndSampleC, true); // Plays the sample in looping mode
}
else
{
olc::SOUND::StopSample(sndSampleC);
}
}
if (GetMouse(0).bHeld && PointInRect(nMouseX, nMouseY, 160, 16, 90, 24))
fFilterVolume += 2.0f * fElapsedTime;
if (GetMouse(0).bHeld && PointInRect(nMouseX, nMouseY, 160, 48, 90, 24))
fFilterVolume -= 2.0f * fElapsedTime;
if (fFilterVolume < 0.0f) fFilterVolume = 0.0f;
if (fFilterVolume > 1.0f) fFilterVolume = 1.0f;
// Detect keyboard - very simple synthesizer
if (IsFocused())
{
bool bKeyIsPressed = false;
float fFrequency = 0.0f;
for (int i = 0; i < 12; i++)
{
if (GetKey(keys[i]).bHeld)
{
bKeyIsPressed = true;
float fOctaveBaseFrequency = 220.0f;
float f12thRootOf2 = pow(2.0f, 1.0f / 12.0f);
fFrequency = fOctaveBaseFrequency * powf(f12thRootOf2, (float)i);
}
}
fSynthFrequency = fFrequency;
bSynthPlaying = bKeyIsPressed;
}
// Draw Buttons
Clear(olc::BLUE);
DrawRect(16, 16, 128, 24);
DrawString(20, 20, "Play Sample A");
DrawRect(16, 48, 128, 24);
DrawString(20, 52, "Play Sample B");
DrawRect(16, 80, 128, 24);
DrawString(20, 84, (bToggle ? "Stop Sample C" : "Loop Sample C"));
DrawRect(160, 16, 90, 24);
DrawString(164, 20, "Volume +");
DrawRect(160, 48, 90, 24);
DrawString(164, 52, "Volume -");
DrawString(164, 80, "Volume: " + std::to_string((int)(fFilterVolume * 10.0f)));
// Draw Keyboard
// White Keys
for (int i = 0; i < 7; i++)
{
FillRect(i * 16 + 8, 160, 16, 64);
DrawRect(i * 16 + 8, 160, 16, 64, olc::BLACK);
DrawString(i * 16 + 12, 212, std::string(1, "ZXCVBNM"[i]), olc::BLACK);
}
// Black Keys
for (int i = 0; i < 6; i++)
{
if (i != 2)
{
FillRect(i * 16 + 18, 160, 12, 32, olc::BLACK);
DrawString(i * 16 + 20, 180, std::string(1, "SDFGHJ"[i]), olc::WHITE);
}
}
// Draw visualisation
int nStartPos = (nSamplePos + 127) % 128;
for (int i = 127; i >= 0; i--)
{
float fSample = fPreviousSamples[(nSamplePos + i) % 128];
DrawLine(124 + i, 210, 124 + i, 210 + (int)(fSample * 20.0f), olc::RED);
}
return true;
}
// Note we must shut down the sound system too!!
bool OnUserDestroy()
{
olc::SOUND::DestroyAudio();
return true;
}
};
bool SoundTest::bSynthPlaying = false;
float SoundTest::fSynthFrequency = 0.0f;
float SoundTest::fFilterVolume = 1.0f;
int SoundTest::nSamplePos = 0;
float SoundTest::fPreviousSamples[128];
int main()
{
SoundTest demo;
if(demo.Construct(256, 240, 4, 4))
demo.Start();
return 0;
}