New ziffers player
This commit is contained in:
54
src/Note.ts
54
src/Note.ts
@ -1,16 +1,15 @@
|
||||
import { Event } from './Event';
|
||||
import { type Editor } from './main';
|
||||
import { MidiConnection } from "./IO/MidiConnection";
|
||||
import { freqToMidi, resolvePitchBend } from 'zifferjs';
|
||||
import { freqToMidi, midiToFreq, resolvePitchBend, noteFromPc, getScale, isScale, parseScala } from 'zifferjs';
|
||||
|
||||
export class Note extends Event {
|
||||
values: { [key: string]: any };
|
||||
midiConnection: MidiConnection;
|
||||
|
||||
constructor(input: number|object, public app: Editor) {
|
||||
super(app);
|
||||
if(typeof input === 'number') input = { 'note': input };
|
||||
this.values = input;
|
||||
if(typeof input === 'number') this.values['note'] = input;
|
||||
else this.values = input;
|
||||
this.midiConnection = app.api.MidiConnection
|
||||
}
|
||||
|
||||
@ -24,6 +23,11 @@ export class Note extends Event {
|
||||
return this;
|
||||
}
|
||||
|
||||
sustain = (value: number): this => {
|
||||
this.values['sustain'] = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
channel = (value: number): this => {
|
||||
this.values['channel'] = value;
|
||||
return this;
|
||||
@ -75,13 +79,49 @@ export class Note extends Event {
|
||||
return this;
|
||||
}
|
||||
|
||||
update = (): void => {
|
||||
console.log(this.values.type);
|
||||
if(this.values.type === 'Pitch') {
|
||||
const [note,bend] = noteFromPc(this.values.key!, this.values.pitch!, this.values.parsedScale!, this.values.octave!);
|
||||
this.values.note = note;
|
||||
this.values.freq = midiToFreq(note);
|
||||
if(bend) {
|
||||
this.values.bend = bend;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
octave = (value: number): this => {
|
||||
this.values['octave'] = value;
|
||||
this.update();
|
||||
return this;
|
||||
}
|
||||
|
||||
key = (value: string): this => {
|
||||
this.values['key'] = value;
|
||||
this.update();
|
||||
return this;
|
||||
}
|
||||
|
||||
scale = (value: string): this => {
|
||||
if(!isScale(value)) {
|
||||
this.values.parsedScale = parseScala(value) as number[];
|
||||
} else {
|
||||
this.values.scaleName = value;
|
||||
this.values.parsedScale = getScale(value) as number[];
|
||||
}
|
||||
this.update();
|
||||
return this;
|
||||
}
|
||||
|
||||
out = (): void => {
|
||||
console.log("NOTE OUT", this.values);
|
||||
const note = this.values.note ? this.values.note : 60;
|
||||
const channel = this.values.channel ? this.values.channel : 0;
|
||||
const velocity = this.values.velocity ? this.values.velocity : 100;
|
||||
|
||||
const duration = this.values.duration ?
|
||||
this.values.duration * this.app.clock.pulse_duration * this.app.api.ppqn() :
|
||||
const sustain = this.values.sustain ?
|
||||
this.values.sustain * this.app.clock.pulse_duration * this.app.api.ppqn() :
|
||||
this.app.clock.pulse_duration * this.app.api.ppqn();
|
||||
|
||||
const bend = this.values.bend ? this.values.bend : undefined;
|
||||
@ -90,7 +130,7 @@ export class Note extends Event {
|
||||
this.midiConnection.getMidiOutputIndex(this.values.port) :
|
||||
this.midiConnection.getCurrentMidiPortIndex();
|
||||
|
||||
this.midiConnection.sendMidiNote(note, channel, velocity, duration, port, bend);
|
||||
this.midiConnection.sendMidiNote(note, channel, velocity, sustain, port, bend);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user