X-Git-Url: http://git.cinelerra-gg.org/git/?p=goodguy%2Fhistory.git;a=blobdiff_plain;f=cinelerra-5.1%2Fguicast%2Funits.C;h=9c2f1d27106b7f4be2cee7ec37f70c5bd91edf04;hp=049d8f92de44f850e8ac8d7171ecf556bb37a305;hb=877bf33862808d3c3fbe11a31de0000feb915f21;hpb=30bdb85eb33a8ee7ba675038a86c6be59c43d7bd diff --git a/cinelerra-5.1/guicast/units.C b/cinelerra-5.1/guicast/units.C index 049d8f92..9c2f1d27 100644 --- a/cinelerra-5.1/guicast/units.C +++ b/cinelerra-5.1/guicast/units.C @@ -27,7 +27,6 @@ #include #include -float* DB::topower = 0; float* DB::topower_base = 0; int* Freq::freqtable = 0; @@ -35,18 +34,11 @@ int* Freq::freqtable = 0; DB::DB(float infinitygain) { this->infinitygain = infinitygain; - if(!topower) { // db to power table - topower_base = new float[(MAXGAIN - INFINITYGAIN) * 10 + 1]; - topower = topower_base + -INFINITYGAIN * 10; - for(int i = INFINITYGAIN * 10; i <= MAXGAIN * 10; i++) { - topower[i] = pow(10, (float)i / 10 / 20); -//printf("%f %f\n", (float)i/10, topower[i]); - } - topower[INFINITYGAIN * 10] = 0; // infinity gain - } - db = 0; + this->db = 0; + this->topower = topower_base + -INFINITYGAIN * 10; } + float DB::fromdb_table() { return db = topower[(int)(db*10)]; @@ -85,7 +77,6 @@ float DB::todb(float power) Freq::Freq() { - if( !freqtable ) init_table(); freq = 0; } @@ -94,30 +85,9 @@ Freq::Freq(const Freq& oldfreq) this->freq = oldfreq.freq; } -void Freq::init_table() -{ - freqtable = new int[TOTALFREQS + 1]; -// starting frequency - double freq1 = 27.5, freq2 = 55; -// Some number divisable by three. This depends on the value of TOTALFREQS - int scale = 105; - - freqtable[0] = 0; - for(int i = 1, j = 0; i <= TOTALFREQS; i++, j++) { - freqtable[i] = (int)(freq1 + (freq2 - freq1) / scale * j + 0.5); -//printf("Freq::init_table %d\n", freqtable[i]); - if(j >= scale) { - freq1 = freq2; - freq2 *= 2; - j = 0; - } - } -} - int Freq::fromfreq() { int i = 0; - if( !freqtable ) init_table(); while( i= TOTALFREQS) index = TOTALFREQS - 1; return freqtable[index]; } @@ -157,6 +125,35 @@ int Freq::operator!=(Freq &newfreq) { return freq != newfreq.freq; } int Freq::operator==(Freq &newfreq) { return freq == newfreq.freq; } int Freq::operator==(int newfreq) { return freq == newfreq; } + + +void Units::init() +{ + DB::topower_base = new float[(MAXGAIN - INFINITYGAIN) * 10 + 1]; + float *topower = DB::topower_base + -INFINITYGAIN * 10; + for(int i = INFINITYGAIN * 10; i <= MAXGAIN * 10; i++) + topower[i] = pow(10, (float)i / 10 / 20); + topower[INFINITYGAIN * 10] = 0; // infinity gain + + Freq::freqtable = new int[TOTALFREQS + 1]; +// starting frequency + double freq1 = 27.5, freq2 = 55; +// Some number divisable by three. This depends on the value of TOTALFREQS + int scale = 105; + + Freq::freqtable[0] = 0; + for(int i = 1, j = 0; i <= TOTALFREQS; i++, j++) { + Freq::freqtable[i] = (int)(freq1 + (freq2 - freq1) / scale * j + 0.5); + if(j < scale) continue; + freq1 = freq2; freq2 *= 2; j = 0; + } +} +void Units::finit() +{ + delete [] DB::topower_base; DB::topower_base = 0; + delete [] Freq::freqtable; Freq::freqtable = 0; +} + // give text representation as time char* Units::totext(char *text, double seconds, int time_format, int sample_rate, float frame_rate, float frames_per_foot) @@ -166,7 +163,8 @@ char* Units::totext(char *text, double seconds, int time_format, switch(time_format) { case TIME_SECONDS: { - seconds = fabs(seconds); +// add 1.0e-6 to prevent round off truncation from glitching a bunch of digits + seconds = fabs(seconds) + 1.0e-6; second = seconds; seconds -= (int64_t)seconds; thousandths = (int64_t)(seconds*1000) % 1000; @@ -174,7 +172,7 @@ char* Units::totext(char *text, double seconds, int time_format, break; } case TIME_HMS: { - seconds = fabs(seconds); + seconds = fabs(seconds) + 1.0e-6; hour = seconds/3600; minute = seconds/60 - hour*60; second = seconds - (hour*3600 + minute*60); @@ -185,7 +183,7 @@ char* Units::totext(char *text, double seconds, int time_format, break; } case TIME_HMS2: { - seconds = fabs(seconds); + seconds = fabs(seconds) + 1.0e-6; hour = seconds/3600; minute = seconds/60 - hour*60; second = seconds - (hour*3600 + minute*60); @@ -193,7 +191,7 @@ char* Units::totext(char *text, double seconds, int time_format, break; } case TIME_HMS3: { - seconds = fabs(seconds); + seconds = fabs(seconds) + 1.0e-6; hour = seconds/3600; minute = seconds/60 - hour*60; second = seconds - (hour*3600 + minute*60); @@ -201,7 +199,7 @@ char* Units::totext(char *text, double seconds, int time_format, break; } case TIME_HMSF: { - seconds = fabs(seconds); + seconds = fabs(seconds) + 1.0e-6; hour = seconds/3600; minute = seconds/60 - hour*60; second = seconds - (hour*3600 + minute*60); @@ -232,7 +230,7 @@ char* Units::totext(char *text, double seconds, int time_format, break; } case TIME_MS1: { - seconds = fabs(seconds); + seconds = fabs(seconds) + 1.0e-6; minute = seconds/60; second = seconds - minute*60; sprintf(text, "%d:%02d", minute, second); @@ -240,7 +238,7 @@ char* Units::totext(char *text, double seconds, int time_format, case TIME_MS2: { int sign = seconds >= 0 ? '+' : '-'; - seconds = fabs(seconds); + seconds = fabs(seconds) + 1.0e-6; minute = seconds/60; second = seconds - minute*60; sprintf(text, "%c%d:%02d", sign, minute, second); @@ -622,14 +620,13 @@ const char* Units::format_to_separators(int time_format) void Units::punctuate(char *string) { - int len = strlen(string); - int commas = (len - 1) / 3; - for(int i = len + commas, j = len, k; j >= 0 && i >= 0; i--, j--) { - k = (len - j - 1) / 3; - if(k * 3 == len - j - 1 && j != len - 1 && string[j] != 0) { - string[i--] = ','; - } - string[i] = string[j]; + int sep = ',', len = strlen(string), commas = (len - 1) / 3; + char *cp = string + len, *bp = cp + commas; + *bp = 0; + for( int k=3; cp < bp && bp > string; ) { + *--bp = *--cp; + if( --k > 0 ) continue; + *--bp = sep; k = 3; } }