Commit f414d8d3 authored by pbethge's avatar pbethge
Browse files

convert muti to mono channel

parent 13e034bb
......@@ -64,15 +64,17 @@ void ofApp::setup() {
ofLogWarning(PACKAGE) << "audio input device does not have enough input channels";
inputChannel = 0;
}
ofLogNotice(PACKAGE) << "audio input channel: " << inputChannel+1;
numInputChannels = inputChannel + 1;
recordedSamplesPerBuffer = bufferSize * numInputChannels;
ofLogNotice(PACKAGE) << "audio input channel: " << numInputChannels;
ofLogNotice(PACKAGE) << "audio input samplerate: " << sampleRate;
ofLogNotice(PACKAGE) << "audio input buffer size: " << bufferSize;
ofLogNotice(PACKAGE) << "audio input buffer size: " << recordedSamplesPerBuffer;
settings.setInDevice(device);
settings.setInListener(this);
settings.sampleRate = sampleRate;
settings.numOutputChannels = 0;
settings.numInputChannels = inputChannel+1;
settings.bufferSize = bufferSize;
settings.numInputChannels = numInputChannels;
settings.bufferSize = bufferSize*numInputChannels;
if(!soundStream.setup(settings)) {
ofLogError(PACKAGE) << "audio input device " << inputDevice << " setup failed";
ofLogError(PACKAGE) << "perhaps try a different device or samplerate?";
......@@ -254,16 +256,14 @@ void ofApp::exit() {
//--------------------------------------------------------------
void ofApp::audioIn(ofSoundBuffer & input) {
// this shouldn't happen... but we don't let it blow up
if(input.getNumFrames() != monoBuffer.size()) {
ofLogWarning(PACKAGE) << "resizing mono input buffer to " << input.getNumFrames();
monoBuffer.resize(input.getNumFrames());
}
// 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 + inputChannel];
float sum = 0;
for(std::size_t j = 0; j < numInputChannels; j++) {
sum = input[i*numInputChannels + j];
}
monoBuffer[i] = sum / numInputChannels;
}
// calculate the root mean square which is a rough way to calculate volume
......
......@@ -69,7 +69,6 @@ class ofApp : public ofBaseApp {
ofSoundStream soundStream;
int inputDevice = -1; // -1 means search for default device
int inputChannel = 0; // 0 means mono, 1 means stereo
std::vector<float> monoBuffer; //< mono inputChannel stream buffer
bool listening = true;
// neural network input parameters
......@@ -80,8 +79,9 @@ class ofApp : public ofBaseApp {
std::size_t bufferSize = 1024;
std::size_t sampleRate = 48000;
std::size_t downsamplingFactor = 3;
std::size_t recordedSamplesPerBuffer;
std::size_t numInputChannels;
static const std::size_t modelSampleRate; //< sample rate expected by model
// since volume detection has some latency we keep a history of buffers
AudioBufferFifo previousBuffers;
......@@ -89,6 +89,7 @@ class ofApp : public ofBaseApp {
// sampleBuffers acts as a buffer for recording (could be fused)
AudioBufferFifo sampleBuffers;
std::size_t numBuffers;
SimpleAudioBuffer monoBuffer; //< mono inputChannel stream buffer
// volume
float curVol = 0.0;
......@@ -104,9 +105,9 @@ class ofApp : public ofBaseApp {
AudioClassifier model;
cppflow::tensor output;
std::size_t inputSeconds = 5;
const std::size_t inputSamplingRate = 16000; // AI was trained on 16kHz
std::size_t inputSize;
float minConfidence = 0.75;
static const std::size_t modelSampleRate; //< sample rate expected by model
// neural network control logic
std::size_t recordingCounter = 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