Skip to content

Commit

Permalink
feat: added multiple melodies to melody for creating two handed melod…
Browse files Browse the repository at this point in the history
…ies, für elise demo
  • Loading branch information
matteobruni committed Dec 24, 2022
1 parent 414dd38 commit b2ed85f
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 8 deletions.
18 changes: 18 additions & 0 deletions plugins/sounds/src/Options/Classes/SoundsMelody.ts
Expand Up @@ -3,9 +3,13 @@ import type { ISoundsMelody } from "../Interfaces/ISoundsMelody";
import { SoundsNote } from "./SoundsNote";

export class SoundsMelody implements ISoundsMelody, IOptionLoader<ISoundsMelody> {
loop;
melodies: SoundsMelody[];
notes: SoundsNote[];

constructor() {
this.loop = false;
this.melodies = [];
this.notes = [];
}

Expand All @@ -14,6 +18,20 @@ export class SoundsMelody implements ISoundsMelody, IOptionLoader<ISoundsMelody>
return;
}

if (data.loop !== undefined) {
this.loop = data.loop;
}

if (data.melodies !== undefined) {
this.melodies = data.melodies.map((s) => {
const tmp = new SoundsMelody();

tmp.load(s);

return tmp;
});
}

if (data.notes !== undefined) {
this.notes = data.notes.map((s) => {
const tmp = new SoundsNote();
Expand Down
2 changes: 2 additions & 0 deletions plugins/sounds/src/Options/Interfaces/ISoundsMelody.ts
@@ -1,5 +1,7 @@
import type { ISoundsNote } from "./ISoundsNote";

export interface ISoundsMelody {
loop: boolean;
melodies: ISoundsMelody[];
notes: ISoundsNote[];
}
26 changes: 22 additions & 4 deletions plugins/sounds/src/SoundsInstance.ts
Expand Up @@ -260,11 +260,15 @@ export class SoundsInstance implements IContainerPlugin {
} else if (event.melodies) {
const melody = itemFromArray(event.melodies);

await this._playNote(melody.notes, 0);
if (melody.melodies.length) {
await Promise.allSettled(melody.melodies.map((m) => this._playNote(m.notes, 0, melody.loop)));
} else {
await this._playNote(melody.notes, 0, melody.loop);
}
} else if (event.notes) {
const note = itemFromArray(event.notes);

await this._playNote([note], 0);
await this._playNote([note], 0, false);
}
};

Expand Down Expand Up @@ -366,7 +370,11 @@ export class SoundsInstance implements IContainerPlugin {
});
}

private async _playNote(notes: SoundsNote[], noteIdx: number): Promise<void> {
private async _playNote(notes: SoundsNote[], noteIdx: number, loop: boolean): Promise<void> {
if (this._container.muted) {
return;
}

const note = notes[noteIdx];

if (!note) {
Expand All @@ -381,7 +389,17 @@ export class SoundsInstance implements IContainerPlugin {

await (promises instanceof Array ? Promise.allSettled(promises) : promises);

await this._playNote(notes, noteIdx + 1);
let nextNoteIdx = noteIdx + 1;

if (loop && nextNoteIdx >= notes.length) {
nextNoteIdx = nextNoteIdx % notes.length;
}

if (this._container.muted) {
return;
}

await this._playNote(notes, nextNoteIdx, loop);
}

private async _playNoteValue(notes: SoundsNote[], noteIdx: number, valueIdx: number): Promise<void> {
Expand Down
11 changes: 7 additions & 4 deletions plugins/sounds/src/utils.ts
Expand Up @@ -12,21 +12,24 @@ notes.set("Ab", [25.96, 51.91, 103.83, 207.65, 415.3, 830.61, 1661.22, 3322.44,
notes.set("A", [27.5, 55.0, 110.0, 220.0, 440.0, 880.0, 1760.0, 3520.0, 7040.0]);
notes.set("Bb", [29.14, 58.27, 116.54, 233.08, 466.16, 932.33, 1864.66, 3729.31, 7458.62]);
notes.set("B", [30.87, 61.74, 123.47, 246.94, 493.88, 987.77, 1975.53, 3951.07, 7902.13]);
notes.set("pause", [0]);

export function getNoteFrequency(note: string): number | undefined {
const regex = /([A-G]b?)(\d)/i,
const regex = /(([A-G]b?)(\d))|pause/i,
result = regex.exec(note);

if (!result || result.length !== 3) {
if (!result || !result.length) {
return;
}

const noteKey = result[1],
console.log(result);

const noteKey = result[2] || result[0],
noteItem = notes.get(noteKey);

if (!noteItem) {
return;
}

return noteItem[parseInt(result[2])];
return noteItem[parseInt(result[3] || "0")];
}
5 changes: 5 additions & 0 deletions utils/configs/src/index.ts
Expand Up @@ -103,6 +103,7 @@ import _snow from "./snow.json";
import _soundsAudio from "./soundsAudio.json";
import _soundsLoop from "./soundsLoop.json";
import _soundsMelodies from "./soundsMelodies.json";
import _soundsMelodyLoop from "./soundsMelodyLoop.json";
import _soundsNotes from "./soundsNotes.json";
import _speedDecay from "./speedDecay.json";
import _spin from "./spin.json";
Expand Down Expand Up @@ -226,6 +227,7 @@ const absorbers = _absorbers as unknown as ISourceOptions,
soundsAudio = _soundsAudio as unknown as ISourceOptions,
soundsLoop = _soundsLoop as unknown as ISourceOptions,
soundsMelodies = _soundsMelodies as unknown as ISourceOptions,
soundsMelodyLoop = _soundsMelodyLoop as unknown as ISourceOptions,
soundsNotes = _soundsNotes as unknown as ISourceOptions,
speedDecay = _speedDecay as unknown as ISourceOptions,
spin = _spin as unknown as ISourceOptions,
Expand Down Expand Up @@ -349,6 +351,7 @@ export type ExportedConfigurations = {
soundsAudio: ISourceOptions;
soundsLoop: ISourceOptions;
soundsMelodies: ISourceOptions;
soundsMelodyLoop: ISourceOptions;
soundsNotes: ISourceOptions;
speedDecay: ISourceOptions;
spin: ISourceOptions;
Expand Down Expand Up @@ -477,6 +480,7 @@ mainConfigs.configs = {
soundsAudio,
soundsLoop,
soundsMelodies,
soundsMelodyLoop,
soundsNotes,
speedDecay,
spin,
Expand Down Expand Up @@ -601,6 +605,7 @@ export {
soundsAudio,
soundsLoop,
soundsMelodies,
soundsMelodyLoop,
soundsNotes,
speedDecay,
spin,
Expand Down

0 comments on commit b2ed85f

Please sign in to comment.