-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAudio.cpp
358 lines (296 loc) · 8.8 KB
/
Audio.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
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
/*
This file is part of Guitar Storm.
Copyright 2008 Zombie Process, Owen Pedrotti
Guitar Storm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Guitar Storm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Guitar Storm. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Audio.h"
double gInOutScaler = 1.0;
#define CONVERT_IN_TO_OUT(in) ((OUTPUT_SAMPLE) ((in) * gInOutScaler))
#define PI (3.141592653589793)
// these have to be global, because the callback can't access the object's memory space
WireConfig_t config;
FILE* outfile;
const int rblen = SAMPLE_RATE;
int rbi = 0;
int rblast = 0;
bool mtick = false;
double tickfreq = 2400.0;
double tickvol = 8000.0;
const int ticklen = 128;
double backvol = 1.0f;
int currvol = 0;
float ringBuffer[rblen];
int ticksamp[ticklen];
int backBuffer[rblen];
Backing back;
// options
int samples = 128;
int srate = 48000;
int guitarOptVol = 100;
int backingOptVol = 100;
int metronomeOptVol = 50;
int inputDevice = 0;
int outputDevice = 0;
bool recordRaw = false;
// static members
PaStream* Audio::stream = NULL;
double* Audio::last = NULL;
string Audio::music;
bool Audio::initialized = false;
bool Audio::playing = false;
void Audio::init()
{
for(int i=0; i<ticklen; i++)
{
ticksamp[i] = sin(2.0 * PI * (i * tickfreq / SAMPLE_RATE));
}
PaError err = Pa_Initialize();
if( err != paNoError )
throw string("Audio failed to initialize");
const PaDeviceInfo *deviceInfo;
int numDevices = Pa_GetDeviceCount();
if (numDevices > 9) numDevices = 9;
Options::instance->devices = new string[10];
for(int i=0; i<numDevices; i++)
{
deviceInfo = Pa_GetDeviceInfo( i );
Options::instance->devices[i] = deviceInfo->name;
}
initialized = true;
}
int Audio::readVol()
{
return currvol;
}
double* Audio::readLast(int l)
{
last = new double[l];
double min = 10000000000.0;
double max = -10000000000.0;
int i = rblast-1, j;
for(j=l-1; j>=0; j--)
{
if(i<0)
i = rblen-1;
last[j] = ringBuffer[i--];
if(last[j] > max)
max = last[j];
if(last[j] < min)
min = last[j];
}
double dif = max - min;
// normalize array
for(j=0; j<l; j++)
{
last[j] = (2 * last[j] - dif) / dif;
}
return last;
}
void Audio::tick()
{
mtick = true;
}
int Audio::wireCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
INPUT_SAMPLE *in;
OUTPUT_SAMPLE *out;
int inStride;
int outStride;
int backStride = 2;
int inDone = 0;
int outDone = 0;
unsigned int i;
int inChannel, outChannel, backChannel;
// adjust volume
guitarOptVol = Options::instance->guitarVolume;
backingOptVol = Options::instance->backingVolume;
metronomeOptVol = Options::instance->metronomeVolume;
/* This may get called with NULL inputBuffer during initial setup. */
if( inputBuffer == NULL) return 0;
if(!recordRaw && playing && back.Valid())
back.GetSamples(backBuffer, framesPerBuffer * 2);
inChannel=0, outChannel=0, backChannel = 0;
while( !(inDone && outDone) )
{
if( config.isInputInterleaved )
{
in = ((INPUT_SAMPLE*)inputBuffer) + inChannel;
inStride = config.numInputChannels;
}
else
{
in = ((INPUT_SAMPLE**)inputBuffer)[inChannel];
inStride = 1;
}
if( config.isOutputInterleaved )
{
out = ((OUTPUT_SAMPLE*)outputBuffer) + outChannel;
outStride = config.numOutputChannels;
}
else
{
out = ((OUTPUT_SAMPLE**)outputBuffer)[outChannel];
outStride = 1;
}
currvol = 0;
for( i=0; i<framesPerBuffer; i++ )
{
if(rbi >= rblen)
rbi = 0;
if(*in > currvol)
currvol = *in;
// create output
*out = CONVERT_IN_TO_OUT( *in * (guitarOptVol/100.0) );
if(mtick && i<ticklen)
*out += tickvol * (metronomeOptVol/100.0) * ticksamp[i];
if(!recordRaw && playing && back.Valid())
*out += backvol * (backingOptVol/100.0) * backBuffer[backChannel + i * backStride];
if(inChannel == 0)
{
ringBuffer[rbi++] = *out;
if(recordRaw)
fwrite( out, sizeof(OUTPUT_SAMPLE), 1, outfile );
}
out += outStride;
in += inStride;
rblast = rbi;
}
if(inChannel < (config.numInputChannels - 1)) inChannel++;
else inDone = 1;
if(outChannel < (config.numOutputChannels - 1)) {outChannel++; backChannel++;}
else outDone = 1;
}
mtick = false;
return 0;
}
void Audio::stopCapture()
{
if(recordRaw)
fclose(outfile);
if(stream) Pa_CloseStream( stream );
Pa_Terminate();
}
void Audio::startCapture()
{
if(!initialized)
return;
PaError err = paNoError;
//WireConfig_t CONFIG;
//config = &CONFIG;
int configIndex = 0;
playing = false;
// read options
char str[10];
strncpy(str, (char*)Options::instance->bsizes[Options::instance->bufferSize].c_str(), 10);
samples = atoi(str);
strncpy(str, (char*)Options::instance->srates[Options::instance->sampleRate].c_str(), 10);
srate = atoi(str);
inputDevice = Options::instance->inputDevice;
outputDevice = Options::instance->outputDevice;
memset(ringBuffer, 0, rblen);
if( INPUT_FORMAT == OUTPUT_FORMAT )
{
gInOutScaler = 1.0;
}
else if( (INPUT_FORMAT == paInt16) && (OUTPUT_FORMAT == paFloat32) )
{
gInOutScaler = 1.0/32768.0;
}
else if( (INPUT_FORMAT == paFloat32) && (OUTPUT_FORMAT == paInt16) )
{
gInOutScaler = 32768.0;
}
config.isInputInterleaved = 0;
config.isOutputInterleaved = 0;
config.numInputChannels = 2;
config.numOutputChannels = 2;
config.framesPerCallback = samples;
int c;
err = paNoError;
PaStreamParameters inputParameters, outputParameters;
inputParameters.device = inputDevice;
if (inputParameters.device == paNoDevice)
throw string("No ASIO Input Device Detected");
inputParameters.channelCount = config.numInputChannels;
inputParameters.sampleFormat = INPUT_FORMAT | (config.isInputInterleaved ? 0 : paNonInterleaved);
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
outputParameters.device = outputDevice;
if (outputParameters.device == paNoDevice)
throw string("No ASIO Output Device Detected");
outputParameters.channelCount = config.numOutputChannels;
outputParameters.sampleFormat = OUTPUT_FORMAT | (config.isOutputInterleaved ? 0 : paNonInterleaved);
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
Backing::SampleRate(srate);
if(recordRaw)
{
stringstream fname;
fname << "rec " << sizeof(OUTPUT_SAMPLE)*8 << "bit "
<< srate << "hz mono.raw";
outfile = fopen(fname.str().c_str(), "wb");
}
err = Pa_OpenStream(
&stream,
&inputParameters,
&outputParameters,
srate,
config.framesPerCallback, /* frames per buffer */
paClipOff, /* we won't output out of range samples so don't bother clipping them */
wireCallback,
NULL );
if( err != paNoError )
throw string("Failed to open stream");
err = Pa_StartStream( stream );
}
// mp3 playback
int Audio::openMusic(string n)
{
if(!initialized)
return -1;
music = n;
back.Open(n);
playing = false;
return !back.Valid();
}
int Audio::playMusic(string n)
{
if(n.length())
{
openMusic(n);
}
if(back.Valid())
playing = true;
return !back.Valid();
}
void Audio::pauseMusic()
{
playing = false;
}
void Audio::advanceMusic(float s)
{
if(!recordRaw)
back.Advance(s);
}
float Audio::timeMusic()
{
return back.GetTime();
}
void Audio::stopMusic()
{
playing = false;
back.Close();
}