Created
May 10, 2020 14:17
-
-
Save moebiussurfing/f8d6421f033d2510cca6c2c642993d86 to your computer and use it in GitHub Desktop.
openFrameworks / soundStream: audioIn / audioOut from maximilian example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//.h | |
// For drawing | |
float waveform[4096]; //make this bigger, just in case | |
int waveIndex; | |
ofSoundStream soundStream; | |
/* ofxMaxi*/ | |
void audioIn(ofSoundBuffer& input) override; // not used in this example | |
void audioOut(ofSoundBuffer& output) override; | |
maxiOsc oscillator1; | |
//-------------------------------------------------------------- | |
void ofApp::audioIn(ofSoundBuffer& input){ | |
std::size_t nChannels = input.getNumChannels(); | |
for (size_t i = 0; i < input.getNumFrames(); i++) | |
{ | |
// handle input here | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::audioOut(ofSoundBuffer& output){ | |
std::size_t outChannels = output.getNumChannels(); | |
for (int i = 0; i < output.getNumFrames(); ++i){ | |
output[i * outChannels] = oscillator1.square(532) * 0.5; | |
output[i * outChannels + 1] = output[i * outChannels]; | |
//Hold the values so the draw method can draw them | |
waveform[waveIndex] = output[i * outChannels]; | |
if (waveIndex < (ofGetWidth() - 1)) { | |
++waveIndex; | |
} else { | |
waveIndex = 0; | |
} | |
} | |
} | |
//------------------------------------------------ | |
void ofApp::setup(){ | |
//Initialize the drawing variables | |
for (int i = 0; i < ofGetWidth(); ++i) { | |
waveform[i] = 0; | |
} | |
waveIndex = 0; | |
// Maximilian audio stuff | |
int sampleRate = 44100; /* Sampling Rate */ | |
int bufferSize= 512; /* Buffer Size. you have to fill this buffer with sound using the for loop in the audioOut method */ | |
ofxMaxiSettings::setup(sampleRate, 2, bufferSize); | |
// Setup ofSound | |
ofSoundStreamSettings settings; | |
settings.setOutListener(this); | |
settings.sampleRate = sampleRate; | |
settings.numOutputChannels = 2; | |
settings.numInputChannels = 0; | |
settings.bufferSize = bufferSize; | |
soundStream.setup(settings); | |
} | |
//-------------------------------------------------------------- | |
void ofApp::draw(){ | |
/////////////// waveform | |
ofTranslate(0, ofGetHeight()/2); | |
ofSetColor(0, 255, 0); | |
ofFill(); | |
ofDrawLine(0, 0, 1, waveform[1] * ofGetHeight()/2.); //first line | |
for(int i = 1; i < (ofGetWidth() - 1); ++i) { | |
ofDrawLine(i, waveform[i] * ofGetHeight()/2., i + 1, waveform[i+1] * ofGetHeight()/2.); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment