7e2243ef498b673f29798760756a55a1f0be38fb
[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 // slots per octave
36 #define OCTAVE 105
37 #define TOTAL_TIMEFORMATS 7
38
39 // h:mm:ss.sss
40 #define TIME_HMS 0
41 #define TIME_HMS_TEXT _("Hours:Minutes:Seconds.xxx")
42 // h:mm:ss:ff
43 #define TIME_HMSF 1
44 #define TIME_HMSF_TEXT _("Hours:Minutes:Seconds:Frames")
45 #define TIME_SAMPLES 2
46 #define TIME_SAMPLES_TEXT _("Samples")
47 #define TIME_SAMPLES_HEX 3
48 #define TIME_SAMPLES_HEX_TEXT _("Hex Samples")
49 #define TIME_FRAMES 4
50 #define TIME_FRAMES_TEXT _("Frames")
51 // fffff-ff
52 #define TIME_FEET_FRAMES 5
53 #define TIME_SECONDS__STR      "ssss.sss"
54 #define TIME_HMS__STR          "h:mm:ss.sss"
55 #define TIME_HMS2__STR         "h:mm:ss"
56 #define TIME_HMS3__STR         "hh:mm:ss"
57 #define TIME_HMSF__STR         "h:mm:ss:ff"
58 #define TIME_SAMPLES__STR      "audio samples"
59 #define TIME_SAMPLES_HEX__STR  "audio samples (hex)"
60 #define TIME_FRAMES__STR       "video frames"
61 #define TIME_FEET_FRAMES__STR  "video frames (feet)"
62 #define TIME_FEET_FRAMES_TEXT _("Feet-frames")
63
64
65 // h:mm:ss
66 #define TIME_HMS2 6
67 #define TIME_HMS2_TEXT  _("Hours:Minutes:Seconds")
68 // hh:mm:ss
69 #define TIME_HMS3 7
70 #define TIME_HMS3_TEXT _("Hours:Minutes:Seconds")
71
72 #define TIME_SECONDS 8
73 #define TIME_SECONDS_TEXT _("Seconds")
74 // m:ss
75 #define TIME_MS1 9
76 // +m:ss
77 #define TIME_MS2 10
78 #define TIME_MS2_TEXT _("Minutes:Seconds")
79
80 class Units;
81
82 class DB
83 {
84         friend class Units;
85 public:
86         DB(float infinitygain = INFINITYGAIN);
87         virtual ~DB() {};
88
89 // return power of db using a table
90         float fromdb_table();
91         float fromdb_table(float db);
92 // return power from db using log10
93         float fromdb();
94         static float fromdb(float db);
95
96 // convert db to power using a formula
97         static float todb(float power);
98
99         inline DB& operator++() { if(db < MAXGAIN) db += 0.1; return *this; };
100         inline DB& operator--() { if(db > INFINITYGAIN) db -= 0.1; return *this; };
101         inline DB& operator=(DB &newdb) { db = newdb.db; return *this; };
102         inline DB& operator=(int newdb) { db = newdb; return *this; };
103         inline int operator==(DB &newdb) { return db == newdb.db; };
104         inline int operator==(int newdb) { return db == newdb; };
105
106         float *topower;
107         float db;
108         float infinitygain;
109  private:
110         static float *topower_base;
111 };
112
113 // Third octave frequency table
114 class Freq
115 {
116         friend class Units;
117 public:
118         Freq();
119         Freq(const Freq& oldfreq);
120         virtual ~Freq() {};
121
122         static void init_table();
123
124 // set freq to index given
125         static int tofreq(int index);
126
127 // return index of frequency
128         int fromfreq();
129         static int fromfreq(int index);
130         static double tofreq_f(double index);
131         static double fromfreq_f(double freq);
132 // increment frequency by one
133         Freq& operator++();
134         Freq& operator--();
135
136         int operator>(Freq &newfreq);
137         int operator<(Freq &newfreq);
138         Freq& operator=(const Freq &newfreq);
139         int operator=(const int newfreq);
140         int operator!=(Freq &newfreq);
141         int operator==(Freq &newfreq);
142         int operator==(int newfreq);
143
144         static int *freqtable;
145         int freq;
146 };
147
148
149 class Units
150 {
151 public:
152         Units() {};
153         static void init();
154         static void finit();
155
156         static int timeformat_totype(char *tcf);
157
158 // No rounding.
159         static float toframes(int64_t samples, int sample_rate, float framerate);
160 // Round up if > .5
161         static int64_t toframes_round(int64_t samples, int sample_rate, float framerate);
162         static double fix_framerate(double value);
163         static double atoframerate(const char *text);
164         static int64_t get_int64(const char*&cp);
165         static double get_double(const char*&cp);
166         static void skip_seperators(const char*&cp);
167
168 // Punctuate with commas
169         static void punctuate(char *string);
170
171
172 // separator strings for BC_TextBox::set_separators
173 // Returns 0 if the format has no separators.
174         static const char* format_to_separators(int time_format);
175         static int text_to_format(const char *string);
176         static const char* print_time_format(int time_format, char *string);
177
178         static int64_t tosamples(double frames, int sample_rate, float framerate);
179 // give text representation as time
180         static char* totext(char *text, int64_t samples, int time_format,
181                 int samplerate, float frame_rate = 0, float frames_per_foot = 0);
182 // give text representation as time
183         static char* totext(char *text, double seconds, int time_format,
184                 int sample_rate = 0, float frame_rate = 0, float frames_per_foot = 0);
185 // convert time to samples
186         static int64_t fromtext(const char *text, int samplerate,
187                 int time_format, float frame_rate, float frames_per_foot);
188 // Convert text to seconds
189         static double text_to_seconds(const char *text, int samplerate,
190                 int time_format, float frame_rate = 0, float frames_per_foot = 0);
191         static char* size_totext(int64_t bytes, char *text);
192
193         static float xy_to_polar(int x, int y);
194         static void polar_to_xy(float angle, int radius, int &x, int &y);
195
196 // Numbers < 0 round down if next digit is < 5
197 // Numbers > 0 round up if next digit is > 5
198         static int64_t round(double result);
199
200 // Flooring type converter rounded to nearest .001
201         static int64_t to_int64(double result);
202
203         static float quantize10(float value);
204         static float quantize(float value, float precision);
205
206         static void* int64_to_ptr(uint64_t value);
207         static uint64_t ptr_to_int64(void *ptr);
208
209 // Comparisons between double seem to work more often when this is called
210 // on the comparison values.
211         static void fix_double(double *x);
212 };
213
214 #endif