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