Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / include / plugin_interface / SC_SndBuf.h
blob5275a5409047a047bc6397bfb5fd263b7dbb2834
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef _SndBuf_
22 #define _SndBuf_
24 #include <sys/types.h>
26 #ifdef SUPERNOVA
27 #include "nova-tt/rw_spinlock.hpp"
28 #endif
30 typedef struct SNDFILE_tag SNDFILE;
32 struct SndBuf
34 double samplerate;
35 double sampledur; // = 1/ samplerate
36 float *data;
37 int channels;
38 int samples;
39 int frames;
40 int mask; // for delay lines
41 int mask1; // for interpolating oscillators.
42 int coord; // used by fft ugens
43 SNDFILE *sndfile; // used by disk i/o
44 // SF_INFO fileinfo; // used by disk i/o
45 #ifdef SUPERNOVA
46 bool isLocal;
47 mutable nova::rw_spinlock lock;
48 #endif
50 typedef struct SndBuf SndBuf;
52 struct SndBufUpdates
54 int reads;
55 int writes;
57 typedef struct SndBufUpdates SndBufUpdates;
59 enum { coord_None, coord_Complex, coord_Polar };
61 inline float PhaseFrac(uint32 inPhase)
63 union { uint32 itemp; float ftemp; } u;
64 u.itemp = 0x3F800000 | (0x007FFF80 & ((inPhase)<<7));
65 return u.ftemp - 1.f;
68 inline float PhaseFrac1(uint32 inPhase)
70 union { uint32 itemp; float ftemp; } u;
71 u.itemp = 0x3F800000 | (0x007FFF80 & ((inPhase)<<7));
72 return u.ftemp;
75 inline float lookup(const float *table, int32 phase, int32 mask)
77 return table[(phase >> 16) & mask];
81 #define xlobits 14
82 #define xlobits1 13
84 inline float lookupi(const float *table, uint32 phase, uint32 mask)
86 float frac = PhaseFrac(phase);
87 const float *tbl = table + ((phase >> 16) & mask);
88 float a = tbl[0];
89 float b = tbl[1];
90 return a + frac * (b - a);
93 inline float lookupi2(const float *table, uint32 phase, uint32 mask)
95 float frac = PhaseFrac1(phase);
96 const float *tbl = table + ((phase >> 16) & mask);
97 float a = tbl[0];
98 float b = tbl[1];
99 return a + frac * b;
102 inline float lookupi1(const float* table0, const float* table1, uint32 pphase, int32 lomask)
104 float pfrac = PhaseFrac1(pphase);
105 uint32 index = ((pphase >> xlobits1) & lomask);
106 float val1 = *(const float*)((const char*)table0 + index);
107 float val2 = *(const float*)((const char*)table1 + index);
108 return val1 + val2 * pfrac;
112 inline float lininterp(float x, float a, float b)
114 return a + x * (b - a);
117 inline float cubicinterp(float x, float y0, float y1, float y2, float y3)
119 // 4-point, 3rd-order Hermite (x-form)
120 float c0 = y1;
121 float c1 = 0.5f * (y2 - y0);
122 float c2 = y0 - 2.5f * y1 + 2.f * y2 - 0.5f * y3;
123 float c3 = 0.5f * (y3 - y0) + 1.5f * (y1 - y2);
125 return ((c3 * x + c2) * x + c1) * x + c0;
129 #endif