diff --git a/src/classes/AbstractEvents.ts b/src/classes/AbstractEvents.ts index 82bd223..7fe450e 100644 --- a/src/classes/AbstractEvents.ts +++ b/src/classes/AbstractEvents.ts @@ -7,6 +7,7 @@ import { safeScale, } from "zifferjs"; import { SkipEvent } from "./SkipEvent"; +import { SoundParams } from "./SoundEvent"; export type EventOperation = (instance: T, ...args: any[]) => void; @@ -236,6 +237,46 @@ export class AbstractEvent { } return this; }; + + protected processSound = ( + sound: string | string[] | SoundParams | SoundParams[], + ): SoundParams => { + if (Array.isArray(sound) && typeof sound[0] === "string") { + const s: string[] = []; + const n: number[] = []; + sound.forEach((str) => { + const parts = (str as string).split(":"); + s.push(parts[0]); + if (parts[1]) { + n.push(parseInt(parts[1])); + } + }); + return { + s, + n: n.length > 0 ? n : undefined, + dur: this.app.clock.convertPulseToSecond(this.app.clock.ppqn), + }; + } else if (typeof sound === "object") { + const validatedObj: SoundParams = { + dur: this.app.clock.convertPulseToSecond(this.app.clock.ppqn), + ...(sound as Partial), + }; + return validatedObj; + } else { + if (sound.includes(":")) { + const vals = sound.split(":"); + const s = vals[0]; + const n = parseInt(vals[1]); + return { + s, + n, + dur: this.app.clock.convertPulseToSecond(this.app.clock.ppqn), + }; + } else { + return { s: sound, dur: 0.5 }; + } + } + }; } export abstract class AudibleEvent extends AbstractEvent { diff --git a/src/classes/SoundEvent.ts b/src/classes/SoundEvent.ts index 272c4a3..13ec682 100644 --- a/src/classes/SoundEvent.ts +++ b/src/classes/SoundEvent.ts @@ -369,46 +369,6 @@ export class SoundEvent extends AudibleEvent { this.values = this.processSound(sound); } - private processSound = ( - sound: string | string[] | SoundParams | SoundParams[], - ): SoundParams => { - if (Array.isArray(sound) && typeof sound[0] === "string") { - const s: string[] = []; - const n: number[] = []; - sound.forEach((str) => { - const parts = (str as string).split(":"); - s.push(parts[0]); - if (parts[1]) { - n.push(parseInt(parts[1])); - } - }); - return { - s, - n: n.length > 0 ? n : undefined, - dur: this.app.clock.convertPulseToSecond(this.app.clock.ppqn), - }; - } else if (typeof sound === "object") { - const validatedObj: SoundParams = { - dur: this.app.clock.convertPulseToSecond(this.app.clock.ppqn), - ...(sound as Partial), - }; - return validatedObj; - } else { - if (sound.includes(":")) { - const vals = sound.split(":"); - const s = vals[0]; - const n = parseInt(vals[1]); - return { - s, - n, - dur: this.app.clock.convertPulseToSecond(this.app.clock.ppqn), - }; - } else { - return { s: sound, dur: 0.5 }; - } - } - }; - // ================================================================================ // AbstactEvent overrides // ================================================================================ diff --git a/src/classes/ZPlayer.ts b/src/classes/ZPlayer.ts index de8bedb..8ce6f74 100644 --- a/src/classes/ZPlayer.ts +++ b/src/classes/ZPlayer.ts @@ -155,14 +155,14 @@ export class Player extends AbstractEvent { return areWeThereYet; }; - sound(name?: string) { + sound(name?: string | string[] | SoundParams | SoundParams[]) { if (this.areWeThereYet()) { const event = this.next() as Pitch | Chord | ZRest; const noteLengthInSeconds = this.app.clock.convertPulseToSecond( event.duration * 4 * this.app.clock.ppqn, ); if (event instanceof Pitch) { - const obj = event.getExisting( + let obj = event.getExisting( "freq", "note", "pitch", @@ -171,10 +171,14 @@ export class Player extends AbstractEvent { "octave", "parsedScale", ) as SoundParams; + if (event.sound) name = event.sound as string; + if(name) obj = {...obj, ...this.processSound(name)}; + else obj.s = "sine"; + if (event.soundIndex) obj.n = event.soundIndex as number; obj.dur = noteLengthInSeconds; - return new SoundEvent(obj, this.app).sound(name || "sine"); + return new SoundEvent(obj, this.app); } else if (event instanceof Chord) { const pitches = event.pitches.map((p) => { return p.getExisting( @@ -187,8 +191,11 @@ export class Player extends AbstractEvent { "parsedScale", ); }) as SoundParams[]; - const add = { dur: noteLengthInSeconds } as SoundParams; - if (name) add.s = name; + + let add = { dur: noteLengthInSeconds} as SoundParams; + if(name) add = {...add, ...this.processSound(name)}; + else add.s = "sine"; + let sound = arrayOfObjectsToObjectWithArrays( pitches, add,