4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcwindowbase.inc"
30 float* DB::topower = 0;
31 float* DB::topower_base = 0;
32 int* Freq::freqtable = 0;
35 DB::DB(float infinitygain)
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]);
45 topower[INFINITYGAIN * 10] = 0; // infinity gain
50 float DB::fromdb_table()
52 return db = topower[(int)(db*10)];
55 float DB::fromdb_table(float db)
57 if(db > MAXGAIN) db = MAXGAIN;
58 if(db <= INFINITYGAIN) return 0;
59 return db = topower[(int)(db*10)];
64 return pow(10, db/20);
67 float DB::fromdb(float db)
69 return pow(10, db/20);
72 // set db to the power given using a formula
73 float DB::todb(float power)
77 db = 20 * log10(power);
78 if(db < -100) db = -100;
88 if( !freqtable ) init_table();
92 Freq::Freq(const Freq& oldfreq)
94 this->freq = oldfreq.freq;
97 void Freq::init_table()
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
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]);
120 if( !freqtable ) init_table();
121 while( i<TOTALFREQS && freqtable[i]<freq ) ++i;
125 int Freq::fromfreq(int index)
128 if( !freqtable ) init_table();
129 while( i<TOTALFREQS && freqtable[i]<index ) ++i;
133 int Freq::tofreq(int index)
135 if( !freqtable ) init_table();
136 if(index >= TOTALFREQS) index = TOTALFREQS - 1;
137 return freqtable[index];
140 Freq& Freq::operator++()
142 if(freq < TOTALFREQS) ++freq;
146 Freq& Freq::operator--()
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; }
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)
164 int64_t hour, feet, frame;
165 int minute, second, thousandths;
167 switch(time_format) {
169 seconds = fabs(seconds);
171 seconds -= (int64_t)seconds;
172 thousandths = (int64_t)(seconds*1000) % 1000;
173 sprintf(text, "%04d.%03d", second, thousandths);
177 seconds = fabs(seconds);
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);
188 seconds = fabs(seconds);
190 minute = seconds/60 - hour*60;
191 second = seconds - (hour*3600 + minute*60);
192 sprintf(text, "%d:%02d:%02d", (int)hour, minute, second);
196 seconds = fabs(seconds);
198 minute = seconds/60 - hour*60;
199 second = seconds - (hour*3600 + minute*60);
200 sprintf(text, "%02d:%02d:%02d", (int)hour, minute, second);
204 seconds = fabs(seconds);
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);
215 sprintf(text, "%09jd", to_int64(seconds * sample_rate));
218 case TIME_SAMPLES_HEX: {
219 sprintf(text, "%08jx", to_int64(seconds * sample_rate));
223 frame = to_int64(seconds * frame_rate);
224 sprintf(text, "%06jd", frame);
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));
235 seconds = fabs(seconds);
237 second = seconds - minute*60;
238 sprintf(text, "%d:%02d", minute, second);
242 int sign = seconds >= 0 ? '+' : '-';
243 seconds = fabs(seconds);
245 second = seconds - minute*60;
246 sprintf(text, "%c%d:%02d", sign, minute, second);
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)
260 return totext(text, (double)samples/samplerate, time_format,
261 samplerate, frame_rate, frames_per_foot);
264 int64_t Units::get_int64(const char *&bp)
266 char string[BCTEXTLEN], *sp=&string[0];
268 for( int j=0; j<10 && isdigit(*cp); ++j ) *sp++ = *cp++;
273 double Units::get_double(const char *&bp)
275 char string[BCTEXTLEN], *sp=&string[0];
277 for( int j=0; j<10 && isdigit(*cp); ++j ) *sp++ = *cp++;
280 for( int j=0; j<10 && isdigit(*cp); ++j ) *sp++ = *cp++;
283 return strtod(string,0);
286 void Units::skip_seperators(const char *&bp)
289 for( int j=0; j<10 && *cp && !isdigit(*cp); ++j ) ++cp;
293 int64_t Units::fromtext(const char *text, int samplerate, int time_format,
294 float frame_rate, float frames_per_foot)
296 int64_t hours, total_samples;
297 int minutes, frames, feet;
298 double seconds, total_seconds;
301 switch(time_format) {
303 total_seconds = get_double(text);
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;
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;
324 return get_int64(text); }
326 case TIME_SAMPLES_HEX: {
327 sscanf(text, "%jx", &total_samples);
328 return total_samples; }
331 total_seconds = get_double(text) / frame_rate;
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;
342 case '+': sign = 1; ++text; break;
343 case '-': sign = -1; ++text; break;
346 minutes = get_int64(text); skip_seperators(text);
347 seconds = get_double(text);
348 total_seconds = sign * (seconds + minutes*60);
355 total_samples = total_seconds * samplerate;
356 return total_samples;
359 double Units::text_to_seconds(const char *text, int samplerate, int time_format,
360 float frame_rate, float frames_per_foot)
362 return (double)fromtext(text, samplerate, time_format,
363 frame_rate, frames_per_foot) / samplerate;
369 int Units::timeformat_totype(char *tcf)
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);
384 float Units::toframes(int64_t samples, int sample_rate, float framerate)
386 return (double)samples/sample_rate * framerate;
387 } // give position in frames
389 int64_t Units::toframes_round(int64_t samples, int sample_rate, float framerate)
392 float result_f = (float)samples / sample_rate * framerate;
393 int64_t result_l = (int64_t)(result_f + 0.5);
397 double Units::fix_framerate(double value)
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;
408 double Units::atoframerate(const char *text)
410 double result = get_double(text);
411 return fix_framerate(result);
415 int64_t Units::tosamples(double frames, int sample_rate, float framerate)
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
423 float Units::xy_to_polar(int x, int y)
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)
436 else if(x == 0 && y > 0)
438 else if(x == 0 && y == 0)
443 void Units::polar_to_xy(float angle, int radius, int &x, int &y)
446 angle += ((int)(-angle)/360 + 1) * 360;
447 else if( angle >= 360 )
448 angle -= ((int)(angle)/360) * 360;
450 x = (int)(cos(angle / 360 * (2*M_PI)) * radius);
451 y = (int)(-sin(angle / 360 * (2*M_PI)) * radius);
454 int64_t Units::round(double result)
456 return (int64_t)(result < 0 ? result - 0.5 : result + 0.5);
459 float Units::quantize10(float value)
461 int64_t temp = (int64_t)(value*10 + 0.5);
465 float Units::quantize(float value, float precision)
467 int64_t temp = (int64_t)(value/precision + 0.5);
468 return temp*precision;
471 int64_t Units::to_int64(double result)
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));
478 const char* Units::print_time_format(int time_format, char *string)
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;
489 case TIME_HMS3: fmt = TIME_HMS3_TEXT; break;
490 case TIME_SECONDS: fmt = TIME_SECONDS_TEXT; break;
492 case TIME_MS2: fmt = TIME_MS2_TEXT; break;
494 return strcpy(string,fmt);
497 int Units::text_to_format(const char *string)
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;
511 char* Units::size_totext(int64_t bytes, char *text)
513 char string[BCTEXTLEN];
514 static const char *sz[] = { "bytes", "KB", "MB", "GB", "TB" };
516 int i = (sizeof(sz) / sizeof(sz[0]));
517 while( --i > 0 && bytes < ((int64_t)1 << (10*i)) );
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]);
527 sprintf(string, "%jd", bytes);
528 if( bytes > 1000 ) punctuate(string);
529 sprintf(text, "%s %s", string, sz[i]);
536 #define BYTE_ORDER ((*(const uint32_t*)"a ") & 0x00000001)
538 void* Units::int64_to_ptr(uint64_t value)
540 unsigned char *value_dissected = (unsigned char*)&value;
542 unsigned char *data = (unsigned char*)&result;
544 // Must be done behind the compiler's back
545 if(sizeof(void*) == 4) {
547 data[0] = value_dissected[4];
548 data[1] = value_dissected[5];
549 data[2] = value_dissected[6];
550 data[3] = value_dissected[7];
553 data[0] = value_dissected[0];
554 data[1] = value_dissected[1];
555 data[2] = value_dissected[2];
556 data[3] = value_dissected[3];
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];
572 uint64_t Units::ptr_to_int64(void *ptr)
574 unsigned char *ptr_dissected = (unsigned char*)&ptr;
576 unsigned char *data = (unsigned char*)&result;
577 // Don't do this at home.
578 if(sizeof(void*) == 4) {
580 data[4] = ptr_dissected[0];
581 data[5] = ptr_dissected[1];
582 data[6] = ptr_dissected[2];
583 data[7] = ptr_dissected[3];
586 data[0] = ptr_dissected[0];
587 data[1] = ptr_dissected[1];
588 data[2] = ptr_dissected[2];
589 data[3] = ptr_dissected[3];
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];
605 const char* Units::format_to_separators(int time_format)
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";
623 void Units::punctuate(char *string)
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) {
632 string[i] = string[j];
636 void Units::fix_double(double *x)