Skip to content

Commit

Permalink
use onFree to catch when ampSynth ends, use ampSynth.isPlaying
Browse files Browse the repository at this point in the history
  • Loading branch information
adcxyz committed Jun 21, 2024
1 parent 833d0ba commit 33bb6c9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
31 changes: 19 additions & 12 deletions SCClassLibrary/Common/Control/Volume.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Volume {

var <ampSynth, <numOutputChannels, <defName, updateFunc, initFunc;
var <>window;
var <group, <ampSynthRunning = false, <listener;
var <group, <listener;

*new { | server, startBus = 0, numChannels, min = -90, max = 6, persist = false |
^super.newCopyArgs(server ?? { Server.default }, startBus, numChannels, min, max, persist).init;
Expand All @@ -27,22 +27,22 @@ Volume {
};

initFunc = {
ampSynthRunning = false;
ampSynth.isPlaying = false;
server.sendMsg(*group.newMsg(RootNode(server), \addToTail));
this.sendSynthDef;

// only create synth now if it won't be created by ServerTree
if (persist.not) { this.updateSynth };

// only start listener is multiple clients expected
// only start listener to volume settings if multiple clients expected
if (server.options.maxLogins > 1) {
this.startListener
}
};
ServerBoot.add(initFunc, server);

updateFunc = {
ampSynthRunning = false;
ampSynth.isPlaying = false;
if(persist) { this.updateSynth }
};

Expand All @@ -64,12 +64,13 @@ Volume {
if (inlag.equalWithPrecision(lag).not) {
lag = inlag;
};
// triggers when gate closed, so we know ampSynth ends
if (ingate <= 0.0) {
ampSynthRunning = false;
ampSynth.isPlaying = false;
volume = invol;
this.changed(\amp, volume);
} {
ampSynthRunning = true;
ampSynth.isPlaying = true;
};
if (inbus != startBus) {
startBus = inbus;
Expand Down Expand Up @@ -109,7 +110,7 @@ Volume {

numChannels { ^numChannels ? server.options.numOutputBusChannels }
numChannels_ { |num|
if(ampSynthRunning and: { num != this.numChannels }) {
if(ampSynth.isPlaying and: { num != this.numChannels }) {
"Change in number of channels will not take effect until volume is reset to 0dB.".warn;
};
numChannels = num;
Expand All @@ -121,7 +122,7 @@ Volume {

if(active) {
if(server.hasBooted) {
if(ampSynthRunning.not) {
if(ampSynth.isPlaying.not) {
// Synth.after(server.defaultGroup, defName,
// [\volumeAmp, amp, \volumeLag, lag, \bus, startBus])

Expand All @@ -133,15 +134,21 @@ Volume {
*[\volumeAmp, amp, \volumeLag, lag, \bus, startBus]
);

ampSynthRunning = true;
// also catches stray endings of ampSynth
ampSynth.onFree {
// "ampSynth was freed".postln;
this.volume = 0;
ampSynth.isPlaying = false;
};
ampSynth.isPlaying = true;
} {
ampSynth.set(\volumeAmp, amp);
}
}
} {
if(ampSynthRunning) {
if(ampSynth.isPlaying) {
ampSynth.set(\volumeAmp, 1).release;
ampSynthRunning = false
ampSynth.isPlaying = false
}
}
}
Expand All @@ -164,7 +171,7 @@ Volume {

freeSynth {
ampSynth.release;
ampSynthRunning = false;
ampSynth.isPlaying = false;
}

// sets volume back to 1 - removes the synth
Expand Down
25 changes: 25 additions & 0 deletions testsuite/classlibrary/TestVolume.sc
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,29 @@ TestVolume : UnitTest {
s.quit.remove;
}

test_resetVolumeAfterKill {
var s = Server(thisMethod.name);

s.options.bindAddress = "0.0.0.0"; // allow connections from any address
s.options.maxLogins = 2; // set to 2 clients

this.bootServer(s);

0.2.wait;
// create ampSynth
s.volume = -3;

0.2.wait;
// kill ampSynth irregularly
s.sendMsg("n_free", s.volume.ampSynth.nodeID);

0.2.wait;
this.assertFloatEquals(s.volume.volume, 0,
"server should reset volume level when ampSynth dies irregularly."
);

s.quit.remove;
}


}

0 comments on commit 33bb6c9

Please sign in to comment.