@@ -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
4780void ofApp::exit (){
@@ -51,7 +84,7 @@ void ofApp::exit(){
5184// --------------------------------------------------------------
5285void 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// --------------------------------------------------------------
133189void 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+
188280void ofApp::deviceRefreshRateChanged (int refreshRate) {
189281
190282}
0 commit comments