add size numeric format in bcfilebox
[goodguy/history.git] / cinelerra-5.1 / guicast / units.C
index eed940dca20255a42539c02712093f47d1c98b6a..9c2f1d27106b7f4be2cee7ec37f70c5bd91edf04 100644 (file)
@@ -27,7 +27,6 @@
 #include <string.h>
 #include <ctype.h>
 
-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 && freqtable[i]<freq ) ++i;
        return i;
 };
@@ -125,14 +95,12 @@ int Freq::fromfreq()
 int Freq::fromfreq(int index)
 {
        int i = 0;
-       if( !freqtable ) init_table();
        while( i<TOTALFREQS && freqtable[i]<index ) ++i;
        return i;
 };
 
 int Freq::tofreq(int index)
 {
-       if( !freqtable ) init_table();
        if(index >= 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)
@@ -623,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;
        }
 }