Skip to content

Commit 684d7cd

Browse files
tctrdanoli3
authored andcommitted
Android example : made ofSoundStream example WORK
android example : finally made the audio input work by adding the AUDIO_RECORD permission in the Manifest (cherry picked from commit 17b913a)
1 parent db2ca99 commit 684d7cd

3 files changed

Lines changed: 126 additions & 10 deletions

File tree

examples/android/androidEmptyExample/ofApp/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1010
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
1111
<uses-permission android:name="android.permission.BLUETOOTH" />
12+
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
1213

1314

1415
<uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
@@ -25,7 +26,8 @@
2526
<uses-feature android:glEsVersion="0x00020000"/>
2627

2728
<!-- Audio Systems -->
28-
<uses-feature android:name="android.hardware.audio.output" android:required="true" />
29+
<uses-feature android:name="android.hardware.audio.output" android:required="false" />
30+
<uses-feature android:name="android.hardware.audio.microphone" android:required="false" />
2931
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
3032

3133
<supports-screens android:resizeable="false"

examples/android/androidEmptyExample/ofApp/src/main/cpp/ofApp.cpp

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ void ofApp::setup(){
77
ofSetVerticalSync(false);
88
ofEnableAlphaBlending();
99

10+
// ofSetOrientation(OF_ORIENTATION_90_LEFT);
11+
12+
// SHADERS
1013
bool loadok = font.load("verdana.ttf", 200, true, false, true, 0.4, 72);
1114
shader.load("shaders/noise.vert", "shaders/noise.frag");
1215

@@ -29,8 +32,7 @@ void ofApp::setup(){
2932
doShader = false;
3033

3134

32-
// SOUND
33-
// ofSetOrientation(OF_ORIENTATION_90_LEFT);
35+
// SOUNDPLAYER
3436
loadok = synth.loadSound("sounds/synth.wav");
3537
loadok = beats.loadSound("sounds/1085.mp3");
3638
loadok = vocals.loadSound("sounds/Violet.mp3");
@@ -42,6 +44,37 @@ void ofApp::setup(){
4244
vocals.setMultiPlay(true);
4345

4446

47+
// SOUNDSTREAM
48+
49+
// Ask for permission to record audio,
50+
// not needed if no in channels used
51+
ofxAndroidRequestPermission(OFX_ANDROID_PERMISSION_RECORD_AUDIO);
52+
bool ok = ofxAndroidCheckPermission(OFX_ANDROID_PERMISSION_RECORD_AUDIO);
53+
54+
sampleRate = 44100;
55+
phase = 0;
56+
phaseAdder = 0.0f;
57+
phaseAdderTarget = 0.0f;
58+
volume = 0.1f;
59+
bNoise = false;
60+
initialBufferSize = 256;
61+
62+
lAudio = new float[initialBufferSize];
63+
rAudio = new float[initialBufferSize];
64+
65+
memset(lAudio, 0, initialBufferSize * sizeof(float));
66+
memset(rAudio, 0, initialBufferSize * sizeof(float));
67+
68+
69+
ofSoundStreamSettings settings;
70+
settings.setOutListener(this);
71+
settings.setInListener(this);
72+
settings.numOutputChannels = 2;
73+
settings.numInputChannels = 2;
74+
settings.numBuffers = 4;
75+
settings.bufferSize = initialBufferSize;
76+
soundStream.setup(settings);
77+
4578
}
4679

4780
void ofApp::exit(){
@@ -51,7 +84,7 @@ void ofApp::exit(){
5184
//--------------------------------------------------------------
5285
void ofApp::update(){
5386
// update the sound playing system:
54-
ofSoundUpdate();
87+
//ofSoundUpdate();
5588
}
5689

5790
//--------------------------------------------------------------
@@ -75,12 +108,28 @@ void ofApp::draw(){
75108

76109
}
77110

78-
//finally draw our text
79-
text.draw();
80111

81-
if( doShader ){
82-
shader.end();
83-
}
112+
// draw the SoudStream audio waves
113+
// draw the left:
114+
ofBeginShape();
115+
for (int i = 0; i < initialBufferSize; i++){
116+
ofVertex(20+i*10,ofGetHeight() / 2 - 250 + lAudio[i]*500.0f);
117+
}
118+
ofEndShape(false);
119+
120+
ofBeginShape();
121+
for (int i = 0; i < initialBufferSize; i++){
122+
ofVertex(20+i*10,ofGetHeight() / 2 + 250 + rAudio[i]*500.0f);
123+
}
124+
ofEndShape(false);
125+
126+
//finally draw our text
127+
text.draw();
128+
129+
if( doShader ){
130+
shader.end();
131+
}
132+
84133
}
85134

86135
//--------------------------------------------------------------
@@ -127,11 +176,18 @@ void ofApp::touchMoved(int x, int y, int id){
127176
if (x >= widthStep && x < widthStep*2){
128177
beats.setSpeed( 0.5f + ((float)(ofGetHeight() - y) / (float)ofGetHeight())*1.0f);
129178
}
179+
180+
int width = ofGetWidth();
181+
pan = (float)x / (float)width;
182+
float height = (float)ofGetHeight();
183+
float heightPct = ((height-y) / height);
184+
targetFrequency = 2000.0f * heightPct;
185+
phaseAdderTarget = (targetFrequency / (float) sampleRate) * TWO_PI;
130186
}
131187

132188
//--------------------------------------------------------------
133189
void ofApp::touchUp(int x, int y, int id){
134-
doShader = false;
190+
//doShader = false;
135191

136192
}
137193

@@ -185,6 +241,42 @@ void ofApp::cancelPressed(){
185241

186242
}
187243

244+
void ofApp::audioOut(ofSoundBuffer & buffer){
245+
//pan = 0.5f;
246+
float leftScale = 1 - pan;
247+
float rightScale = pan;
248+
249+
// sin (n) seems to have trouble when n is very large, so we
250+
// keep phase in the range of 0-TWO_PI like this:
251+
while (phase > TWO_PI){
252+
phase -= TWO_PI;
253+
}
254+
255+
if ( bNoise == true){
256+
// ---------------------- noise --------------
257+
for (int i = 0; i < buffer.getNumFrames(); i++){
258+
lAudio[i] = buffer.getSample(i, 0) = ofRandomf() * volume * leftScale;
259+
rAudio[i] = buffer.getSample(i, 1) = ofRandomf() * volume * rightScale;
260+
}
261+
} else {
262+
263+
for (int i = 0; i < buffer.getNumFrames(); i++){
264+
phaseAdder = 0.6f * phaseAdder + 0.4f * phaseAdderTarget;
265+
phase += phaseAdder;
266+
float sample = sin(phase);
267+
lAudio[i%256] = buffer.getSample(i, 0) = sample * volume * leftScale;
268+
buffer.getSample(i, 1) = sample * volume * rightScale;
269+
}
270+
}
271+
}
272+
273+
274+
void ofApp::audioIn(ofSoundBuffer & buffer){
275+
for (int i = 0; i < buffer.getNumFrames(); i++){
276+
rAudio[i%256] = buffer.getSample(i, 0) + buffer.getSample(i, 1) ;
277+
}
278+
}
279+
188280
void ofApp::deviceRefreshRateChanged(int refreshRate) {
189281

190282
}

examples/android/androidEmptyExample/ofApp/src/main/cpp/ofApp.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "ofMain.h"
44
#include "ofxAndroid.h"
5+
#include <map>
56

67
class ofApp : public ofxAndroidApp{
78

@@ -37,12 +38,33 @@ class ofApp : public ofxAndroidApp{
3738
void deviceRefreshRateChangedEvent(int &refreshRate);
3839
void deviceHighestRefreshRateChangedEvent(int & refreshRate);
3940

41+
// Using Shaders and fonts
4042
ofPath text;
4143
ofTrueTypeFont font;
4244
ofShader shader;
4345
bool doShader;
4446

47+
// Using ofSoundPlayer
4548
ofSoundPlayer beats;
4649
ofSoundPlayer synth;
4750
ofSoundPlayer vocals;
51+
52+
// Using ofSoundStream
53+
void audioOut(ofSoundBuffer & buffer);
54+
void audioIn(ofSoundBuffer & buffer);
55+
56+
float pan;
57+
int sampleRate;
58+
bool bNoise;
59+
float volume;
60+
int initialBufferSize;
61+
float * lAudio;
62+
float * rAudio;
63+
// For the simple sine wave synthesis
64+
float targetFrequency;
65+
float phase;
66+
float phaseAdder;
67+
float phaseAdderTarget;
68+
69+
ofSoundStream soundStream;
4870
};

0 commit comments

Comments
 (0)