-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClockFace.sc
92 lines (80 loc) · 1.89 KB
/
ClockFace.sc
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
ClockFace {
var <starttime, <tempo, <>inc, <cursecs, isPlaying = false, <clock, <window, timeString;
var remFun, <mod, startBeats, <>onMod, <>onBeat;
*new{ arg starttime = 0, tempo = 1, inc = 0.1, alwaysOnTop=false;
^super.newCopyArgs(starttime, tempo, inc).init(alwaysOnTop);
}
init {|alwaysOnTop = false|
cursecs = starttime;
this.digitalGUI;
this.alwaysOnTop = alwaysOnTop;
}
start { this.play; }
alwaysOnTop_ {|bool|
^window.alwaysOnTop_(bool);
}
play {
var cur, last, floor;
clock = TempoClock.new(tempo);
startBeats = clock.elapsedBeats;
remFun = {this.stop};
CmdPeriod.add(remFun);
last = 0.0;
isPlaying = true;
clock.sched(inc, {
cur = clock.elapsedBeats - startBeats + starttime;
mod.notNil.if({
cur = cur%mod;
(cur < last).if({
{onMod.value;
onBeat.value(cur.floor.asInt);
}.defer;
});
});
this.cursecs_(cur, false);
onBeat.notNil.if({
(((floor = cur.floor) - last.floor) == 1).if({
{onBeat.value(floor.asInt)}.defer;
});
});
last = cur;
inc;
})
}
cursecs_ {arg curtime, updateStart = true;
var curdisp;
cursecs = curtime;
curdisp = curtime.asTimeString;
curdisp = curdisp[0 .. (curdisp.size-3)];
updateStart.if({starttime = cursecs; (isPlaying).if({
startBeats = clock.elapsedBeats;
});
});
{timeString.string_(curdisp)}.defer;
}
stop {
starttime = cursecs;
isPlaying = false;
clock.clear;
CmdPeriod.remove(remFun);
clock.stop;
}
tempo_ {arg newBPS = 1;
tempo = newBPS;
isPlaying.if({clock.tempo_(tempo)});
}
mod_ {arg newMod = 0;
(newMod == 0).if({
mod = nil;
}, {
mod = newMod;
})
}
digitalGUI {
window = GUI.window.new("Digital Clock", Rect(10, 250, 450, 110)).front;
timeString = GUI.staticText.new(window, Rect(0, 0, 430, 100))
.string_(cursecs.asTimeString)
.font_(Font("Arial", 40));
window.onClose_({this.stop});
}
}