annotate examples/10-Instruments/d-box/spear_parser.h @ 507:1cec96845a23 prerelease

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