r877: Fix files that were missing from a make dist tarball.
[cinelerra_cv.git] / guicast / units.h
blob88d3600fcb29cb3ee56f942ae5cf3ff1a4c7324d
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
29 #define TIME_SECONDS__STR "ssss.sss"
30 #define TIME_HMS__STR "h:mm:ss.sss"
31 #define TIME_HMS2__STR "h:mm:ss"
32 #define TIME_HMS3__STR "hh:mm:ss"
33 #define TIME_HMSF__STR "h:mm:ss:ff"
34 #define TIME_SAMPLES__STR "audio samples"
35 #define TIME_SAMPLES_HEX__STR "audio samples (hex)"
36 #define TIME_FRAMES__STR "video frames"
37 #define TIME_FEET_FRAMES__STR "video frames (feet)"
39 class DB
41 public:
42 DB(float infinitygain = INFINITYGAIN);
43 virtual ~DB() {};
45 // return power of db using a table
46 float fromdb_table();
47 float fromdb_table(float db);
48 // return power from db using log10
49 float fromdb();
50 static float fromdb(float db);
52 // convert db to power using a formula
53 static float todb(float power);
55 inline DB& operator++() { if(db < MAXGAIN) db += 0.1; return *this; };
56 inline DB& operator--() { if(db > INFINITYGAIN) db -= 0.1; return *this; };
57 inline DB& operator=(DB &newdb) { db = newdb.db; return *this; };
58 inline DB& operator=(int newdb) { db = newdb; return *this; };
59 inline int operator==(DB &newdb) { return db == newdb.db; };
60 inline int operator==(int newdb) { return db == newdb; };
62 static float *topower;
63 float db;
64 float infinitygain;
65 private:
66 static float *allocated;
69 // Third octave frequency table
70 class Freq
72 public:
73 Freq();
74 Freq(const Freq& oldfreq);
75 virtual ~Freq() {};
77 static void init_table();
79 // set freq to index given
80 static int tofreq(int index);
82 // return index of frequency
83 int fromfreq();
84 static int fromfreq(int index);
86 // increment frequency by one
87 Freq& operator++();
88 Freq& operator--();
90 int operator>(Freq &newfreq);
91 int operator<(Freq &newfreq);
92 Freq& operator=(const Freq &newfreq);
93 int operator=(const int newfreq);
94 int operator!=(Freq &newfreq);
95 int operator==(Freq &newfreq);
96 int operator==(int newfreq);
98 static int *freqtable;
99 int freq;
103 class Units
105 public:
106 Units() {};
108 static int timeformat_totype(char *tcf);
110 // No rounding.
111 static float toframes(int64_t samples, int sample_rate, float framerate);
112 // Round up if > .5
113 static int64_t toframes_round(int64_t samples, int sample_rate, float framerate);
114 static double fix_framerate(double value);
115 static double atoframerate(char *text);
118 // Punctuate with commas
119 static void punctuate(char *string);
122 // separator strings for BC_TextBox::set_separators
123 // Returns 0 if the format has no separators.
124 static char* format_to_separators(int time_format);
126 static int64_t tosamples(float frames, int sample_rate, float framerate);
127 // give text representation as time
128 static char* totext(char *text,
129 int64_t samples,
130 int time_format,
131 int samplerate,
132 float frame_rate = 0,
133 float frames_per_foot = 0);
134 // give text representation as time
135 static char* totext(char *text,
136 double seconds,
137 int time_format,
138 int sample_rate = 0,
139 float frame_rate = 0,
140 float frames_per_foot = 0);
141 // convert time to samples
142 static int64_t fromtext(char *text,
143 int samplerate,
144 int time_format,
145 float frame_rate,
146 float frames_per_foot);
147 // Convert text to seconds
148 static double text_to_seconds(char *text,
149 int samplerate,
150 int time_format,
151 float frame_rate,
152 float frames_per_foot);
154 static char* print_time_format(int time_format, char *string);
156 static float xy_to_polar(int x, int y);
157 static void polar_to_xy(float angle, int radius, int &x, int &y);
159 // Numbers < 0 round down if next digit is < 5
160 // Numbers > 0 round up if next digit is > 5
161 static int64_t round(double result);
163 // Flooring type converter rounded to nearest .001
164 static int64_t to_int64(double result);
166 static float quantize10(float value);
167 static float quantize(float value, float precision);
169 static void* int64_to_ptr(uint64_t value);
170 static uint64_t ptr_to_int64(void *ptr);
172 // Comparisons between double seem to work more often when this is called
173 // on the comparison values.
174 static void fix_double(double *x);
177 #endif