Skip to content

Commit bbbd441

Browse files
authored
Making setMaster and setSynth as previews when they are standalone (#4396)
* Feat: Preview_Voice * removed extra line
1 parent cfe01ec commit bbbd441

File tree

6 files changed

+71
-56
lines changed

6 files changed

+71
-56
lines changed

js/blocks/VolumeBlocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ function setupVolumeBlocks(activity) {
578578
}
579579
}
580580

581-
Singer.VolumeActions.setSynthVolume(arg0, arg1, turtle);
581+
Singer.VolumeActions.setSynthVolume(arg0, arg1, turtle, blk);
582582
}
583583
}
584584

js/logo.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,6 @@ class Logo {
367367
tur.singer.synthVolume[DEFAULTVOICE] = [DEFAULTVOLUME];
368368
}
369369

370-
Singer.setMasterVolume(this, DEFAULTVOLUME);
371370
for (const turtle in this.activity.turtles.turtleList) {
372371
for (const synth in this.activity.turtles.ithTurtle(turtle).singer.synthVolume) {
373372
Singer.setSynthVolume(this, turtle, synth, DEFAULTVOLUME);
@@ -1030,7 +1029,6 @@ class Logo {
10301029

10311030
Singer.masterBPM = TARGETBPM;
10321031
Singer.defaultBPMFactor = TONEBPM / TARGETBPM;
1033-
Singer.masterVolume = [DEFAULTVOLUME];
10341032
this.synth.changeInTemperament = false;
10351033

10361034
this._checkingCompletionState = false;

js/piemenus.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2594,7 +2594,6 @@ const piemenuVoices = (block, voiceLabels, voiceValues, categories, voice, rotat
25942594
}
25952595

25962596
setTimeout(() => {
2597-
that.activity.logo.synth.setMasterVolume(DEFAULTVOLUME);
25982597
Singer.setSynthVolume(that.activity.logo, 0, voice, DEFAULTVOLUME);
25992598
that.activity.logo.synth.trigger(0, "G4", 1 / 4, voice, null, null, false);
26002599
that.activity.logo.synth.start();

js/turtle-singer.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -665,11 +665,12 @@ class Singer {
665665
* @param {Number} volume
666666
* @returns {void}
667667
*/
668-
static setMasterVolume(logo, volume) {
668+
static setMasterVolume(logo, volume, blk) {
669669
const activity = logo.activity;
670+
const firstConnection = activity.logo.blockList[blk].connections[0];
671+
const lastConnection = last(activity.logo.blockList[blk].connections);
670672
volume = Math.min(Math.max(volume, 0), 100);
671-
672-
logo.synth.setMasterVolume(volume);
673+
logo.synth.setMasterVolume(volume, firstConnection, lastConnection);
673674
for (const turtle of activity.turtles.turtleList) {
674675
for (const synth in turtle.singer.synthVolume) {
675676
turtle.singer.synthVolume[synth].push(volume);
@@ -687,18 +688,18 @@ class Singer {
687688
* @param {Number} volume
688689
* @returns {void}
689690
*/
690-
static setSynthVolume(logo, turtle, synth, volume) {
691+
static setSynthVolume(logo, turtle, synth, volume, blk) {
691692
volume = Math.min(Math.max(volume, 0), 100);
692693

693694
switch (synth) {
694695
case "noise1":
695696
case "noise2":
696697
case "noise3":
697698
// Noise is very very loud
698-
logo.synth.setVolume(turtle, synth, volume / 25);
699+
logo.synth.setVolume(turtle, synth, volume / 25, blk);
699700
break;
700701
default:
701-
logo.synth.setVolume(turtle, synth, volume);
702+
logo.synth.setVolume(turtle, synth, volume, blk);
702703
}
703704
}
704705

js/turtleactions/VolumeActions.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,15 @@ function setupVolumeActions(activity) {
172172

173173
if (volume === 0) activity.errorMsg(_("Setting volume to 0."), blk);
174174

175+
if(Singer.masterVolume.length === 2) {
176+
Singer.masterVolume.pop();
177+
}
178+
175179
Singer.masterVolume.push(volume);
176180

177181
const tur = activity.turtles.ithTurtle(turtle);
178182
if (!tur.singer.suppressOutput) {
179-
Singer.setMasterVolume(activity.logo, volume);
183+
Singer.setMasterVolume(activity.logo, volume, blk);
180184
}
181185
}
182186

@@ -210,8 +214,10 @@ function setupVolumeActions(activity) {
210214
* @param {Number} turtle - Turtle index in turtles.turtleList
211215
* @returns {void}
212216
*/
213-
static setSynthVolume(synthname, volume, turtle) {
217+
static setSynthVolume(synthname, volume, turtle, blk) {
214218
let synth = null;
219+
const firstConnection = activity.logo.blockList[blk].connections[0];
220+
const lastConnection = last(activity.logo.blockList[blk].connections);
215221

216222
if (synthname === DEFAULTVOICE || synthname === _(DEFAULTVOICE)) {
217223
synth = DEFAULTVOICE;
@@ -263,6 +269,12 @@ function setupVolumeActions(activity) {
263269
tur.singer.synthVolume[synth].push(volume);
264270
if (!tur.singer.suppressOutput) {
265271
Singer.setSynthVolume(activity.logo, turtle, synth, volume);
272+
if(firstConnection === null && lastConnection === null) {
273+
setTimeout(()=>{
274+
activity.logo.synth.trigger(0, "G4", 1 / 4, synthname, null, null, false);
275+
},250)
276+
}
277+
266278
}
267279
}
268280

js/utils/synthutils.js

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,32 +1375,41 @@ function Synth() {
13751375

13761376
this._loadSample(sourceName);
13771377
if (sourceName in this.samples.voice || sourceName in this.samples.drum) {
1378-
instruments[turtle][instrumentName] = this._createSampleSynth(
1379-
turtle,
1380-
instrumentName,
1381-
sourceName,
1382-
params
1383-
).toDestination();
1378+
if (!instruments[turtle][instrumentName]) {
1379+
instruments[turtle][instrumentName] = this._createSampleSynth(
1380+
turtle,
1381+
instrumentName,
1382+
sourceName,
1383+
params
1384+
)
1385+
}
13841386
} else if (sourceName in BUILTIN_SYNTHS) {
1385-
instruments[turtle][instrumentName] = this._createBuiltinSynth(
1386-
turtle,
1387-
instrumentName,
1388-
sourceName,
1389-
params
1390-
).toDestination();
1387+
if (!instruments[turtle][instrumentName]) {
1388+
instruments[turtle][instrumentName] = this._createBuiltinSynth(
1389+
turtle,
1390+
instrumentName,
1391+
sourceName,
1392+
params
1393+
);
1394+
}
13911395
} else if (sourceName in CUSTOM_SYNTHS) {
1392-
instruments[turtle][instrumentName] = this._createCustomSynth(
1393-
sourceName,
1394-
params
1395-
).toDestination();
1396+
if (!instruments[turtle][instrumentName]) {
1397+
instruments[turtle][instrumentName] = this._createCustomSynth(
1398+
sourceName,
1399+
params
1400+
)
1401+
}
1402+
13961403
instrumentsSource[instrumentName] = [0, "poly"];
13971404
} else if (sourceName in CUSTOMSAMPLES) {
1398-
instruments[turtle][instrumentName] = this._createSampleSynth(
1399-
turtle,
1400-
instrumentName,
1401-
sourceName,
1402-
params
1403-
).toDestination();
1405+
if (!instruments[turtle][instrumentName]) {
1406+
instruments[turtle][instrumentName] = this._createSampleSynth(
1407+
turtle,
1408+
instrumentName,
1409+
sourceName,
1410+
params
1411+
)
1412+
}
14041413
} else {
14051414
if (sourceName.length >= 4) {
14061415
if (sourceName.slice(0, 4) === "http") {
@@ -1975,35 +1984,31 @@ function Synth() {
19751984
instruments[turtle][instrumentName].volume.value = db;
19761985
}
19771986
};
1978-
1979-
/**
1980-
* Gets the volume of a specific instrument for a given turtle.
1981-
* @function
1982-
* @memberof Synth
1983-
* @param {string} turtle - The name of the turtle.
1984-
* @param {string} instrumentName - The name of the instrument.
1985-
* @returns {number} The volume level.
1986-
* @deprecated This method is currently unused and may not return correct values.
1987-
*/
1988-
this.getVolume = (turtle, instrumentName) => {
1989-
if (instrumentName in instruments[turtle]) {
1990-
return instruments[turtle][instrumentName].volume.value;
1991-
} else {
1992-
// eslint-disable-next-line no-console
1993-
console.debug("instrument not found");
1994-
return 50;
1995-
}
1996-
};
19971987

19981988
/**
19991989
* Sets the master volume for all instruments.
20001990
* @function
20011991
* @memberof Synth
20021992
* @param {number} volume - The master volume level (0 to 100).
20031993
*/
2004-
this.setMasterVolume = volume => {
2005-
const db = Tone.gainToDb(volume / 100);
2006-
Tone.Destination.volume.rampTo(db, 0.01);
1994+
this.setMasterVolume = (volume, firstConnection, lastConnection) => {
1995+
if (!instruments[0]["electronic synth"]) {
1996+
this.createDefaultSynth(0);
1997+
}
1998+
this.setVolume(0, "electronic synth", volume);
1999+
2000+
2001+
if (firstConnection === null && lastConnection === null) {
2002+
// Reset volume to default (0 dB) first
2003+
Tone.Destination.volume.rampTo(0, 0.01);
2004+
setTimeout(()=>{
2005+
this.trigger(0, "G4", 1 / 4, "electronic synth", null, null, false);
2006+
},200)
2007+
}
2008+
else{
2009+
const db = Tone.gainToDb(volume / 100);
2010+
Tone.Destination.volume.rampTo(db, 0.01);
2011+
}
20072012
};
20082013

20092014
/**

0 commit comments

Comments
 (0)