/
UIUGens.mm
211 lines (162 loc) · 5.81 KB
/
UIUGens.mm
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
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
*/
// ********** this version for mac. for windows and linux see UIUGens.cpp
#include <SC_Lock.h>
#import <AppKit/AppKit.h>
#include <unistd.h>
#include "SC_PlugIn.h"
static InterfaceTable* ft;
struct KeyState : public Unit {
float m_y1, m_b1, m_lag;
};
struct MouseUGenGlobalState {
float mouseX, mouseY;
bool mouseButton;
} gMouseUGenGlobals;
struct MouseInputUGen : public Unit {
float m_y1, m_b1, m_lag;
};
//////////////////////////////////////////////////////////////////////////////////////////////////
std::atomic_bool inputThreadRunning = { false };
void gstate_update_func() {
CGDirectDisplayID display = kCGDirectMainDisplay; // to grab the main display ID
CGRect bounds = CGDisplayBounds(display);
float rscreenWidth = 1. / bounds.size.width;
float rscreenHeight = 1. / bounds.size.height;
while (inputThreadRunning.load(std::memory_order_relaxed)) {
NSPoint p;
p = [NSEvent mouseLocation];
gMouseUGenGlobals.mouseX = (float)p.x * rscreenWidth;
gMouseUGenGlobals.mouseY = (float)p.y * rscreenHeight;
gMouseUGenGlobals.mouseButton = (bool)[NSEvent pressedMouseButtons];
std::this_thread::sleep_for(std::chrono::milliseconds(17));
}
return;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void KeyState_next(KeyState* unit, int inNumSamples) {
CGKeyCode keynum = (CGKeyCode)ZIN0(0);
int val = CGEventSourceKeyState(kCGEventSourceStateCombinedSessionState, keynum);
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->m_b1 = 0.f;
unit->m_lag = 0.f;
KeyState_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void MouseX_next(MouseInputUGen* unit, int inNumSamples) {
// minval, maxval, warp, lag
float minval = ZIN0(0);
float maxval = ZIN0(1);
float warp = 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 : (float)exp(log001 / (lag * unit->mRate->mSampleRate));
unit->m_lag = lag;
}
float y0 = gMouseUGenGlobals.mouseX;
if (warp == 0.0) {
y0 = (maxval - minval) * y0 + minval;
} else {
y0 = pow(maxval / minval, y0) * minval;
}
ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
unit->m_y1 = zapgremlins(y1);
}
void MouseX_Ctor(MouseInputUGen* unit) {
SETCALC(MouseX_next);
unit->m_b1 = 0.f;
unit->m_lag = 0.f;
MouseX_next(unit, 1);
}
void MouseY_next(MouseInputUGen* unit, int inNumSamples) {
// minval, maxval, warp, lag
float minval = ZIN0(0);
float maxval = ZIN0(1);
float warp = 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 : (float)exp(log001 / (lag * unit->mRate->mSampleRate));
unit->m_lag = lag;
}
float y0 = gMouseUGenGlobals.mouseY;
if (warp == 0.0) {
y0 = (maxval - minval) * y0 + minval;
} else {
y0 = pow(maxval / minval, y0) * minval;
}
ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
unit->m_y1 = zapgremlins(y1);
}
void MouseY_Ctor(MouseInputUGen* unit) {
SETCALC(MouseY_next);
unit->m_b1 = 0.f;
unit->m_lag = 0.f;
MouseY_next(unit, 1);
}
void MouseButton_next(MouseInputUGen* unit, int inNumSamples) {
// minval, maxval, warp, lag
float minval = ZIN0(0);
float maxval = ZIN0(1);
float lag = ZIN0(2);
float y1 = unit->m_y1;
float b1 = unit->m_b1;
if (lag != unit->m_lag) {
unit->m_b1 = lag == 0.f ? 0.f : (float)exp(log001 / (lag * unit->mRate->mSampleRate));
unit->m_lag = lag;
}
float y0 = gMouseUGenGlobals.mouseButton ? maxval : minval;
ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
unit->m_y1 = zapgremlins(y1);
}
void MouseButton_Ctor(MouseInputUGen* unit) {
SETCALC(MouseButton_next);
unit->m_b1 = 0.f;
unit->m_lag = 0.f;
MouseButton_next(unit, 1);
}
SC_Thread uiListenThread;
PluginLoad(UIUGens) {
ft = inTable;
inputThreadRunning = true;
uiListenThread = std::thread(gstate_update_func);
DefineSimpleUnit(KeyState);
DefineUnit("MouseX", sizeof(MouseInputUGen), (UnitCtorFunc)&MouseX_Ctor, 0, 0);
DefineUnit("MouseY", sizeof(MouseInputUGen), (UnitCtorFunc)&MouseY_Ctor, 0, 0);
DefineUnit("MouseButton", sizeof(MouseInputUGen), (UnitCtorFunc)&MouseButton_Ctor, 0, 0);
}
C_LINKAGE SC_API_EXPORT void unload(InterfaceTable* inTable) {
inputThreadRunning = false;
uiListenThread.join();
}