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