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