Skip to content

Commit

Permalink
Moved processSound() to AbstractEvent for Ziffers
Browse files Browse the repository at this point in the history
  • Loading branch information
amiika committed Dec 9, 2023
1 parent 35c8c1b commit 204a5ae
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
41 changes: 41 additions & 0 deletions src/classes/AbstractEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
safeScale,
} from "zifferjs";
import { SkipEvent } from "./SkipEvent";
import { SoundParams } from "./SoundEvent";

export type EventOperation<T> = (instance: T, ...args: any[]) => void;

Expand Down Expand Up @@ -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<SoundParams>),
};
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 {
Expand Down
40 changes: 0 additions & 40 deletions src/classes/SoundEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SoundParams>),
};
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
// ================================================================================
Expand Down
17 changes: 12 additions & 5 deletions src/classes/ZPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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(
Expand All @@ -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,
Expand Down

0 comments on commit 204a5ae

Please sign in to comment.