File: KeyboardUGens.cpp

package info (click to toggle)
supercollider 1%3A3.4.5-1wheezy1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 26,972 kB
  • sloc: cpp: 116,645; lisp: 64,914; ansic: 10,725; python: 3,548; perl: 766; ruby: 487; sh: 152; makefile: 117; xml: 13
file content (180 lines) | stat: -rw-r--r-- 3,875 bytes parent folder | download
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

    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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*/


#if SC_DARWIN
#include <Carbon/Carbon.h>
#include <unistd.h>
#else
# ifndef SC_WIN32
#  include <time.h>
#  include <X11/Intrinsic.h>
# else
# endif
#endif

#include "SC_PlugIn.h"

static InterfaceTable *ft;

struct KeyboardUGenGlobalState {
#if SC_DARWIN
//	uint8 keys[16];
	KeyMap keys;
#else
	uint8 keys[32];
#endif
} gKeyStateGlobals;

struct KeyState : public Unit
{
	KeyboardUGenGlobalState* gstate;
	float m_y1, m_b1, m_lag;
};

//////////////////////////////////////////////////////////////////////////////////////////////////

extern "C"
{
	void load(InterfaceTable *inTable);

	void KeyState_next(KeyState *unit, int inNumSamples);
	void KeyState_Ctor(KeyState *unit);
};

//////////////////////////////////////////////////////////////////////////////////////////////////

#if SC_DARWIN

void* gstate_update_func(void* arg)
{
	KeyboardUGenGlobalState* gstate = &gKeyStateGlobals;
	for (;;) {
		Point p;
		GetKeys(gstate->keys);
		usleep(17000);
	}
    
	return 0;
}

#elif defined(SC_WIN32)

void* gstate_update_func(void* arg)
{
	POINT p;
	int mButton;
	KeyboardUGenGlobalState* gstate;

	gstate = &gKeyStateGlobals;

	/* // "KeyState" is disabled for now, on Windows...
	for(;;)	{
		//GetKey((long*)gstate->keys);
		::Sleep(17); // 17msec.
	}
	*/
	return 0;
}

# else
static Display * d = 0;
void* gstate_update_func(void* arg)
{
	KeyboardUGenGlobalState* gstate;
	Window r;
	struct timespec requested_time , remaining_time;

	requested_time.tv_sec = 0;
	requested_time.tv_nsec = 17000 * 1000;

	d = XOpenDisplay ( NULL );
	if (!d) return 0;

	gstate = &gKeyStateGlobals;

	for (;;) {
		XQueryKeymap ( d , (char *) (gstate->keys) );
		nanosleep ( &requested_time , &remaining_time );
	}

	return 0;
}
#endif

//////////////////////////////////////////////////////////////////////////////////////////////////

void KeyState_next(KeyState *unit, int inNumSamples)
{
	// minval, maxval, warp, lag
	uint8 *keys = (uint8*)unit->gstate->keys;
	int keynum = (int)ZIN0(0);
#if SC_DARWIN
	int byte = (keynum >> 3) & 15;
#else
	int byte = (keynum >> 3) & 31;
#endif
	int bit = keynum & 7;
	int val = keys[byte] & (1 << bit);

	float minval = ZIN0(1);
	float maxval = ZIN0(2);
	float lag = ZIN0(3);

	float y1 = unit->m_y1;
	float b1 = unit->m_b1;

	if (lag != unit->m_lag) {
		unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
		unit->m_lag = lag;
	}
	float y0 = val ? maxval : minval;
	ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
	unit->m_y1 = zapgremlins(y1);
}

void KeyState_Ctor(KeyState *unit)
{
	SETCALC(KeyState_next);
	unit->gstate = &gKeyStateGlobals;
	unit->m_b1 = 0.f;
	unit->m_lag = 0.f;
	KeyState_next(unit, 1);
}

void load(InterfaceTable *inTable)
{
	ft = inTable;

	pthread_t keybListenThread;
	pthread_create (&keybListenThread, NULL, gstate_update_func, (void*)0);

	DefineSimpleUnit(KeyState);
}

#ifdef __GNUC__
#if !defined(SC_DARWIN) && !defined(SC_WIN32)
static void __attribute__ ((destructor)) finalize(void)
{
	if (d)
		XCloseDisplay(d);
}
#endif
#endif