File: Clock.sc

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 (343 lines) | stat: -rw-r--r-- 9,242 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

// clocks for timing threads.

Clock {
	// abstract class
	*play { | task | this.sched(0, task) }
	*seconds { ^thisThread.seconds }

	// tempo clock compatibility
	*beats { ^thisThread.seconds }
	*beats2secs { | beats | ^beats }
	*secs2beats { | secs | ^secs }
	*beats2bars { ^0 }
	*bars2beats { ^0 }
	*timeToNextBeat { ^0 }
	*nextTimeOnGrid { | quant = 1, phase = 0|
		if (quant ==0) { ^this.beats + phase };
		if (phase < 0) { phase = phase % quant };
		^roundUp(this.beats - (phase % quant), quant) + phase;
	}
}

SystemClock : Clock {
	*clear {
		_SystemClock_Clear
		^this.primitiveFailed
	}
	*sched { arg delta, item;
		_SystemClock_Sched
		^this.primitiveFailed
	}
	*schedAbs { arg time, item;
		_SystemClock_SchedAbs
		^this.primitiveFailed
	}

}

AppClock : Clock {
	classvar scheduler;
	*initClass {
		scheduler = Scheduler.new(this, true);
	}
	*clear {
		scheduler.clear;
	}
	*sched { arg delta, item;
		scheduler.sched(delta, item)
	}
	*tick {
		var saveClock = thisThread.clock;
		thisThread.clock = this;
		scheduler.seconds = Main.elapsedTime;
		thisThread.clock = saveClock;
	}

}

Scheduler {
	var clock, drift, beats = 0.0, <seconds = 0.0, queue;

	*new { arg clock, drift = false;
		^super.newCopyArgs(clock, drift).init;
	}
	init {
		beats = thisThread.beats;
		queue = PriorityQueue.new;
	}

	play { arg task;
		this.sched(0, task)
	}

	schedAbs { | time, item |
		queue.put(time, item);
	}

	sched { | delta, item |
		var fromTime;
		if (delta.notNil, {
			fromTime = if (drift, { Main.elapsedTime },{ seconds });
			queue.put(fromTime + delta, item);
		});
	}
	clear { // adc: priorityqueue has no pairsDo method, array has
		if(queue.array.notNil,{
			queue.array.pairsDo { | time, item | item.removedFromScheduler };
		});
		queue.clear
	}

	isEmpty { ^queue.isEmpty }

	advance { | delta |
		this.seconds = seconds + delta;
	}

	seconds_ { | newSeconds  |
		var delta, item;
		while ({
			seconds = queue.topPriority;
			seconds.notNil and: { seconds <= newSeconds }
		},{
			item = queue.pop;
			delta = item.awake( beats, seconds, clock );
			if (delta.isNumber, {
				this.sched(delta, item);
			});
		});
		seconds = newSeconds;
		beats = clock.secs2beats(newSeconds);
	}
}

TempoClock : Clock {
	classvar <all, <>default;

	var <queue, ptr;

	var <beatsPerBar=4.0, barsPerBeat=0.25;
	var <baseBarBeat=0.0, <baseBar=0.0;
	var <>permanent=false;

/*
You should only change the tempo 'now'. You can't set the tempo at some beat in the future or past, even though you might think so from the methods.

There are several ideas of now:
	elapsed time, i.e. "real time"
	logical time in the current time base.
	logical time in another time base.

logical time is time that is incremented by exact amounts from the time you started. It is not affected by the actual time your task gets scheduled, which may shift around somewhat due to system load. By calculating using logical time instead of actual time, your process will not drift out of sync over long periods. every thread stores a clock and its current logical time in seconds and beats relative to that clock.

elapsed time is whatever the system clock says it is right now. elapsed time is always advancing. logical time only advances when your task yields or returns.

*/

	*new { arg tempo, beats, seconds, queueSize=256;
		^super.new.init(tempo, beats, seconds, queueSize)
	}

	*initClass {
		default = this.new(queueSize: 2048).permanent_(true);
		CmdPeriod.add(this);
	}

	*cmdPeriod {
		all.do({ arg item; item.clear(false) });
		all.do({ arg item; if (item.permanent.not, { item.stop })  })
	}

	init { arg tempo, beats, seconds, queueSize;
		queue = Array.new(queueSize);
		this.prStart(tempo, beats, seconds);
		all = all.add(this);
	}

	stop {
		this.changed(\stop);
		this.releaseDependants;
		all.take(this);
		this.prStop;
	}

	play { arg task, quant = 1;
		this.schedAbs(quant.nextTimeOnGrid(this), task)
	}

	playNextBar { arg task; this.schedAbs(this.nextBar, task) }

	tempo {
		_TempoClock_Tempo
		^this.primitiveFailed
	}
	beatDur {
		_TempoClock_BeatDur
		^this.primitiveFailed
	}
	elapsedBeats {
		_TempoClock_ElapsedBeats
		^this.primitiveFailed
		/* primitive does this:
			^this.secs2beats(Main.elapsedTime).
		*/
	}
	beats {
		// returns the appropriate beats for this clock from any thread
		_TempoClock_Beats
		^this.primitiveFailed
		/* primitive does this:
			if (thisThread.clock == this) { ^thisThread.beats }
			^this.secs2beats(thisThread.seconds)
		*/
	}

	seconds { ^thisThread.seconds }

	sched { arg delta, item;
		_TempoClock_Sched
		^this.primitiveFailed
	}
	schedAbs { arg beat, item;
		_TempoClock_SchedAbs
		^this.primitiveFailed
	}
	clear { | releaseNodes = true |
		// flag tells EventStreamPlayers that CmdPeriod is removing them, so
		// nodes are already freed
		queue.pairsDo { arg time, item; item.removedFromScheduler(releaseNodes) };
		^this.prClear;
	}


	// for setting the tempo at the current logical time
	// (even another TempoClock's logical time).
	tempo_ { arg newTempo;
		this.setTempoAtBeat(newTempo, this.beats);
		this.changed(\tempo);  // this line is added
	}
	beatsPerBar_ { arg newBeatsPerBar;
		if (thisThread.clock != this) {
			"should only change beatsPerBar within the scheduling thread.".error;
			^this
		};
		this.setMeterAtBeat(newBeatsPerBar, thisThread.beats);
	}

	// for setting the tempo at the current elapsed time .
	etempo_ { arg newTempo;
		this.setTempoAtSec(newTempo, Main.elapsedTime);
	}

	beats2secs { arg beats;
		_TempoClock_BeatsToSecs
		^this.primitiveFailed
	}
	secs2beats { arg secs;
		_TempoClock_SecsToBeats
		^this.primitiveFailed
	}

	prDump {
		_TempoClock_Dump
		^this.primitiveFailed
	}

	nextTimeOnGrid { arg quant = 1, phase = 0;
		if (quant == 0) { ^this.beats + phase };
		if (quant < 0) { quant = beatsPerBar * quant.neg };
		if (phase < 0) { phase = phase % quant };
		^roundUp(this.beats - baseBarBeat - (phase % quant), quant) + baseBarBeat + phase
	}

	timeToNextBeat { arg quant=1.0; // logical time to next beat
		^quant.nextTimeOnGrid(this) - this.beats
	}

	beats2bars { arg beats;
		^(beats - baseBarBeat) * barsPerBeat + baseBar;
	}
	bars2beats { arg bars;
		^(bars - baseBar) * beatsPerBar + baseBarBeat;
	}
	bar {
		// return the current bar.
		^this.beats2bars(this.beats).floor;
	}
	nextBar { arg beat;
		// given a number of beats, determine number beats at the next bar line.
		if (beat.isNil) { beat = this.beats };
		^this.bars2beats(this.beats2bars(beat).ceil);
	}
	beatInBar {
		// return the beat of the bar, range is 0 to < t.beatsPerBar
		^this.beats - this.bars2beats(this.bar)
	}

	// PRIVATE
	prStart { arg tempo;
		_TempoClock_New
		^this.primitiveFailed
	}
	prStop {
		_TempoClock_Free
		^this.primitiveFailed
	}
	prClear {
		_TempoClock_Clear
		^this.primitiveFailed
	}
	setTempoAtBeat { arg newTempo, beats;
		_TempoClock_SetTempoAtBeat
		^this.primitiveFailed
	}
	setTempoAtSec { arg newTempo, secs;
		_TempoClock_SetTempoAtTime
		^this.primitiveFailed
	}
	// meter should only be changed in the TempoClock's thread.
	setMeterAtBeat { arg newBeatsPerBar, beats;
		// bar must be integer valued when meter changes or confusion results later.
		baseBar = round((beats - baseBarBeat) * barsPerBeat + baseBar, 1);
		baseBarBeat = beats;
		beatsPerBar = newBeatsPerBar;
		barsPerBeat = beatsPerBar.reciprocal;
		this.changed(\meter);
	}

// these methods allow TempoClock to act as TempoClock.default
   *stop { TempoClock.default.stop  }
   *play { | task, quant | TempoClock.default.play(task, quant)  }
   *sched { | delta, item | TempoClock.default.sched(delta, item)  }
   *schedAbs { | beat, item | TempoClock.default.schedAbs(beat, item)  }
   *clear { | releaseNodes | TempoClock.default.clear(releaseNodes)  }
   *tempo_ { | newTempo | TempoClock.default.tempo_(newTempo)  }
   *etempo_ { | newTempo | TempoClock.default.etempo_(newTempo)  }

   *tempo { ^TempoClock.default.tempo }
   *beats { ^TempoClock.default.beats }
   *beats2secs { | beats | ^TempoClock.default.beats2secs(beats)  }
   *secs2beats { | secs | ^TempoClock.default.secs2beats(secs)  }
   *nextTimeOnGrid { | quant = 1, phase = 0 | ^TempoClock.default.nextTimeOnGrid(quant, phase)  }
   *timeToNextBeat { | quant = 1 | ^TempoClock.default.timeToNextBeat(quant)  }

   *setTempoAtBeat { | newTempo, beats | TempoClock.default.setTempoAtBeat(newTempo, beats)  }
   *setTempoAtSec { | newTempo, secs | TempoClock.default.setTempoAtSec(newTempo, secs)  }
   *setMeterAtBeat { | newBeatsPerBar, beats | TempoClock.default.setMeterAtBeat(newBeatsPerBar, beats)  }

   *beatsPerBar { ^TempoClock.default.beatsPerBar  }
   *baseBarBeat { ^TempoClock.default.baseBarBeat  }
   *baseBar { ^TempoClock.default.baseBar  }
   *playNextBar { | task | ^TempoClock.default.playNextBar(task)  }
   *beatDur { ^TempoClock.default.beatDur  }
   *elapsedBeats { ^TempoClock.default.elapsedBeats  }
   *beatsPerBar_ { | newBeatsPerBar | TempoClock.default.beatsPerBar_(newBeatsPerBar)  }
   *beats2bars { | beats | ^TempoClock.default.beats2bars(beats)  }
   *bars2beats { | bars | ^TempoClock.default.bars2beats(bars)  }
   *bar { ^TempoClock.default.bar  }
   *nextBar { | beat | ^TempoClock.default.nextBar  }
   *beatInBar { ^TempoClock.default.beatInBar  }

}