Created
December 10, 2016 22:36
-
-
Save mayakraft/236a776f507e18024a65739a42ef0317 to your computer and use it in GitHub Desktop.
OpenFrameworks record audio 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
#include "ofApp.h" | |
//-------------------------------------------------------------- | |
void ofApp::setup(){ | |
ofSetVerticalSync(true); | |
soundStreamInput.printDeviceList(); | |
bufferSize = 256; | |
soundStreamInput.setup(this, 0, 1, 44100, bufferSize, 4); | |
soundStreamOutput.setup(this, 2, 0, 44100, bufferSize, 4); | |
//if you want to set a different device id | |
// soundStreamInput.setDeviceID(2); //bear in mind the device id corresponds to all audio devices, including input-only and output-only devices. | |
// soundStreamOutput.setDeviceID(2); //bear in mind the device id corresponds to all audio devices, including input-only and output-only devices. | |
playingBufferOffset = 0; | |
recordingBufferOffset = 0; | |
soundStreamOutput.stop(); | |
soundStreamInput.stop(); | |
} | |
//-------------------------------------------------------------- | |
void ofApp::update(){ | |
if(playingBufferOffset * bufferSize >= SAMPLE_LENGTH - bufferSize){ | |
isPlaying = false; | |
soundStreamOutput.stop(); | |
} | |
if(recordingBufferOffset * bufferSize >= SAMPLE_LENGTH - bufferSize){ | |
isRecording = false; | |
soundStreamInput.stop(); | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::draw(){ | |
ofSetColor(225); | |
if(isPlaying){ | |
ofDrawBitmapString("PLAYING", 31, 92); | |
} | |
if(isRecording){ | |
ofDrawBitmapString("RECORDING", 31, 130); | |
} | |
string reportString = "recordBuffer: "+ofToString(recordingBufferOffset)+"\nplayBuffer: "+ofToString(playingBufferOffset); | |
ofDrawBitmapString(reportString, 32, 200); | |
} | |
//-------------------------------------------------------------- | |
void ofApp::audioIn(float * input, int bufferSize, int nChannels){ | |
if(isRecording){ | |
for (int i = 0; i < bufferSize; i++){ | |
recording[i+recordingBufferOffset*bufferSize] = input[i]; | |
} | |
recordingBufferOffset++; | |
} | |
} | |
void ofApp::audioOut(float * output, int bufferSize, int nChannels){ | |
if (isPlaying) { | |
for (int i = 0; i < bufferSize; i++){ | |
output[ i*2+0 ] = recording[i + playingBufferOffset*bufferSize]; | |
output[ i*2+1 ] = recording[i + playingBufferOffset*bufferSize]; | |
} | |
playingBufferOffset++; | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::keyPressed (int key){ | |
if( key == 'r' ){ | |
if(isRecording) { | |
isRecording = false; | |
soundStreamInput.stop(); | |
} | |
else{ | |
isRecording = true; | |
soundStreamInput.start(); | |
recordingBufferOffset = 0; | |
} | |
} | |
if( key == 'p' ){ | |
if(isPlaying) { | |
isPlaying = false; | |
soundStreamOutput.stop(); | |
} | |
else{ | |
isPlaying = true; | |
soundStreamOutput.start(); | |
playingBufferOffset = 0; | |
} | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::keyReleased(int key){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseMoved(int x, int y ){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseDragged(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mousePressed(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseReleased(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseEntered(int x, int y){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseExited(int x, int y){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::windowResized(int w, int h){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::gotMessage(ofMessage msg){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::dragEvent(ofDragInfo dragInfo){ | |
} | |
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
#pragma once | |
#include "ofMain.h" | |
#define SAMPLE_LENGTH 441000 // ten seconds | |
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(float * input, int bufferSize, int nChannels); | |
void audioOut(float * output, int bufferSize, int nChannels); | |
ofSoundStream soundStreamInput; | |
ofSoundStream soundStreamOutput; | |
float recording[SAMPLE_LENGTH]; | |
int recordingBufferOffset; | |
int playingBufferOffset; | |
bool isPlaying; | |
bool isRecording; | |
int bufferSize; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment