-
Notifications
You must be signed in to change notification settings - Fork 3
/
audio_helper.cpp
192 lines (164 loc) · 5.95 KB
/
audio_helper.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
#include "audio_helper.h"
#include "Visualizer.h"
#include "ArgMapper.h"
#include <string.h>
#include <fftw3.h>
#include <map>
#include <string>
extern Packet *sharedBuffer;
extern ArgMapper mapper;
SF_Container sf;
WindowType wt;
bool finished;
//return a PaStreamParameters for use with startAudio()
//use true for input, false for output
PaStreamParameters getStreamParams(bool output){
PaStreamParameters params;
PaError error;
params.device = (output) ? Pa_GetDefaultOutputDevice() : Pa_GetDefaultInputDevice();
params.channelCount = (output) ? OUT_CHANNELS : IN_CHANNELS;
params.sampleFormat = paFloat32;
if (output)
params.suggestedLatency = Pa_GetDeviceInfo(params.device)->defaultLowOutputLatency;
else params.suggestedLatency = Pa_GetDeviceInfo(params.device)->defaultLowInputLatency;
params.hostApiSpecificStreamInfo = NULL;
return params;
}
static int paCallback( const void *inputBuffer,
void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData)
{
int i, j, bufferIndex;
float *out = (float*) outputBuffer, *in = (float*) inputBuffer, sample;
float fileBuffer[framesPerBuffer*PAC_CHANNELS];
static int order = 0;
//search through the shared buffer for free packet
for (i=0, bufferIndex=0; i<BUFFER_SIZE; i++, bufferIndex++){
//if free packet found, break loop
if (sharedBuffer[i].free) break;
//if we're on the last packet and none are free, return
else if (i>=BUFFER_SIZE) return paContinue;
}
//set and increment order
sharedBuffer[bufferIndex].order = order;
order++;
//get samples from sound file
int readcount = framesPerBuffer;
if (!inputBuffer) readcount = sf_readf_float(sf.file, fileBuffer, framesPerBuffer);
//fill buffer with samples from sound file
for (i=0; i<framesPerBuffer; i++){
for (j=0; j<PAC_CHANNELS; j++){
//send sample to output buffer
if (!inputBuffer){ //from sound file
if (sf.info.channels == STEREO) sample = fileBuffer[STEREO*i+j];
else if (sf.info.channels == MONO) sample = fileBuffer[i];
}
else{ // from mic
if (IN_CHANNELS == STEREO) sample = in[STEREO*i + j];
else if (IN_CHANNELS == MONO) sample = in[i];
}
out[STEREO*i + j] = sample;
//window and send sample to shared buffer
sharedBuffer[bufferIndex].averageAmp += (float)sample;
if (wt != Rect){
sample = window(sample, i, framesPerBuffer, wt);
}
sharedBuffer[bufferIndex].frames[i][j] = sample;
}
}
sharedBuffer[bufferIndex].averageAmp /= (float)framesPerBuffer;
sharedBuffer[bufferIndex].free = false;
//if we've reached the end of the file, end callback
if (readcount < framesPerBuffer){
finished = true;
return paComplete;
}
//else continue
return paContinue;
}
//return true if no error; else print error msg and return false
bool printError(PaError error, string msg){
if (error == paNoError) return true;
//sorry for the cout
else cout << msg << Pa_GetErrorText(error) << endl;
return false;
}
//open and start the audio stream - takes stream, callback function, and userdata
bool startAudio(PaStream *stream, const char* filename, const char* windowname){
InputSource inputSource = strcasecmp(filename, "") == 0 ? Mic : File;
int samplerate = SAMPLE_RATE; //default if no file specified
//open file if we're not using Mic
if (inputSource == File){
printf("Reading from file %s\n", filename);
if ((sf.file = sf_open(filename, SFM_READ, &sf.info) ) == NULL) {
printf("Error opening file '%s' (see manual for accepted formats)\n", filename);
return false;
}
//force audio file to be mono or stereo (for now)
if (sf.info.channels != MONO && sf.info.channels != STEREO){
printf("Error: file must be stereo or mono");
return false;
}
samplerate = sf.info.samplerate;
} else {
printf("Reading from default input\n");
}
//port audio init stuff
Pa_Initialize();
PaStreamParameters outputParams, inputParams;
outputParams = getStreamParams(true);
inputParams = getStreamParams(false);
//open the stream
PaError error;
error = Pa_OpenStream(&stream,
(inputSource == File) ? NULL : &inputParams,
&outputParams,
samplerate,
BUFFER,
paNoFlag,
paCallback,
NULL);
if (! printError(error, "PortAudio error while opening stream: ")) return false;
//get window type
WindowType windowType;
std::string win = mapper.getCompoundArg('w');
if (win == "hann") windowType = Hann;
else if (win == "hamming") windowType = Hamming;
else if (win == "cosine") windowType = Cosine;
else{
printf("Unknown window type '%s' - defaulting to Rect\n", windowname);
windowType = Rect;
}
wt = windowType;
//start the stream
error = Pa_StartStream(stream);
if (! printError(error, "PortAudio error while starting stream: ")) return false;
return true;
}
//end PaStream and close sound file
void endAudio(PaStream *stream){
Pa_StopStream(stream);
Pa_CloseStream(stream);
Pa_Terminate();
sf_close( sf.file );
}
//various window functions that take and return one sample
//(meant to be used iteratively)
float window(float sample, int index, int width, WindowType windowType){
switch (windowType){
case Hann:
sample *= .5 * (1 - cos((2*PI*index) / (width - 1)));
break;
case Hamming:
sample *= .54 - .46 * cos((2*PI*index) / (width - 1));
break;
case Cosine:
sample *= sin((PI*index) / (width - 1));
default:
break;
}
return sample;
}