Skip to content

Commit

Permalink
iOS examples - audioInputExample update (#7290)
Browse files Browse the repository at this point in the history
#changelog #ios
  • Loading branch information
dimitre authored Jan 31, 2023
1 parent ca50df8 commit 72a6ea3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 41 deletions.
9 changes: 5 additions & 4 deletions examples/ios/audioInputExample/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ class ofApp : public ofxiOSApp{
void gotMemoryWarning();
void deviceOrientationChanged(int newOrientation);

void audioIn(float * input, int bufferSize, int nChannels);
void audioIn(ofSoundBuffer & input);

int initialBufferSize;
int sampleRate;
int bufferSize = 512;
int drawCounter;
int bufferCounter;
float * buffer;
float buffer[512] = { 0 };

ofSoundPlayer sound;
ofSoundStream soundStream;

};

62 changes: 25 additions & 37 deletions examples/ios/audioInputExample/src/ofApp.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,29 @@

#include "ofApp.h"

// IMPORTANT!!! if your sound doesn't work in the simulator
// read this post => http://www.cocos2d-iphone.org/forum/topic/4159
// which requires you set the input stream to 24bit!!

//--------------------------------------------------------------
void ofApp::setup(){
ofSetFrameRate(60);
ofBackground(255);
ofSetOrientation(OF_ORIENTATION_90_RIGHT);//Set iOS to Orientation Landscape Right

//for some reason on the iphone simulator 256 doesn't work - it comes in as 512!
//so we do 512 - otherwise we crash
initialBufferSize = 512;
sampleRate = 44100;
drawCounter = 0;
bufferCounter = 0;

buffer = new float[initialBufferSize];
memset(buffer, 0, initialBufferSize * sizeof(float));

// 0 output channels,
// 1 input channels
// 44100 samples per second
// 512 samples per buffer
// 1 buffer
ofSoundStreamSetup(0, 1, this, sampleRate, initialBufferSize, 1);


sound.load("sounds/beat.caf");
sound.setLoop(true);
sound.play();
sound.setVolume(0);

ofSoundStreamSettings settings;
settings.setInListener(this);
settings.sampleRate = 44100;
settings.numOutputChannels = 0;
settings.numInputChannels = 1;
settings.numBuffers = 1;
settings.bufferSize = bufferSize;
soundStream.setup(settings);

}

//--------------------------------------------------------------
Expand All @@ -51,8 +43,8 @@
float y1 = ofGetHeight() * 0.5;
ofDrawLine(0, y1, ofGetWidth(), y1);

for(int i=0; i<initialBufferSize; i++){
float p = i / (float)(initialBufferSize-1);
for(int i=0; i<bufferSize; i++){
float p = i / (float)(bufferSize-1);
float x = p * ofGetWidth();
float y2 = y1 + buffer[i] * 200;

Expand All @@ -69,38 +61,34 @@
}

//--------------------------------------------------------------
void ofApp::exit(){
//
}

//--------------------------------------------------------------
void ofApp::audioIn(float * input, int bufferSize, int nChannels){
if(initialBufferSize < bufferSize){
ofLog(OF_LOG_ERROR, "your buffer size was set to %i - but the stream needs a buffer size of %i", initialBufferSize, bufferSize);
}

int minBufferSize = MIN(initialBufferSize, bufferSize);
for(int i=0; i<minBufferSize; i++) {
void ofApp::audioIn(ofSoundBuffer & input){
for(int i=0; i<input.getNumFrames(); i++) {
buffer[i] = input[i];
}

bufferCounter++;
}

//--------------------------------------------------------------
void ofApp::touchDown(ofTouchEventArgs & touch){
sound.setVolume(1.0);
void ofApp::exit(){
//
}

//--------------------------------------------------------------
void ofApp::touchMoved(ofTouchEventArgs & touch){

void ofApp::touchDown(ofTouchEventArgs & touch){
sound.setVolume(1.0);
}

//--------------------------------------------------------------
void ofApp::touchUp(ofTouchEventArgs & touch){
sound.setVolume(0.0);
}

//--------------------------------------------------------------
void ofApp::touchMoved(ofTouchEventArgs & touch){

}

//--------------------------------------------------------------
void ofApp::touchDoubleTap(ofTouchEventArgs & touch){

Expand Down

0 comments on commit 72a6ea3

Please sign in to comment.