Mercurial > hg > multitrack-audio-matcher
diff src/testApp.cpp @ 0:c4f9e49226eb
Initialising repository. Live osc input registered. Files analysed offline.
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Tue, 31 Jan 2012 13:54:17 +0000 |
parents | |
children | 852173ca8365 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/testApp.cpp Tue Jan 31 13:54:17 2012 +0000 @@ -0,0 +1,276 @@ +#include "testApp.h" +#include "stdio.h" +//#include "aubio.h" +#include <iostream> +#include <cstring> +#include <string> +#include <cstdlib> + + +const double samplingFrequency = 44100.0; + +//-------------------------------------------------------------- +void testApp::setup(){ + + // 2 output channels, + // 0 input channels + // 22050 samples per second + // 256 samples per buffer + // 4 num buffers (latency) + + //nb THIS CODE WOULD BE USEFUL IF WE EVER WANTED REAL-TIME INPUT - VIA ofSoundSteam + + receiver.setup( PORT ); + + sampleRate = 44100; + ofSoundStreamSetup(2,0,this, sampleRate,256, 4); + + ofSetFrameRate(30); + + LoadedAudioHolder lah; + const char *infilename = "../../../data/sound/bach4_short1.wav"; + + +// lah.loadAudioFile(infilename); +// loadedAudioFiles.push_back(lah); + + //Take care here - we need a pointer to create new instance + //but not then delete the instance before the vector of all audio tracks has been used + //the above code using lah has problem that it deletes objects once out of the scope of testApp.setup() + //when lah is in theory no longer used - something like that possible? - at least pointers to onset detection seem deleted + loadedAudioPtr = new LoadedAudioHolder; + loadedAudioPtr->loadAudioFile(infilename); +// loadedAudioFiles.push_back(*loadedAudioPtr); + loadedAudioFiles[0] = *loadedAudioPtr; + + loadedAudioFiles[0].fileLoader.onsetDetect.window.setToRelativeSize(0, 0.0, 1, 0.25); + +// printf("Loaded audio %i\n", (int)numberOfAudioTracks); + printf("loaded max val is %f\n", loadedAudioFiles[0].fileLoader.onsetDetect.onsetDetector.maximumDetectionValue); + + printf("BEFORE LOADING 1\n"); + keyPressed('p'); + + loadedAudioPtr = new LoadedAudioHolder; + loadedAudioPtr->loadAudioFile(infilename); +// loadedAudioFiles.push_back(*loadedAudioPtr); + loadedAudioFiles[1] = *loadedAudioPtr; + loadedAudioFiles[1].fileLoader.onsetDetect.window.setToRelativeSize(0, 0.3, 1, 0.25); + + printf("AFTER LOADING 1\n"); + keyPressed('p'); + + numberOfAudioTracks = 2; + + + //audioFilePlayer.loadAudioFile(infilename); +} + + + +//-------------------------------------------------------------- +void testApp::update(){ + + for (int i = 0;i < numberOfAudioTracks;i++) + loadedAudioFiles[i].updateToPlayPosition(); +// audioFilePlayer.updateToPlayPosition(); + + checkForOSCmessages(); + +} + +void testApp::checkForOSCmessages(){ + // check for waiting messages + while( receiver.hasWaitingMessages() ) + { + // get the next message + ofxOscMessage m; + receiver.getNextMessage( &m ); + + // check for mouse moved message + if ( m.getAddress() == "/aubioPitch" ) + { + float pitchIn = m.getArgAsFloat(0); + int timeIn = m.getArgAsInt32(1); + printf("AUBIO PITCH RECEIVED %f at time %i\n", pitchIn, timeIn); + } + } +} + +//-------------------------------------------------------------- +void testApp::draw(){ + + for (int i = 0;i < numberOfAudioTracks;i++){ + loadedAudioFiles[i].draw(); + } + + + eventMatcher.draw(); + +// audioFilePlayer.draw(); + +} + + + +//-------------------------------------------------------------- +void testApp::keyPressed (int key){ + if (key == '-'){ + volume -= 0.05; + volume = MAX(volume, 0); + } else if (key == '+'){ + volume += 0.05; + volume = MIN(volume, 1); + } + + if (key == 'q'){ + for (int i = 0;i < numberOfAudioTracks;i++) + loadedAudioFiles[i].switchScreens(); +// audioFilePlayer.switchScreens(); + } + + if (key == OF_KEY_RIGHT){ +// audioFilePlayer.loadedAudio.setPosition(min(1.0, audioFilePlayer.loadedAudio.getPosition() + (audioFilePlayer.fileLoader.audioHolder.audioScaleSamples/(4.0*audioFilePlayer.fileLoader.audioHolder.audioVector.size()))) ); + + } + + if (key == OF_KEY_LEFT){ +// audioFilePlayer.loadedAudio.setPosition(max(0.0, audioFilePlayer.loadedAudio.getPosition() - (audioFilePlayer.fileLoader.audioHolder.audioScaleSamples/(4.0*audioFilePlayer.fileLoader.audioHolder.audioVector.size())))); + + } + + + if (key == ' '){ + for (int i = 0;i < numberOfAudioTracks;i++) + loadedAudioFiles[i].togglePlay(); +// audioFilePlayer.togglePlay(); + } + + if (key == OF_KEY_RETURN){ + for (int i = 0;i < numberOfAudioTracks;i++) + loadedAudioFiles[i].stop(); + +// audioFilePlayer.stop(); + } + + + if (key == 'o'){ + openNewAudioFileWithdialogBox(); + + } + + if (key == 'p'){ + loadedAudioFiles[0].fileLoader.onsetDetect.printChromaInfo(); + loadedAudioFiles[0].printEvents(); + } + + + if (key == OF_KEY_UP){ + for (int i = 0;i < numberOfAudioTracks;i++) + loadedAudioFiles[i].fileLoader.zoomOut(); +// audioFilePlayer.fileLoader.zoomOut(); + } + + if (key == OF_KEY_DOWN){ + for (int i = 0;i < numberOfAudioTracks;i++) + loadedAudioFiles[i].fileLoader.zoomIn(); + + // audioFilePlayer.fileLoader.zoomIn(); + + } + +} + +//-------------------------------------------------------------- +void testApp::keyReleased (int key){ + +} + + +//-------------------------------------------------------------- +void testApp::mouseMoved(int x, int y ){ + + +} + +//-------------------------------------------------------------- +void testApp::mouseDragged(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::mousePressed(int x, int y, int button){ + +} + + +//-------------------------------------------------------------- +void testApp::mouseReleased(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::windowResized(int w, int h){ + for (int i = 0;i < numberOfAudioTracks;i++) + loadedAudioFiles[i].windowResized(w, h); + //audioFilePlayer.windowResized(w, h); + + +} +//-------------------------------------------------------------- +void testApp::audioRequested (float * output, int bufferSize, int nChannels){ + //pan = 0.5f; + float leftScale = 1 - pan; + float rightScale = pan; + +} + + + +//-------------------------------------------------------------- +void testApp::openNewAudioFileWithdialogBox(){ + std::string filename; + getFilenameFromDialogBox(&filename); + loadNewAudio(filename); + +} + + +void testApp::loadNewAudio(string soundFileName){ + + loadedAudioFiles[0].loadAudioFile(soundFileName); + +// for (int i = 0;i < numberOfAudioTracks;i++) +// loadedAudioFiles[i].loadAudioFile(soundFileName); + +// audioFilePlayer.loadAudioFile(soundFileName); + +} + + +bool testApp::getFilenameFromDialogBox(std::string* fileNameToSave){ + //this uses a pointer structure within the loader and returns true if the dialogue box was used successfully + // first, create a string that will hold the URL + string URL; + + // openFile(string& URL) returns 1 if a file was picked + // returns 0 when something went wrong or the user pressed 'cancel' + int response = ofxFileDialogOSX::openFile(URL); + if(response){ + // now you can use the URL + *fileNameToSave = URL; + //printf("\n filename is %s \n", soundFileName.c_str()); + return true; + } + else { + // soundFileName = "OPEN canceled. "; + printf("\n open file cancelled \n"); + return false; + } + +} + + + + +