1 |
schoenebeck |
53 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
schoenebeck |
56 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
1212 |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
7 |
schoenebeck |
53 |
* * |
8 |
|
|
* This program is free software; you can redistribute it and/or modify * |
9 |
|
|
* it under the terms of the GNU General Public License as published by * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This program is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this program; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#include "RTMath.h" |
25 |
|
|
|
26 |
schoenebeck |
2942 |
// for unsafeMicroSeconds() implementation |
27 |
|
|
#if !defined(WIN32) && !defined(__APPLE__) |
28 |
|
|
# include <time.h> |
29 |
|
|
#endif |
30 |
|
|
|
31 |
schoenebeck |
1212 |
static float CentsToFreqTable[CONFIG_MAX_PITCH * 1200 * 2 + 1]; // +-1200 cents per octave |
32 |
|
|
|
33 |
schoenebeck |
319 |
float* RTMathBase::pCentsToFreqTable(InitCentsToFreqTable()); |
34 |
schoenebeck |
53 |
|
35 |
schoenebeck |
361 |
#if defined(__APPLE__) |
36 |
|
|
#include <mach/mach_time.h> |
37 |
|
|
typedef uint64_t time_stamp_t; |
38 |
|
|
static inline time_stamp_t GetMachTime() { |
39 |
|
|
return (time_stamp_t) mach_absolute_time(); |
40 |
|
|
} |
41 |
|
|
#endif |
42 |
|
|
|
43 |
schoenebeck |
328 |
/* |
44 |
|
|
* Creates a real time stamp for the current moment. Out of efficiency this |
45 |
|
|
* is implemented in inline assembly for each CPU independently; we currently |
46 |
|
|
* don't use a generic solution for CPUs that are not yet covered by the |
47 |
|
|
* assembly code, instead an error message is prompted on compile time, forcing |
48 |
|
|
* the user to contact us. |
49 |
|
|
*/ |
50 |
|
|
RTMathBase::time_stamp_t RTMathBase::CreateTimeStamp() { |
51 |
|
|
#if defined(__i386__) || defined(__x86_64__) |
52 |
|
|
uint64_t t; |
53 |
|
|
__asm__ __volatile__ ("rdtsc" : "=A" (t)); |
54 |
|
|
return t >> 8; |
55 |
|
|
#elif defined(__ia64__) |
56 |
|
|
time_stamp_t t; |
57 |
|
|
__asm__ __volatile__ ("mov %0=ar.itc" : "=r"(t)); |
58 |
|
|
return t; |
59 |
|
|
#elif defined(__powerpc__) |
60 |
|
|
time_stamp_t t; |
61 |
|
|
__asm__ __volatile__ ( |
62 |
|
|
"98: mftb %0\n" |
63 |
|
|
"99:\n" |
64 |
|
|
".section __ftr_fixup,\"a\"\n" |
65 |
|
|
" .long %1\n" |
66 |
|
|
" .long 0\n" |
67 |
|
|
" .long 98b\n" |
68 |
|
|
" .long 99b\n" |
69 |
|
|
".previous" |
70 |
|
|
: "=r" (t) : "i" (0x00000100) |
71 |
|
|
); |
72 |
|
|
return t; |
73 |
|
|
#elif defined(__alpha__) |
74 |
|
|
time_stamp_t t; |
75 |
|
|
__asm__ __volatile__ ("rpcc %0" : "=r"(t)); |
76 |
|
|
return t; |
77 |
schoenebeck |
361 |
#elif defined(__APPLE__) |
78 |
|
|
return GetMachTime(); |
79 |
schoenebeck |
328 |
#else // we don't want to use a slow generic solution |
80 |
|
|
# error "Sorry, LinuxSampler lacks time stamp code for your system." |
81 |
|
|
# error "Please report this error and the CPU you are using to the LinuxSampler developers mailing list!" |
82 |
|
|
#endif |
83 |
|
|
} |
84 |
|
|
|
85 |
schoenebeck |
2942 |
RTMathBase::usecs_t RTMathBase::unsafeMicroSeconds(clock_source_t source) { |
86 |
|
|
#if defined(WIN32) |
87 |
|
|
LARGE_INTEGER t; |
88 |
|
|
LARGE_INTEGER f; |
89 |
|
|
QueryPerformanceCounter(&t); |
90 |
|
|
if (!QueryPerformanceFrequency(&f)) return 0; |
91 |
|
|
return usecs_t( double(t) / double(f) * 1000000.0 ); |
92 |
|
|
#elif defined(__APPLE__) |
93 |
|
|
static mach_timebase_info_data_t tb; |
94 |
|
|
double t = mach_absolute_time(); |
95 |
|
|
// if this method is run for the first time, get the internal time base |
96 |
|
|
if (!tb.denom) mach_timebase_info(&tb); // get nanoseconds per tick |
97 |
|
|
// convert from internal (abstract) time value to microseconds |
98 |
|
|
return usecs_t( t * double(tb.numer) / double(tb.denom) / 1000.0 ); |
99 |
|
|
#else |
100 |
|
|
timespec t; |
101 |
|
|
clockid_t cid; |
102 |
|
|
switch (source) { |
103 |
|
|
case process_clock: cid = CLOCK_PROCESS_CPUTIME_ID; break; |
104 |
|
|
case thread_clock: cid = CLOCK_THREAD_CPUTIME_ID; break; |
105 |
|
|
case real_clock: cid = CLOCK_MONOTONIC; break; |
106 |
|
|
default: return 0; |
107 |
|
|
} |
108 |
|
|
clock_gettime(cid, &t); |
109 |
|
|
return usecs_t( (double(t.tv_sec) * 1000000000.0 + double(t.tv_nsec)) / 1000.0 ); |
110 |
|
|
#endif |
111 |
|
|
} |
112 |
|
|
|
113 |
schoenebeck |
53 |
/** |
114 |
|
|
* Will automatically be called once to initialize the 'Cents to frequency |
115 |
|
|
* ratio' table. |
116 |
|
|
*/ |
117 |
schoenebeck |
319 |
float* RTMathBase::InitCentsToFreqTable() { |
118 |
schoenebeck |
554 |
float* pMiddleOfTable = &CentsToFreqTable[CONFIG_MAX_PITCH * 1200]; |
119 |
persson |
799 |
for (int i = -CONFIG_MAX_PITCH * 1200; i <= CONFIG_MAX_PITCH * 1200; i++) { |
120 |
schoenebeck |
53 |
pMiddleOfTable[i] = pow(TWELVEHUNDREDTH_ROOT_OF_TWO, i); |
121 |
|
|
} |
122 |
|
|
return pMiddleOfTable; |
123 |
|
|
} |