Refactor object mangling and add midi support

This commit is contained in:
2023-11-03 16:07:39 +02:00
parent 269df3f899
commit a09e3a76be
7 changed files with 242 additions and 161 deletions

View File

@ -5,6 +5,7 @@ import { SkipEvent } from "./SkipEvent";
import { SoundEvent, SoundParams } from "./SoundEvent";
import { MidiEvent, MidiParams } from "./MidiEvent";
import { RestEvent } from "./RestEvent";
import { arrayOfObjectsToObjectWithArrays } from "../Utils/Generic";
export type InputOptions = { [key: string]: string | number };
@ -139,37 +140,39 @@ export class Player extends Event {
};
sound(name?: string) {
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(
"freq",
"note",
"pitch",
"key",
"scale",
"octave",
"parsedScale"
);
) as SoundParams;
if(event.sound) name = event.sound as string;
if(event.soundIndex) obj.n = event.soundIndex;
if(event.soundIndex) obj.n = event.soundIndex as number;
obj.dur = noteLengthInSeconds;
return new SoundEvent(obj, this.app).sound(name || "sine");
} else if (event instanceof Chord) {
const pitches = event.pitches.map((p) => {
return p.getExisting(
"freq",
"note",
"pitch",
"key",
"scale",
"octave",
"parsedScale"
);
});
const sound: SoundParams = {dur: noteLengthInSeconds};
if(name) sound.s = name;
return new SoundEvent(sound, this.app).chord(pitches);
}) as SoundParams[];
const add = { dur: noteLengthInSeconds } as SoundParams;
if(name) add.s = name;
let sound = arrayOfObjectsToObjectWithArrays(pitches,add) as SoundParams;
return new SoundEvent(sound, this.app);
} else if (event instanceof ZRest) {
return RestEvent.createRestProxy(event.duration, this.app);
}
@ -189,16 +192,17 @@ export class Player extends Event {
"scale",
"octave",
"parsedScale",
);
) as MidiParams;
if (event instanceof Pitch) {
if(event.soundIndex) obj.channel = event.soundIndex;
if(event.soundIndex) obj.channel = event.soundIndex as number;
const note = new MidiEvent(obj, this.app);
return value ? note.note(value) : note;
} else if (event instanceof ZRest) {
return RestEvent.createRestProxy(event.duration, this.app);
} else if (event instanceof Chord) {
const pitches = event.midiChord() as MidiParams[];
return new MidiEvent(obj, this.app).chord(pitches);
const obj = arrayOfObjectsToObjectWithArrays(pitches) as MidiParams;
return new MidiEvent(obj, this.app);
}
} else {
return SkipEvent.createSkipProxy();