-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdNoiseMaker.h
243 lines (197 loc) · 7.24 KB
/
oldNoiseMaker.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
/*
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
~~~~~~~~
1.0 - 14/01/17
- Controls audio output hardware behind the scenes so you can just focus
on creating and listening to interesting waveforms.
- Currently MS Windows only
Documentation
~~~~~~~~~~~~~
See video: https://youtu.be/tgamhuQnOkM
This will improve as it grows!
*/
#pragma once
#include <alsa/asoundlib.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <thread>
#include <atomic>
#include <condition_variable>
#include <mutex>
const double PI = 2.0 * acos(0.0);
template<class T>
class LinuxNoiseMaker {
public:
LinuxNoiseMaker(std::string sOutputDevice = "default", unsigned int nSampleRate = 44100,
unsigned int nChannels = 1, unsigned int nBlocks = 8, unsigned int nBlockSamples = 512) {
Create(sOutputDevice, nSampleRate, nChannels, nBlocks, nBlockSamples);
}
~LinuxNoiseMaker() {
Destroy();
}
bool Create(std::string sOutputDevice, unsigned int nSampleRate = 44100, unsigned int nChannels = 1,
unsigned int nBlocks = 8, unsigned int nBlockSamples = 512) {
m_bReady = false;
m_nSampleRate = nSampleRate;
m_nChannels = nChannels;
m_nBlockCount = nBlocks;
m_nBlockSamples = nBlockSamples;
m_nBlockFree = m_nBlockCount;
m_nBlockCurrent = 0;
// Open PCM device
int rc = snd_pcm_open(&m_handle, sOutputDevice.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
std::cerr << "Cannot open audio device: " << snd_strerror(rc) << std::endl;
return false;
}
// Allocate hardware parameters object
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(¶ms);
// Fill it with default values
snd_pcm_hw_params_any(m_handle, params);
// Set parameters
rc = snd_pcm_hw_params_set_access(m_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (rc < 0) {
std::cerr << "Cannot set interleaved mode: " << snd_strerror(rc) << std::endl;
return false;
}
// Signed 16-bit little-endian format
if (sizeof(T) == 2)
rc = snd_pcm_hw_params_set_format(m_handle, params, SND_PCM_FORMAT_S16_LE);
else
rc = snd_pcm_hw_params_set_format(m_handle, params, SND_PCM_FORMAT_FLOAT);
if (rc < 0) {
std::cerr << "Cannot set format: " << snd_strerror(rc) << std::endl;
return false;
}
// Set channels
rc = snd_pcm_hw_params_set_channels(m_handle, params, m_nChannels);
if (rc < 0) {
std::cerr << "Cannot set channels: " << snd_strerror(rc) << std::endl;
return false;
}
// Set sample rate
unsigned int actual_rate = m_nSampleRate;
rc = snd_pcm_hw_params_set_rate_near(m_handle, params, &actual_rate, 0);
if (rc < 0) {
std::cerr << "Cannot set sample rate: " << snd_strerror(rc) << std::endl;
return false;
}
// Apply hardware parameters
rc = snd_pcm_hw_params(m_handle, params);
if (rc < 0) {
std::cerr << "Cannot set parameters: " << snd_strerror(rc) << std::endl;
return false;
}
// Allocate buffer memory
m_pBlockMemory = new T[m_nBlockCount * m_nBlockSamples];
if (m_pBlockMemory == nullptr)
return false;
memset(m_pBlockMemory, 0, sizeof(T) * m_nBlockCount * m_nBlockSamples);
m_bReady = true;
m_thread = std::thread(&LinuxNoiseMaker::MainThread, this);
return true;
}
bool Destroy() {
if (m_handle) {
snd_pcm_drain(m_handle);
snd_pcm_close(m_handle);
}
delete[] m_pBlockMemory;
return true;
}
void Stop() {
m_bReady = false;
m_thread.join();
}
// Override to process current sample
virtual double UserProcess(double dTime) {
return 0.0;
}
double GetTime() {
return m_dGlobalTime;
}
void SetUserFunction(double(*func)(double)) {
m_userFunction = func;
}
double clip(double dSample, double dMax) {
if (dSample >= 0.0)
return fmin(dSample, dMax);
else
return fmax(dSample, -dMax);
}
private:
double(*m_userFunction)(double) = nullptr;
snd_pcm_t *m_handle = nullptr;
unsigned int m_nSampleRate;
unsigned int m_nChannels;
unsigned int m_nBlockCount;
unsigned int m_nBlockSamples;
unsigned int m_nBlockCurrent;
T* m_pBlockMemory;
std::thread m_thread;
std::atomic<bool> m_bReady;
std::atomic<unsigned int> m_nBlockFree;
std::atomic<double> m_dGlobalTime;
void MainThread() {
m_dGlobalTime = 0.0;
double dTimeStep = 1.0 / (double)m_nSampleRate;
T nMaxSample = (T)pow(2, (sizeof(T) * 8) - 1) - 1;
double dMaxSample = (double)nMaxSample;
while (m_bReady) {
int nCurrentBlock = m_nBlockCurrent * m_nBlockSamples;
for (unsigned int n = 0; n < m_nBlockSamples; n++) {
double dOutput = 0.0;
if (m_userFunction == nullptr)
dOutput = UserProcess(m_dGlobalTime);
else
dOutput = m_userFunction(m_dGlobalTime);
T nNewSample = (T)(clip(dOutput, 1.0) * dMaxSample);
m_pBlockMemory[nCurrentBlock + n] = nNewSample;
m_dGlobalTime = m_dGlobalTime + dTimeStep;
}
// Write block to sound device
snd_pcm_sframes_t frames = snd_pcm_writei(m_handle,
&m_pBlockMemory[nCurrentBlock],
m_nBlockSamples);
if (frames < 0)
frames = snd_pcm_recover(m_handle, frames, 0);
if (frames < 0) {
std::cerr << "snd_pcm_writei failed: " << snd_strerror(frames) << std::endl;
break;
}
m_nBlockCurrent++;
m_nBlockCurrent %= m_nBlockCount;
}
}
};