Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Bubobubobubobubo/Topos
Browse files Browse the repository at this point in the history
  • Loading branch information
Bubobubobubobubo committed Nov 30, 2023
2 parents ee6dbf9 + 278dce0 commit 077e7ac
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 58 deletions.
46 changes: 45 additions & 1 deletion src/classes/AbstractEvents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Editor } from "../main";
import { freqToMidi, resolvePitchBend, safeScale } from "zifferjs";
import { freqToMidi, chord as parseChord, noteNameToMidi, resolvePitchBend, safeScale } from "zifferjs";
import { SkipEvent } from "./SkipEvent";

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

Expand Down Expand Up @@ -310,6 +311,49 @@ export abstract class AudibleEvent extends AbstractEvent {
return this;
};

protected updateValue<T>(
key: string,
value: T | T[] | null
): this {
if (value == null) return this;
this.values[key] = value;
return this;
}

public note = (value: number | string | null, ...kwargs: number[]|string[]) => {
if (typeof value === "string") {
const parsedNote = noteNameToMidi(value);
return this.updateValue("note", [parsedNote, ...kwargs].flat(Infinity));
} else if (typeof value == null || value == undefined) {
return new SkipEvent();
} else {
return this.updateValue("note", [value, ...kwargs].flat(Infinity));
}
};

public chord = (value: number|string, ...kwargs: number[]) => {
if(typeof value === "string") {
const chord = parseChord(value);
return this.updateValue("note", chord);
} else {
const chord = [value, ...kwargs].flat(Infinity);
return this.updateValue("note", chord);
}
};

public invert = (howMany: number = 0) => {
if (this.values.note) {
let notes = [...this.values.note];
notes = howMany < 0 ? [...notes].reverse() : notes;
for (let i = 0; i < Math.abs(howMany); i++) {
notes[i % notes.length] += howMany <= 0 ? -12 : 12;
}
return this.updateValue("note", notes);
} else {
return this;
}
};

freq = (value: number | number[], ...kwargs: number[]): this => {
/*
* This function is used to set the frequency of the Event.
Expand Down
12 changes: 1 addition & 11 deletions src/classes/MidiEvent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AudibleEvent } from "./AbstractEvents";
import { type Editor } from "../main";
import { MidiConnection } from "../IO/MidiConnection";
import { noteFromPc, chord as parseChord } from "zifferjs";
import { noteFromPc } from "zifferjs";
import {
filterObject,
arrayOfObjectsToObjectWithArrays,
Expand Down Expand Up @@ -29,16 +29,6 @@ export class MidiEvent extends AudibleEvent {
this.midiConnection = app.api.MidiConnection;
}

public chord = (value: string) => {
this.values.note = parseChord(value);
return this;
};

note = (value: number | number[]): this => {
this.values["note"] = value;
return this;
};

sustain = (value: number | number[]): this => {
this.values["sustain"] = value;
return this;
Expand Down
43 changes: 0 additions & 43 deletions src/classes/SoundEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import {
objectWithArraysToArrayOfObjects,
} from "../Utils/Generic";
import {
chord as parseChord,
midiToFreq,
noteFromPc,
noteNameToMidi,
} from "zifferjs";

import {
Expand All @@ -36,15 +34,6 @@ export class SoundEvent extends AudibleEvent {
nudge: number;
sound: any;

public updateValue<T>(
key: string,
value: T | T[] | SoundParams[] | null,
): this {
if (value == null) return this;
this.values[key] = value;
return this;
}

private static methodMap = {
volume: ["volume", "vol"],
zrand: ["zrand", "zr"],
Expand Down Expand Up @@ -453,38 +442,6 @@ export class SoundEvent extends AudibleEvent {
this.values.freq = newArrays.freq;
};

public chord = (value: string) => {
const chord = parseChord(value);
return this.updateValue("note", chord);
};

public invert = (howMany: number = 0) => {
if (this.values.chord) {
let notes = this.values.chord.map(
(obj: { [key: string]: number }) => obj.note,
);
notes = howMany < 0 ? [...notes].reverse() : notes;
for (let i = 0; i < Math.abs(howMany); i++) {
notes[i % notes.length] += howMany <= 0 ? -12 : 12;
}
const chord = notes.map((note: number) => {
return { note: note, freq: midiToFreq(note) };
});
return this.updateValue("chord", chord);
} else {
return this;
}
};
public note = (value: number | string | null) => {
if (typeof value === "string") {
return this.updateValue("note", noteNameToMidi(value));
} else if (typeof value == null || value == undefined) {
return this.updateValue("note", 0).updateValue("gain", 0);
} else {
return this.updateValue("note", value);
}
};

out = (orbit?: number | number[]): void => {
if (orbit) this.values["orbit"] = orbit;
const events = objectWithArraysToArrayOfObjects(this.values, [
Expand Down
7 changes: 4 additions & 3 deletions src/extensions/NumberExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type UserAPI } from "../API";
import { MidiEvent } from "../classes/MidiEvent";
import { Player } from "../classes/ZPlayer";
import { SoundEvent } from "../classes/SoundEvent";
import { SkipEvent } from "../classes/SkipEvent";

declare global {
interface Number {
Expand All @@ -24,7 +25,7 @@ declare global {
z15(): Player;
z16(): Player;
midi(): MidiEvent;
sound(name: string): SoundEvent;
sound(name: string): SoundEvent|SkipEvent;
}
}

Expand Down Expand Up @@ -101,11 +102,11 @@ export const makeNumberExtensions = (api: UserAPI) => {
return api.midi(this.valueOf(), ...kwargs);
};

Number.prototype.sound = function (name: string) {
Number.prototype.sound = function (name: string): SoundEvent|SkipEvent {
if (Number.isInteger(this.valueOf())) {
return (api.sound(name) as SoundEvent).note(this.valueOf());
} else {
return (api.sound(name) as SoundEvent).freq(this.valueOf());
}
}
};
};

0 comments on commit 077e7ac

Please sign in to comment.