annotate src/testApp.cpp @ 2:179c09199b3c

bayesian vector now adding gaussians for kick onsets
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Tue, 31 Jan 2012 21:34:19 +0000
parents 852173ca8365
children 5e188c0035b6
rev   line source
andrew@0 1 #include "testApp.h"
andrew@0 2 #include "stdio.h"
andrew@0 3 //#include "aubio.h"
andrew@0 4 #include <iostream>
andrew@0 5 #include <cstring>
andrew@0 6 #include <string>
andrew@0 7 #include <cstdlib>
andrew@0 8
andrew@0 9
andrew@0 10 const double samplingFrequency = 44100.0;
andrew@0 11
andrew@0 12 //--------------------------------------------------------------
andrew@0 13 void testApp::setup(){
andrew@0 14
andrew@0 15 // 2 output channels,
andrew@0 16 // 0 input channels
andrew@0 17 // 22050 samples per second
andrew@0 18 // 256 samples per buffer
andrew@0 19 // 4 num buffers (latency)
andrew@0 20
andrew@0 21 //nb THIS CODE WOULD BE USEFUL IF WE EVER WANTED REAL-TIME INPUT - VIA ofSoundSteam
andrew@0 22
andrew@0 23 receiver.setup( PORT );
andrew@0 24
andrew@0 25 sampleRate = 44100;
andrew@0 26 ofSoundStreamSetup(2,0,this, sampleRate,256, 4);
andrew@0 27
andrew@0 28 ofSetFrameRate(30);
andrew@0 29
andrew@1 30
andrew@1 31
andrew@1 32 eventMatcher.recordedTracks.loadTestAudio();
andrew@1 33
andrew@1 34
andrew@1 35 //audioFilePlayer.loadAudioFile(infilename);
andrew@1 36 }
andrew@1 37 /*
andrew@1 38 void testApp::loadTestAudio(){
andrew@1 39
andrew@1 40
andrew@0 41 const char *infilename = "../../../data/sound/bach4_short1.wav";
andrew@0 42
andrew@1 43 //LoadedAudioHolder lah;
andrew@1 44 // lah.loadAudioFile(infilename);
andrew@1 45 // loadedAudioFiles.push_back(lah);
andrew@0 46
andrew@0 47 //Take care here - we need a pointer to create new instance
andrew@0 48 //but not then delete the instance before the vector of all audio tracks has been used
andrew@0 49 //the above code using lah has problem that it deletes objects once out of the scope of testApp.setup()
andrew@0 50 //when lah is in theory no longer used - something like that possible? - at least pointers to onset detection seem deleted
andrew@0 51 loadedAudioPtr = new LoadedAudioHolder;
andrew@0 52 loadedAudioPtr->loadAudioFile(infilename);
andrew@1 53 // loadedAudioFiles.push_back(*loadedAudioPtr);
andrew@0 54 loadedAudioFiles[0] = *loadedAudioPtr;
andrew@0 55
andrew@0 56 loadedAudioFiles[0].fileLoader.onsetDetect.window.setToRelativeSize(0, 0.0, 1, 0.25);
andrew@0 57
andrew@1 58 // printf("Loaded audio %i\n", (int)numberOfAudioTracks);
andrew@0 59 printf("loaded max val is %f\n", loadedAudioFiles[0].fileLoader.onsetDetect.onsetDetector.maximumDetectionValue);
andrew@0 60
andrew@0 61 printf("BEFORE LOADING 1\n");
andrew@0 62 keyPressed('p');
andrew@0 63
andrew@0 64 loadedAudioPtr = new LoadedAudioHolder;
andrew@0 65 loadedAudioPtr->loadAudioFile(infilename);
andrew@1 66 // loadedAudioFiles.push_back(*loadedAudioPtr);
andrew@0 67 loadedAudioFiles[1] = *loadedAudioPtr;
andrew@0 68 loadedAudioFiles[1].fileLoader.onsetDetect.window.setToRelativeSize(0, 0.3, 1, 0.25);
andrew@1 69
andrew@1 70 printf("AFTER LOADING 1\n");
andrew@0 71 keyPressed('p');
andrew@0 72
andrew@0 73 }
andrew@1 74 */
andrew@0 75
andrew@0 76 //--------------------------------------------------------------
andrew@0 77 void testApp::update(){
andrew@0 78
andrew@1 79 eventMatcher.recordedTracks.updatePosition();
andrew@1 80
andrew@0 81 // audioFilePlayer.updateToPlayPosition();
andrew@0 82
andrew@0 83 checkForOSCmessages();
andrew@0 84
andrew@0 85 }
andrew@0 86
andrew@0 87 void testApp::checkForOSCmessages(){
andrew@0 88 // check for waiting messages
andrew@0 89 while( receiver.hasWaitingMessages() )
andrew@0 90 {
andrew@0 91 // get the next message
andrew@0 92 ofxOscMessage m;
andrew@0 93 receiver.getNextMessage( &m );
andrew@0 94
andrew@0 95 // check for mouse moved message
andrew@1 96 if ( m.getAddress() == "/aubioPitch" ){
andrew@0 97 float pitchIn = m.getArgAsFloat(0);
andrew@0 98 int timeIn = m.getArgAsInt32(1);
andrew@0 99 printf("AUBIO PITCH RECEIVED %f at time %i\n", pitchIn, timeIn);
andrew@1 100 eventMatcher.newPitchEvent(pitchIn, timeIn);
andrew@0 101 }
andrew@2 102
andrew@2 103 if ( m.getAddress() == "/kick" ){
andrew@2 104 // float pitchIn = m.getArgAsFloat(0);
andrew@2 105 double timeIn = m.getArgAsInt32(0);
andrew@2 106 printf("kick RECEIVED at time %f\n", timeIn);
andrew@2 107 eventMatcher.newKickEvent(timeIn);
andrew@2 108 }
andrew@2 109
andrew@2 110 if ( m.getAddress() == "/snare" ){
andrew@2 111 // float pitchIn = m.getArgAsFloat(0);
andrew@2 112 double timeIn = m.getArgAsInt32(0);
andrew@2 113 printf("snare RECEIVED at time %f\n", timeIn);
andrew@2 114 eventMatcher.newSnareEvent(timeIn);
andrew@2 115 }
andrew@0 116 }
andrew@0 117 }
andrew@0 118
andrew@0 119 //--------------------------------------------------------------
andrew@0 120 void testApp::draw(){
andrew@0 121
andrew@0 122 eventMatcher.draw();
andrew@0 123
andrew@0 124 // audioFilePlayer.draw();
andrew@0 125
andrew@0 126 }
andrew@0 127
andrew@0 128
andrew@0 129
andrew@0 130 //--------------------------------------------------------------
andrew@0 131 void testApp::keyPressed (int key){
andrew@0 132 if (key == '-'){
andrew@0 133 volume -= 0.05;
andrew@0 134 volume = MAX(volume, 0);
andrew@0 135 } else if (key == '+'){
andrew@0 136 volume += 0.05;
andrew@0 137 volume = MIN(volume, 1);
andrew@0 138 }
andrew@0 139
andrew@0 140 if (key == 'q'){
andrew@1 141 eventMatcher.recordedTracks.switchScreens();
andrew@0 142 }
andrew@0 143
andrew@0 144 if (key == OF_KEY_RIGHT){
andrew@0 145 // audioFilePlayer.loadedAudio.setPosition(min(1.0, audioFilePlayer.loadedAudio.getPosition() + (audioFilePlayer.fileLoader.audioHolder.audioScaleSamples/(4.0*audioFilePlayer.fileLoader.audioHolder.audioVector.size()))) );
andrew@0 146
andrew@0 147 }
andrew@0 148
andrew@0 149 if (key == OF_KEY_LEFT){
andrew@0 150 // audioFilePlayer.loadedAudio.setPosition(max(0.0, audioFilePlayer.loadedAudio.getPosition() - (audioFilePlayer.fileLoader.audioHolder.audioScaleSamples/(4.0*audioFilePlayer.fileLoader.audioHolder.audioVector.size()))));
andrew@0 151
andrew@0 152 }
andrew@0 153
andrew@0 154
andrew@0 155 if (key == ' '){
andrew@1 156
andrew@1 157 eventMatcher.recordedTracks.togglePlay();
andrew@0 158 }
andrew@0 159
andrew@0 160 if (key == OF_KEY_RETURN){
andrew@1 161
andrew@0 162
andrew@1 163 eventMatcher.recordedTracks.stop();
andrew@0 164 }
andrew@0 165
andrew@0 166
andrew@0 167 if (key == 'o'){
andrew@0 168 openNewAudioFileWithdialogBox();
andrew@0 169
andrew@0 170 }
andrew@0 171
andrew@0 172 if (key == 'p'){
andrew@1 173
andrew@0 174 }
andrew@0 175
andrew@0 176
andrew@0 177 if (key == OF_KEY_UP){
andrew@1 178 eventMatcher.recordedTracks.zoomOut();
andrew@1 179
andrew@0 180 }
andrew@0 181
andrew@0 182 if (key == OF_KEY_DOWN){
andrew@1 183 eventMatcher.recordedTracks.zoomIn();
andrew@0 184 }
andrew@0 185
andrew@0 186 }
andrew@0 187
andrew@0 188 //--------------------------------------------------------------
andrew@0 189 void testApp::keyReleased (int key){
andrew@0 190
andrew@0 191 }
andrew@0 192
andrew@0 193
andrew@0 194 //--------------------------------------------------------------
andrew@0 195 void testApp::mouseMoved(int x, int y ){
andrew@0 196
andrew@0 197
andrew@0 198 }
andrew@0 199
andrew@0 200 //--------------------------------------------------------------
andrew@0 201 void testApp::mouseDragged(int x, int y, int button){
andrew@0 202
andrew@0 203 }
andrew@0 204
andrew@0 205 //--------------------------------------------------------------
andrew@0 206 void testApp::mousePressed(int x, int y, int button){
andrew@0 207
andrew@0 208 }
andrew@0 209
andrew@0 210
andrew@0 211 //--------------------------------------------------------------
andrew@0 212 void testApp::mouseReleased(int x, int y, int button){
andrew@0 213
andrew@0 214 }
andrew@0 215
andrew@0 216 //--------------------------------------------------------------
andrew@0 217 void testApp::windowResized(int w, int h){
andrew@1 218
andrew@1 219 eventMatcher.windowResized(w, h);
andrew@0 220
andrew@0 221
andrew@0 222 }
andrew@0 223 //--------------------------------------------------------------
andrew@0 224 void testApp::audioRequested (float * output, int bufferSize, int nChannels){
andrew@0 225 //pan = 0.5f;
andrew@0 226 float leftScale = 1 - pan;
andrew@0 227 float rightScale = pan;
andrew@0 228
andrew@0 229 }
andrew@0 230
andrew@0 231
andrew@0 232
andrew@0 233 //--------------------------------------------------------------
andrew@0 234 void testApp::openNewAudioFileWithdialogBox(){
andrew@0 235 std::string filename;
andrew@0 236 getFilenameFromDialogBox(&filename);
andrew@0 237 loadNewAudio(filename);
andrew@0 238
andrew@0 239 }
andrew@0 240
andrew@0 241
andrew@0 242 void testApp::loadNewAudio(string soundFileName){
andrew@0 243
andrew@1 244 eventMatcher.recordedTracks.loadedAudioFiles[0].loadAudioFile(soundFileName);
andrew@0 245
andrew@0 246 // for (int i = 0;i < numberOfAudioTracks;i++)
andrew@0 247 // loadedAudioFiles[i].loadAudioFile(soundFileName);
andrew@0 248
andrew@0 249 // audioFilePlayer.loadAudioFile(soundFileName);
andrew@0 250
andrew@0 251 }
andrew@0 252
andrew@0 253
andrew@0 254 bool testApp::getFilenameFromDialogBox(std::string* fileNameToSave){
andrew@0 255 //this uses a pointer structure within the loader and returns true if the dialogue box was used successfully
andrew@0 256 // first, create a string that will hold the URL
andrew@0 257 string URL;
andrew@0 258
andrew@0 259 // openFile(string& URL) returns 1 if a file was picked
andrew@0 260 // returns 0 when something went wrong or the user pressed 'cancel'
andrew@0 261 int response = ofxFileDialogOSX::openFile(URL);
andrew@0 262 if(response){
andrew@0 263 // now you can use the URL
andrew@0 264 *fileNameToSave = URL;
andrew@0 265 //printf("\n filename is %s \n", soundFileName.c_str());
andrew@0 266 return true;
andrew@0 267 }
andrew@0 268 else {
andrew@0 269 // soundFileName = "OPEN canceled. ";
andrew@0 270 printf("\n open file cancelled \n");
andrew@0 271 return false;
andrew@0 272 }
andrew@0 273
andrew@0 274 }
andrew@0 275
andrew@0 276
andrew@0 277
andrew@0 278
andrew@0 279