how to generate a sine wave in a buffer and play the wave from the buffer
I would:
img.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_COLOR);
ofPixels& data = img.getPixels();
int yOffset = ofGetHeight()/2;
for(int x = 0; x < ofGetWidth(); x++){
float sinedY = sin(x*0.05) * 20;
float y = yOffset + sinedY;
data.setColor(x, y, ofColor(255,255,255));
}
img.update();
//then
img.draw(0,0);
ok but I did not mean to draw the wave, but to generate a sine wave in a buffer and play the sound from the buffer
Hi!
Examples -> sounds -> audioOutputExample does something like that.
They don’t use a buffer there, but call sin() instead.
You can fill a wavetable-buffer in setup() like this:
for(int i=0; i<BUFSIZE; i++){
buffer[i] = sin((float)i/BUFSIZE*TWO_PI);
cout << buffer[i] << endl;
}
In the audioOut function you can read the data out of the wavetable-buffer to the lAudio and rAudio buffers, like this:
float freq = 200.;
float readPosIncrement = (1./(sampleRate / (BUFSIZE * freq)));
void ofApp::audioOut(float * output, int bufferSize, int nChannels){
for (int i = 0; i < bufferSize; i++){
readPos += readPosIncrement;
if(readPos >= BUFSIZE)
readPos = readPos - BUFSIZE;
float sample = buffer[(int)readPos];
lAudio[i] = output[i*nChannels ] = sample * 0.5;
rAudio[i] = output[i*nChannels + 1] = sample * 0.5;
}
}
Something like this… Hope it gets you started!