class library: SynthDef - replaceUGen fixes
[supercollider.git] / server / plugins / KeyboardUGens.cpp
blob9066c0f6bc760acb83b36e4f101c4f7ca76d770b
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
22 #ifdef __APPLE__
23 #include <Carbon/Carbon.h>
24 #include <unistd.h>
25 #else
26 # ifndef _WIN32
27 # include <time.h>
28 # include <X11/Intrinsic.h>
29 # else
30 #include <windows.h>
31 # endif
32 #endif
34 #include "SC_PlugIn.h"
36 static InterfaceTable *ft;
38 struct KeyboardUGenGlobalState {
39 #ifdef __APPLE__
40 // uint8 keys[16];
41 KeyMap keys;
42 #else
43 uint8 keys[32];
44 #endif
45 } gKeyStateGlobals;
47 struct KeyState : public Unit
49 KeyboardUGenGlobalState* gstate;
50 float m_y1, m_b1, m_lag;
53 //////////////////////////////////////////////////////////////////////////////////////////////////
55 extern "C"
57 void KeyState_next(KeyState *unit, int inNumSamples);
58 void KeyState_Ctor(KeyState *unit);
61 //////////////////////////////////////////////////////////////////////////////////////////////////
63 #ifdef __APPLE__
65 void* gstate_update_func(void* arg)
67 KeyboardUGenGlobalState* gstate = &gKeyStateGlobals;
68 for (;;) {
69 Point p;
70 GetKeys(gstate->keys);
71 usleep(17000);
74 return 0;
77 #elif defined(_WIN32)
79 void* gstate_update_func(void* arg)
81 POINT p;
82 int mButton;
83 KeyboardUGenGlobalState* gstate;
85 gstate = &gKeyStateGlobals;
87 /* // "KeyState" is disabled for now, on Windows...
88 for(;;) {
89 //GetKey((long*)gstate->keys);
90 ::Sleep(17); // 17msec.
93 return 0;
96 # else
97 static Display * d = 0;
98 void* gstate_update_func(void* arg)
100 KeyboardUGenGlobalState* gstate;
101 Window r;
102 struct timespec requested_time , remaining_time;
104 requested_time.tv_sec = 0;
105 requested_time.tv_nsec = 17000 * 1000;
107 XInitThreads(); // x api is called by both mouse and keyboard ugens
109 d = XOpenDisplay ( NULL );
110 if (!d) return 0;
112 gstate = &gKeyStateGlobals;
114 for (;;) {
115 XQueryKeymap ( d , (char *) (gstate->keys) );
116 nanosleep ( &requested_time , &remaining_time );
119 return 0;
121 #endif
123 //////////////////////////////////////////////////////////////////////////////////////////////////
125 void KeyState_next(KeyState *unit, int inNumSamples)
127 // minval, maxval, warp, lag
128 uint8 *keys = (uint8*)unit->gstate->keys;
129 int keynum = (int)ZIN0(0);
130 #ifdef __APPLE__
131 int byte = (keynum >> 3) & 15;
132 #else
133 int byte = (keynum >> 3) & 31;
134 #endif
135 int bit = keynum & 7;
136 int val = keys[byte] & (1 << bit);
138 float minval = ZIN0(1);
139 float maxval = ZIN0(2);
140 float lag = ZIN0(3);
142 float y1 = unit->m_y1;
143 float b1 = unit->m_b1;
145 if (lag != unit->m_lag) {
146 unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
147 unit->m_lag = lag;
149 float y0 = val ? maxval : minval;
150 ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
151 unit->m_y1 = zapgremlins(y1);
154 void KeyState_Ctor(KeyState *unit)
156 SETCALC(KeyState_next);
157 unit->gstate = &gKeyStateGlobals;
158 unit->m_b1 = 0.f;
159 unit->m_lag = 0.f;
160 KeyState_next(unit, 1);
163 PluginLoad(KeyboardUGens)
165 ft = inTable;
167 pthread_t keybListenThread;
168 pthread_create (&keybListenThread, NULL, gstate_update_func, (void*)0);
170 DefineSimpleUnit(KeyState);
173 #ifdef __GNUC__
174 #if !defined(__APPLE__) && !defined(_WIN32)
175 static void __attribute__ ((destructor)) finalize(void)
177 if (d)
178 XCloseDisplay(d);
180 #endif
181 #endif