Mercurial > hg > beaglert
comparison projects/d-box/spear_parser.h @ 0:8a575ba3ab52
Initial commit.
author | andrewm |
---|---|
date | Fri, 31 Oct 2014 19:10:17 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8a575ba3ab52 |
---|---|
1 /* | |
2 * spear_parser.h v1.2 | |
3 * | |
4 * Created on: May 6, 2014 | |
5 * Author: Victor Zappi | |
6 */ | |
7 | |
8 #ifndef SPEAR_PARSER_H_ | |
9 #define SPEAR_PARSER_H_ | |
10 | |
11 #include <iostream> | |
12 #include <fstream> | |
13 #include <cstring> | |
14 #include <string> | |
15 #include <stdlib.h> // atoi, atof | |
16 #include <math.h> | |
17 #include <algorithm> // std::fill | |
18 | |
19 #include <sys/time.h> | |
20 | |
21 using namespace std; | |
22 | |
23 | |
24 //------------------------------------------------------------------------------------------------ | |
25 // partials | |
26 //------------------------------------------------------------------------------------------------ | |
27 | |
28 class Spear_parser; // for class friendship | |
29 | |
30 class Partials | |
31 { | |
32 friend class Spear_parser; | |
33 friend class Dbox_parser; | |
34 | |
35 public: | |
36 int **partialSamples; // sample at which each frame is | |
37 float **partialFrequencies; // frequencies at each frame | |
38 float **partialAmplitudes; // amplitudes at each frame | |
39 unsigned int *partialNumFrames; // Length of each partial in frames | |
40 unsigned int *partialStartFrame; // frame at which each partial begins | |
41 float **partialFreqDelta; // constant frequency slope for each partial in each frame interval | |
42 float **partialAmpDelta; // constant amplitude slope for each partial in each frame interval | |
43 float *partialFreqMean; // frequency mean for each partial, over all its frames | |
44 | |
45 unsigned short *activePartialNum; // num of each active partial at each frame | |
46 unsigned int **activePartials; // indices of all active partials at each frame | |
47 | |
48 | |
49 int getPartialNum(); | |
50 int getHopNum(); | |
51 int getMaxActivePartialNum(); | |
52 | |
53 private: | |
54 Partials(); | |
55 ~Partials(); | |
56 | |
57 unsigned int *partialStartSample; // sample at which each partial begins | |
58 unsigned int *partialEndSample; // sample at which each partial ends [sample gap between 2 consecutive frames can be an integer multiple of hopSize] | |
59 unsigned int parNum; | |
60 unsigned int currentSample; | |
61 unsigned int hopSize; | |
62 unsigned int hopNum; | |
63 unsigned int maxActiveParNum; | |
64 | |
65 void init(int parNum, int hopSize, bool isDBX=false); | |
66 void update(int parIndex, int frameNum); | |
67 void setFreqDelta(int parIndex, int frameNum, double delta); | |
68 void setAmpDelta(int parIndex, int frameNum, double delta); | |
69 void setHopNum(int hopNum); | |
70 }; | |
71 | |
72 inline int Partials::getPartialNum() | |
73 { | |
74 return parNum; | |
75 } | |
76 | |
77 inline void Partials::setHopNum(int hopN) | |
78 { | |
79 hopNum = hopN; | |
80 | |
81 // prepare data structures | |
82 activePartialNum = new unsigned short[hopNum+1]; // +1 cos total num of frames = num of hops+1 | |
83 activePartials = new unsigned int *[hopNum+1]; | |
84 } | |
85 | |
86 // useful to increase current sample using a modulo on the total number of samples [easy to be deduced from the total num or hops] | |
87 inline int Partials::getHopNum() | |
88 { | |
89 return hopNum; | |
90 } | |
91 | |
92 inline void Partials::setFreqDelta(int parIndex, int frameNum, double delta) | |
93 { | |
94 partialFreqDelta[parIndex][frameNum] = delta; | |
95 } | |
96 | |
97 inline void Partials::setAmpDelta(int parIndex, int frameNum, double delta) | |
98 { | |
99 partialAmpDelta[parIndex][frameNum] = delta; | |
100 } | |
101 | |
102 inline int Partials::getMaxActivePartialNum() | |
103 { | |
104 return maxActiveParNum; | |
105 } | |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | |
112 | |
113 //------------------------------------------------------------------------------------------------ | |
114 // spear parser | |
115 //------------------------------------------------------------------------------------------------ | |
116 | |
117 class Spear_parser | |
118 { | |
119 public: | |
120 Spear_parser(); | |
121 ~Spear_parser(); | |
122 | |
123 Partials partials; | |
124 | |
125 bool parseFile(string filename, int hopsize=-1, int samplerate = 44100); | |
126 bool parseFile(char *filename, int hopsize=-1, int samplerate = 44100); | |
127 int getHopSize(); | |
128 int getFileSampleRate(); | |
129 double getDeltaTime(); | |
130 | |
131 private: | |
132 | |
133 int hopSize; | |
134 int fileSampleRate; | |
135 double deltaTime; // min time gap between consecutive frames | |
136 | |
137 timeval start, stop; | |
138 unsigned long hopSizeT, parserT, staticT; | |
139 | |
140 void calculateDeltaTime(); | |
141 void calculateHopSize(char *filename); | |
142 bool parser(char *filename, int hopsize=-1, int samplerate=44100); | |
143 bool DBXparser(char *filename, int samplerate=44100); | |
144 bool TXTparser(char *filename, int hopsize=-1, int samplerate=44100); | |
145 int fromTimeToSamples(float time); | |
146 int interpolateSamples(int parIndex, int *frameIndex, int missCnt, int nextSample, | |
147 double nextFreq, double nextAmp, double *prevFreq, double *prevAmp); | |
148 void staticCalculations(); | |
149 | |
150 }; | |
151 | |
152 inline bool Spear_parser::parseFile(string filename, int hopsize, int samplerate) | |
153 { | |
154 return parser((char *)filename.c_str(), hopsize, samplerate); | |
155 } | |
156 | |
157 inline bool Spear_parser::parseFile(char *filename, int hopsize, int samplerate) | |
158 { | |
159 return parser(filename, hopsize, samplerate); | |
160 } | |
161 | |
162 inline void Spear_parser::calculateDeltaTime() | |
163 { | |
164 deltaTime = (double)hopSize/ (double)fileSampleRate; | |
165 } | |
166 | |
167 // each time value in the file is rounded, and 2 consecutive frames can differ of a time gap = i*deltaTime, where i is a positive integer | |
168 inline int Spear_parser::fromTimeToSamples(float time) | |
169 { | |
170 return round(time/deltaTime)*hopSize; // round is necessary since in the file log time values are rounded, so they do not apparently look like integer multiples of deltaTime | |
171 } | |
172 | |
173 inline int Spear_parser::getHopSize() | |
174 { | |
175 return hopSize; | |
176 } | |
177 | |
178 inline int Spear_parser::getFileSampleRate() | |
179 { | |
180 return fileSampleRate; | |
181 } | |
182 | |
183 inline double Spear_parser::getDeltaTime() | |
184 { | |
185 return deltaTime; | |
186 } | |
187 | |
188 #endif /* SPEAR_PARSER_H_ */ |