home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / guicast / units.C < prev    next >
C/C++ Source or Header  |  2000-11-29  |  9KB  |  432 lines

  1. #include "units.h"
  2. #include <stdlib.h>
  3.  
  4. float* DB::topower = 0;
  5. int* Freq::freqtable = 0;
  6.  
  7. DB::DB(float infinitygain)
  8. {
  9.     this->infinitygain = infinitygain;
  10.     if(!topower)
  11.     {
  12.         int i;
  13.         float value;
  14.  
  15.         // db to power table
  16.         topower = new float[(MAXGAIN - INFINITYGAIN) * 10 + 1];
  17.         topower += -INFINITYGAIN * 10;
  18.         for(i = INFINITYGAIN * 10; i <= MAXGAIN * 10; i++)
  19.         {
  20.             topower[i] = pow(10, (float)i / 10 / 20);
  21.             
  22. //printf("%f %f\n", (float)i/10, topower[i]);
  23.         }
  24.         topower[INFINITYGAIN * 10] = 0;   // infinity gain
  25.     }
  26.     db = 0;
  27. }
  28.  
  29. float DB::fromdb_table() 
  30.     return db = topower[(int)(db * 10)]; 
  31. }
  32.  
  33. float DB::fromdb_table(float db) 
  34.     if(db > MAXGAIN) db = MAXGAIN;
  35.     if(db <= INFINITYGAIN) return 0;
  36.     return db = topower[(int)(db * 10)]; 
  37. }
  38.  
  39. float DB::fromdb()
  40. {
  41.     return pow(10, db / 20);
  42. }
  43.  
  44. float DB::fromdb(float db)
  45. {
  46.     return pow(10, db / 20);
  47. }
  48.  
  49. // set db to the power given using a formula
  50. float DB::todb(float power)
  51. {
  52.     float db;
  53.     if(power == 0) 
  54.         db = -100;
  55.     else 
  56.     {
  57.         db = (float)(20 * log10(power));
  58.         if(db < -100) db = -100;
  59.     }
  60.     return db;
  61. }
  62.  
  63.  
  64. Freq::Freq()
  65. {
  66.     if(!freqtable)
  67.     {
  68.           int i;
  69.  
  70.         freqtable = new int[TOTALFREQS + 1];
  71.           double freq = 28;  // starting frequency
  72.           long scale = 60;   // some number divisable by three
  73.  
  74.           freqtable[0] = 0;
  75.           for(i = 1; i <= TOTALFREQS; i++)
  76.           {
  77.             freqtable[i] = (int)freq;
  78.             freq = freq + freq / scale;
  79.           }
  80.     }
  81.     freq = 0;
  82. }
  83.  
  84. Freq::Freq(const Freq& oldfreq)
  85. {
  86.     this->freq = oldfreq.freq;
  87. }
  88.  
  89. int Freq::fromfreq() 
  90. {
  91.     int i;
  92.  
  93.       for(i = 0; i < TOTALFREQS && freqtable[i] < freq; i++)
  94.         ;
  95.       return(i);
  96. };
  97.  
  98. int Freq::fromfreq(int index) 
  99. {
  100.     int i;
  101.  
  102.       for(i = 0; i < TOTALFREQS && freqtable[i] < index; i++)
  103.         ;
  104.       return(i);
  105. };
  106.  
  107. int Freq::tofreq(int index) 
  108.     freq = freqtable[index]; 
  109.     return freq; 
  110. }
  111.  
  112. Freq& Freq::operator++() 
  113. {
  114.     if(freq < TOTALFREQS) freq++;
  115.     return *this;
  116. }
  117.     
  118. Freq& Freq::operator--()
  119. {
  120.     if(freq > 0) freq--;
  121.     return *this;
  122. }
  123.     
  124. int Freq::operator>(Freq &newfreq) { return freq > newfreq.freq; }
  125. int Freq::operator<(Freq &newfreq) { return freq < newfreq.freq; }
  126. Freq& Freq::operator=(const Freq &newfreq) { freq = newfreq.freq; return *this; }
  127. int Freq::operator=(const int newfreq) { freq = newfreq; return newfreq; }
  128. int Freq::operator!=(Freq &newfreq) { return freq != newfreq.freq; }
  129. int Freq::operator==(Freq &newfreq) { return freq == newfreq.freq; }
  130. int Freq::operator==(int newfreq) { return freq == newfreq; }
  131.  
  132. char* Units::totext(char *text, 
  133.             double seconds, 
  134.             int time_format, 
  135.             int sample_rate, 
  136.             float frame_rate, 
  137.             float frames_per_foot)    // give text representation as time
  138. {
  139.     int hour, minute;
  140.     long frame, feet;
  141.  
  142.     switch(time_format)
  143.     {
  144.         case TIME_HMS:
  145.         {
  146.             float second;
  147.  
  148.               hour = (int)(seconds / 3600);
  149.               minute = (int)(seconds / 60 - hour * 60);
  150.               second = (float)seconds - (long)hour * 3600 - (long)minute * 60;
  151.               sprintf(text, "%d:%d:%.3f", hour, minute, second);
  152.             return text;
  153.         }
  154.           break;
  155.         
  156.         case TIME_HMS2:
  157.         {
  158.             float second;
  159.  
  160.               hour = (int)(seconds / 3600);
  161.               minute = (int)(seconds / 60 - hour * 60);
  162.               second = (float)seconds - (long)hour * 3600 - (long)minute * 60;
  163.               sprintf(text, "%d:%02d:%02d", hour, minute, (int)second);
  164.             return text;
  165.         }
  166.           break;
  167.  
  168.         case TIME_HMSF:
  169.         {
  170.             int second;
  171.  
  172.               hour = (int)(seconds / 3600);
  173.               minute = (int)(seconds / 60 - hour * 60);
  174.               second = (int)(seconds - hour * 3600 - minute * 60);
  175.               frame = (long)(frame_rate * 
  176.                     (float)((float)seconds - (long)hour * 3600 - (long)minute * 60 - second))
  177.                     ;
  178.               sprintf(text, "%01d:%02d:%02d:%02ld", hour, minute, second, frame);
  179.             return text;
  180.         }
  181.             break;
  182.             
  183.         case TIME_SAMPLES:
  184.               sprintf(text, "%ld", (long)(seconds * sample_rate + 0.5));
  185.             break;
  186.         
  187.         case TIME_SAMPLES_HEX:
  188.               sprintf(text, "%x", (long)(seconds * sample_rate + 0.5));
  189.             break;
  190.         
  191.         case TIME_FRAMES:
  192.             frame = (long)(seconds * frame_rate + 0.5);
  193.             sprintf(text, "%ld", frame);
  194.             return text;
  195.             break;
  196.         
  197.         case TIME_FEET_FRAMES:
  198.             frame = (long)(seconds * frame_rate + 0.5);
  199.             feet = (long)(frame / frames_per_foot);
  200.             sprintf(text, "%ld-%ld", feet, (long)(frame - feet * frames_per_foot));
  201.             return text;
  202.             break;
  203.     }
  204.     return text;
  205. }
  206.  
  207.  
  208. char* Units::totext(char *text, 
  209.         long samples, 
  210.         int samplerate, 
  211.         int time_format, 
  212.         float frame_rate,
  213.         float frames_per_foot)
  214. {
  215.     return totext(text, (double)samples / samplerate, time_format, samplerate, frame_rate, frames_per_foot);
  216. }    // give text representation as time
  217.  
  218. long Units::fromtext(char *text, 
  219.             int samplerate, 
  220.             int time_format, 
  221.             float frame_rate,
  222.             float frames_per_foot)
  223. {
  224.     long hours, minutes, frames, total_samples, i, j;
  225.     long feet;
  226.     float seconds;
  227.     char string[256];
  228.     
  229.     switch(time_format)
  230.     {
  231.         case 0:
  232. // get hours
  233.             i = 0;
  234.             j = 0;
  235.             while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
  236.             string[j] = 0;
  237.             hours = atol(string);
  238. // get minutes
  239.             j = 0;
  240. // skip separator
  241.             while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
  242.             while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
  243.             string[j] = 0;
  244.             minutes = atol(string);
  245.             
  246. // get seconds
  247.             j = 0;
  248. // skip separator
  249.             while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
  250.             while((text[i] == '.' || (text[i] >=48 && text[i] <= 57)) && j < 10) string[j++] = text[i++];
  251.             string[j] = 0;
  252.             seconds = atof(string);
  253.             
  254.             total_samples = (long)(((float)seconds + minutes*60 + hours*3600) * samplerate);
  255.             return total_samples;
  256.             break;
  257.  
  258.         case 1:
  259. // get hours
  260.             i = 0;
  261.             j = 0;
  262.             while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
  263.             string[j] = 0;
  264.             hours = atol(string);
  265.             
  266. // get minutes
  267.             j = 0;
  268. // skip separator
  269.             while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
  270.             while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
  271.             string[j] = 0;
  272.             minutes = atol(string);
  273.             
  274. // get seconds
  275.             j = 0;
  276. // skip separator
  277.             while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
  278.             while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
  279.             string[j] = 0;
  280.             seconds = atof(string);
  281.             
  282. // skip separator
  283.             while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
  284. // get frames
  285.             j = 0;
  286.             while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
  287.             string[j] = 0;
  288.             frames = atol(string);
  289.             
  290.             total_samples = (long)(((float)frames / frame_rate + seconds + minutes*60 + hours*3600) * samplerate);
  291.             return total_samples;
  292.             break;
  293.  
  294.         case 2:
  295.             return atol(text);
  296.             break;
  297.         
  298.         case 3:
  299.             sscanf(text, "%x", &total_samples);
  300.             return total_samples;
  301.         
  302.         case 4:
  303.             return (long)(atof(text) / frame_rate * samplerate);
  304.             break;
  305.         
  306.         case 5:
  307. // Get feet
  308.             i = 0;
  309.             j = 0;
  310.             while(text[i] >=48 && text[i] <= 57 && text[i] != 0 && j < 10) string[j++] = text[i++];
  311.             string[j] = 0;
  312.             feet = atol(string);
  313.  
  314. // skip separator
  315.             while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
  316.  
  317. // Get frames
  318.             j = 0;
  319.             while(text[i] >=48 && text[i] <= 57 && text[i] != 0 && j < 10) string[j++] = text[i++];
  320.             string[j] = 0;
  321.             frames = atol(string);
  322.             return (long)(((float)feet * frames_per_foot + frames) / frame_rate * samplerate);
  323.             break;
  324.     }
  325.     return 0;
  326. }
  327.  
  328. double Units::text_to_seconds(char *text, 
  329.                 int samplerate, 
  330.                 int time_format, 
  331.                 float frame_rate, 
  332.                 float frames_per_foot)
  333. {
  334.     return (double)fromtext(text, samplerate, time_format, frame_rate, frames_per_foot) / samplerate;
  335. }
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342. float Units::toframes(long samples, int sample_rate, float framerate) 
  343.     return (float)samples / sample_rate * framerate; 
  344. } // give position in frames
  345.  
  346. long Units::toframes_round(long samples, int sample_rate, float framerate) 
  347. {
  348. // used in editing
  349.     float result_f = (float)samples / sample_rate * framerate; 
  350.     long result_l = (long)(result_f + 0.5);
  351.     return result_l;
  352. }
  353.  
  354. long Units::tosamples(float frames, int sample_rate, float framerate) 
  355.     float result = (frames / framerate * sample_rate);
  356.     
  357.     if(result - (int)result) result += 1;
  358.     return (long)result;
  359. } // give position in samples
  360.  
  361.  
  362. float Units::xy_to_polar(int x, int y)
  363. {
  364.     float angle;
  365.     if(x > 0 && y <= 0)
  366.     {
  367.         angle = atan((float)-y / x) / (2 * M_PI) * 360;
  368.     }
  369.     else
  370.     if(x < 0 && y <= 0)
  371.     {
  372.         angle = 180 - atan((float)-y / -x) / (2 * M_PI) * 360;
  373.     }
  374.     else
  375.     if(x < 0 && y > 0)
  376.     {
  377.         angle = 180 - atan((float)-y / -x) / (2 * M_PI) * 360;
  378.     }
  379.     else
  380.     if(x > 0 && y > 0)
  381.     {
  382.         angle = 360 + atan((float)-y / x) / (2 * M_PI) * 360;
  383.     }
  384.     else
  385.     if(x == 0 && y < 0)
  386.     {
  387.         angle = 90;
  388.     }
  389.     else
  390.     if(x == 0 && y > 0)
  391.     {
  392.         angle = 270;
  393.     }
  394.     else
  395.     if(x == 0 && y == 0)
  396.     {
  397.         angle = 0;
  398.     }
  399.  
  400.     return angle;
  401. }
  402.  
  403. void Units::polar_to_xy(float angle, int radius, int &x, int &y)
  404. {
  405.     while(angle < 0) angle += 360;
  406.  
  407.     x = (int)(cos(angle / 360 * (2 * M_PI)) * radius);
  408.     y = (int)(-sin(angle / 360 * (2 * M_PI)) * radius);
  409. }
  410.  
  411. long Units::round(double result)
  412. {
  413.     return (long)(result < 0 ? result - 0.5 : result + 0.5);
  414. }
  415.  
  416. float Units::quantize10(float value)
  417. {
  418.     long temp = (long)(value * 10 + 0.5);
  419.     value = (float)temp / 10;
  420.     return value;
  421. }
  422.  
  423.  
  424.  
  425.  
  426.  
  427.