annotate DrumTimingLoader_OF/ofxAudioFileLoader/ofxSoundFileLoader.cpp @ 0:82352cfc0b23

Added files from ISMIR groove drum timing work
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Mon, 01 Oct 2012 22:24:32 +0100
parents
children 50ba55abea8c
rev   line source
andrew@0 1 /*
andrew@0 2 * ofxSoundFileLoader.cpp
andrew@0 3 * audioFileLoaderSVN1
andrew@0 4 *
andrew@0 5 * Created by Andrew on 04/09/2011.
andrew@0 6 * Copyright 2011 QMUL. All rights reserved.
andrew@0 7 *
andrew@0 8 */
andrew@0 9
andrew@0 10 #include "ofxSoundFileLoader.h"
andrew@0 11
andrew@0 12 //NB zooming relies on screenToDraw - this needs to be 1
andrew@0 13
andrew@0 14 ofxSoundFileLoader::ofxSoundFileLoader(){
andrew@0 15 sfinfo.format = 0;
andrew@0 16 // onsetDetect = new ofxAubioOnsetDetection();
andrew@0 17 //onsetDetect->reset();
andrew@0 18
andrew@0 19 soundFileName = "";
andrew@0 20 screenToDraw = 1;
andrew@0 21 }
andrew@0 22
andrew@0 23 ofxSoundFileLoader::~ofxSoundFileLoader(){
andrew@0 24 // delete onsetDetect ;
andrew@0 25 // printf("file loader delete onset detect\n");
andrew@0 26
andrew@0 27 }
andrew@0 28
andrew@0 29 void ofxSoundFileLoader::updateToAudioPosition(const float& audioPosition){
andrew@0 30
andrew@0 31 audioHolder.playPosition = audioPosition * audioHolder.audioVector.size();
andrew@0 32 onsetDetect.playPosition = audioPosition;
andrew@0 33 }
andrew@0 34
andrew@0 35 void ofxSoundFileLoader::updateToMillisPosition(const double& millis){
andrew@0 36
andrew@0 37 // audioHolder.playPosition = audioPosition * audioHolder.audioVector.size();
andrew@0 38 onsetDetect.playPosition = millis * 44.1 / (double)totalNumberOfSamples;
andrew@0 39
andrew@0 40 }
andrew@0 41
andrew@0 42 void ofxSoundFileLoader::drawFile(){
andrew@0 43 if (screenToDraw == 0){
andrew@0 44 audioHolder.drawAudioVectorSamples(audioHolder.playPosition - audioHolder.audioScaleSamples*0.5,
andrew@0 45 audioHolder.playPosition + audioHolder.audioScaleSamples*0.5);
andrew@0 46 }else{
andrew@0 47 onsetDetect.drawOnsetDetectionScrolling();
andrew@0 48 }
andrew@0 49
andrew@0 50 }
andrew@0 51
andrew@0 52 #pragma mark -loadAudio
andrew@0 53 void ofxSoundFileLoader::loadNewAudio(std::string filename){
andrew@0 54 loadLibSndFile(filename.c_str());
andrew@0 55 }
andrew@0 56
andrew@0 57 void ofxSoundFileLoader::loadLibSndFile(const char *infilename){
andrew@0 58
andrew@0 59 // if (!sf_close(infile)){
andrew@0 60 // printf("closed sndfile okay \n");
andrew@0 61 // }
andrew@0 62
andrew@0 63 // Open Input File with lib snd file
andrew@0 64 if (! (infile = sf_open (infilename, SFM_READ, &sfinfo)))
andrew@0 65 { // Open failed
andrew@0 66 printf ("SF OPEN routine Not able to open input file %s.\n", infilename) ;
andrew@0 67 // Print the error message from libsndfile.
andrew@0 68 puts (sf_strerror (NULL)) ;
andrew@0 69
andrew@0 70 } else{
andrew@0 71 printf("SF OPEN : file %s okay, ", infilename);
andrew@0 72 printf("number of channels is %i\n", sfinfo.channels);
andrew@0 73 soundFileName = infilename;
andrew@0 74 //sndfileInfoString = "Opened okay ";
andrew@0 75
andrew@0 76 };
andrew@0 77
andrew@0 78 readAudio();
andrew@0 79 onsetDetect.printOnsetList();
andrew@0 80 printf("max val of onset det is %f\n", onsetDetect.onsetDetector.maximumDetectionValue);
andrew@0 81 }
andrew@0 82
andrew@0 83 void ofxSoundFileLoader::readAudio(){
andrew@0 84
andrew@0 85 onsetDetect.reset();
andrew@0 86 //could add this in - check the stored github
andrew@0 87
andrew@0 88 // HERE IS THE CLASSIC LOADING FILE CODE
andrew@0 89 //DEALS WITH MORE THAN MONO
andrew@0 90 int channels = sfinfo.channels;
andrew@0 91 int blocksize = FRAMESIZE;
andrew@0 92
andrew@0 93 float buf [channels * blocksize] ;
andrew@0 94 int k, m, readcount ;
andrew@0 95
andrew@0 96 DoubleVector d;
andrew@0 97 while ((readcount = sf_readf_float (infile, buf, blocksize)) > 0){
andrew@0 98 for (k = 0 ; k < readcount ; k++){
andrew@0 99 //readcount is a chunk - eg 512 samples - of audio that is processed
andrew@0 100 d.clear();
andrew@0 101 for (m = 0 ; m < channels ; m++){
andrew@0 102 d.push_back(buf [k * channels + m]);
andrew@0 103 // fprintf (outfile, " % 12.10f", buf [k * channels + m]) ;
andrew@0 104 // fprintf (outfile, "\n") ;
andrew@0 105 if (m == 0){
andrew@0 106 //makes the vector hold the mono file - the left channel
andrew@0 107 audioHolder.audioVector.push_back(buf[k * channels + 0]);
andrew@0 108 frame[k] = buf[k*channels + 0];
andrew@0 109 }
andrew@0 110
andrew@0 111 }
andrew@0 112 audioHolder.audioMatrix.push_back(d);
andrew@0 113 //storing the full soundfile in multiple channels in the audioMatrix
andrew@0 114 }
andrew@0 115 //printf("processing at readcount %i\n", readcount);
andrew@0 116 //could call this here
andrew@0 117 onsetDetect.processFrame(&frame[0], blocksize);
andrew@0 118
andrew@0 119
andrew@0 120 }//end readcount
andrew@0 121
andrew@0 122 //printf("audio vector size is %i\n", (int) audioHolder.audioVector.size());
andrew@0 123 audioHolder.length = (int) audioHolder.audioVector.size();
andrew@0 124 totalNumberOfSamples = audioHolder.length;
andrew@0 125
andrew@0 126 printf("Total number of samples %i onset frames %i\n", totalNumberOfSamples, onsetDetect.frameCountIndex);
andrew@0 127
andrew@0 128 freeMemory();
andrew@0 129 }
andrew@0 130
andrew@0 131
andrew@0 132 void ofxSoundFileLoader::freeMemory(){
andrew@0 133 printf("FREE MEMORY in file loader\n");
andrew@0 134 audioHolder.audioMatrix.clear();
andrew@0 135 audioHolder.audioVector.clear();
andrew@0 136 }
andrew@0 137
andrew@0 138 void ofxSoundFileLoader::zoomOut(){
andrew@0 139 if (screenToDraw == 0){
andrew@0 140 audioHolder.audioScaleSamples *= 2.;
andrew@0 141 }
andrew@0 142 if (screenToDraw == 1){
andrew@0 143 onsetDetect.amplitudeNumber *= 2;
andrew@0 144 }
andrew@0 145 }
andrew@0 146
andrew@0 147 void ofxSoundFileLoader::zoomIn(){
andrew@0 148 if (screenToDraw == 0){
andrew@0 149 audioHolder.audioScaleSamples /= 2.;
andrew@0 150 }
andrew@0 151 if (screenToDraw == 1 && onsetDetect.amplitudeNumber > 2){
andrew@0 152 onsetDetect.amplitudeNumber /= 2;
andrew@0 153 }
andrew@0 154 }
andrew@0 155