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