File: PmonoStreams.sc

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (147 lines) | stat: -rw-r--r-- 3,603 bytes parent folder | download | duplicates (3)
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
// these are internal classes, used by Pmono and PmonoArtic
// they're not likely to work well if you try to use them outside that context

PmonoStream : Stream {
	var	pattern,
		id, server, cleanup, currentCleanupFunc,
		event,
		streamout, name,
		streampairs, endval,
		msgFunc, hasGate, synthLib, desc, schedBundleArray, schedBundle;

	*new { |pattern|
		^super.newCopyArgs(pattern)
	}

	embedInStream { |inevent|
		inevent ?? { ^nil.yield };

		this.prInit(inevent);

		loop {
			if(this.prDoStreams) {
				// always on the first iteration; should not happen thereafter
				if(id.isNil) { this.prInitNode };
				cleanup.update(event);
				inevent = event.yield;
				this.prSetNextEvent(inevent);
			} {
				^cleanup.exit(inevent)
			}
		}
	}

		// private methods abstracted out for the benefit of subclasses
		// you should not use these directly
	prInit { |inevent|
		cleanup = EventStreamCleanup.new;
		streampairs = pattern.patternpairs.copy;
		endval = pattern.patternpairs.size - 1;
		forBy (1, endval, 2) { | i | streampairs[i] = streampairs[i].asStream };

		event = inevent.copy;
		event.use {
			synthLib = ~synthLib ?? { SynthDescLib.global };
			~synthDesc = desc = synthLib.match(pattern.synthName);
			if (desc.notNil) {
				~hasGate = hasGate = desc.hasGate;
				~msgFunc = desc.msgFunc;
			}{
				~msgFunc = ~defaultMsgFunc;
				~hasGate = hasGate = false;
			};
			msgFunc = ~msgFunc;
		}
	}

	prInitNode {
		event.use {
			if (~id.notNil) {
				~type = \monoSet;
				id = ~id;
			} {
				// If the event is a rest, no node would be created
				// so there is no need to add a cleanup function
				// (for a node that will never exist). If a later
				// event is not a rest, ~id will be nil and the
				// cleanup will be set at that time.
				if(event.isRest.not) {
					~type = \monoNote;
					~instrument = pattern.synthName;
					currentCleanupFunc = cleanup.addFunction(event, { | flag |
						if (flag and: { id.notNil }) { (id: id, server: server, type: \off,
							hasGate: hasGate,
							schedBundleArray: schedBundleArray,
							schedBundle: schedBundle).play
						};
						currentCleanupFunc = nil;
						id = nil;
					});
				};
			};
			// this should happen whether or not ~id is nil
			~updatePmono = { | argID, argServer |
				id = argID;
				server = argServer;
				schedBundleArray = ~schedBundleArray;
				schedBundle = ~schedBundle;
			};
		};
	}

	prDoStreams {
		forBy (0, endval, 2) { | i |
			name = streampairs[i];
			streamout = streampairs[i+1].next(event);
			streamout ?? { ^false };
			if (name.isSequenceableCollection) {
				name.do { | n, i | event[n] = streamout[i] };
			}{
				event[name] = streamout;
			};
		};
		^true
	}

	prSetNextEvent { |inevent|
		event = inevent.copy;
		event.use{
			~server = server;
			~id = id;
			~type = \monoSet;
			~msgFunc= msgFunc;
		};
	}
}

PmonoArticStream : PmonoStream {
	embedInStream { |inevent|
		var	sustain, rearticulating = true;
		inevent ?? { ^nil.yield };

		this.prInit(inevent);

		loop {
			if(this.prDoStreams) {
				if(rearticulating or: { event[\id].isNil } and: { event.isRest.not }) {
					event[\id] = nil;
					this.prInitNode;
					rearticulating = false;
				};
				sustain = event.use { ~sustain.value };
				if(sustain.notNil and: { sustain < event.delta }) {
					// no removal from cleanup now: faster stop behavior
					thisThread.clock.sched(sustain, {
						currentCleanupFunc.value(true);
						rearticulating = true;
					});
				};
				cleanup.update(event);
				inevent = event.yield;
				this.prSetNextEvent(inevent);
			} {
				^cleanup.exit(inevent)
			}
		}
	}
}