refactor onTrigger

This commit is contained in:
Felix Roos 2022-11-12 20:17:57 +01:00
parent 5fc8f10602
commit b668a2c0d2
6 changed files with 122 additions and 141 deletions

View File

@ -32,11 +32,8 @@ function speak(words, lang, voice) {
} }
Pattern.prototype._speak = function (lang, voice) { Pattern.prototype._speak = function (lang, voice) {
return this._withHap((hap) => { return this.onTrigger((_, hap) => {
const onTrigger = (time, hap) => {
speak(hap.value, lang, voice); speak(hap.value, lang, voice);
};
return hap.setContext({ ...hap.context, onTrigger });
}); });
}; };

View File

@ -65,9 +65,7 @@ Pattern.prototype.midi = async function (output, channel = 1) {
}')`, }')`,
); );
} }
return this._withHap((hap) => { return this.onTrigger((time, hap) => {
// const onTrigger = (time: number, hap: any) => {
const onTrigger = (time, hap) => {
let note = getPlayableNoteValue(hap); let note = getPlayableNoteValue(hap);
const velocity = hap.context?.velocity ?? 0.9; const velocity = hap.context?.velocity ?? 0.9;
if (!isNote(note)) { if (!isNote(note)) {
@ -104,7 +102,5 @@ Pattern.prototype.midi = async function (output, channel = 1) {
duration: hap.duration.valueOf() * 1000 - 5, duration: hap.duration.valueOf() * 1000 - 5,
attack: velocity, attack: velocity,
}); });
};
return hap.setContext({ ...hap.context, onTrigger });
}); });
}; };

View File

@ -15,12 +15,13 @@ function connect() {
const osc = new OSC(); const osc = new OSC();
osc.open(); osc.open();
osc.on('open', () => { osc.on('open', () => {
logger('OSC connected!'); const url = osc.options?.plugin?.socket?.url;
logger(`[osc] connected${url ? ` to ${url}` : ''}`);
resolve(osc); resolve(osc);
}); });
osc.on('close', () => { osc.on('close', () => {
connection = undefined; // allows new connection afterwards connection = undefined; // allows new connection afterwards
console.log('osc connection closed'); console.log('[osc] disconnected');
reject('OSC connection closed'); reject('OSC connection closed');
}); });
osc.on('error', (err) => reject(err)); osc.on('error', (err) => reject(err));
@ -45,8 +46,7 @@ let startedAt = -1;
*/ */
Pattern.prototype.osc = async function () { Pattern.prototype.osc = async function () {
const osc = await connect(); const osc = await connect();
return this._withHap((hap) => { return this.onTrigger((time, hap, currentTime, cps = 1) => {
const onTrigger = (time, hap, currentTime, cps = 1) => {
const cycle = hap.wholeOrPart().begin.valueOf(); const cycle = hap.wholeOrPart().begin.valueOf();
const delta = hap.duration.valueOf(); const delta = hap.duration.valueOf();
// time should be audio time of onset // time should be audio time of onset
@ -64,7 +64,5 @@ Pattern.prototype.osc = async function () {
const bundle = new OSC.Bundle([message], ts); const bundle = new OSC.Bundle([message], ts);
bundle.timestamp(ts); // workaround for https://github.com/adzialocha/osc-js/issues/60 bundle.timestamp(ts); // workaround for https://github.com/adzialocha/osc-js/issues/60
osc.send(bundle); osc.send(bundle);
};
return hap.setContext({ ...hap.context, onTrigger });
}); });
}; };

View File

@ -24,11 +24,10 @@ export async function getWriter(br=38400) {
const writableStreamClosed = textEncoder.readable.pipeTo(port.writable); const writableStreamClosed = textEncoder.readable.pipeTo(port.writable);
const writer = textEncoder.writable.getWriter(); const writer = textEncoder.writable.getWriter();
serialWriter = function (message) { serialWriter = function (message) {
writer.write(message) writer.write(message);
} };
} } else {
else { throw 'Webserial is not available in this browser.';
throw('Webserial is not available in this browser.')
} }
} }
@ -40,7 +39,7 @@ Pattern.prototype.serial = function (...args) {
getWriter(...args); getWriter(...args);
} }
const onTrigger = (time, hap, currentTime) => { const onTrigger = (time, hap, currentTime) => {
var message = ""; var message = '';
if (typeof hap.value === 'object') { if (typeof hap.value === 'object') {
if ('action' in hap.value) { if ('action' in hap.value) {
message += hap.value['action'] + '('; message += hap.value['action'] + '(';
@ -51,26 +50,23 @@ Pattern.prototype.serial = function (...args) {
} }
if (first) { if (first) {
first = false; first = false;
} } else {
else {
message += ','; message += ',';
} }
message += `${key}:${val}` message += `${key}:${val}`;
} }
message += ')'; message += ')';
} } else {
else {
for (const [key, val] of Object.entries(hap.value)) { for (const [key, val] of Object.entries(hap.value)) {
message += `${key}:${val};` message += `${key}:${val};`;
} }
} }
} } else {
else {
message = hap.value; message = hap.value;
} }
const offset = (time - currentTime + latency) * 1000; const offset = (time - currentTime + latency) * 1000;
window.setTimeout(serialWriter, offset, message); window.setTimeout(serialWriter, offset, message);
}; };
return hap.setContext({ ...hap.context, onTrigger }); return hap.setContext({ ...hap.context, onTrigger, dominantTrigger: true });
}); });
}; };

View File

@ -50,8 +50,7 @@ export const getDefaultSynth = () => {
// with this function, you can play the pattern with any tone synth // with this function, you can play the pattern with any tone synth
Pattern.prototype.tone = function (instrument) { Pattern.prototype.tone = function (instrument) {
return this._withHap((hap) => { return this.onTrigger((time, hap) => {
const onTrigger = (time, hap) => {
let note; let note;
let velocity = hap.context?.velocity ?? 0.75; let velocity = hap.context?.velocity ?? 0.75;
if (instrument instanceof PluckSynth) { if (instrument instanceof PluckSynth) {
@ -74,8 +73,6 @@ Pattern.prototype.tone = function (instrument) {
note = getPlayableNoteValue(hap); note = getPlayableNoteValue(hap);
instrument.triggerAttackRelease(note, hap.duration.valueOf(), time, velocity); instrument.triggerAttackRelease(note, hap.duration.valueOf(), time, velocity);
} }
};
return hap.setContext({ ...hap.context, instrument, onTrigger });
}); });
}; };

View File

@ -62,8 +62,7 @@ export function loadWebDirt(config) {
*/ */
Pattern.prototype.webdirt = function () { Pattern.prototype.webdirt = function () {
// create a WebDirt object and initialize Web Audio context // create a WebDirt object and initialize Web Audio context
return this._withHap((hap) => { return this.onTrigger(async (time, e, currentTime) => {
const onTrigger = async (time, e, currentTime) => {
if (!webDirt) { if (!webDirt) {
throw new Error('WebDirt not initialized!'); throw new Error('WebDirt not initialized!');
} }
@ -92,7 +91,5 @@ Pattern.prototype.webdirt = function () {
webDirt.playSample(msg, deadline); webDirt.playSample(msg, deadline);
} }
} }
};
return hap.setContext({ ...hap.context, onTrigger });
}); });
}; };