dvb chan tuner api upgrade, slip/ripple handle drag keyfrm fix, load menu tweaks
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / units.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifndef UNITS_H
23 #define UNITS_H
24
25 #include "sizes.h"
26
27 #include <math.h>
28 #include <stdint.h>
29 #include <stdio.h>
30
31
32 #define INFINITYGAIN -96
33 #define MAXGAIN 50
34 #define TOTALFREQS 1024
35 #define TOTAL_TIMEFORMATS 7
36
37 // h:mm:ss.sss
38 #define TIME_HMS 0
39 #define TIME_HMS_TEXT _("Hours:Minutes:Seconds.xxx")
40 // h:mm:ss:ff
41 #define TIME_HMSF 1
42 #define TIME_HMSF_TEXT _("Hours:Minutes:Seconds:Frames")
43 #define TIME_SAMPLES 2
44 #define TIME_SAMPLES_TEXT _("Samples")
45 #define TIME_SAMPLES_HEX 3
46 #define TIME_SAMPLES_HEX_TEXT _("Hex Samples")
47 #define TIME_FRAMES 4
48 #define TIME_FRAMES_TEXT _("Frames")
49 // fffff-ff
50 #define TIME_FEET_FRAMES 5
51 #define TIME_SECONDS__STR      "ssss.sss"
52 #define TIME_HMS__STR          "h:mm:ss.sss"
53 #define TIME_HMS2__STR         "h:mm:ss"
54 #define TIME_HMS3__STR         "hh:mm:ss"
55 #define TIME_HMSF__STR         "h:mm:ss:ff"
56 #define TIME_SAMPLES__STR      "audio samples"
57 #define TIME_SAMPLES_HEX__STR  "audio samples (hex)"
58 #define TIME_FRAMES__STR       "video frames"
59 #define TIME_FEET_FRAMES__STR  "video frames (feet)"
60 #define TIME_FEET_FRAMES_TEXT _("Feet-frames")
61
62
63 // h:mm:ss
64 #define TIME_HMS2 6
65 #define TIME_HMS2_TEXT  _("Hours:Minutes:Seconds")
66 // hh:mm:ss
67 #define TIME_HMS3 7
68 #define TIME_HMS3_TEXT _("Hours:Minutes:Seconds")
69
70 #define TIME_SECONDS 8
71 #define TIME_SECONDS_TEXT _("Seconds")
72 // m:ss
73 #define TIME_MS1 9
74 // +m:ss
75 #define TIME_MS2 10
76 #define TIME_MS2_TEXT _("Minutes:Seconds")
77
78 class Units;
79
80 class DB
81 {
82         friend class Units;
83 public:
84         DB(float infinitygain = INFINITYGAIN);
85         virtual ~DB() {};
86
87 // return power of db using a table
88         float fromdb_table();
89         float fromdb_table(float db);
90 // return power from db using log10
91         float fromdb();
92         static float fromdb(float db);
93
94 // convert db to power using a formula
95         static float todb(float power);
96
97         inline DB& operator++() { if(db < MAXGAIN) db += 0.1; return *this; };
98         inline DB& operator--() { if(db > INFINITYGAIN) db -= 0.1; return *this; };
99         inline DB& operator=(DB &newdb) { db = newdb.db; return *this; };
100         inline DB& operator=(int newdb) { db = newdb; return *this; };
101         inline int operator==(DB &newdb) { return db == newdb.db; };
102         inline int operator==(int newdb) { return db == newdb; };
103
104         float *topower;
105         float db;
106         float infinitygain;
107  private:
108         static float *topower_base;
109 };
110
111 // Third octave frequency table
112 class Freq
113 {
114         friend class Units;
115 public:
116         Freq();
117         Freq(const Freq& oldfreq);
118         virtual ~Freq() {};
119
120         static void init_table();
121
122 // set freq to index given
123         static int tofreq(int index);
124
125 // return index of frequency
126         int fromfreq();
127         static int fromfreq(int index);
128
129 // increment frequency by one
130         Freq& operator++();
131         Freq& operator--();
132
133         int operator>(Freq &newfreq);
134         int operator<(Freq &newfreq);
135         Freq& operator=(const Freq &newfreq);
136         int operator=(const int newfreq);
137         int operator!=(Freq &newfreq);
138         int operator==(Freq &newfreq);
139         int operator==(int newfreq);
140
141         static int *freqtable;
142         int freq;
143 };
144
145
146 class Units
147 {
148 public:
149         Units() {};
150         static void init();
151         static void finit();
152
153         static int timeformat_totype(char *tcf);
154
155 // No rounding.
156         static float toframes(int64_t samples, int sample_rate, float framerate);
157 // Round up if > .5
158         static int64_t toframes_round(int64_t samples, int sample_rate, float framerate);
159         static double fix_framerate(double value);
160         static double atoframerate(const char *text);
161         static int64_t get_int64(const char*&cp);
162         static double get_double(const char*&cp);
163         static void skip_seperators(const char*&cp);
164
165 // Punctuate with commas
166         static void punctuate(char *string);
167
168
169 // separator strings for BC_TextBox::set_separators
170 // Returns 0 if the format has no separators.
171         static const char* format_to_separators(int time_format);
172         static int text_to_format(const char *string);
173         static const char* print_time_format(int time_format, char *string);
174
175         static int64_t tosamples(double frames, int sample_rate, float framerate);
176 // give text representation as time
177         static char* totext(char *text, int64_t samples, int time_format,
178                 int samplerate, float frame_rate = 0, float frames_per_foot = 0);
179 // give text representation as time
180         static char* totext(char *text, double seconds, int time_format,
181                 int sample_rate = 0, float frame_rate = 0, float frames_per_foot = 0);
182 // convert time to samples
183         static int64_t fromtext(const char *text, int samplerate,
184                 int time_format, float frame_rate, float frames_per_foot);
185 // Convert text to seconds
186         static double text_to_seconds(const char *text, int samplerate,
187                 int time_format, float frame_rate = 0, float frames_per_foot = 0);
188         static char* size_totext(int64_t bytes, char *text);
189
190         static float xy_to_polar(int x, int y);
191         static void polar_to_xy(float angle, int radius, int &x, int &y);
192
193 // Numbers < 0 round down if next digit is < 5
194 // Numbers > 0 round up if next digit is > 5
195         static int64_t round(double result);
196
197 // Flooring type converter rounded to nearest .001
198         static int64_t to_int64(double result);
199
200         static float quantize10(float value);
201         static float quantize(float value, float precision);
202
203         static void* int64_to_ptr(uint64_t value);
204         static uint64_t ptr_to_int64(void *ptr);
205
206 // Comparisons between double seem to work more often when this is called
207 // on the comparison values.
208         static void fix_double(double *x);
209 };
210
211 #endif