1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include <stdint.h>
#define GETSNDFILE(x) ((SNDFILE*)x->sndfile)
#ifdef SUPERNOVA
# include <atomic>
# include <cassert>
# ifdef __SSE2__
# include <emmintrin.h>
# endif
class rw_spinlock {
static const uint32_t unlocked_state = 0;
static const uint32_t locked_state = 0x80000000;
static const uint32_t reader_mask = 0x7fffffff;
# ifdef __SSE2__
static inline void pause() { _mm_pause(); }
# else
static inline void pause() {}
# endif
public:
struct unique_lock {
explicit unique_lock(rw_spinlock& sl): sl_(sl) { sl_.lock(); }
~unique_lock() { sl_.unlock(); }
private:
rw_spinlock& sl_;
};
typedef unique_lock unique_lock;
struct shared_lock {
explicit shared_lock(rw_spinlock& sl): sl_(sl) { sl_.lock_shared(); }
~shared_lock() { sl_.unlock_shared(); }
private:
rw_spinlock& sl_;
};
rw_spinlock() = default;
rw_spinlock(rw_spinlock const& rhs) = delete;
rw_spinlock& operator=(rw_spinlock const& rhs) = delete;
rw_spinlock(rw_spinlock&& rhs) = delete;
~rw_spinlock() { assert(state == unlocked_state); }
void lock() {
for (;;) {
while (state.load(std::memory_order_relaxed) != unlocked_state)
pause();
uint32_t expected = unlocked_state;
if (state.compare_exchange_weak(expected, locked_state, std::memory_order_acquire))
break;
}
}
bool try_lock() {
uint32_t expected = unlocked_state;
if (state.compare_exchange_strong(expected, locked_state, std::memory_order_acquire))
return true;
else
return false;
}
void unlock() {
assert(state.load(std::memory_order_relaxed) == locked_state);
state.store(unlocked_state, std::memory_order_release);
}
void lock_shared() {
for (;;) {
/* with the mask, the cas will fail, locked exclusively */
uint32_t current_state = state.load(std::memory_order_acquire) & reader_mask;
const uint32_t next_state = current_state + 1;
if (state.compare_exchange_weak(current_state, next_state, std::memory_order_acquire))
break;
pause();
}
}
bool try_lock_shared() {
/* with the mask, the cas will fail, locked exclusively */
uint32_t current_state = state.load(std::memory_order_acquire) & reader_mask;
const uint32_t next_state = current_state + 1;
if (state.compare_exchange_strong(current_state, next_state, std::memory_order_acquire))
return true;
else
return false;
}
void unlock_shared() {
for (;;) {
uint32_t current_state = state.load(std::memory_order_relaxed); /* we don't need the reader_mask */
const uint32_t next_state = current_state - 1;
if (state.compare_exchange_weak(current_state, uint32_t(next_state)))
break;
pause();
}
}
private:
std::atomic<uint32_t> state { unlocked_state };
};
#endif
struct SndBuf {
double samplerate;
double sampledur; // = 1/ samplerate
float* data;
int channels;
int samples;
int frames;
int mask; // for delay lines
int mask1; // for interpolating oscillators.
int coord; // used by fft ugens
void* sndfile; // used by disk i/o
// SF_INFO fileinfo; // used by disk i/o
#ifdef SUPERNOVA
bool isLocal;
mutable rw_spinlock lock;
#endif
};
typedef struct SndBuf SndBuf;
struct SndBufUpdates {
int reads;
int writes;
};
typedef struct SndBufUpdates SndBufUpdates;
enum { coord_None, coord_Complex, coord_Polar };
inline float PhaseFrac(uint32_t inPhase) {
union {
uint32_t itemp;
float ftemp;
} u;
u.itemp = 0x3F800000 | (0x007FFF80 & ((inPhase) << 7));
return u.ftemp - 1.f;
}
inline float PhaseFrac1(uint32_t inPhase) {
union {
uint32_t itemp;
float ftemp;
} u;
u.itemp = 0x3F800000 | (0x007FFF80 & ((inPhase) << 7));
return u.ftemp;
}
inline float lookup(const float* table, int32_t phase, int32_t mask) { return table[(phase >> 16) & mask]; }
#define xlobits 14
#define xlobits1 13
inline float lookupi(const float* table, uint32_t phase, uint32_t mask) {
float frac = PhaseFrac(phase);
const float* tbl = table + ((phase >> 16) & mask);
float a = tbl[0];
float b = tbl[1];
return a + frac * (b - a);
}
inline float lookupi2(const float* table, uint32_t phase, uint32_t mask) {
float frac = PhaseFrac1(phase);
const float* tbl = table + ((phase >> 16) & mask);
float a = tbl[0];
float b = tbl[1];
return a + frac * b;
}
inline float lookupi1(const float* table0, const float* table1, uint32_t pphase, int32_t lomask) {
float pfrac = PhaseFrac1(pphase);
uint32_t index = ((pphase >> xlobits1) & (uint32_t)lomask);
float val1 = *(const float*)((const char*)table0 + index);
float val2 = *(const float*)((const char*)table1 + index);
return val1 + val2 * pfrac;
}
inline float lininterp(float x, float a, float b) { return a + x * (b - a); }
inline float cubicinterp(float x, float y0, float y1, float y2, float y3) {
// 4-point, 3rd-order Hermite (x-form)
float c0 = y1;
float c1 = 0.5f * (y2 - y0);
float c2 = y0 - 2.5f * y1 + 2.f * y2 - 0.5f * y3;
float c3 = 0.5f * (y3 - y0) + 1.5f * (y1 - y2);
return ((c3 * x + c2) * x + c1) * x + c0;
}
|