Skip to content

Commit be2914a

Browse files
committed
fix(music): handle undefined array indices in chord/note arrays
1 parent 188e73b commit be2914a

1 file changed

Lines changed: 34 additions & 12 deletions

File tree

src/scripts/music.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -388,21 +388,29 @@ export function playChord(type: "function" | "loop" | "conditional"): void {
388388
switch (type) {
389389
case "function":
390390
// Major chord feel
391-
chord = [scale[0], scale[2], scale[4]];
391+
chord = [scale[0], scale[2], scale[4]].filter(
392+
(n): n is string => n !== undefined,
393+
);
392394
break;
393395
case "loop":
394396
// Suspended feel
395-
chord = [scale[0], scale[3], scale[4]];
397+
chord = [scale[0], scale[3], scale[4]].filter(
398+
(n): n is string => n !== undefined,
399+
);
396400
break;
397401
case "conditional":
398402
// Minor chord feel
399-
chord = [scale[0], scale[2], scale[5]];
403+
chord = [scale[0], scale[2], scale[5]].filter(
404+
(n): n is string => n !== undefined,
405+
);
400406
break;
401407
default:
402-
chord = [scale[0]];
408+
chord = [scale[0]].filter((n): n is string => n !== undefined);
403409
}
404410

405-
padSynth.triggerAttackRelease(chord, "2n");
411+
if (chord.length > 0) {
412+
padSynth.triggerAttackRelease(chord, "2n");
413+
}
406414
}
407415

408416
export function playPatternSound(patterns: {
@@ -468,25 +476,39 @@ export async function playCode(
468476
const maxNotes = Math.min(lines.length, 16);
469477
for (let i = 0; i < maxNotes; i++) {
470478
const note = scale[i % scale.length];
471-
melodySynth.triggerAttackRelease(note, "16n", time);
479+
if (note) {
480+
melodySynth.triggerAttackRelease(note, "16n", time);
481+
}
472482
time += noteDelay;
473483
}
474484

475485
// Play pattern chords after the melody
476486
const chordTime = time + 0.2;
477487

478488
if (patterns.hasFunction) {
479-
const chord = [scale[0], scale[2], scale[4]];
480-
padSynth.triggerAttackRelease(chord, "2n", chordTime);
489+
const chord = [scale[0], scale[2], scale[4]].filter(
490+
(n): n is string => n !== undefined,
491+
);
492+
if (chord.length > 0) {
493+
padSynth.triggerAttackRelease(chord, "2n", chordTime);
494+
}
481495
}
482496

483497
if (patterns.hasLoop) {
484-
const chord = [scale[0], scale[3], scale[4]];
485-
padSynth.triggerAttackRelease(chord, "2n", chordTime + 0.5);
498+
const chord = [scale[0], scale[3], scale[4]].filter(
499+
(n): n is string => n !== undefined,
500+
);
501+
if (chord.length > 0) {
502+
padSynth.triggerAttackRelease(chord, "2n", chordTime + 0.5);
503+
}
486504
}
487505

488506
if (patterns.hasConditional) {
489-
const chord = [scale[0], scale[2], scale[5]];
490-
padSynth.triggerAttackRelease(chord, "2n", chordTime + 1);
507+
const chord = [scale[0], scale[2], scale[5]].filter(
508+
(n): n is string => n !== undefined,
509+
);
510+
if (chord.length > 0) {
511+
padSynth.triggerAttackRelease(chord, "2n", chordTime + 1);
512+
}
491513
}
492514
}

0 commit comments

Comments
 (0)