r665: Merged the official release 2.0.
[cinelerra_cv.git] / guicast / units.h
blob4079d80e94770a11bc364b4cb3fb9fa3908bc0e9
1 #ifndef UNITS_H
2 #define UNITS_H
4 #include "sizes.h"
6 #include <math.h>
7 #include <stdint.h>
8 #include <stdio.h>
11 #define INFINITYGAIN -40
12 #define MAXGAIN 50
13 #define TOTALFREQS 1024
15 // h:mm:ss.sss
16 #define TIME_HMS 0
17 // h:mm:ss
18 #define TIME_HMS2 6
19 // hh:mm:ss
20 #define TIME_HMS3 7
21 // h:mm:ss:ff
22 #define TIME_SECONDS 8
23 #define TIME_HMSF 1
24 #define TIME_SAMPLES 2
25 #define TIME_SAMPLES_HEX 3
26 #define TIME_FRAMES 4
27 // fffff-ff
28 #define TIME_FEET_FRAMES 5
30 class DB
32 public:
33 DB(float infinitygain = INFINITYGAIN);
34 virtual ~DB() {};
36 // return power of db using a table
37 float fromdb_table();
38 float fromdb_table(float db);
39 // return power from db using log10
40 float fromdb();
41 static float fromdb(float db);
43 // convert db to power using a formula
44 static float todb(float power);
46 inline DB& operator++() { if(db < MAXGAIN) db += 0.1; return *this; };
47 inline DB& operator--() { if(db > INFINITYGAIN) db -= 0.1; return *this; };
48 inline DB& operator=(DB &newdb) { db = newdb.db; return *this; };
49 inline DB& operator=(int newdb) { db = newdb; return *this; };
50 inline int operator==(DB &newdb) { return db == newdb.db; };
51 inline int operator==(int newdb) { return db == newdb; };
53 static float *topower;
54 float db;
55 float infinitygain;
56 private:
57 static float *allocated;
60 // Third octave frequency table
61 class Freq
63 public:
64 Freq();
65 Freq(const Freq& oldfreq);
66 virtual ~Freq() {};
68 static void init_table();
70 // set freq to index given
71 static int tofreq(int index);
73 // return index of frequency
74 int fromfreq();
75 static int fromfreq(int index);
77 // increment frequency by one
78 Freq& operator++();
79 Freq& operator--();
81 int operator>(Freq &newfreq);
82 int operator<(Freq &newfreq);
83 Freq& operator=(const Freq &newfreq);
84 int operator=(const int newfreq);
85 int operator!=(Freq &newfreq);
86 int operator==(Freq &newfreq);
87 int operator==(int newfreq);
89 static int *freqtable;
90 int freq;
94 class Units
96 public:
97 Units() {};
99 // No rounding.
100 static float toframes(int64_t samples, int sample_rate, float framerate);
101 // Round up if > .5
102 static int64_t toframes_round(int64_t samples, int sample_rate, float framerate);
103 static double fix_framerate(double value);
104 static double atoframerate(char *text);
106 // Punctuate with commas
107 static void punctuate(char *string);
110 // separator strings for BC_TextBox::set_separators
111 // Returns 0 if the format has no separators.
112 static char* format_to_separators(int time_format);
114 static int64_t tosamples(float frames, int sample_rate, float framerate);
115 // give text representation as time
116 static char* totext(char *text,
117 int64_t samples,
118 int time_format,
119 int samplerate,
120 float frame_rate = 0,
121 float frames_per_foot = 0);
122 // give text representation as time
123 static char* totext(char *text,
124 double seconds,
125 int time_format,
126 int sample_rate = 0,
127 float frame_rate = 0,
128 float frames_per_foot = 0);
129 // convert time to samples
130 static int64_t fromtext(char *text,
131 int samplerate,
132 int time_format,
133 float frame_rate,
134 float frames_per_foot);
135 // Convert text to seconds
136 static double text_to_seconds(char *text,
137 int samplerate,
138 int time_format,
139 float frame_rate,
140 float frames_per_foot);
142 static char* print_time_format(int time_format, char *string);
144 static float xy_to_polar(int x, int y);
145 static void polar_to_xy(float angle, int radius, int &x, int &y);
147 // Numbers < 0 round down if next digit is < 5
148 // Numbers > 0 round up if next digit is > 5
149 static int64_t round(double result);
151 // Flooring type converter rounded to nearest .001
152 static int64_t to_int64(double result);
154 static float quantize10(float value);
155 static float quantize(float value, float precision);
157 static void* int64_to_ptr(uint64_t value);
158 static uint64_t ptr_to_int64(void *ptr);
162 #endif