4 float* DB::topower = 0;
5 int* Freq::freqtable = 0;
7 DB::DB(float infinitygain)
9 this->infinitygain = infinitygain;
16 topower = new float[(MAXGAIN - INFINITYGAIN) * 10 + 1];
17 topower += -INFINITYGAIN * 10;
18 for(i = INFINITYGAIN * 10; i <= MAXGAIN * 10; i++)
20 topower[i] = pow(10, (float)i / 10 / 20);
22 //printf("%f %f\n", (float)i/10, topower[i]);
24 topower[INFINITYGAIN * 10] = 0; // infinity gain
29 float DB::fromdb_table()
31 return db = topower[(int)(db * 10)];
34 float DB::fromdb_table(float db)
36 if(db > MAXGAIN) db = MAXGAIN;
37 if(db <= INFINITYGAIN) return 0;
38 return db = topower[(int)(db * 10)];
43 return pow(10, db / 20);
46 float DB::fromdb(float db)
48 return pow(10, db / 20);
51 // set db to the power given using a formula
52 float DB::todb(float power)
59 db = (float)(20 * log10(power));
60 if(db < -100) db = -100;
72 Freq::Freq(const Freq& oldfreq)
74 this->freq = oldfreq.freq;
77 void Freq::init_table()
81 freqtable = new int[TOTALFREQS + 1];
83 double freq1 = 27.5, freq2 = 55;
84 // Some number divisable by three. This depends on the value of TOTALFREQS
88 for(int i = 1, j = 0; i <= TOTALFREQS; i++, j++)
90 freqtable[i] = (int)(freq1 + (freq2 - freq1) / scale * j + 0.5);
91 //printf("Freq::init_table %d\n", freqtable[i]);
106 for(i = 0; i < TOTALFREQS && freqtable[i] < freq; i++)
111 int Freq::fromfreq(int index)
116 for(i = 0; i < TOTALFREQS && freqtable[i] < index; i++)
121 int Freq::tofreq(int index)
124 int freq = freqtable[index];
128 Freq& Freq::operator++()
130 if(freq < TOTALFREQS) freq++;
134 Freq& Freq::operator--()
140 int Freq::operator>(Freq &newfreq) { return freq > newfreq.freq; }
141 int Freq::operator<(Freq &newfreq) { return freq < newfreq.freq; }
142 Freq& Freq::operator=(const Freq &newfreq) { freq = newfreq.freq; return *this; }
143 int Freq::operator=(const int newfreq) { freq = newfreq; return newfreq; }
144 int Freq::operator!=(Freq &newfreq) { return freq != newfreq.freq; }
145 int Freq::operator==(Freq &newfreq) { return freq == newfreq.freq; }
146 int Freq::operator==(int newfreq) { return freq == newfreq; }
148 char* Units::totext(char *text,
153 float frames_per_foot) // give text representation as time
155 int hour, minute, second, thousandths;
162 seconds = fabs(seconds);
163 hour = (int)(seconds / 3600);
164 minute = (int)(seconds / 60 - hour * 60);
165 second = (int)seconds - (int64_t)hour * 3600 - (int64_t)minute * 60;
166 thousandths = (int)(seconds * 1000) % 1000;
167 sprintf(text, "%d:%02d:%02d.%03d",
179 seconds = fabs(seconds);
180 hour = (int)(seconds / 3600);
181 minute = (int)(seconds / 60 - hour * 60);
182 second = (float)seconds - (int64_t)hour * 3600 - (int64_t)minute * 60;
183 sprintf(text, "%d:%02d:%02d", hour, minute, (int)second);
191 seconds = fabs(seconds);
192 hour = (int)(seconds / 3600);
193 minute = (int)(seconds / 60 - hour * 60);
194 second = (int)(seconds - hour * 3600 - minute * 60);
195 frame = (int64_t)(frame_rate *
196 (float)((float)seconds - (int64_t)hour * 3600 - (int64_t)minute * 60 - second))
198 sprintf(text, "%01d:%02d:%02d:%02ld", hour, minute, second, frame);
204 sprintf(text, "%09ld", to_int64(seconds * sample_rate));
207 case TIME_SAMPLES_HEX:
208 sprintf(text, "%08x", to_int64(seconds * sample_rate));
212 frame = to_int64(seconds * frame_rate);
213 sprintf(text, "%06ld", frame);
217 case TIME_FEET_FRAMES:
218 frame = to_int64(seconds * frame_rate);
219 feet = (int64_t)(frame / frames_per_foot);
220 sprintf(text, "%05ld-%02ld",
222 (int64_t)(frame - feet * frames_per_foot));
230 char* Units::totext(char *text,
235 float frames_per_foot)
237 return totext(text, (double)samples / samplerate, time_format, samplerate, frame_rate, frames_per_foot);
238 } // give text representation as time
240 int64_t Units::fromtext(char *text,
244 float frames_per_foot)
246 int64_t hours, minutes, frames, total_samples, i, j;
257 while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
259 hours = atol(string);
263 while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
264 while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
266 minutes = atol(string);
271 while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
272 while((text[i] == '.' || (text[i] >=48 && text[i] <= 57)) && j < 10) string[j++] = text[i++];
274 seconds = atof(string);
276 total_samples = (uint64_t)(((double)seconds + minutes * 60 + hours * 3600) * samplerate);
277 return total_samples;
284 while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
286 hours = atol(string);
291 while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
292 while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
294 minutes = atol(string);
299 while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
300 while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
302 seconds = atof(string);
305 while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
308 while(text[i] >=48 && text[i] <= 57 && j < 10) string[j++] = text[i++];
310 frames = atol(string);
312 total_samples = (int64_t)(((float)frames / frame_rate + seconds + minutes*60 + hours*3600) * samplerate);
313 return total_samples;
321 sscanf(text, "%x", &total_samples);
322 return total_samples;
325 return (int64_t)(atof(text) / frame_rate * samplerate);
332 while(text[i] >=48 && text[i] <= 57 && text[i] != 0 && j < 10) string[j++] = text[i++];
337 while((text[i] < 48 || text[i] > 57) && text[i] != 0) i++;
341 while(text[i] >=48 && text[i] <= 57 && text[i] != 0 && j < 10) string[j++] = text[i++];
343 frames = atol(string);
344 return (int64_t)(((float)feet * frames_per_foot + frames) / frame_rate * samplerate);
350 double Units::text_to_seconds(char *text,
354 float frames_per_foot)
356 return (double)fromtext(text,
360 frames_per_foot) / samplerate;
368 float Units::toframes(int64_t samples, int sample_rate, float framerate)
370 return (float)samples / sample_rate * framerate;
371 } // give position in frames
373 int64_t Units::toframes_round(int64_t samples, int sample_rate, float framerate)
376 float result_f = (float)samples / sample_rate * framerate;
377 int64_t result_l = (int64_t)(result_f + 0.5);
381 double Units::fix_framerate(double value)
383 if(value > 29.5 && value < 30)
384 value = (double)30000 / (double)1001;
386 if(value > 59.5 && value < 60)
387 value = (double)60000 / (double)1001;
389 if(value > 23.5 && value < 24)
390 value = (double)24000 / (double)1001;
395 double Units::atoframerate(char *text)
397 double result = atof(text);
398 return fix_framerate(result);
402 int64_t Units::tosamples(float frames, int sample_rate, float framerate)
404 float result = (frames / framerate * sample_rate);
406 if(result - (int)result) result += 1;
407 return (int64_t)result;
408 } // give position in samples
411 float Units::xy_to_polar(int x, int y)
416 angle = atan((float)-y / x) / (2 * M_PI) * 360;
421 angle = 180 - atan((float)-y / -x) / (2 * M_PI) * 360;
426 angle = 180 - atan((float)-y / -x) / (2 * M_PI) * 360;
431 angle = 360 + atan((float)-y / x) / (2 * M_PI) * 360;
452 void Units::polar_to_xy(float angle, int radius, int &x, int &y)
454 while(angle < 0) angle += 360;
456 x = (int)(cos(angle / 360 * (2 * M_PI)) * radius);
457 y = (int)(-sin(angle / 360 * (2 * M_PI)) * radius);
460 int64_t Units::round(double result)
462 return (int64_t)(result < 0 ? result - 0.5 : result + 0.5);
465 float Units::quantize10(float value)
467 int64_t temp = (int64_t)(value * 10 + 0.5);
468 value = (float)temp / 10;
472 float Units::quantize(float value, float precision)
474 int64_t temp = (int64_t)(value / precision + 0.5);
475 value = (float)temp * precision;
480 int64_t Units::to_int64(double result)
482 // This must round up if result is one sample within cieling.
483 // Sampling rates below 48000 may cause more problems.
484 return (int64_t)(result < 0 ? (result - 0.005) : (result + 0.005));
487 char* Units::print_time_format(int time_format, char *string)
491 case 0: sprintf(string, "Hours:Minutes:Seconds.xxx"); break;
492 case 1: sprintf(string, "Hours:Minutes:Seconds:Frames"); break;
493 case 2: sprintf(string, "Samples"); break;
494 case 3: sprintf(string, "Hex Samples"); break;
495 case 4: sprintf(string, "Frames"); break;
496 case 5: sprintf(string, "Feet-frames"); break;
503 #define BYTE_ORDER ((*(u_int32_t*)"a ") & 0x00000001)
505 void* Units::int64_to_ptr(uint64_t value)
507 unsigned char *value_dissected = (unsigned char*)&value;
509 unsigned char *data = (unsigned char*)&result;
511 // Must be done behind the compiler's back
512 if(sizeof(void*) == 4)
516 data[0] = value_dissected[4];
517 data[1] = value_dissected[5];
518 data[2] = value_dissected[6];
519 data[3] = value_dissected[7];
523 data[0] = value_dissected[0];
524 data[1] = value_dissected[1];
525 data[2] = value_dissected[2];
526 data[3] = value_dissected[3];
531 data[0] = value_dissected[0];
532 data[1] = value_dissected[1];
533 data[2] = value_dissected[2];
534 data[3] = value_dissected[3];
535 data[4] = value_dissected[4];
536 data[5] = value_dissected[5];
537 data[6] = value_dissected[6];
538 data[7] = value_dissected[7];
543 uint64_t Units::ptr_to_int64(void *ptr)
545 unsigned char *ptr_dissected = (unsigned char*)&ptr;
547 unsigned char *data = (unsigned char*)&result;
549 // Don't do this at home.
550 if(sizeof(void*) == 4)
554 data[4] = ptr_dissected[0];
555 data[5] = ptr_dissected[1];
556 data[6] = ptr_dissected[2];
557 data[7] = ptr_dissected[3];
561 data[0] = ptr_dissected[0];
562 data[1] = ptr_dissected[1];
563 data[2] = ptr_dissected[2];
564 data[3] = ptr_dissected[3];
569 data[0] = ptr_dissected[0];
570 data[1] = ptr_dissected[1];
571 data[2] = ptr_dissected[2];
572 data[3] = ptr_dissected[3];
573 data[4] = ptr_dissected[4];
574 data[5] = ptr_dissected[5];
575 data[6] = ptr_dissected[6];
576 data[7] = ptr_dissected[7];