This commit is contained in:
2023-12-01 11:16:16 +01:00
parent 31adc17a36
commit a34f1a33eb
7 changed files with 117 additions and 172 deletions

View File

@ -1581,7 +1581,9 @@ export class UserAPI {
// Low Frequency Oscillators
// =============================================================
line = (start: number, end: number, step: number = 1): number[] => {
public range = (v: number, a: number, b: number): number => v * (b - a) + a;
public line = (start: number, end: number, step: number = 1): number[] => {
/**
* Returns an array of values between start and end, with a given step.
*
@ -1603,7 +1605,11 @@ export class UserAPI {
return result;
};
sine = (freq: number = 1, times: number = 1, offset: number = 0): number => {
public sine = (
freq: number = 1,
times: number = 1,
offset: number = 0,
): number => {
/**
* Returns a sine wave between -1 and 1.
*
@ -1617,7 +1623,11 @@ export class UserAPI {
);
};
usine = (freq: number = 1, times: number = 1, offset: number = 0): number => {
public usine = (
freq: number = 1,
times: number = 1,
offset: number = 0,
): number => {
/**
* Returns a sine wave between 0 and 1.
*

View File

@ -4,7 +4,8 @@ import { tryEvaluate } from "./Evaluator";
import { getAudioContext } from "superdough";
// @ts-ignore
import "zyklus";
const zeroPad = (num: number, places: number) => String(num).padStart(places, "0");
const zeroPad = (num: number, places: number) =>
String(num).padStart(places, "0");
export interface TimePosition {
/**
@ -61,7 +62,10 @@ export class Clock {
this.running = true;
this.deadline = 0;
this.timeviewer = document.getElementById("timeviewer")!;
this.clock = getAudioContext().createClock(this.clockCallback, this.pulse_duration)
this.clock = getAudioContext().createClock(
this.clockCallback,
this.pulse_duration,
);
}
clockCallback = (time: number, duration: number, tick: number) => {
@ -89,7 +93,6 @@ export class Clock {
}
// Implement TransportNode clock callback and update clock info with it
};
convertTicksToTimeposition(ticks: number): TimePosition {
@ -183,13 +186,13 @@ export class Clock {
this.app.audioContext.resume();
this.running = true;
this.app.api.MidiConnection.sendStartMessage();
this.clock.start()
this.clock.start();
}
public pause(): void {
this.running = false;
this.app.api.MidiConnection.sendStopMessage();
this.clock.pause()
this.clock.pause();
}
public stop(): void {

View File

@ -1,68 +0,0 @@
import { tryEvaluate } from "./Evaluator";
const zeroPad = (num, places) => String(num).padStart(places, "0");
export class TransportNode extends AudioWorkletNode {
constructor(context, options, application) {
super(context, "transport", options);
this.app = application;
this.port.addEventListener("message", this.handleMessage);
this.port.start();
this.timeviewer = document.getElementById("timeviewer");
}
/** @type {(this: MessagePort, ev: MessageEvent<any>) => any} */
handleMessage = (message) => {
if (message.data) {
if (message.data.type === "bang") {
if (this.app.clock.running) {
if (this.app.settings.send_clock) {
this.app.api.MidiConnection.sendMidiClock();
}
const futureTimeStamp = this.app.clock.convertTicksToTimeposition(
this.app.clock.tick,
);
this.app.clock.time_position = futureTimeStamp;
if (futureTimeStamp.pulse % this.app.clock.ppqn == 0) {
this.timeviewer.innerHTML = `${zeroPad(futureTimeStamp.bar, 2)}:${
futureTimeStamp.beat + 1
} / ${this.app.clock.bpm}`;
}
if (this.app.exampleIsPlaying) {
tryEvaluate(this.app, this.app.example_buffer);
} else {
tryEvaluate(this.app, this.app.global_buffer);
}
this.app.clock.incrementTick(message.data.bpm);
}
}
}
};
start() {
this.port.postMessage({ type: "start" });
}
pause() {
this.port.postMessage({ type: "pause" });
}
resume() {
this.port.postMessage({ type: "resume" });
}
setBPM(bpm) {
this.port.postMessage({ type: "bpm", value: bpm });
}
setPPQN(ppqn) {
this.port.postMessage({ type: "ppqn", value: ppqn });
}
setNudge(nudge) {
this.port.postMessage({ type: "nudge", value: nudge });
}
stop() {
this.port.postMessage({ type: "stop" });
}
}

View File

@ -1,5 +1,11 @@
import { type Editor } from "../main";
import { freqToMidi, chord as parseChord, noteNameToMidi, 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;
@ -311,16 +317,16 @@ export abstract class AudibleEvent extends AbstractEvent {
return this;
};
protected updateValue<T>(
key: string,
value: T | T[] | null
): 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[]) => {
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));
@ -331,8 +337,8 @@ export abstract class AudibleEvent extends AbstractEvent {
}
};
public chord = (value: number|string, ...kwargs: number[]) => {
if(typeof value === "string") {
public chord = (value: number | string, ...kwargs: number[]) => {
if (typeof value === "string") {
const chord = parseChord(value);
return this.updateValue("note", chord);
} else {

View File

@ -5,10 +5,7 @@ import {
arrayOfObjectsToObjectWithArrays,
objectWithArraysToArrayOfObjects,
} from "../Utils/Generic";
import {
midiToFreq,
noteFromPc,
} from "zifferjs";
import { midiToFreq, noteFromPc } from "zifferjs";
import {
superdough,
@ -66,7 +63,7 @@ export class SoundEvent extends AudibleEvent {
phaserDepth: ["phaserDepth", "phasdepth"],
phaserSweep: ["phaserSweep", "phassweep"],
phaserCenter: ["phaserCenter", "phascenter"],
fmadsr: function(
fmadsr: function (
self: SoundEvent,
a: number,
d: number,
@ -79,7 +76,7 @@ export class SoundEvent extends AudibleEvent {
self.updateValue("fmrelease", r);
return self;
},
fmad: function(self: SoundEvent, a: number, d: number) {
fmad: function (self: SoundEvent, a: number, d: number) {
self.updateValue("fmattack", a);
self.updateValue("fmdecay", d);
return self;
@ -90,7 +87,7 @@ export class SoundEvent extends AudibleEvent {
decay: ["decay", "dec"],
sustain: ["sustain", "sus"],
release: ["release", "rel"],
adsr: function(
adsr: function (
self: SoundEvent,
a: number,
d: number,
@ -103,21 +100,21 @@ export class SoundEvent extends AudibleEvent {
self.updateValue("release", r);
return self;
},
ad: function(self: SoundEvent, a: number, d: number) {
ad: function (self: SoundEvent, a: number, d: number) {
self.updateValue("attack", a);
self.updateValue("decay", d);
self.updateValue("sustain", 0.0);
self.updateValue("release", 0.0);
return self;
},
scope: function(self: SoundEvent) {
self.updateValue("analyze", true)
return self
scope: function (self: SoundEvent) {
self.updateValue("analyze", true);
return self;
},
debug: function(self: SoundEvent, callback?: Function) {
self.updateValue("debug", true)
debug: function (self: SoundEvent, callback?: Function) {
self.updateValue("debug", true);
if (callback) {
self.updateValue("debugFunction", callback)
self.updateValue("debugFunction", callback);
}
return self;
},
@ -126,27 +123,27 @@ export class SoundEvent extends AudibleEvent {
lpdecay: ["lpdecay", "lpd"],
lpsustain: ["lpsustain", "lps"],
lprelease: ["lprelease", "lpr"],
cutoff: function(self: SoundEvent, value: number, resonance?: number) {
cutoff: function (self: SoundEvent, value: number, resonance?: number) {
self.updateValue("cutoff", value);
if (resonance) {
self.updateValue("resonance", resonance);
}
return self;
},
lpf: function(self: SoundEvent, value: number, resonance?: number) {
lpf: function (self: SoundEvent, value: number, resonance?: number) {
self.updateValue("cutoff", value);
if (resonance) {
self.updateValue("resonance", resonance);
}
return self;
},
resonance: function(self: SoundEvent, value: number) {
resonance: function (self: SoundEvent, value: number) {
if (value >= 0 && value <= 1) {
self.updateValue("resonance", 50 * value);
}
return self;
},
lpadsr: function(
lpadsr: function (
self: SoundEvent,
depth: number,
a: number,
@ -161,7 +158,7 @@ export class SoundEvent extends AudibleEvent {
self.updateValue("lprelease", r);
return self;
},
lpad: function(self: SoundEvent, depth: number, a: number, d: number) {
lpad: function (self: SoundEvent, depth: number, a: number, d: number) {
self.updateValue("lpenv", depth);
self.updateValue("lpattack", a);
self.updateValue("lpdecay", d);
@ -174,25 +171,25 @@ export class SoundEvent extends AudibleEvent {
hpdecay: ["hpdecay", "hpd"],
hpsustain: ["hpsustain", "hpsus"],
hprelease: ["hprelease", "hpr"],
hcutoff: function(self: SoundEvent, value: number, resonance?: number) {
hcutoff: function (self: SoundEvent, value: number, resonance?: number) {
self.updateValue("hcutoff", value);
if (resonance) {
self.updateValue("hresonance", resonance);
}
return self;
},
hpf: function(self: SoundEvent, value: number, resonance?: number) {
hpf: function (self: SoundEvent, value: number, resonance?: number) {
self.updateValue("hcutoff", value);
if (resonance) {
self.updateValue("hresonance", resonance);
}
return self;
},
hpq: function(self: SoundEvent, value: number) {
hpq: function (self: SoundEvent, value: number) {
self.updateValue("hresonance", value);
return self;
},
hpadsr: function(
hpadsr: function (
self: SoundEvent,
depth: number,
a: number,
@ -207,7 +204,7 @@ export class SoundEvent extends AudibleEvent {
self.updateValue("hprelease", r);
return self;
},
hpad: function(self: SoundEvent, depth: number, a: number, d: number) {
hpad: function (self: SoundEvent, depth: number, a: number, d: number) {
self.updateValue("hpenv", depth);
self.updateValue("hpattack", a);
self.updateValue("hpdecay", d);
@ -220,14 +217,14 @@ export class SoundEvent extends AudibleEvent {
bpdecay: ["bpdecay", "bpd"],
bpsustain: ["bpsustain", "bps"],
bprelease: ["bprelease", "bpr"],
bandf: function(self: SoundEvent, value: number, resonance?: number) {
bandf: function (self: SoundEvent, value: number, resonance?: number) {
self.updateValue("bandf", value);
if (resonance) {
self.updateValue("bandq", resonance);
}
return self;
},
bpf: function(self: SoundEvent, value: number, resonance?: number) {
bpf: function (self: SoundEvent, value: number, resonance?: number) {
self.updateValue("bandf", value);
if (resonance) {
self.updateValue("bandq", resonance);
@ -235,7 +232,7 @@ export class SoundEvent extends AudibleEvent {
return self;
},
bandq: ["bandq", "bpq"],
bpadsr: function(
bpadsr: function (
self: SoundEvent,
depth: number,
a: number,
@ -250,7 +247,7 @@ export class SoundEvent extends AudibleEvent {
self.updateValue("bprelease", r);
return self;
},
bpad: function(self: SoundEvent, depth: number, a: number, d: number) {
bpad: function (self: SoundEvent, depth: number, a: number, d: number) {
self.updateValue("bpenv", depth);
self.updateValue("bpattack", a);
self.updateValue("bpdecay", d);
@ -260,7 +257,7 @@ export class SoundEvent extends AudibleEvent {
},
vib: ["vib"],
vibmod: ["vibmod"],
fm: function(self: SoundEvent, value: number | string) {
fm: function (self: SoundEvent, value: number | string) {
if (typeof value === "number") {
self.values["fmi"] = value;
} else {
@ -276,11 +273,11 @@ export class SoundEvent extends AudibleEvent {
begin: ["begin"],
end: ["end"],
gain: ["gain"],
dbgain: function(self: SoundEvent, value: number) {
dbgain: function (self: SoundEvent, value: number) {
self.updateValue("gain", Math.min(Math.pow(10, value / 20), 10));
return self;
},
db: function(self: SoundEvent, value: number) {
db: function (self: SoundEvent, value: number) {
self.updateValue("gain", Math.min(Math.pow(10, value / 20), 10));
return self;
},
@ -303,32 +300,32 @@ export class SoundEvent extends AudibleEvent {
roomlp: ["roomlp", "rlp"],
roomdim: ["roomdim", "rdim"],
sound: ["s", "sound"],
size: function(self: SoundEvent, value: number) {
size: function (self: SoundEvent, value: number) {
self.updateValue("roomsize", value);
return self;
},
sz: function(self: SoundEvent, value: number) {
sz: function (self: SoundEvent, value: number) {
self.updateValue("roomsize", value);
return self;
},
comp: ["compressor", "cmp"],
ratio: function(self: SoundEvent, value: number) {
ratio: function (self: SoundEvent, value: number) {
self.updateValue("compressorRatio", value);
return self;
},
knee: function(self: SoundEvent, value: number) {
knee: function (self: SoundEvent, value: number) {
self.updateValue("compressorKnee", value);
return self;
},
compAttack: function(self: SoundEvent, value: number) {
compAttack: function (self: SoundEvent, value: number) {
self.updateValue("compressorAttack", value);
return self;
},
compRelease: function(self: SoundEvent, value: number) {
compRelease: function (self: SoundEvent, value: number) {
self.updateValue("compressorRelease", value);
return self;
},
stretch: function(self: SoundEvent, beat: number) {
stretch: function (self: SoundEvent, beat: number) {
self.updateValue("unit", "c");
self.updateValue("speed", 1 / beat);
self.updateValue("cut", beat);
@ -467,16 +464,12 @@ export class SoundEvent extends AudibleEvent {
}
if (this.values["debug"]) {
if (this.values["debugFunction"]) {
this.values["debugFunction"](filteredEvent)
this.values["debugFunction"](filteredEvent);
} else {
console.log(filteredEvent)
console.log(filteredEvent);
}
}
superdough(
filteredEvent,
this.app.clock.deadline,
filteredEvent.dur,
);
superdough(filteredEvent, this.app.clock.deadline, filteredEvent.dur);
}
};
}

View File

@ -13,8 +13,9 @@ ${makeExample(
"Feeding a sine to the oscilloscope",
`
beat(1)::sound('sine').freq(200).ad(0, .2).scope().out()
`, true
)}
`,
true,
)}
Here is a layout of the scope configuration options:
@ -35,7 +36,7 @@ scope({
})
`,
true,
)}
)}
${makeExample(
"Demo with multiple scope mode",
@ -56,7 +57,7 @@ scope({enabled: true, thickness: 8,
size: 0.5, fftSize: 2048})
`,
true,
)}
)}
Note that these values can be patterned as well! You can transform the oscilloscope into its own light show if you want. The picture is not stable anyway so you won't have much use of it for precision work :)

View File

@ -25,7 +25,7 @@ declare global {
z15(): Player;
z16(): Player;
midi(): MidiEvent;
sound(name: string): SoundEvent|SkipEvent;
sound(name: string): SoundEvent | SkipEvent;
}
}
@ -102,7 +102,7 @@ export const makeNumberExtensions = (api: UserAPI) => {
return api.midi(this.valueOf(), ...kwargs);
};
Number.prototype.sound = function (name: string): SoundEvent|SkipEvent {
Number.prototype.sound = function (name: string): SoundEvent | SkipEvent {
if (Number.isInteger(this.valueOf())) {
return (api.sound(name) as SoundEvent).note(this.valueOf());
} else {