@GusCarr wrote:
Hello dear people:
I’m facing an issue. Based on a post by the user @ayruos, I wrote a class to accept and generate audio. I noticed that the way of calling the audioIn method (“audioIn(float * input, int bufferSize, int nChannels)”) was deprecated, so I tried to use the current call: “audioIn(ofSoundBuffer & buffer)”. Well, the current format does not seem to call that method. I’ve put an inBufferCounter variable inside that method to check if it’s being called. That is the number shown on the screen.
This program shows a couple of sinewaves which can be modulated in frequency by the incoming audio. The amount of modulation is set with the corresponding slider.
Here is the code, as small as I could put it to show this issue. Notice that commenting and uncommenting the corresponding lines one can switch versions from current to “legacy”, so to speak. The current way of calling the method is uncommented, so it means that the sines are audible and drawn but no modulation is heard or shown. The audio is routed to/from a Lexicon Omega using jack.
Am I missing anything, maybe?// ofApp.h #pragma once #include "ofMain.h" #include "ofxGui.h" #include "Voice.hpp" class ofApp : public ofBaseApp{ public: void setup(); void update(); void draw(); void keyPressed(int key); void keyReleased(int key); void mouseMoved(int x, int y ); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); void mouseEntered(int x, int y); void mouseExited(int x, int y); void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); void audioIn(ofSoundBuffer & buffer); // to comment out in legacy version. void audioOut(ofSoundBuffer & buffer); // to comment out in legacy version. //~ void audioIn(float * input, int bufferSize, int nChannels); // to uncomment in legacy version. //~ void audioOut(float * output, int bufferSeize, int nChannels); // to uncomment in legacy version. unsigned int bufferSize; unsigned int nChannels; unsigned int sampleRate; unsigned int inBufferCounter; Voice voiceObject0; Voice voiceObject1; vector<float> audioBuffer; ofxPanel gui0; ofxPanel gui1; ofParameterGroup parameters0; ofParameterGroup parameters1; ofSoundStream soundStream; }; // eof.- ofApp.h
// ofApp.cpp #include "ofApp.h" #include "ofMain.h" #include "ofxGui.h" //-------------------------------------------------------------- void ofApp::setup(){ ofSetBackgroundColor(25); bufferSize = 256; nChannels = 2; sampleRate = 48000; audioBuffer.resize(2 * bufferSize); voiceObject0.setup("Voice0", bufferSize, sampleRate, 0.5); voiceObject1.setup("Voice1", bufferSize, sampleRate, 0.75); parameters0.add(voiceObject0.parameters); gui0.setup(parameters0); gui0.setPosition(10,10); parameters1.add(voiceObject1.parameters); gui1.setup(parameters1); gui1.setPosition(230,10); //~ ofSoundStreamSetup(2, 2, sampleRate, bufferSize, 4); // to uncomment in legacy version. ////// ============= ofSoundStreamSettings settings; // to comment out in legacy version. settings.setOutListener(this); // to comment out in legacy version. settings.sampleRate = sampleRate; // to comment out in legacy version. settings.numInputChannels = 2; // to comment out in legacy version. settings.numOutputChannels = 2; // to comment out in legacy version. settings.bufferSize = bufferSize; // to comment out in legacy version. soundStream.setup(settings); // to comment out in legacy version. ////// ============= inBufferCounter = 0; } //-------------------------------------------------------------- void ofApp::update(){ voiceObject0.update(); voiceObject1.update(); } //-------------------------------------------------------------- void ofApp::draw(){ gui0.draw(); gui1.draw(); voiceObject0.draw(); voiceObject1.draw(); ofDrawBitmapString(to_string(inBufferCounter), 50, ofGetHeight()-50); } //-------------------------------------------------------------- void ofApp::audioIn(ofSoundBuffer & buffer){ voiceObject0.audioInput(buffer); voiceObject1.audioInput(buffer); inBufferCounter++; } //-------------------------------------------------------------- void ofApp::audioOut(ofSoundBuffer & buffer){ // to comment out in legacy version. //~ void ofApp::audioOut(float * buffer, int bufferSize, int nChannels){ // to uncomment in legacy version. double audioL, audioR; unsigned int bSize, nChan; bSize = buffer.getNumFrames(); // to comment out in legacy version. nChan = buffer.getNumChannels(); // to comment out in legacy version. //~ bSize = bufferSize; //~ nChan = nChannels; voiceObject0.audioOutput(bSize, nChan); // process internal buffer audioBufferOut. voiceObject1.audioOutput(bSize, nChan); // process internal buffer audioBufferOut. for(unsigned int i = 0; i<bufferSize; i++) { audioL = 0.0; audioR = 0.0; audioL += voiceObject0.audioBufferOut[2*i]; audioR += voiceObject0.audioBufferOut[2*i+1]; audioL += voiceObject1.audioBufferOut[2*i]; audioR += voiceObject1.audioBufferOut[2*i+1]; buffer[i*nChan ] = audioL * 0.25; buffer[i*nChan + 1] = audioR * 0.25; } } // only non empty methods were shown. // eof.- App.cpp
// Voice.hpp #include "ofMain.h" #include "ofxGui.h" class Voice { public: void setup(string name, int bufferSize, int sampleRate, float vOffset); void update(); void draw(); //~ void audioInput(float *input, int bufferSize, int nChannels); void audioInput(ofSoundBuffer & buffer); //~ void audioInput(); //int bufferSize, int nChannels); void audioOutput(int bufferSize, int nChannels); int sampleRate; int bufferSize; vector<float> drawBuffer; vector<float> audioBuffer; vector<float> audioBufferOut; double pseudoTime, out; float vOffset; double fl; double L0; double fr; double R0; double phaseL; double phaseR; double fbckInL; double fbckInR; ofParameterGroup parameters; ofParameter<float> rawLevel; ofParameter<float> amplitude; ofParameter<float> fmod; ofParameter<float> pitch; }; // eof.- Voice.hpp
// Voice.cpp #include "Voice.hpp" #include <iostream> #include "ofxGui.h" //-------------------------------------------------------------- void Voice::setup(string name, int bufferSizeIn, int sampleRateIn, float vOffsetIn){ bufferSize = bufferSizeIn; sampleRate = sampleRateIn; pseudoTime = TWO_PI / (double)sampleRate; parameters.setName(name); parameters.add(rawLevel.set("RawLevel",0.1,0.,1.)); parameters.add(pitch.set("Pitch",55.,0.,440.)); parameters.add(fmod.set("Fmod",1000.0,0.0,12000.)); parameters.add(amplitude.set("Amplitude",0.1,0.,1.)); fbckInL = 0.0; // amount of incoming audio to modulate sine (left). fbckInR = 0.0; // amount of incoming audio to modulate sine (right). audioBuffer.resize(2 * bufferSize); audioBufferOut.resize(2 * bufferSize); drawBuffer.resize(2*bufferSizeIn); vOffset = vOffsetIn; // offset in fraction of screen height, for wave display only. } //-------------------------------------------------------------- void Voice::update(){ } //-------------------------------------------------------------- void Voice::draw(){ float spacing = ofGetWidth()/256.0; for(unsigned int i = 0; i<drawBuffer.size()-1; i++){ ofDrawLine(i*spacing+5, ofGetHeight()*vOffset, i*spacing+5, ofGetHeight()*vOffset + drawBuffer[i]*256); } } //-------------------------------------------------------------- //~ void Voice::audioInput(float *buffer, int bufferSize, int nChannels){ // to uncomment in legacy version. void Voice::audioInput(ofSoundBuffer & buffer){ // to comment out in legacy version. double inL, inR; for(int i = 0; i<buffer.getNumFrames(); i++){ //~ for(int i = 0; i<bufferSize; i++){ inL = buffer[i*2]; inR = buffer[i*2+1]; audioBuffer[2*i] = inL; audioBuffer[2*i+1] = inR; } } //-------------------------------------------------------------- //~ void Voice::audioOutput(float *output, int bufferSize, int nChannels) // to uncomment in legacy version. void Voice::audioOutput(int bufferSize, int nChannels){ // to comment out in legacy version. int counter = 0; // Calculates vector of values for current new target value for each parameter and current block of samples. for(unsigned int i = 0; i<bufferSize; i++) { fbckInL = audioBuffer[2*i]; fbckInR = audioBuffer[2*i+1]; fl = pitch + fmod * fbckInL; fr = pitch + fmod * fbckInR; phaseL += fl * pseudoTime; if (phaseL > TWO_PI) {phaseL -= TWO_PI;} if (phaseL < TWO_PI) {phaseL += TWO_PI;} L0 = rawLevel * sin(phaseL); phaseR += fr * pseudoTime; if (phaseR > TWO_PI) {phaseR -= TWO_PI;} if (phaseR < TWO_PI) {phaseR += TWO_PI;} R0 = rawLevel * sin(phaseR); audioBufferOut[i * 2] = L0 * amplitude; audioBufferOut[i * 2 + 1] = R0 * amplitude; // ==== Wave drawing data ==== drawBuffer[counter%drawBuffer.size()] = audioBufferOut[i * 2]; //output[i * 2]; drawBuffer[counter%drawBuffer.size()+1] = audioBufferOut[i * 2 + 1]; //output[i * 2 + 1]; counter++; // ==== end of Wave drawing data ==== } } // eof.- Voice.cpp
Thank you very much for your assistance and apologies for the length of the post.
Best regards.Gus
Posts: 1
Participants: 1