add sampler + players + switchify onTrigger

This commit is contained in:
Felix Roos 2022-03-06 10:34:09 +01:00
parent 63eb912434
commit 05804262f0

View file

@ -16,7 +16,8 @@ import {
NoiseSynth, NoiseSynth,
PluckSynth, PluckSynth,
Sampler, Sampler,
getDestination getDestination,
Players,
} from 'tone'; } from 'tone';
import { Piano } from '@tonejs/piano'; import { Piano } from '@tonejs/piano';
@ -30,15 +31,35 @@ Pattern.prototype.tone = function (instrument) {
// instrument.toDestination(); // instrument.toDestination();
return this._withEvent((event) => { return this._withEvent((event) => {
const onTrigger = (time, event) => { const onTrigger = (time, event) => {
if (instrument.constructor.name === 'PluckSynth') { let note = event.value;
instrument.triggerAttack(event.value, time); switch (instrument.constructor.name) {
} else if (instrument.constructor.name === 'NoiseSynth') { case 'PluckSynth':
instrument.triggerAttackRelease(event.duration, time); // noise has no value // note = getPlayableNoteValue(event);
} else if (instrument.constructor.name === 'Piano') { instrument.triggerAttack(note, time);
instrument.keyDown({ note: event.value, time, velocity: 0.5 }); break;
instrument.keyUp({ note: event.value, time: time + event.duration }); case 'NoiseSynth':
} else { instrument.triggerAttackRelease(event.duration, time); // noise has no value
instrument.triggerAttackRelease(event.value, event.duration, time); break;
case 'Piano':
// note = getPlayableNoteValue(event);
instrument.keyDown({ note, time, velocity: 0.5 });
instrument.keyUp({ note, time: time + event.duration });
break;
case 'Sampler':
// note = getPlayableNoteValue(event);
instrument.triggerAttackRelease(note, event.duration, time);
break;
case 'Players':
if (!instrument.has(event.value)) {
throw new Error(`name "${event.value}" not defined for players`);
}
const player = instrument.player(event.value);
player.start(time);
player.stop(time + event.duration);
break;
default:
// note = getPlayableNoteValue(event);
instrument.triggerAttackRelease(note, event.duration, time);
} }
}; };
return event.setContext({ ...event.context, instrument, onTrigger }); return event.setContext({ ...event.context, instrument, onTrigger });
@ -57,7 +78,18 @@ export const monosynth = (options) => new MonoSynth(options);
export const noise = (options) => new NoiseSynth(options); export const noise = (options) => new NoiseSynth(options);
export const pluck = (options) => new PluckSynth(options); export const pluck = (options) => new PluckSynth(options);
export const polysynth = (options) => new PolySynth(options); export const polysynth = (options) => new PolySynth(options);
export const sampler = (options) => new Sampler(options); export const sampler = (options, baseUrl) =>
new Promise((resolve) => {
const s = new Sampler(options, () => resolve(s), baseUrl);
});
export const players = (options, baseUrl = '') => {
options = !baseUrl
? options
: Object.fromEntries(Object.entries(options).map(([key, value]: any) => [key, baseUrl + value]));
return new Promise((resolve) => {
const s = new Players(options, () => resolve(s));
});
};
export const synth = (options) => new Synth(options); export const synth = (options) => new Synth(options);
export const piano = async (options = { velocities: 1 }) => { export const piano = async (options = { velocities: 1 }) => {
const p = new Piano(options); const p = new Piano(options);