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
23 #include <Carbon/Carbon.h>
28 # include <X11/Intrinsic.h>
34 #include "SC_PlugIn.h"
36 static InterfaceTable
*ft
;
38 struct KeyboardUGenGlobalState
{
47 struct KeyState
: public Unit
49 KeyboardUGenGlobalState
* gstate
;
50 float m_y1
, m_b1
, m_lag
;
53 //////////////////////////////////////////////////////////////////////////////////////////////////
57 void KeyState_next(KeyState
*unit
, int inNumSamples
);
58 void KeyState_Ctor(KeyState
*unit
);
61 //////////////////////////////////////////////////////////////////////////////////////////////////
65 void* gstate_update_func(void* arg
)
67 KeyboardUGenGlobalState
* gstate
= &gKeyStateGlobals
;
70 GetKeys(gstate
->keys
);
79 void* gstate_update_func(void* arg
)
83 KeyboardUGenGlobalState
* gstate
;
85 gstate
= &gKeyStateGlobals
;
87 /* // "KeyState" is disabled for now, on Windows...
89 //GetKey((long*)gstate->keys);
90 ::Sleep(17); // 17msec.
97 static Display
* d
= 0;
98 void* gstate_update_func(void* arg
)
100 KeyboardUGenGlobalState
* gstate
;
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
);
112 gstate
= &gKeyStateGlobals
;
115 XQueryKeymap ( d
, (char *) (gstate
->keys
) );
116 nanosleep ( &requested_time
, &remaining_time
);
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);
131 int byte
= (keynum
>> 3) & 15;
133 int byte
= (keynum
>> 3) & 31;
135 int bit
= keynum
& 7;
136 int val
= keys
[byte
] & (1 << bit
);
138 float minval
= ZIN0(1);
139 float maxval
= ZIN0(2);
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
));
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
;
160 KeyState_next(unit
, 1);
163 PluginLoad(KeyboardUGens
)
167 pthread_t keybListenThread
;
168 pthread_create (&keybListenThread
, NULL
, gstate_update_func
, (void*)0);
170 DefineSimpleUnit(KeyState
);
174 #if !defined(__APPLE__) && !defined(_WIN32)
175 static void __attribute__ ((destructor
)) finalize(void)