Commit fb135c0a authored by Dan Wilcox's avatar Dan Wilcox
Browse files

fixed buffer size issue when using input chan > 1, removed redundant member vars

parent 98174ef5
......@@ -64,17 +64,15 @@ void ofApp::setup() {
ofLogWarning(PACKAGE) << "audio input device does not have enough input channels";
inputChannel = 0;
}
numInputChannels = inputChannel+1;
recordedSamplesPerBuffer = bufferSize * numInputChannels;
ofLogNotice(PACKAGE) << "audio input channel: " << inputChannel;
ofLogNotice(PACKAGE) << "audio input samplerate: " << sampleRate;
ofLogNotice(PACKAGE) << "audio input buffer size: " << recordedSamplesPerBuffer;
ofLogNotice(PACKAGE) << "audio input buffer size: " << bufferSize;
settings.setInDevice(device);
settings.setInListener(this);
settings.sampleRate = sampleRate;
settings.numOutputChannels = 0;
settings.numInputChannels = numInputChannels;
settings.bufferSize = recordedSamplesPerBuffer;
settings.numInputChannels = inputChannel + 1;
settings.bufferSize = bufferSize * (inputChannel + 1);
if(!soundStream.setup(settings)) {
ofLogError(PACKAGE) << "audio input device " << inputDevice << " setup failed";
ofLogError(PACKAGE) << "perhaps try a different device or samplerate?";
......@@ -84,6 +82,9 @@ void ofApp::setup() {
if(!listening) {
soundStream.stop();
}
if(soundStream.getSampleRate() == 44100) {
ofLogWarning(PACKAGE) << "treating sample rate of 44100 as 48000, may or may not affect detection";
}
// display
volHistory.assign(400, 0.0);
......@@ -255,11 +256,13 @@ void ofApp::exit() {
//--------------------------------------------------------------
void ofApp::audioIn(ofSoundBuffer & input) {
// beh, ofSoundBuffer::getNumFrames() actually returns the buffer size?
std::size_t numFrames = input.getNumFrames() / input.getNumChannels();
// copy desired channel out of interleaved stream into mono buffer,
// assume input stream has enough channels...
for(std::size_t i = 0; i < input.getNumFrames(); i++) {
monoBuffer[i] = input[i*numInputChannels+inputChannel];
for(std::size_t i = 0; i < numFrames; i++) {
monoBuffer[i] = input[(i*input.getNumChannels())+inputChannel];
}
// calculate the root mean square which is a rough way to calculate volume
......
......@@ -67,8 +67,8 @@ class ofApp : public ofBaseApp {
// audio
ofSoundStream soundStream;
int inputDevice = -1; // -1 means search for default device
int inputChannel = 0; // 0 means mono, 1 means stereo
int inputDevice = -1; // -1 means search for default device
int inputChannel = 0; // 0 - chan 1 (left), 1 - chan 2 (right), 2 - chan 3, etc
bool listening = true;
// neural network input parameters
......@@ -76,20 +76,17 @@ class ofApp : public ofBaseApp {
// we want to keep the buffersize a multiple of the downsampling factor
// downsamplingFactor = sampleRate / modelSampleRate
// downsampling is required for microphones that do not have 16kHz sampling
std::size_t bufferSize = 1024;
std::size_t bufferSize = 1024; //< in this case, number of sample frames
std::size_t sampleRate = 48000;
std::size_t downsamplingFactor = 3;
std::size_t recordedSamplesPerBuffer;
std::size_t numInputChannels;
// since volume detection has some latency we keep a history of buffers
// since volume detection has some latency,d we keep a history of buffers
AudioBufferFifo previousBuffers;
std::size_t numPreviousBuffers = 10; // how many buffers to save before trigger happens
// sampleBuffers acts as a buffer for recording (could be fused)
AudioBufferFifo sampleBuffers;
std::size_t numBuffers;
SimpleAudioBuffer monoBuffer; //< mono inputChannel stream buffer
SimpleAudioBuffer monoBuffer; //< mono inputChannel stream buffer
// volume
float curVol = 0.0;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment