Adding sample patterning to ziffers
This commit is contained in:
@ -3,6 +3,14 @@ import { type Editor } from "../main";
|
||||
import { MidiConnection } from "../IO/MidiConnection";
|
||||
import { midiToFreq, noteFromPc } from "zifferjs";
|
||||
|
||||
export type MidiParams = {
|
||||
note: number;
|
||||
bend?: number;
|
||||
channel?: number;
|
||||
port?: number;
|
||||
sustain?: number;
|
||||
}
|
||||
|
||||
export class MidiEvent extends AudibleEvent {
|
||||
midiConnection: MidiConnection;
|
||||
|
||||
@ -13,7 +21,7 @@ export class MidiEvent extends AudibleEvent {
|
||||
this.midiConnection = app.api.MidiConnection;
|
||||
}
|
||||
|
||||
chord = (value: number[]): this => {
|
||||
chord = (value: MidiParams[]): this => {
|
||||
this.values["chord"] = value;
|
||||
return this;
|
||||
};
|
||||
@ -79,9 +87,12 @@ export class MidiEvent extends AudibleEvent {
|
||||
};
|
||||
|
||||
out = (): void => {
|
||||
function play(note: number, event: MidiEvent): void {
|
||||
const channel = event.values.channel ? event.values.channel : 0;
|
||||
function play(event: MidiEvent, params?: MidiParams): void {
|
||||
const paramChannel = params && params.channel ? params.channel : 0;
|
||||
const channel = event.values.channel ? event.values.channel : paramChannel;
|
||||
const velocity = event.values.velocity ? event.values.velocity : 100;
|
||||
const paramNote = params && params.note ? params.note : 60;
|
||||
const note = event.values.note ? event.values.note : paramNote;
|
||||
|
||||
const sustain = event.values.sustain
|
||||
? event.values.sustain *
|
||||
@ -106,12 +117,11 @@ export class MidiEvent extends AudibleEvent {
|
||||
}
|
||||
|
||||
if(this.values.chord) {
|
||||
this.values.chord.forEach((note: number) => {
|
||||
play(note, this);
|
||||
this.values.chord.forEach((p: MidiParams) => {
|
||||
play(this, p);
|
||||
});
|
||||
} else {
|
||||
const note = this.values.note ? this.values.note : 60;
|
||||
play(note, this);
|
||||
play(this);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@ -7,6 +7,11 @@ import {
|
||||
// @ts-ignore
|
||||
} from "superdough";
|
||||
|
||||
export type SoundParams = {
|
||||
dur: number;
|
||||
s?: string;
|
||||
}
|
||||
|
||||
export class SoundEvent extends AudibleEvent {
|
||||
constructor(sound: string | object, public app: Editor) {
|
||||
super(app);
|
||||
|
||||
@ -2,8 +2,8 @@ import { Chord, Pitch, Rest as ZRest, Ziffers } from "zifferjs";
|
||||
import { Editor } from "../main";
|
||||
import { Event } from "./AbstractEvents";
|
||||
import { SkipEvent } from "./SkipEvent";
|
||||
import { SoundEvent } from "./SoundEvent";
|
||||
import { MidiEvent } from "./MidiEvent";
|
||||
import { SoundEvent, SoundParams } from "./SoundEvent";
|
||||
import { MidiEvent, MidiParams } from "./MidiEvent";
|
||||
import { RestEvent } from "./RestEvent";
|
||||
|
||||
export type InputOptions = { [key: string]: string | number };
|
||||
@ -131,7 +131,7 @@ export class Player extends Event {
|
||||
return areWeThereYet;
|
||||
};
|
||||
|
||||
sound(name: string) {
|
||||
sound(name?: string) {
|
||||
|
||||
if (this.areWeThereYet()) {
|
||||
const event = this.next() as Pitch | Chord | ZRest;
|
||||
@ -145,8 +145,10 @@ export class Player extends Event {
|
||||
"octave",
|
||||
"parsedScale"
|
||||
);
|
||||
if(event.sound) name = event.sound as string;
|
||||
if(event.soundIndex) obj.n = event.soundIndex;
|
||||
obj.dur = noteLengthInSeconds;
|
||||
return new SoundEvent(obj, this.app).sound(name);
|
||||
return new SoundEvent(obj, this.app).sound(name || "sine");
|
||||
} else if (event instanceof Chord) {
|
||||
const pitches = event.pitches.map((p) => {
|
||||
return p.getExisting(
|
||||
@ -158,7 +160,9 @@ export class Player extends Event {
|
||||
"parsedScale"
|
||||
);
|
||||
});
|
||||
return new SoundEvent({dur: noteLengthInSeconds}, this.app).chord(pitches).sound(name);
|
||||
const sound: SoundParams = {dur: noteLengthInSeconds};
|
||||
if(name) sound.s = name;
|
||||
return new SoundEvent(sound, this.app).chord(pitches);
|
||||
} else if (event instanceof ZRest) {
|
||||
return RestEvent.createRestProxy(event.duration, this.app);
|
||||
}
|
||||
@ -177,15 +181,16 @@ export class Player extends Event {
|
||||
"key",
|
||||
"scale",
|
||||
"octave",
|
||||
"parsedScale"
|
||||
"parsedScale",
|
||||
);
|
||||
if (event instanceof Pitch) {
|
||||
if(event.soundIndex) obj.channel = event.soundIndex;
|
||||
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.notes();
|
||||
const pitches = event.midiChord() as MidiParams[];
|
||||
return new MidiEvent(obj, this.app).chord(pitches);
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user