rework keyframe hide popup, keyframe auto render, textbox set_selection wide text
[goodguy/history.git] / cinelerra-5.1 / guicast / units.C
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 #include "bcwindowbase.inc"
23 #include "units.h"
24 #include "language.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29
30 float* DB::topower = 0;
31 float* DB::topower_base = 0;
32 int* Freq::freqtable = 0;
33
34
35 DB::DB(float infinitygain)
36 {
37         this->infinitygain = infinitygain;
38         if(!topower) { // db to power table
39                 topower_base = new float[(MAXGAIN - INFINITYGAIN) * 10 + 1];
40                 topower = topower_base + -INFINITYGAIN * 10;
41                 for(int i = INFINITYGAIN * 10; i <= MAXGAIN * 10; i++) {
42                         topower[i] = pow(10, (float)i / 10 / 20);
43 //printf("%f %f\n", (float)i/10, topower[i]);
44                 }
45                 topower[INFINITYGAIN * 10] = 0;   // infinity gain
46         }
47         db = 0;
48 }
49
50 float DB::fromdb_table()
51 {
52         return db = topower[(int)(db*10)];
53 }
54
55 float DB::fromdb_table(float db)
56 {
57         if(db > MAXGAIN) db = MAXGAIN;
58         if(db <= INFINITYGAIN) return 0;
59         return db = topower[(int)(db*10)];
60 }
61
62 float DB::fromdb()
63 {
64         return pow(10, db/20);
65 }
66
67 float DB::fromdb(float db)
68 {
69         return pow(10, db/20);
70 }
71
72 // set db to the power given using a formula
73 float DB::todb(float power)
74 {
75         float db;
76         if(power > 0) {
77                 db = 20 * log10(power);
78                 if(db < -100) db = -100;
79         }
80         else
81                 db = -100;
82         return db;
83 }
84
85
86 Freq::Freq()
87 {
88         if( !freqtable ) init_table();
89         freq = 0;
90 }
91
92 Freq::Freq(const Freq& oldfreq)
93 {
94         this->freq = oldfreq.freq;
95 }
96
97 void Freq::init_table()
98 {
99         freqtable = new int[TOTALFREQS + 1];
100 // starting frequency
101         double freq1 = 27.5, freq2 = 55;
102 // Some number divisable by three.  This depends on the value of TOTALFREQS
103         int scale = 105;
104
105         freqtable[0] = 0;
106         for(int i = 1, j = 0; i <= TOTALFREQS; i++, j++) {
107                 freqtable[i] = (int)(freq1 + (freq2 - freq1) / scale * j + 0.5);
108 //printf("Freq::init_table %d\n", freqtable[i]);
109                 if(j >= scale) {
110                         freq1 = freq2;
111                         freq2 *= 2;
112                         j = 0;
113                 }
114         }
115 }
116
117 int Freq::fromfreq()
118 {
119         int i = 0;
120         if( !freqtable ) init_table();
121         while( i<TOTALFREQS && freqtable[i]<freq ) ++i;
122         return i;
123 };
124
125 int Freq::fromfreq(int index)
126 {
127         int i = 0;
128         if( !freqtable ) init_table();
129         while( i<TOTALFREQS && freqtable[i]<index ) ++i;
130         return i;
131 };
132
133 int Freq::tofreq(int index)
134 {
135         if( !freqtable ) init_table();
136         if(index >= TOTALFREQS) index = TOTALFREQS - 1;
137         return freqtable[index];
138 }
139
140 Freq& Freq::operator++()
141 {
142         if(freq < TOTALFREQS) ++freq;
143         return *this;
144 }
145
146 Freq& Freq::operator--()
147 {
148         if(freq > 0) --freq;
149         return *this;
150 }
151
152 int Freq::operator>(Freq &newfreq) { return freq > newfreq.freq; }
153 int Freq::operator<(Freq &newfreq) { return freq < newfreq.freq; }
154 Freq& Freq::operator=(const Freq &newfreq) { freq = newfreq.freq; return *this; }
155 int Freq::operator=(const int newfreq) { freq = newfreq; return newfreq; }
156 int Freq::operator!=(Freq &newfreq) { return freq != newfreq.freq; }
157 int Freq::operator==(Freq &newfreq) { return freq == newfreq.freq; }
158 int Freq::operator==(int newfreq) { return freq == newfreq; }
159
160 // give text representation as time
161 char* Units::totext(char *text, double seconds, int time_format,
162                         int sample_rate, float frame_rate, float frames_per_foot)
163 {
164         int64_t hour, feet, frame;
165         int minute, second, thousandths;
166
167         switch(time_format) {
168         case TIME_SECONDS: {
169                 seconds = fabs(seconds);
170                 second = seconds;
171                 seconds -= (int64_t)seconds;
172                 thousandths = (int64_t)(seconds*1000) % 1000;
173                 sprintf(text, "%04d.%03d", second, thousandths);
174                 break; }
175
176         case TIME_HMS: {
177                 seconds = fabs(seconds);
178                 hour = seconds/3600;
179                 minute = seconds/60 - hour*60;
180                 second = seconds - (hour*3600 + minute*60);
181                 seconds -= (int64_t)seconds;
182                 thousandths = (int64_t)(seconds*1000) % 1000;
183                 sprintf(text, "%d:%02d:%02d.%03d",
184                         (int)hour, minute, second, thousandths);
185                 break; }
186
187         case TIME_HMS2: {
188                 seconds = fabs(seconds);
189                 hour = seconds/3600;
190                 minute = seconds/60 - hour*60;
191                 second = seconds - (hour*3600 + minute*60);
192                 sprintf(text, "%d:%02d:%02d", (int)hour, minute, second);
193                 break; }
194
195         case TIME_HMS3: {
196                 seconds = fabs(seconds);
197                 hour = seconds/3600;
198                 minute = seconds/60 - hour*60;
199                 second = seconds - (hour*3600 + minute*60);
200                 sprintf(text, "%02d:%02d:%02d", (int)hour, minute, second);
201                 break; }
202
203         case TIME_HMSF: {
204                 seconds = fabs(seconds);
205                 hour = seconds/3600;
206                 minute = seconds/60 - hour*60;
207                 second = seconds - (hour*3600 + minute*60);
208                 seconds -= (int64_t)seconds;
209 //              frame = round(frame_rate * (seconds-(int)seconds));
210                 frame = frame_rate*seconds + 1.0e-6;
211                 sprintf(text, "%01d:%02d:%02d:%02d", (int)hour, minute, second, (int)frame);
212                 break; }
213
214         case TIME_SAMPLES: {
215                 sprintf(text, "%09jd", to_int64(seconds * sample_rate));
216                 break; }
217
218         case TIME_SAMPLES_HEX: {
219                 sprintf(text, "%08jx", to_int64(seconds * sample_rate));
220                 break; }
221
222         case TIME_FRAMES: {
223                 frame = to_int64(seconds * frame_rate);
224                 sprintf(text, "%06jd", frame);
225                 break; }
226
227         case TIME_FEET_FRAMES: {
228                 frame = to_int64(seconds * frame_rate);
229                 feet = (int64_t)(frame / frames_per_foot);
230                 sprintf(text, "%05jd-%02jd", feet,
231                         (int64_t)(frame - feet * frames_per_foot));
232                 break; }
233
234         case TIME_MS1: {
235                 seconds = fabs(seconds);
236                 minute = seconds/60;
237                 second = seconds - minute*60;
238                 sprintf(text, "%d:%02d", minute, second);
239                 break; }
240
241         case TIME_MS2: {
242                 int sign = seconds >= 0 ? '+' : '-';
243                 seconds = fabs(seconds);
244                 minute = seconds/60;
245                 second = seconds - minute*60;
246                 sprintf(text, "%c%d:%02d", sign, minute, second);
247                 break; }
248         default: {
249                 *text = 0;
250                 break; }
251         }
252         return text;
253 }
254
255
256 // give text representation as time
257 char* Units::totext(char *text, int64_t samples, int samplerate,
258                 int time_format, float frame_rate, float frames_per_foot)
259 {
260         return totext(text, (double)samples/samplerate, time_format,
261                         samplerate, frame_rate, frames_per_foot);
262 }
263
264 int64_t Units::get_int64(const char *&bp)
265 {
266         char string[BCTEXTLEN], *sp=&string[0];
267         const char *cp = bp;
268         for( int j=0; j<10 && isdigit(*cp); ++j ) *sp++ = *cp++;
269         *sp = 0;  bp = cp;
270         return atol(string);
271 }
272
273 double Units::get_double(const char *&bp)
274 {
275         char string[BCTEXTLEN], *sp=&string[0];
276         const char *cp = bp;
277         for( int j=0; j<10 && isdigit(*cp); ++j ) *sp++ = *cp++;
278         if( *cp == '.' ) {
279                 *sp++ = *cp++;
280                 for( int j=0; j<10 && isdigit(*cp); ++j ) *sp++ = *cp++;
281         }
282         *sp = 0;  bp = cp;
283         return strtod(string,0);
284 }
285
286 void Units::skip_seperators(const char *&bp)
287 {
288         const char *cp = bp;
289         for( int j=0; j<10 && *cp && !isdigit(*cp); ++j ) ++cp;
290         bp = cp;
291 }
292
293 int64_t Units::fromtext(const char *text, int samplerate, int time_format,
294                         float frame_rate, float frames_per_foot)
295 {
296         int64_t hours, total_samples;
297         int minutes, frames, feet;
298         double seconds, total_seconds;
299         int sign = 1;
300
301         switch(time_format) {
302         case TIME_SECONDS: {
303                 total_seconds = get_double(text);
304                 break; }
305
306         case TIME_HMS:
307         case TIME_HMS2:
308         case TIME_HMS3: {
309                 hours = get_int64(text);    skip_seperators(text);
310                 minutes = get_int64(text);  skip_seperators(text);
311                 seconds = get_int64(text);
312                 total_seconds = seconds + minutes*60 + hours*3600;
313                 break; }
314
315         case TIME_HMSF: {
316                 hours = get_int64(text);    skip_seperators(text);
317                 minutes = get_int64(text);  skip_seperators(text);
318                 seconds = get_int64(text);  skip_seperators(text);
319                 frames = get_int64(text);
320                 total_seconds = frames/frame_rate + seconds + minutes*60 + hours*3600;
321                 break; }
322
323         case TIME_SAMPLES: {
324                 return get_int64(text); }
325
326         case TIME_SAMPLES_HEX: {
327                 sscanf(text, "%jx", &total_samples);
328                 return total_samples; }
329
330         case TIME_FRAMES: {
331                 total_seconds = get_double(text) / frame_rate;
332                 break; }
333
334         case TIME_FEET_FRAMES: {
335                 feet = get_int64(text);    skip_seperators(text);
336                 frames = get_int64(text);
337                 total_seconds = (feet*frames_per_foot + frames) / frame_rate;
338                 break; }
339
340         case TIME_MS2: {
341                 switch( *text ) {
342                 case '+':  sign = 1;   ++text;  break;
343                 case '-':  sign = -1;  ++text;  break;
344                 } } // fall through
345         case TIME_MS1: {
346                 minutes = get_int64(text);   skip_seperators(text);
347                 seconds = get_double(text);
348                 total_seconds = sign * (seconds + minutes*60);
349                 break; }
350         default: {
351                 total_seconds = 0;
352                 break; }
353         }
354
355         total_samples = total_seconds * samplerate;
356         return total_samples;
357 }
358
359 double Units::text_to_seconds(const char *text, int samplerate, int time_format,
360                                 float frame_rate, float frames_per_foot)
361 {
362         return (double)fromtext(text, samplerate, time_format,
363                                 frame_rate, frames_per_foot) / samplerate;
364 }
365
366
367
368
369 int Units::timeformat_totype(char *tcf)
370 {
371         if (!strcmp(tcf,TIME_SECONDS__STR)) return(TIME_SECONDS);
372         if (!strcmp(tcf,TIME_HMS__STR)) return(TIME_HMS);
373         if (!strcmp(tcf,TIME_HMS2__STR)) return(TIME_HMS2);
374         if (!strcmp(tcf,TIME_HMS3__STR)) return(TIME_HMS3);
375         if (!strcmp(tcf,TIME_HMSF__STR)) return(TIME_HMSF);
376         if (!strcmp(tcf,TIME_SAMPLES__STR)) return(TIME_SAMPLES);
377         if (!strcmp(tcf,TIME_SAMPLES_HEX__STR)) return(TIME_SAMPLES_HEX);
378         if (!strcmp(tcf,TIME_FRAMES__STR)) return(TIME_FRAMES);
379         if (!strcmp(tcf,TIME_FEET_FRAMES__STR)) return(TIME_FEET_FRAMES);
380         return(-1);
381 }
382
383
384 float Units::toframes(int64_t samples, int sample_rate, float framerate)
385 {
386         return (double)samples/sample_rate * framerate;
387 } // give position in frames
388
389 int64_t Units::toframes_round(int64_t samples, int sample_rate, float framerate)
390 {
391 // used in editing
392         float result_f = (float)samples / sample_rate * framerate;
393         int64_t result_l = (int64_t)(result_f + 0.5);
394         return result_l;
395 }
396
397 double Units::fix_framerate(double value)
398 {
399         if(value > 29.5 && value < 30)
400                 value = (double)30000 / (double)1001;
401         else if(value > 59.5 && value < 60)
402                 value = (double)60000 / (double)1001;
403         else if(value > 23.5 && value < 24)
404                 value = (double)24000 / (double)1001;
405         return value;
406 }
407
408 double Units::atoframerate(const char *text)
409 {
410         double result = get_double(text);
411         return fix_framerate(result);
412 }
413
414
415 int64_t Units::tosamples(double frames, int sample_rate, float framerate)
416 {
417         double result = frames/framerate * sample_rate;
418         if(result - (int)result >= 1.0e-6 ) result += 1;
419         return (int64_t)result;
420 } // give position in samples
421
422
423 float Units::xy_to_polar(int x, int y)
424 {
425         float angle = 0.;
426         if(x > 0 && y <= 0)
427                 angle = atan((float)-y / x) / (2*M_PI) * 360;
428         else if(x < 0 && y <= 0)
429                 angle = 180 - atan((float)-y / -x) / (2*M_PI) * 360;
430         else if(x < 0 && y > 0)
431                 angle = 180 - atan((float)-y / -x) / (2*M_PI) * 360;
432         else if(x > 0 && y > 0)
433                 angle = 360 + atan((float)-y / x) / (2*M_PI) * 360;
434         else if(x == 0 && y < 0)
435                 angle = 90;
436         else if(x == 0 && y > 0)
437                 angle = 270;
438         else if(x == 0 && y == 0)
439                 angle = 0;
440         return angle;
441 }
442
443 void Units::polar_to_xy(float angle, int radius, int &x, int &y)
444 {
445         if( angle < 0 )
446                 angle += ((int)(-angle)/360 + 1) * 360;
447         else if( angle >= 360 )
448                 angle -= ((int)(angle)/360) * 360;
449
450         x = (int)(cos(angle / 360 * (2*M_PI)) * radius);
451         y = (int)(-sin(angle / 360 * (2*M_PI)) * radius);
452 }
453
454 int64_t Units::round(double result)
455 {
456         return (int64_t)(result < 0 ? result - 0.5 : result + 0.5);
457 }
458
459 float Units::quantize10(float value)
460 {
461         int64_t temp = (int64_t)(value*10 + 0.5);
462         return temp/10.;
463 }
464
465 float Units::quantize(float value, float precision)
466 {
467         int64_t temp = (int64_t)(value/precision + 0.5);
468         return temp*precision;
469 }
470
471 int64_t Units::to_int64(double result)
472 {
473 // This must round up if result is one sample within ceiling.
474 // Sampling rates below 48000 may cause more problems.
475         return (int64_t)(result < 0 ? (result - 0.005) : (result + 0.005));
476 }
477
478 const char* Units::print_time_format(int time_format, char *string)
479 {
480         const char *fmt = "Unknown";
481         switch(time_format) {
482         case TIME_HMS:         fmt = TIME_HMS_TEXT;                   break;
483         case TIME_HMSF:        fmt = TIME_HMSF_TEXT;                  break;
484         case TIME_SAMPLES:     fmt = TIME_SAMPLES_TEXT;               break;
485         case TIME_SAMPLES_HEX: fmt = TIME_SAMPLES_HEX_TEXT;           break;
486         case TIME_FRAMES:      fmt = TIME_FRAMES_TEXT;                break;
487         case TIME_FEET_FRAMES: fmt = TIME_FEET_FRAMES_TEXT;           break;
488         case TIME_HMS2:
489         case TIME_HMS3:        fmt = TIME_HMS3_TEXT;                  break;
490         case TIME_SECONDS:     fmt = TIME_SECONDS_TEXT;               break;
491         case TIME_MS1:
492         case TIME_MS2:         fmt = TIME_MS2_TEXT;                   break;
493         }
494         return strcpy(string,fmt);
495 }
496
497 int Units::text_to_format(const char *string)
498 {
499         if(!strcmp(string, TIME_HMS_TEXT)) return TIME_HMS;
500         if(!strcmp(string, TIME_HMSF_TEXT)) return TIME_HMSF;
501         if(!strcmp(string, TIME_SAMPLES_TEXT)) return TIME_SAMPLES;
502         if(!strcmp(string, TIME_SAMPLES_HEX_TEXT)) return TIME_SAMPLES_HEX;
503         if(!strcmp(string, TIME_FRAMES_TEXT)) return TIME_FRAMES;
504         if(!strcmp(string, TIME_FEET_FRAMES_TEXT)) return TIME_FEET_FRAMES;
505         if(!strcmp(string, TIME_HMS3_TEXT)) return TIME_HMS3;
506         if(!strcmp(string, TIME_SECONDS_TEXT)) return TIME_SECONDS;
507         if(!strcmp(string, TIME_MS2_TEXT)) return TIME_MS2;
508         return TIME_HMS;
509 }
510
511 char* Units::size_totext(int64_t bytes, char *text)
512 {
513         char string[BCTEXTLEN];
514         static const char *sz[] = { "bytes", "KB", "MB", "GB", "TB" };
515
516         int i = (sizeof(sz) / sizeof(sz[0]));
517         while( --i > 0 && bytes < ((int64_t)1 << (10*i)) );
518
519         if( i > 0 ) {
520                 bytes >>= 10*(i-1);
521                 int frac = bytes % 1000;
522                 sprintf(string, "%jd", bytes/1000);
523                 if( bytes > 1000 ) punctuate(string);
524                 sprintf(text, "%s.%03d %s", string, frac, sz[i]);
525         }
526         else {
527                 sprintf(string, "%jd", bytes);
528                 if( bytes > 1000 ) punctuate(string);
529                 sprintf(text, "%s %s", string, sz[i]);
530         }
531         return text;
532 }
533
534
535 #undef BYTE_ORDER
536 #define BYTE_ORDER ((*(const uint32_t*)"a   ") & 0x00000001)
537
538 void* Units::int64_to_ptr(uint64_t value)
539 {
540         unsigned char *value_dissected = (unsigned char*)&value;
541         void *result;
542         unsigned char *data = (unsigned char*)&result;
543
544 // Must be done behind the compiler's back
545         if(sizeof(void*) == 4) {
546                 if(!BYTE_ORDER) {
547                         data[0] = value_dissected[4];
548                         data[1] = value_dissected[5];
549                         data[2] = value_dissected[6];
550                         data[3] = value_dissected[7];
551                 }
552                 else {
553                         data[0] = value_dissected[0];
554                         data[1] = value_dissected[1];
555                         data[2] = value_dissected[2];
556                         data[3] = value_dissected[3];
557                 }
558         }
559         else {
560                 data[0] = value_dissected[0];
561                 data[1] = value_dissected[1];
562                 data[2] = value_dissected[2];
563                 data[3] = value_dissected[3];
564                 data[4] = value_dissected[4];
565                 data[5] = value_dissected[5];
566                 data[6] = value_dissected[6];
567                 data[7] = value_dissected[7];
568         }
569         return result;
570 }
571
572 uint64_t Units::ptr_to_int64(void *ptr)
573 {
574         unsigned char *ptr_dissected = (unsigned char*)&ptr;
575         int64_t result = 0;
576         unsigned char *data = (unsigned char*)&result;
577 // Don't do this at home.
578         if(sizeof(void*) == 4) {
579                 if(!BYTE_ORDER) {
580                         data[4] = ptr_dissected[0];
581                         data[5] = ptr_dissected[1];
582                         data[6] = ptr_dissected[2];
583                         data[7] = ptr_dissected[3];
584                 }
585                 else {
586                         data[0] = ptr_dissected[0];
587                         data[1] = ptr_dissected[1];
588                         data[2] = ptr_dissected[2];
589                         data[3] = ptr_dissected[3];
590                 }
591         }
592         else {
593                 data[0] = ptr_dissected[0];
594                 data[1] = ptr_dissected[1];
595                 data[2] = ptr_dissected[2];
596                 data[3] = ptr_dissected[3];
597                 data[4] = ptr_dissected[4];
598                 data[5] = ptr_dissected[5];
599                 data[6] = ptr_dissected[6];
600                 data[7] = ptr_dissected[7];
601         }
602         return result;
603 }
604
605 const char* Units::format_to_separators(int time_format)
606 {
607         switch(time_format) {
608                 case TIME_SECONDS:     return "0000.000";
609                 case TIME_HMS:         return "0:00:00.000";
610                 case TIME_HMS2:        return "0:00:00";
611                 case TIME_HMS3:        return "00:00:00";
612                 case TIME_HMSF:        return "0:00:00:00";
613                 case TIME_SAMPLES:     return 0;
614                 case TIME_SAMPLES_HEX: return 0;
615                 case TIME_FRAMES:      return 0;
616                 case TIME_FEET_FRAMES: return "00000-00";
617                 case TIME_MS1:         return "0:00";
618                 case TIME_MS2:         return "+0:00";
619         }
620         return 0;
621 }
622
623 void Units::punctuate(char *string)
624 {
625         int len = strlen(string);
626         int commas = (len - 1) / 3;
627         for(int i = len + commas, j = len, k; j >= 0 && i >= 0; i--, j--) {
628                 k = (len - j - 1) / 3;
629                 if(k * 3 == len - j - 1 && j != len - 1 && string[j] != 0) {
630                         string[i--] = ',';
631                 }
632                 string[i] = string[j];
633         }
634 }
635
636 void Units::fix_double(double *x)
637 {
638         *x = *x;
639 }
640
641