Mercurial > hg > audio-file-loader
comparison src/SoundFileLoader.cpp @ 2:fa2af670b5c5 tip
SoundFileLoader might have moved
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Fri, 06 Jan 2012 00:23:26 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:ba2a17cf81bf | 2:fa2af670b5c5 |
---|---|
1 /* | |
2 * SoundFileLoader.cpp | |
3 * audioFileLoaderSVN1 | |
4 * | |
5 * Created by Andrew on 04/09/2011. | |
6 * Copyright 2011 QMUL. All rights reserved. | |
7 * | |
8 */ | |
9 | |
10 #include "SoundFileLoader.h" | |
11 | |
12 | |
13 SoundFileLoader::SoundFileLoader(){ | |
14 sfinfo.format = 0; | |
15 | |
16 chromaG = &chromoGramm; | |
17 | |
18 } | |
19 | |
20 void SoundFileLoader::loadLibSndFile(const char *infilename){ | |
21 | |
22 if (!sf_close(infile)){ | |
23 printf("closed sndfile okay \n"); | |
24 } | |
25 | |
26 // Open Input File with lib snd file | |
27 if (! (infile = sf_open (infilename, SFM_READ, &sfinfo))) | |
28 { // Open failed | |
29 printf ("SF OPEN routine Not able to open input file %s.\n", infilename) ; | |
30 // Print the error message from libsndfile. | |
31 puts (sf_strerror (NULL)) ; | |
32 | |
33 } else{ | |
34 printf("SF OPEN : file %s okay, ", infilename); | |
35 printf("number of channels is %i\n", sfinfo.channels); | |
36 //sndfileInfoString = "Opened okay "; | |
37 | |
38 }; | |
39 | |
40 processAudioToDoubleMatrix(); | |
41 | |
42 } | |
43 | |
44 | |
45 | |
46 | |
47 void SoundFileLoader::processAudioToDoubleMatrix(){ | |
48 | |
49 | |
50 | |
51 chromaAnalysis.chromaMatrix.clear(); | |
52 chromaAnalysis.energyVector.clear(); | |
53 | |
54 audioHolder.audioVector.clear(); | |
55 audioHolder.audioMatrix.clear(); | |
56 | |
57 //energyIndex = 0; | |
58 | |
59 chromaG->initialise(FRAMESIZE,2048);//framesize 512 and hopsize 2048 | |
60 chromaG->maximumChromaValue = 0; | |
61 | |
62 | |
63 // HERE IS THE CLASSIC LOADING FILE CODE | |
64 //DEALS WITH MORE THAN MONO | |
65 int channels = sfinfo.channels; | |
66 int blocksize = FRAMESIZE; | |
67 | |
68 float buf [channels * blocksize] ; | |
69 int k, m, readcount ; | |
70 | |
71 DoubleVector d; | |
72 while ((readcount = sf_readf_float (infile, buf, blocksize)) > 0){ | |
73 for (k = 0 ; k < readcount ; k++){ | |
74 d.clear(); | |
75 for (m = 0 ; m < channels ; m++){ | |
76 d.push_back(buf [k * channels + m]); | |
77 // fprintf (outfile, " % 12.10f", buf [k * channels + m]) ; | |
78 // fprintf (outfile, "\n") ; | |
79 if (m == 0){ | |
80 //makes the vector hold the mono file | |
81 //this is the one we use for chromagram analysis etc | |
82 audioHolder.audioVector.push_back(buf[k * channels + 0]); | |
83 frame[k] = buf[k*channels + 0]; | |
84 } | |
85 | |
86 } | |
87 audioHolder.audioMatrix.push_back(d); | |
88 //storing the full soundfile in multiple channels in the audioMatrix | |
89 } | |
90 | |
91 | |
92 chromaG->processframe(frame); | |
93 | |
94 if (chromaG->chromaready) | |
95 { | |
96 DoubleVector d; | |
97 | |
98 for (int i = 0;i<12;i++){ | |
99 //chromoGramVector[chromaIndex][i] = chromoGramm.rawChroma[i] / chromoGramm.maximumChromaValue; | |
100 d.push_back(chromaG->rawChroma[i]);// / chromaG->maximumChromaValue); | |
101 | |
102 } | |
103 | |
104 chromaAnalysis.chromaMatrix.push_back(d); | |
105 | |
106 //There was a method to detect chord but deleted | |
107 // chord.C_Detect(chromoGramm.chroma,chromoGramm.chroma_low); | |
108 // rootChord[chromaIndex] = chord.root; | |
109 | |
110 | |
111 }//end if chromagRamm ready | |
112 | |
113 // frameIndex++; | |
114 | |
115 //get energy of the current frame and wait | |
116 double energyValue = getEnergyOfFrame(); | |
117 chromaAnalysis.energyVector.push_back(energyValue); | |
118 | |
119 | |
120 | |
121 }//end readcount | |
122 | |
123 | |
124 | |
125 printf("Max chroma value is %f \n", chromaG->maximumChromaValue); | |
126 | |
127 | |
128 | |
129 //normalise | |
130 int length = chromaAnalysis.chromaMatrix.size(); | |
131 printf("length of chromagram is %d frames\n", length); | |
132 length = (chromaAnalysis.chromaMatrix[0]).size(); | |
133 printf("height of dmatrix is %d\n", length); | |
134 | |
135 for (int i = 0; i < chromaAnalysis.chromaMatrix.size();i++){ | |
136 for (int j = 0; j < (chromaAnalysis.chromaMatrix[0]).size();j++){ | |
137 chromaAnalysis.chromaMatrix[i][j] /= chromaG->maximumChromaValue; | |
138 } | |
139 } | |
140 | |
141 | |
142 printf("size of energy vector is %d \n", (int) chromaAnalysis.energyVector.size()); | |
143 | |
144 totalNumberOfFrames = (int) chromaAnalysis.energyVector.size();//frameIndex;//used to use this - but switch to energy vector's size instead | |
145 | |
146 // printf("Total frames %i energy index %i and Chroma index %i \n", frameIndex, energyIndex, chromaIndex); | |
147 | |
148 printf("audio vector size is %i\n", (int) audioHolder.audioVector.size()); | |
149 audioHolder.length = (int) audioHolder.audioVector.size(); | |
150 } | |
151 | |
152 | |
153 | |
154 double SoundFileLoader::getEnergyOfFrame(){ | |
155 | |
156 float totalEnergyInFrame = 0; | |
157 | |
158 for (int i = 0;i<FRAMESIZE;i++){ | |
159 | |
160 totalEnergyInFrame += (frame[i] * frame[i]); | |
161 | |
162 } | |
163 totalEnergyInFrame = sqrt(totalEnergyInFrame); | |
164 | |
165 return totalEnergyInFrame; | |
166 } | |
167 |