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
|
Volume {
var <server, <startBus, numChannels, <min, <max, <persist;
var <volume = 0.0, <lag = 0.1, <isMuted = false;
var <ampSynth, <numOutputChannels, defName, updateFunc, initFunc;
var <>window;
*new { | server, startBus = 0, numChannels, min = -90, max = 6, persist = false |
^super.newCopyArgs(server ?? { Server.default }, startBus, numChannels, min, max, persist).init;
}
init {
// execute immediately if we're already past server tree functions
if(server.serverRunning) {
this.sendSynthDef;
this.updateSynth;
};
initFunc = {
ampSynth = nil;
this.sendSynthDef;
// only create synth now if it won't be created by ServerTree
if (persist.not) { this.updateSynth };
};
ServerBoot.add(initFunc, server)
}
sendSynthDef {
if (server.hasBooted) {
fork {
numOutputChannels = this.numChannels;
defName = (\volumeAmpControl ++ numOutputChannels).asSymbol;
SynthDef(defName, { | volumeAmp = 1, volumeLag = 0.1, gate=1, bus |
XOut.ar(bus,
Linen.kr(gate, releaseTime: 0.05, doneAction:2),
In.ar(bus, numOutputChannels) * Lag.kr(volumeAmp, volumeLag)
);
}).send(server);
server.sync;
updateFunc = {
ampSynth = nil;
if(persist) { this.updateSynth }
};
ServerTree.add(updateFunc, server);
}
};
}
numChannels { ^numChannels ? server.options.numOutputBusChannels }
numChannels_ { |num|
if(ampSynth.notNil and: { num != this.numChannels }) {
"Change in number of channels will not take effect until volume is reset to 0dB.".warn;
};
numChannels = num;
}
updateSynth {
var amp = if(isMuted.not) { volume.dbamp } { 0.0 };
var active = amp != 1.0;
if(active) {
if(server.hasBooted) {
if(ampSynth.isNil) {
ampSynth = Synth.after(server.defaultGroup, defName,
[\volumeAmp, amp, \volumeLag, lag, \bus, startBus])
} {
ampSynth.set(\volumeAmp, amp);
}
}
} {
if(ampSynth.notNil) { ampSynth.release; ampSynth = nil }
}
}
mute {
if(isMuted.not) {
isMuted = true;
this.changed(\mute, true);
this.updateSynth;
}
}
unmute {
if(isMuted) {
isMuted = false;
this.changed(\mute, false);
this.updateSynth;
}
}
freeSynth {
ServerTree.remove(updateFunc, server);
updateFunc = nil;
ampSynth.release;
ampSynth = nil
}
// sets volume back to 1 - removes the synth
reset {
isMuted = false;
this.volume = 0.0;
}
volume_ { | aVolume |
volume = aVolume.clip(min, max);
this.changed(\amp, volume);
this.updateSynth;
}
lag_ { | aLagTime |
lag = aLagTime;
if(ampSynth.notNil) { ampSynth.set(\volumeLag, lag) }
}
setVolumeRange { | argMin, argMax |
var clippedVolume;
argMin !? { min = argMin };
argMax !? { max = argMax };
this.changed(\ampRange, min, max);
clippedVolume = volume.clip(min, max);
if(clippedVolume != volume) { this.volume_(clippedVolume) }
}
gui { | window, bounds |
^VolumeGui(this, window, bounds)
}
close {
window.close;
}
}
VolumeGui {
var <model, <window;
*new { | model, win, bounds |
^super.newCopyArgs(model).init(win, bounds)
}
init { | win, bounds |
var slider, box, simpleController;
var spec = [model.min, model.max, \db].asSpec;
bounds = bounds ?? { Rect(100, 100, 80, 330) };
window = win ?? { Window.new("Volume", bounds).front };
box = NumberBox(window, Rect(10, 10, 60, 30))
.value_(model.volume);
slider = Slider(window, Rect(10, 40, 60, 280))
.value_(spec.unmap(model.volume));
slider.action_({ | item |
model.volume_(spec.map(item.value));
});
box.action_({ | item |
model.volume_(item.value);
});
window.onClose_({
simpleController.remove;
});
simpleController = SimpleController(model)
.put(\amp, {|changer, what, volume|
box.value_(volume.round(0.01));
slider.value_(spec.unmap(volume));
})
.put(\ampRange, {|changer, what, min, max|
spec = [min, max, \db].asSpec.debug;
slider.value_(spec.unmap(model.volume));
})
}
}
|