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_base = 0;
31 int* Freq::freqtable = 0;
34 DB::DB(float infinitygain)
36 this->infinitygain = infinitygain;
38 this->topower = topower_base + -INFINITYGAIN * 10;
42 float DB::fromdb_table()
44 return db = topower[(int)(db*10)];
47 float DB::fromdb_table(float db)
49 if(db > MAXGAIN) db = MAXGAIN;
50 if(db <= INFINITYGAIN) return 0;
51 return db = topower[(int)(db*10)];
56 return pow(10, db/20);
59 float DB::fromdb(float db)
61 return pow(10, db/20);
64 // set db to the power given using a formula
65 float DB::todb(float power)
69 db = 20 * log10(power);
70 if(db < -100) db = -100;
83 Freq::Freq(const Freq& oldfreq)
85 this->freq = oldfreq.freq;
91 while( i<TOTALFREQS && freqtable[i]<freq ) ++i;
95 int Freq::fromfreq(int index)
98 while( i<TOTALFREQS && freqtable[i]<index ) ++i;
102 int Freq::tofreq(int index)
104 if(index >= TOTALFREQS) index = TOTALFREQS - 1;
105 return freqtable[index];
108 // frequency doubles for every OCTAVE slots. OCTAVE must be divisible by 3
115 double Freq::tofreq_f(double index)
117 if( index < 0.5 ) return 0;
118 return 440.0 * pow(2, (double)(index - 421) / OCTAVE);
120 double Freq::fromfreq_f(double f)
122 if( f < 0.5 ) return 0;
123 double result = log(f / 440) / log(2.0) * OCTAVE + 421;
124 return result < 0 ? 0 : result;
127 Freq& Freq::operator++()
129 if(freq < TOTALFREQS) ++freq;
133 Freq& Freq::operator--()
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; }
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
157 Freq::freqtable = new int[TOTALFREQS + 1];
158 for( int i=0; i<=TOTALFREQS; ++i )
159 Freq::freqtable[i] = Freq::tofreq_f(i);
163 delete [] DB::topower_base; DB::topower_base = 0;
164 delete [] Freq::freqtable; Freq::freqtable = 0;
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)
171 int64_t hour, feet, frame;
172 int minute, second, thousandths;
174 switch(time_format) {
176 // add 1.0e-6 to prevent round off truncation from glitching a bunch of digits
177 seconds = fabs(seconds) + 1.0e-6;
179 seconds -= (int64_t)seconds;
180 thousandths = (int64_t)(seconds*1000) % 1000;
181 sprintf(text, "%04d.%03d", second, thousandths);
185 seconds = fabs(seconds) + 1.0e-6;
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);
196 seconds = fabs(seconds) + 1.0e-6;
198 minute = seconds/60 - hour*60;
199 second = seconds - (hour*3600 + minute*60);
200 sprintf(text, "%d:%02d:%02d", (int)hour, minute, second);
204 seconds = fabs(seconds) + 1.0e-6;
206 minute = seconds/60 - hour*60;
207 second = seconds - (hour*3600 + minute*60);
208 sprintf(text, "%02d:%02d:%02d", (int)hour, minute, second);
212 seconds = fabs(seconds) + 1.0e-6;
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);
223 sprintf(text, "%09jd", to_int64(seconds * sample_rate));
226 case TIME_SAMPLES_HEX: {
227 sprintf(text, "%08jx", to_int64(seconds * sample_rate));
231 frame = to_int64(seconds * frame_rate);
232 sprintf(text, "%06jd", frame);
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));
243 seconds = fabs(seconds) + 1.0e-6;
245 second = seconds - minute*60;
246 sprintf(text, "%d:%02d", minute, second);
250 int sign = seconds >= 0 ? '+' : '-';
251 seconds = fabs(seconds) + 1.0e-6;
253 second = seconds - minute*60;
254 sprintf(text, "%c%d:%02d", sign, minute, second);
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)
268 return totext(text, (double)samples/samplerate, time_format,
269 samplerate, frame_rate, frames_per_foot);
272 int64_t Units::get_int64(const char *&bp)
274 char string[BCTEXTLEN], *sp=&string[0];
276 for( int j=0; j<10 && isdigit(*cp); ++j ) *sp++ = *cp++;
281 double Units::get_double(const char *&bp)
283 char string[BCTEXTLEN], *sp=&string[0];
285 for( int j=0; j<10 && isdigit(*cp); ++j ) *sp++ = *cp++;
288 for( int j=0; j<10 && isdigit(*cp); ++j ) *sp++ = *cp++;
291 return strtod(string,0);
294 void Units::skip_seperators(const char *&bp)
297 for( int j=0; j<10 && *cp && !isdigit(*cp); ++j ) ++cp;
301 int64_t Units::fromtext(const char *text, int samplerate, int time_format,
302 float frame_rate, float frames_per_foot)
304 int64_t hours, total_samples;
305 int minutes, frames, feet;
306 double seconds, total_seconds;
309 switch(time_format) {
311 total_seconds = get_double(text);
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;
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;
332 return get_int64(text); }
334 case TIME_SAMPLES_HEX: {
335 sscanf(text, "%jx", &total_samples);
336 return total_samples; }
339 total_seconds = get_double(text) / frame_rate;
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;
350 case '+': sign = 1; ++text; break;
351 case '-': sign = -1; ++text; break;
354 minutes = get_int64(text); skip_seperators(text);
355 seconds = get_double(text);
356 total_seconds = sign * (seconds + minutes*60);
363 total_samples = total_seconds * samplerate;
364 return total_samples;
367 double Units::text_to_seconds(const char *text, int samplerate, int time_format,
368 float frame_rate, float frames_per_foot)
370 return (double)fromtext(text, samplerate, time_format,
371 frame_rate, frames_per_foot) / samplerate;
377 int Units::timeformat_totype(char *tcf)
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);
392 float Units::toframes(int64_t samples, int sample_rate, float framerate)
394 return (double)samples/sample_rate * framerate;
395 } // give position in frames
397 int64_t Units::toframes_round(int64_t samples, int sample_rate, float framerate)
400 float result_f = (float)samples / sample_rate * framerate;
401 int64_t result_l = (int64_t)(result_f + 0.5);
405 double Units::fix_framerate(double value)
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;
416 double Units::atoframerate(const char *text)
418 double result = get_double(text);
419 return fix_framerate(result);
423 int64_t Units::tosamples(double frames, int sample_rate, float framerate)
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
431 float Units::xy_to_polar(int x, int y)
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)
444 else if(x == 0 && y > 0)
446 else if(x == 0 && y == 0)
451 void Units::polar_to_xy(float angle, int radius, int &x, int &y)
454 angle += ((int)(-angle)/360 + 1) * 360;
455 else if( angle >= 360 )
456 angle -= ((int)(angle)/360) * 360;
458 x = (int)(cos(angle / 360 * (2*M_PI)) * radius);
459 y = (int)(-sin(angle / 360 * (2*M_PI)) * radius);
462 int64_t Units::round(double result)
464 return (int64_t)(result < 0 ? result - 0.5 : result + 0.5);
467 float Units::quantize10(float value)
469 int64_t temp = (int64_t)(value*10 + 0.5);
473 float Units::quantize(float value, float precision)
475 int64_t temp = (int64_t)(value/precision + 0.5);
476 return temp*precision;
479 int64_t Units::to_int64(double result)
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));
486 const char* Units::print_time_format(int time_format, char *string)
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;
497 case TIME_HMS3: fmt = TIME_HMS3_TEXT; break;
498 case TIME_SECONDS: fmt = TIME_SECONDS_TEXT; break;
500 case TIME_MS2: fmt = TIME_MS2_TEXT; break;
502 return strcpy(string,fmt);
505 int Units::text_to_format(const char *string)
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;
519 char* Units::size_totext(int64_t bytes, char *text)
521 char string[BCTEXTLEN];
522 static const char *sz[] = { "bytes", "KB", "MB", "GB", "TB" };
524 int i = (sizeof(sz) / sizeof(sz[0]));
525 while( --i > 0 && bytes < ((int64_t)1 << (10*i)) );
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]);
535 sprintf(string, "%jd", bytes);
536 if( bytes > 1000 ) punctuate(string);
537 sprintf(text, "%s %s", string, sz[i]);
544 #define BYTE_ORDER ((*(const uint32_t*)"a ") & 0x00000001)
546 void* Units::int64_to_ptr(uint64_t value)
548 unsigned char *value_dissected = (unsigned char*)&value;
550 unsigned char *data = (unsigned char*)&result;
552 // Must be done behind the compiler's back
553 if(sizeof(void*) == 4) {
555 data[0] = value_dissected[4];
556 data[1] = value_dissected[5];
557 data[2] = value_dissected[6];
558 data[3] = value_dissected[7];
561 data[0] = value_dissected[0];
562 data[1] = value_dissected[1];
563 data[2] = value_dissected[2];
564 data[3] = value_dissected[3];
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];
580 uint64_t Units::ptr_to_int64(void *ptr)
582 unsigned char *ptr_dissected = (unsigned char*)&ptr;
584 unsigned char *data = (unsigned char*)&result;
585 // Don't do this at home.
586 if(sizeof(void*) == 4) {
588 data[4] = ptr_dissected[0];
589 data[5] = ptr_dissected[1];
590 data[6] = ptr_dissected[2];
591 data[7] = ptr_dissected[3];
594 data[0] = ptr_dissected[0];
595 data[1] = ptr_dissected[1];
596 data[2] = ptr_dissected[2];
597 data[3] = ptr_dissected[3];
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];
613 const char* Units::format_to_separators(int time_format)
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";
631 void Units::punctuate(char *string)
633 int sep = ',', len = strlen(string), commas = (len - 1) / 3;
634 char *cp = string + len, *bp = cp + commas;
636 for( int k=3; cp < bp && bp > string; ) {
638 if( --k > 0 ) continue;
643 void Units::fix_double(double *x)