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

Annotation of /linuxsampler/branches/release2_1_0/src/common/RTMath.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3373 - (hide annotations) (download)
Sat Nov 25 17:31:53 2017 UTC (6 years, 6 months ago) by schoenebeck
File size: 5023 byte(s)
Created linuxsampler branch 'release2_1_0'.
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 3054 * Copyright (C) 2005 - 2016 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     #endif
39    
40 schoenebeck 328 /*
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 schoenebeck 3054 return time_stamp_t(t >> 8);
52 schoenebeck 328 #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 schoenebeck 361 #elif defined(__APPLE__)
75 schoenebeck 3054 return (time_stamp_t) mach_absolute_time();
76 schoenebeck 328 #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 schoenebeck 2942 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 schoenebeck 2943 return usecs_t( double(t.QuadPart) / double(f.QuadPart) * 1000000.0 );
89 schoenebeck 2942 #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 schoenebeck 53 /**
111     * Will automatically be called once to initialize the 'Cents to frequency
112     * ratio' table.
113     */
114 schoenebeck 319 float* RTMathBase::InitCentsToFreqTable() {
115 schoenebeck 554 float* pMiddleOfTable = &CentsToFreqTable[CONFIG_MAX_PITCH * 1200];
116 persson 799 for (int i = -CONFIG_MAX_PITCH * 1200; i <= CONFIG_MAX_PITCH * 1200; i++) {
117 schoenebeck 53 pMiddleOfTable[i] = pow(TWELVEHUNDREDTH_ROOT_OF_TWO, i);
118     }
119     return pMiddleOfTable;
120     }

  ViewVC Help
Powered by ViewVC