/[svn]/linuxsampler/trunk/src/common/RTMath.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/common/RTMath.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3054 - (show annotations) (download)
Thu Dec 15 12:47:45 2016 UTC (7 years, 4 months ago) by schoenebeck
File size: 5023 byte(s)
* Fixed numerous compiler warnings.
* Bumped version (2.0.0.svn32).

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005 - 2016 Christian Schoenebeck *
7 * *
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 // for unsafeMicroSeconds() implementation
27 #if !defined(WIN32) && !defined(__APPLE__)
28 # include <time.h>
29 #endif
30
31 static float CentsToFreqTable[CONFIG_MAX_PITCH * 1200 * 2 + 1]; // +-1200 cents per octave
32
33 float* RTMathBase::pCentsToFreqTable(InitCentsToFreqTable());
34
35 #if defined(__APPLE__)
36 #include <mach/mach_time.h>
37 typedef uint64_t time_stamp_t;
38 #endif
39
40 /*
41 * Creates a real time stamp for the current moment. Out of efficiency this
42 * is implemented in inline assembly for each CPU independently; we currently
43 * don't use a generic solution for CPUs that are not yet covered by the
44 * assembly code, instead an error message is prompted on compile time, forcing
45 * the user to contact us.
46 */
47 RTMathBase::time_stamp_t RTMathBase::CreateTimeStamp() {
48 #if defined(__i386__) || defined(__x86_64__)
49 uint64_t t;
50 __asm__ __volatile__ ("rdtsc" : "=A" (t));
51 return time_stamp_t(t >> 8);
52 #elif defined(__ia64__)
53 time_stamp_t t;
54 __asm__ __volatile__ ("mov %0=ar.itc" : "=r"(t));
55 return t;
56 #elif defined(__powerpc__)
57 time_stamp_t t;
58 __asm__ __volatile__ (
59 "98: mftb %0\n"
60 "99:\n"
61 ".section __ftr_fixup,\"a\"\n"
62 " .long %1\n"
63 " .long 0\n"
64 " .long 98b\n"
65 " .long 99b\n"
66 ".previous"
67 : "=r" (t) : "i" (0x00000100)
68 );
69 return t;
70 #elif defined(__alpha__)
71 time_stamp_t t;
72 __asm__ __volatile__ ("rpcc %0" : "=r"(t));
73 return t;
74 #elif defined(__APPLE__)
75 return (time_stamp_t) mach_absolute_time();
76 #else // we don't want to use a slow generic solution
77 # error "Sorry, LinuxSampler lacks time stamp code for your system."
78 # error "Please report this error and the CPU you are using to the LinuxSampler developers mailing list!"
79 #endif
80 }
81
82 RTMathBase::usecs_t RTMathBase::unsafeMicroSeconds(clock_source_t source) {
83 #if defined(WIN32)
84 LARGE_INTEGER t;
85 LARGE_INTEGER f;
86 QueryPerformanceCounter(&t);
87 if (!QueryPerformanceFrequency(&f)) return 0;
88 return usecs_t( double(t.QuadPart) / double(f.QuadPart) * 1000000.0 );
89 #elif defined(__APPLE__)
90 static mach_timebase_info_data_t tb;
91 double t = mach_absolute_time();
92 // if this method is run for the first time, get the internal time base
93 if (!tb.denom) mach_timebase_info(&tb); // get nanoseconds per tick
94 // convert from internal (abstract) time value to microseconds
95 return usecs_t( t * double(tb.numer) / double(tb.denom) / 1000.0 );
96 #else
97 timespec t;
98 clockid_t cid;
99 switch (source) {
100 case process_clock: cid = CLOCK_PROCESS_CPUTIME_ID; break;
101 case thread_clock: cid = CLOCK_THREAD_CPUTIME_ID; break;
102 case real_clock: cid = CLOCK_MONOTONIC; break;
103 default: return 0;
104 }
105 clock_gettime(cid, &t);
106 return usecs_t( (double(t.tv_sec) * 1000000000.0 + double(t.tv_nsec)) / 1000.0 );
107 #endif
108 }
109
110 /**
111 * Will automatically be called once to initialize the 'Cents to frequency
112 * ratio' table.
113 */
114 float* RTMathBase::InitCentsToFreqTable() {
115 float* pMiddleOfTable = &CentsToFreqTable[CONFIG_MAX_PITCH * 1200];
116 for (int i = -CONFIG_MAX_PITCH * 1200; i <= CONFIG_MAX_PITCH * 1200; i++) {
117 pMiddleOfTable[i] = pow(TWELVEHUNDREDTH_ROOT_OF_TWO, i);
118 }
119 return pMiddleOfTable;
120 }

  ViewVC Help
Powered by ViewVC