-
Notifications
You must be signed in to change notification settings - Fork 47
/
MagicSine.cpp
155 lines (113 loc) · 4.3 KB
/
MagicSine.cpp
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
/*----------------------------------------------------------------------------
ChucK MagicSine Unit Generator
Implements efficient sine oscillator using the iterative "magic circle"
algorithm, at the expensive of not being able to set the phase (for now at
least). Uses 4 multiplies + 2 adds per sample.
MagicSine is about 25% faster when running a fixed freq sine wave, so its
main use is when you need a lot of sine waves and are hitting a performance
bottleneck.
Copyright (c) 2012 Spencer Salazar. All rights reserved.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
U.S.A.
-----------------------------------------------------------------------------*/
#include "chugin.h"
#include <stdio.h>
#include <limits.h>
#include <math.h>
CK_DLL_CTOR(magicsine_ctor);
CK_DLL_DTOR(magicsine_dtor);
CK_DLL_MFUN(magicsine_setFreq);
CK_DLL_MFUN(magicsine_getFreq);
CK_DLL_TICK(magicsine_tick);
t_CKINT magicsine_data_offset = 0;
class MagicSine
{
public:
MagicSine(float fs)
{
m_fs = fs;
setFreq(220);
m_x = 1;
m_y = 0;
}
SAMPLE tick(SAMPLE in)
{
m_x = m_x + m_epsilon*m_y;
m_y = -m_epsilon*m_x + m_y;
return m_y;
}
t_CKFLOAT setFreq(t_CKFLOAT f)
{
m_freq = f;
m_epsilon = 2.0*sin(2.0*CK_ONE_PI*(m_freq/m_fs)/2.0);
return m_freq;
}
t_CKFLOAT getFreq() { return m_freq; }
private:
SAMPLE m_x, m_y;
t_CKFLOAT m_fs;
t_CKFLOAT m_freq;
t_CKFLOAT m_epsilon;
};
CK_DLL_QUERY(MagicSine)
{
QUERY->setname(QUERY, "MagicSine");
QUERY->begin_class(QUERY, "MagicSine", "UGen");
QUERY->doc_class(QUERY, "Fast, recursive sine wave generator using the so-called "magic circle" algorithm (see <a href=\"https://ccrma.stanford.edu/~jos/pasp/Digital_Sinusoid_Generators.html\">https://ccrma.stanford.edu/~jos/pasp/Digital_Sinusoid_Generators.html</a>). "
"Can be 30-40% faster than regular SinOsc. "
"Frequency modulation will negate this performance benefit; most useful when pure sine tones are desired or for additive synthesis. ");
QUERY->add_ctor(QUERY, magicsine_ctor);
QUERY->add_dtor(QUERY, magicsine_dtor);
QUERY->add_ugen_func(QUERY, magicsine_tick, NULL, 1, 1);
QUERY->add_mfun(QUERY, magicsine_setFreq, "float", "freq");
QUERY->add_arg(QUERY, "float", "arg");
QUERY->doc_func(QUERY, "Oscillator frequency [Hz]. ");
QUERY->add_mfun(QUERY, magicsine_getFreq, "float", "freq");
QUERY->doc_func(QUERY, "Oscillator frequency [Hz]. ");
magicsine_data_offset = QUERY->add_mvar(QUERY, "int", "@magicsine_data", false);
QUERY->end_class(QUERY);
return TRUE;
}
CK_DLL_CTOR(magicsine_ctor)
{
OBJ_MEMBER_INT(SELF, magicsine_data_offset) = 0;
MagicSine * bcdata = new MagicSine(API->vm->srate(VM));
OBJ_MEMBER_INT(SELF, magicsine_data_offset) = (t_CKINT) bcdata;
}
CK_DLL_DTOR(magicsine_dtor)
{
MagicSine * bcdata = (MagicSine *) OBJ_MEMBER_INT(SELF, magicsine_data_offset);
if(bcdata)
{
delete bcdata;
OBJ_MEMBER_INT(SELF, magicsine_data_offset) = 0;
bcdata = NULL;
}
}
CK_DLL_TICK(magicsine_tick)
{
MagicSine * c = (MagicSine *) OBJ_MEMBER_INT(SELF, magicsine_data_offset);
if(c) *out = c->tick(in);
return TRUE;
}
CK_DLL_MFUN(magicsine_setFreq)
{
MagicSine * bcdata = (MagicSine *) OBJ_MEMBER_INT(SELF, magicsine_data_offset);
// TODO: sanity check
RETURN->v_float = bcdata->setFreq(GET_NEXT_FLOAT(ARGS));
}
CK_DLL_MFUN(magicsine_getFreq)
{
MagicSine * bcdata = (MagicSine *) OBJ_MEMBER_INT(SELF, magicsine_data_offset);
RETURN->v_float = bcdata->getFreq();
}