mirror of
https://codeberg.org/uzu/strudel
synced 2026-07-13 06:19:33 -04:00
add getPlayableNoteValue helper
This commit is contained in:
+7
-4
@@ -16,9 +16,10 @@ import {
|
||||
NoiseSynth,
|
||||
PluckSynth,
|
||||
Sampler,
|
||||
getDestination
|
||||
getDestination,
|
||||
} from 'tone';
|
||||
import { Piano } from '@tonejs/piano';
|
||||
import { getPlayableNoteValue } from '../../util.mjs';
|
||||
|
||||
// what about
|
||||
// https://www.charlie-roberts.com/gibberish/playground/
|
||||
@@ -30,13 +31,15 @@ Pattern.prototype.tone = function (instrument) {
|
||||
// instrument.toDestination();
|
||||
return this._withEvent((event) => {
|
||||
const onTrigger = (time, event) => {
|
||||
const note = getPlayableNoteValue(event);
|
||||
// TODO: test if all tonejs instruments can handle freqs
|
||||
if (instrument.constructor.name === 'PluckSynth') {
|
||||
instrument.triggerAttack(event.value, time);
|
||||
instrument.triggerAttack(note, time);
|
||||
} else if (instrument.constructor.name === 'NoiseSynth') {
|
||||
instrument.triggerAttackRelease(event.duration, time); // noise has no value
|
||||
} else if (instrument.constructor.name === 'Piano') {
|
||||
instrument.keyDown({ note: event.value, time, velocity: 0.5 });
|
||||
instrument.keyUp({ note: event.value, time: time + event.duration });
|
||||
instrument.keyDown({ note, time, velocity: 0.5 });
|
||||
instrument.keyUp({ note, time: time + event.duration });
|
||||
} else {
|
||||
instrument.triggerAttackRelease(event.value, event.duration, time);
|
||||
}
|
||||
|
||||
+2
-10
@@ -1,6 +1,5 @@
|
||||
import { useCallback, useState, useMemo } from 'react';
|
||||
import { isNote } from 'tone';
|
||||
import { fromMidi } from '../../util.mjs';
|
||||
import { getPlayableNoteValue } from '../../util.mjs';
|
||||
import { evaluate } from './evaluate';
|
||||
import type { Pattern } from './types';
|
||||
import useCycle from './useCycle';
|
||||
@@ -64,15 +63,8 @@ function useRepl({ tune, defaultSynth, autolink = true, onEvent, onDraw }: any)
|
||||
onEvent?.(event);
|
||||
const { onTrigger } = event.context;
|
||||
if (!onTrigger) {
|
||||
let note = event.value;
|
||||
// if value is number => interpret as midi number...
|
||||
// TODO: what if value is meant as frequency? => check context
|
||||
if (typeof event.value === 'number') {
|
||||
note = fromMidi(event.value);
|
||||
} else if (!isNote(note)) {
|
||||
throw new Error('not a note: ' + note);
|
||||
}
|
||||
if (defaultSynth) {
|
||||
const note = getPlayableNoteValue(event);
|
||||
defaultSynth.triggerAttackRelease(note, event.duration, time);
|
||||
} else {
|
||||
throw new Error('no defaultSynth passed to useRepl.');
|
||||
|
||||
Reference in New Issue
Block a user