aboutsummaryrefslogtreecommitdiffstats
path: root/ofxMaxim/ofMaximExample007OSX_Granular/src
diff options
context:
space:
mode:
authorMick Grierson <mickgrierson@gmail.com>2012-04-26 13:10:53 +0100
committerMick Grierson <mickgrierson@gmail.com>2012-04-26 13:10:53 +0100
commit62b43e8f32af364cbc8e46fffed2854c27533acc (patch)
treedf3847f7a649db853df547096f704dbc45cd9e96 /ofxMaxim/ofMaximExample007OSX_Granular/src
parent8c23fae3b55609bececab93003d7633f58ece083 (diff)
new oF 007 granular pitch stretch example
Diffstat (limited to 'ofxMaxim/ofMaximExample007OSX_Granular/src')
-rw-r--r--ofxMaxim/ofMaximExample007OSX_Granular/src/main.cpp16
-rw-r--r--ofxMaxim/ofMaximExample007OSX_Granular/src/testApp.cpp213
-rw-r--r--ofxMaxim/ofMaximExample007OSX_Granular/src/testApp.h57
3 files changed, 286 insertions, 0 deletions
diff --git a/ofxMaxim/ofMaximExample007OSX_Granular/src/main.cpp b/ofxMaxim/ofMaximExample007OSX_Granular/src/main.cpp
new file mode 100644
index 0000000..6a32c6a
--- /dev/null
+++ b/ofxMaxim/ofMaximExample007OSX_Granular/src/main.cpp
@@ -0,0 +1,16 @@
+#include "ofMain.h"
+#include "testApp.h"
+#include "ofAppGlutWindow.h"
+
+//========================================================================
+int main( ){
+
+ ofAppGlutWindow window;
+ ofSetupOpenGL(&window, 1024,768, OF_WINDOW); // <-------- setup the GL context
+
+ // this kicks off the running of my app
+ // can be OF_WINDOW or OF_FULLSCREEN
+ // pass in width and height too:
+ ofRunApp( new testApp());
+
+}
diff --git a/ofxMaxim/ofMaximExample007OSX_Granular/src/testApp.cpp b/ofxMaxim/ofMaximExample007OSX_Granular/src/testApp.cpp
new file mode 100644
index 0000000..0daf76d
--- /dev/null
+++ b/ofxMaxim/ofMaximExample007OSX_Granular/src/testApp.cpp
@@ -0,0 +1,213 @@
+/* This is an example of how to integrate maximilain into openFrameworks,
+ including using audio received for input and audio requested for output.
+
+
+ You can copy and paste this and use it as a starting example.
+
+ */
+
+
+#include "testApp.h"
+#include "maximilian.h"/* include the lib */
+#include "time.h"
+
+
+
+//-------------------------------------------------------------
+testApp::~testApp() {
+ delete ts, ts2;
+}
+
+
+//--------------------------------------------------------------
+void testApp::setup(){
+
+ //samples from http://freesound.org
+ samp.load(ofToDataPath("2630__Jovica__133_bpm_ATTACK_LOOP_04_electrified_analog_kit_variation_16_mono.wav"));
+ samp2.load(ofToDataPath("24620__anamorphosis__GMB_Kantilan_1.wav"));
+ samp3.load(ofToDataPath("26393__brfindla__Calango1berimbau.wav"));
+ samp4.load(ofToDataPath("68373__juskiddink__Cello_open_string_bowed.wav"));
+ samp5.load(ofToDataPath("71515__Microscopia__Wilhelm_Bruder_Sohne_Organ.wav"));
+// samp5.load(ofToDataPath("sine1sec.wav"));
+
+
+ ofEnableAlphaBlending();
+ ofSetupScreen();
+ ofBackground(0, 0, 0);
+ ofSetFrameRate(60);
+
+
+ /* This is stuff you always need.*/
+ sampleRate = 44100; /* Sampling Rate */
+ initialBufferSize = 512; /* Buffer Size. you have to fill this buffer with sound*/
+ lAudioOut = new float[initialBufferSize];/* outputs */
+ rAudioOut = new float[initialBufferSize];
+ lAudioIn = new float[initialBufferSize];/* inputs */
+ rAudioIn = new float[initialBufferSize];
+
+
+ /* This is a nice safe piece of code */
+ memset(lAudioOut, 0, initialBufferSize * sizeof(float));
+ memset(rAudioOut, 0, initialBufferSize * sizeof(float));
+
+ memset(lAudioIn, 0, initialBufferSize * sizeof(float));
+ memset(rAudioIn, 0, initialBufferSize * sizeof(float));
+
+
+
+ ts = new maxiPitchStretch<grainPlayerWin>(&samp);
+ ts2 = new maxiPitchStretch<grainPlayerWin>(&samp2);
+ ts3 = new maxiPitchStretch<grainPlayerWin>(&samp3);
+ ts4 = new maxiPitchStretch<grainPlayerWin>(&samp4);
+ ts5 = new maxiPitchStretch<grainPlayerWin>(&samp5);
+ stretches.push_back(ts);
+ stretches.push_back(ts2);
+ stretches.push_back(ts3);
+ stretches.push_back(ts4);
+ stretches.push_back(ts5);
+ speed = 1;
+ rate = 2;
+ grainLength = 0.05;
+ current=0;
+
+ fft.setup(1024, 512, 256);
+ oct.setup(44100, 1024, 10);
+
+ int current = 0;
+ ofxMaxiSettings::setup(sampleRate, 2, initialBufferSize);
+ ofSoundStreamSetup(2,0, this, maxiSettings::sampleRate, initialBufferSize, 4);/* Call this last ! */
+
+ ofSetVerticalSync(true);
+ ofEnableAlphaBlending();
+ ofEnableSmoothing();
+}
+
+//--------------------------------------------------------------
+void testApp::update(){
+}
+
+//--------------------------------------------------------------
+void testApp::draw(){
+ ofSetColor(160,32,240, 150);
+ ofDrawBitmapString(":: ofxMaxim Granular Timestretching Example ::", 10,20);
+ ofDrawBitmapString("Move the mouse left to right to change playback speed and direction.", 10,40);
+ ofDrawBitmapString("Move the mouse up and down to change the grain length.", 10,60);
+ ofDrawBitmapString("Click to cycle through samples.", 10,80);
+
+ stringstream s;
+ s << "Speed: " << speed;
+ ofDrawBitmapString(s.str(), 10,750);
+ s.str("");
+ s << "Grain length: " << round(grainLength*1000.0) << " ms";
+ ofDrawBitmapString(s.str(), 180,750);
+
+ ofNoFill();
+// for(int i=0; i < oct.nAverages; i++) {
+// ofSetColor(200 + ((int)(ofGetFrameNum() * 0.8) % 255),
+// 100 + ((int)(ofGetFrameNum() * 1.4) % 255),
+// ofGetFrameNum() % 255,
+// oct.averages[i] / 20.0 * 255.0);
+//// ofCircle(ofGetWidth() / 2, ofGetHeight()/2, i * 5);
+// glPushMatrix();
+// glTranslatef(ofGetWidth()/2,ofGetHeight()/2, 0);
+// glRotatef(0.01 * ofGetFrameNum() * speed * i, 1, 0.1, 0);
+//// glutWireSphere(i * 5, 2 + (10 - (fabs(speed) * 10)), 2 + (fabs(speed) * 10));
+// glutWireSphere(i * 5, 10, 10);
+// glPopMatrix();
+// }
+
+
+}
+
+//--------------------------------------------------------------
+void testApp::audioRequested (float * output, int bufferSize, int nChannels){
+ for (int i = 0; i < bufferSize; i++){
+// wave = stretches[current]->play(speed, grainLength, 5, 0);
+ wave = stretches[current]->play(speed*2, rate, 0.1, 4, 0);
+// wave = stretches[current]->play2(pos, 0.1, 4);
+ if (fft.process(wave)) {
+ oct.calculate(fft.magnitudes);
+ }
+
+ //play result
+ mymix.stereo(wave, outputs, 0.5);
+ lAudioOut[i] = output[i*nChannels ] = outputs[0]; /* You may end up with lots of outputs. add them here */
+ rAudioOut[i] = output[i*nChannels + 1] = outputs[1];
+ }
+}
+
+//--------------------------------------------------------------
+void testApp::audioReceived (float * input, int bufferSize, int nChannels){
+ /* You can just grab this input and stick it in a double, then use it above to create output*/
+ for (int i = 0; i < bufferSize; i++){
+ /* you can also grab the data out of the arrays*/
+ lAudioIn[i] = input[i*2];
+ rAudioIn[i] = input[i*2+1];
+ }
+}
+
+//--------------------------------------------------------------
+void testApp::keyPressed(int key){
+ switch (key) {
+ case 'a':
+ case 'A':
+ current = 0;
+ break;
+ case 's':
+ case 'S':
+ current = 1;
+ break;
+ case 'd':
+ case 'D':
+ current = 2;
+ break;
+ case 'f':
+ case 'F':
+ current = 3;
+ break;
+ case 'g':
+ case 'G':
+ current = 4;
+ break;
+ }
+
+}
+
+//--------------------------------------------------------------
+void testApp::keyReleased(int key){
+
+}
+
+//--------------------------------------------------------------
+void testApp::mouseMoved(int x, int y ){
+ speed = ((double ) x / ofGetWidth() * 4.0) - 2.0;
+ grainLength = ((double) y / ofGetHeight() * 0.1) + 0.001;
+ pos = ((double) x / ofGetWidth() * 2.0);
+
+ rate = ((double ) y / ofGetHeight() * 4.0) - 2.0;
+
+
+// cout << pos << endl;
+
+}
+
+//--------------------------------------------------------------
+void testApp::mouseDragged(int x, int y, int button){
+}
+
+//--------------------------------------------------------------
+void testApp::mousePressed(int x, int y, int button){
+ if (++current > stretches.size()-1) current = 0;
+
+}
+
+//--------------------------------------------------------------
+void testApp::mouseReleased(int x, int y, int button){
+
+}
+
+//--------------------------------------------------------------
+void testApp::windowResized(int w, int h){
+
+}
+
diff --git a/ofxMaxim/ofMaximExample007OSX_Granular/src/testApp.h b/ofxMaxim/ofMaximExample007OSX_Granular/src/testApp.h
new file mode 100644
index 0000000..cf5509e
--- /dev/null
+++ b/ofxMaxim/ofMaximExample007OSX_Granular/src/testApp.h
@@ -0,0 +1,57 @@
+#ifndef _TEST_APP
+#define _TEST_APP
+
+
+#include "ofMain.h"
+#include "ofxMaxim.h"
+#include "maxiGrains.h"
+#include <sys/time.h>
+
+typedef hannWinFunctor grainPlayerWin;
+
+class testApp : public ofBaseApp{
+
+ public:
+ ~testApp();/* deconsructor is very useful */
+ 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 windowResized(int w, int h);
+
+ void audioRequested (float * input, int bufferSize, int nChannels); /* output method */
+ void audioReceived (float * input, int bufferSize, int nChannels); /* input method */
+
+ float * lAudioOut; /* outputs */
+ float * rAudioOut;
+
+ float * lAudioIn; /* inputs */
+ float * rAudioIn;
+
+ int initialBufferSize; /* buffer size */
+ int sampleRate;
+
+
+ /* stick your maximilian stuff below */
+
+ double wave,sample,outputs[2];
+ maxiSample samp, samp2, samp3, samp4, samp5;
+ vector<maxiPitchStretch<grainPlayerWin>*> stretches;
+ maxiMix mymix;
+ maxiPitchStretch<grainPlayerWin> *ts, *ts2, *ts3, *ts4, *ts5;
+ double speed, grainLength, rate;
+
+ ofxMaxiFFT fft;
+ ofxMaxiFFTOctaveAnalyzer oct;
+ int current;
+ double pos;
+
+};
+
+#endif