Updating zifferjs and rearrange classes under folder

This commit is contained in:
2023-08-24 00:17:57 +03:00
parent f483262b7d
commit c6a2f5f1a8
8 changed files with 121 additions and 80 deletions

View File

@ -4,10 +4,10 @@ import { tryEvaluate } from "./Evaluator";
import { DrunkWalk } from "./Utils/Drunk";
import { scale } from "./Scales";
import { Editor } from "./main";
import { Sound } from "./Sound";
import { Note } from "./Note";
import { Sound } from "./classes/Sound";
import { Note } from "./classes/Note";
import { LRUCache } from "lru-cache";
import { Player } from "./ZPlayer";
import { Player } from "./classes/ZPlayer";
import {
samples,
initAudioOnFirstClick,

View File

@ -1,6 +1,7 @@
import { type Editor } from './main';
import { type Editor } from '../main';
import { freqToMidi, resolvePitchBend, getScale, isScale, parseScala } from 'zifferjs';
export class Event {
export abstract class Event {
seedValue: string|undefined = undefined;
randomGen: Function = Math.random;
app: Editor;
@ -72,4 +73,50 @@ export class Event {
return this;
}
}
export abstract class SoundEvent extends Event {
constructor(app: Editor) {
super(app);
}
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;
}
freq = (value: number): this => {
this.values['freq'] = value;
const midiNote = freqToMidi(value);
if(midiNote % 1 !== 0) {
this.values['note'] = Math.floor(midiNote);
this.values['bend'] = resolvePitchBend(midiNote)[1];
} else {
this.values['note'] = midiNote;
}
return this;
}
update = (): void => {
// Overwrite in subclasses
}
}

View File

@ -1,9 +1,9 @@
import { Event } from './Event';
import { type Editor } from './main';
import { MidiConnection } from "./IO/MidiConnection";
import { freqToMidi, midiToFreq, resolvePitchBend, noteFromPc, getScale, isScale, parseScala } from 'zifferjs';
import { SoundEvent } from './Event';
import { type Editor } from '../main';
import { MidiConnection } from "../IO/MidiConnection";
import { midiToFreq, noteFromPc } from 'zifferjs';
export class Note extends Event {
export class Note extends SoundEvent {
midiConnection: MidiConnection;
constructor(input: number|object, public app: Editor) {
@ -18,11 +18,6 @@ export class Note extends Event {
return this;
}
duration = (value: number): this => {
this.values['duration'] = value;
return this;
}
sustain = (value: number): this => {
this.values['sustain'] = value;
return this;
@ -55,18 +50,6 @@ export class Note extends Event {
}
}
freq = (value: number): this => {
this.values['freq'] = value;
const midiNote = freqToMidi(value);
if(midiNote % 1 !== 0) {
this.values['note'] = Math.floor(midiNote);
this.values['bend'] = resolvePitchBend(midiNote)[1];
} else {
this.values['note'] = midiNote;
}
return this;
}
bend = (value: number): this => {
this.values['bend'] = value;
return this;
@ -80,40 +63,14 @@ export class Note extends Event {
}
update = (): void => {
// TODO: Figure out why _ is sometimes added?
if(this.values.type === 'Pitch' || this.values.type === '_Pitch') {
const [note,bend] = noteFromPc(this.values.key!, this.values.pitch!, this.values.parsedScale!, this.values.octave!);
if(this.values.key && this.values.pitch && this.values.parsedScale && this.values.octave) {
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;
}
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 => {
const note = this.values.note ? this.values.note : 60;
const channel = this.values.channel ? this.values.channel : 0;

View File

@ -1,4 +1,4 @@
import { type Editor } from './main';
import { type Editor } from '../main';
import { Event } from "./Event";
export class Rest extends Event {
@ -23,6 +23,10 @@ export class Rest extends Event {
// @ts-ignore
return target[propKey];
},
// @ts-ignore
set(target, propKey, value, receiver) {
return false;
}
});
}

View File

@ -1,12 +1,13 @@
import { type Editor } from './main';
import { Event } from './Event';
import { type Editor } from '../main';
import { SoundEvent } from './Event';
import { midiToFreq, noteFromPc } from 'zifferjs';
import {
superdough,
// @ts-ignore
} from "superdough";
export class Sound extends Event {
export class Sound extends SoundEvent {
constructor(sound: string|object, public app: Editor) {
super(app);
@ -26,12 +27,6 @@ export class Sound extends Event {
};
dec = this.decay;
sustain = (value: number): this => {
this.values["sustain"] = value;
return this;
};
sus = this.sustain;
release = (value: number): this => {
this.values["release"] = value;
return this;
@ -223,11 +218,19 @@ export class Sound extends Event {
}
};
dur = (value: number): this => {
// NOTE: Sustain of the sound. duration() from the superclass Event is used to change the note length.
sustain = (value: number): this => {
this.values["dur"] = value;
return this;
};
update = (): void => {
if(this.values.key && this.values.pitch && this.values.parsedScale && this.values.octave) {
const [note,_] = noteFromPc(this.values.key, this.values.pitch, this.values.parsedScale, this.values.octave);
this.values.freq = midiToFreq(note);
}
}
out = (): object => {
return superdough(
this.values,
@ -236,5 +239,4 @@ export class Sound extends Event {
);
};
}

View File

@ -1,5 +1,5 @@
import { Chord, Pitch, Rest as ZRest, Ziffers } from "zifferjs";
import { Editor } from "./main";
import { Editor } from "../main";
import { Event } from "./Event";
import { Sound } from "./Sound";
import { Note } from "./Note";
@ -11,6 +11,8 @@ export class Player extends Event {
callTime: number = 0;
played: boolean = false;
current!: Pitch|Chord|ZRest;
retro: boolean = false;
tick: number = 0;
constructor(input: string, options: object, public app: Editor) {
super(app);
@ -25,7 +27,13 @@ export class Player extends Event {
}
areWeThereYet = (): boolean => {
return (this.ziffers.notStarted() || this.app.api.epulse() > this.callTime+(this.current.duration*this.app.api.ppqn()))
const howAboutNow = (this.ziffers.notStarted() || this.app.api.epulse() > this.callTime+(this.current.duration*this.app.api.ppqn()));
if(howAboutNow) {
this.tick = 0;
} else {
this.tick++;
}
return howAboutNow;
}
sound(name: string) {
@ -33,7 +41,8 @@ export class Player extends Event {
const event = this.next() as Pitch|Chord|ZRest;
if(event instanceof Pitch) {
// TODO: Quick hack. Select which attributes to use, but some ziffers stuff is needed for chaining key change etc.
return new Sound(event.asObject(), this.app).sound(name);
const obj = event.getExisting("freq","pitch","key","scale","octave");
return new Sound(obj, this.app).sound(name);
} else if(event instanceof Rest) {
return Rest.createRestProxy(event.duration, this.app);
}
@ -48,7 +57,8 @@ export class Player extends Event {
const event = this.next() as Pitch|Chord|ZRest;
if(event instanceof Pitch) {
// TODO: Quick hack. Select which attributes to use, but some ziffers stuff is needed for chaining key change etc.
const note = new Note(event.asObject(), this.app);
const obj = event.getExisting("note","pitch","bend","key","scale","octave");
const note = new Note(obj, this.app);
return value ? note.note(value) : note;
} else if(event instanceof ZRest) {
return Rest.createRestProxy(event.duration, this.app);
@ -59,6 +69,28 @@ export class Player extends Event {
}
}
scale(name: string) {
this.ziffers.scale(name);
return this;
}
key(name: string) {
this.ziffers.key(name);
return this;
}
octave(value: number) {
this.ziffers.octave(value);
return this;
}
retrograde() {
if(this.tick === 0 && this.ziffers.index === 0) {
this.ziffers.retrograde();
}
return this;
}
out = (): void => {
// TODO?
}