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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <TargetConditionals.h>
23 #if defined(SC_IPHONE) && !TARGET_IPHONE_SIMULATOR
25 #import <UIKit/UIKit.h>
27 #include "SC_PlugIn.h"
30 #if TARGET_IPHONE_SIMULATOR
31 const double pi = 3.14f;
35 static InterfaceTable *ft;
37 @interface AccelerometerDelegate : NSObject<UIAccelerometerDelegate>
39 UIAccelerationValue accel_x, accel_y, accel_z;
46 static AccelerometerDelegate *delegate = 0;
49 struct AccelerometerUGen : public Unit
51 float m_y1, m_b1, m_lag;
54 //////////////////////////////////////////////////////////////////////////////////////////////////
58 void load(InterfaceTable *inTable);
60 void AccelerometerX_next(AccelerometerUGen *unit, int inNumSamples);
61 void AccelerometerY_next(AccelerometerUGen *unit, int inNumSamples);
62 void AccelerometerZ_next(AccelerometerUGen *unit, int inNumSamples);
64 void AccelerometerX_Ctor(AccelerometerUGen *unit);
65 void AccelerometerY_Ctor(AccelerometerUGen *unit);
66 void AccelerometerZ_Ctor(AccelerometerUGen *unit);
69 //////////////////////////////////////////////////////////////////////////////////////////////////
71 void AccelerometerX_next(AccelerometerUGen *unit, int inNumSamples)
73 // minval, maxval, warp, lag
75 float minval = ZIN0(0);
76 float maxval = ZIN0(1);
80 float y1 = unit->m_y1;
81 float b1 = unit->m_b1;
83 if (lag != unit->m_lag) {
84 unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
87 float y0 = ([delegate accel_x] + 1.0f)*0.5f;
89 y0 = (maxval - minval) * y0 + minval;
91 y0 = pow(maxval/minval, y0) * minval;
93 ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
94 unit->m_y1 = zapgremlins(y1);
97 void AccelerometerX_Ctor(AccelerometerUGen *unit)
99 SETCALC(AccelerometerX_next);
102 AccelerometerX_next(unit, 1);
106 void AccelerometerY_next(AccelerometerUGen *unit, int inNumSamples)
108 // minval, maxval, warp, lag
110 float minval = ZIN0(0);
111 float maxval = ZIN0(1);
112 float warp = ZIN0(2);
115 float y1 = unit->m_y1;
116 float b1 = unit->m_b1;
118 if (lag != unit->m_lag) {
119 unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
122 float y0 = ([delegate accel_y] + 1.0f)*0.5f;
124 y0 = (maxval - minval) * y0 + minval;
126 y0 = pow(maxval/minval, y0) * minval;
128 ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
129 unit->m_y1 = zapgremlins(y1);
132 void AccelerometerY_Ctor(AccelerometerUGen *unit)
134 SETCALC(AccelerometerY_next);
137 AccelerometerY_next(unit, 1);
140 void AccelerometerZ_next(AccelerometerUGen *unit, int inNumSamples)
142 // minval, maxval, warp, lag
144 float minval = ZIN0(0);
145 float maxval = ZIN0(1);
146 float warp = ZIN0(2);
149 float y1 = unit->m_y1;
150 float b1 = unit->m_b1;
152 if (lag != unit->m_lag) {
153 unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
156 float y0 = ([delegate accel_z] + 1.0f)*0.5f;
158 y0 = (maxval - minval) * y0 + minval;
160 y0 = pow(maxval/minval, y0) * minval;
162 ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
163 unit->m_y1 = zapgremlins(y1);
166 void AccelerometerZ_Ctor(AccelerometerUGen *unit)
168 SETCALC(AccelerometerZ_next);
171 AccelerometerZ_next(unit, 1);
174 @implementation AccelerometerDelegate
178 if (self=[super init])
185 - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
187 accel_x = acceleration.x;
188 accel_y = acceleration.y;
189 accel_z = acceleration.z;
192 accel_x += ([acceleration x] - accel_x)*0.8f;
193 accel_y += ([acceleration y] - accel_y)*0.8f;
194 accel_z += ([acceleration z] - accel_z)*0.8f;
200 return (float) accel_x;
205 return (float) accel_y;
210 return (float) accel_z;
219 delegate = [[AccelerometerDelegate alloc] init];
220 UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
221 [accel setUpdateInterval:0.01f];
222 [accel setDelegate:delegate];
223 DefineUnit("AccelerometerX", sizeof(AccelerometerUGen), (UnitCtorFunc)&AccelerometerX_Ctor, 0, 0);
224 DefineUnit("AccelerometerY", sizeof(AccelerometerUGen), (UnitCtorFunc)&AccelerometerY_Ctor, 0, 0);
225 DefineUnit("AccelerometerZ", sizeof(AccelerometerUGen), (UnitCtorFunc)&AccelerometerZ_Ctor, 0, 0);