fix pattern checks for minified code

This commit is contained in:
Felix Roos 2022-03-25 22:32:42 +01:00
parent ef75590853
commit 9458faf5b0
7 changed files with 1359 additions and 22 deletions

View File

@ -563,7 +563,7 @@ class Pattern {
// to avoid object checking for every pattern method, we can remove it here...
// in the future, patternified args should be marked as well + some better object handling
args = args.map((arg) =>
arg.constructor?.name === 'Pattern' ? arg.fmap((value) => value.value || value) : arg
isPattern(arg) ? arg.fmap((value) => value.value || value) : arg
);
const pat_arg = sequence(...args)
// arg.locations has to go somewhere..
@ -830,14 +830,18 @@ export const tri = fastcat(isaw, saw)
export const square = signal(t => Math.floor((t*2) % 2))
export const square2 = _toBipolar(square)
export function isPattern(thing) {
// thing?.constructor?.name !== 'Pattern' // <- this will fail when code is mangled
return thing instanceof Pattern;
}
function reify(thing) {
// Tunrs something into a pattern, unless it's already a pattern
if (thing?.constructor?.name == "Pattern") {
return thing
// Turns something into a pattern, unless it's already a pattern
if (isPattern(thing)) {
return thing;
}
return pure(thing)
}
// Basic functions for combining patterns
function stack(...pats) {

View File

@ -49,7 +49,8 @@ export const evaluate = async (code) => {
drawHelpers.cleanup();
uiHelpers.cleanup();
let evaluated = await eval(shapeshifted);
if (evaluated?.constructor?.name !== 'Pattern') {
if (!isPattern(evaluated)) {
console.log('evaluated', evaluated);
const message = `got "${typeof evaluated}" instead of pattern`;
throw new Error(message + (typeof evaluated === 'function' ? ', did you forget to call a function?' : '.'));
}

View File

@ -1,13 +1,11 @@
import { isNote } from 'tone';
import _WebMidi from 'webmidi';
import { Pattern as _Pattern } from '@strudel/core/strudel.mjs';
import { Pattern, isPattern } from '@strudel/core/strudel.mjs';
import { Tone } from '@strudel/tone';
// if you use WebMidi from outside of this package, make sure to import that instance:
export const WebMidi = _WebMidi;
const Pattern = _Pattern;
export function enableWebMidi() {
return new Promise((resolve, reject) => {
if (WebMidi.enabled) {
@ -28,7 +26,7 @@ const outputByName = (name) => WebMidi.getOutputByName(name);
// Pattern.prototype.midi = function (output: string | number, channel = 1) {
Pattern.prototype.midi = function (output, channel = 1) {
if (output?.constructor?.name === 'Pattern') {
if (isPattern(output?.constructor?.name)) {
throw new Error(
`.midi does not accept Pattern input. Make sure to pass device name with single quotes. Example: .midi('${
WebMidi.outputs?.[0]?.name || 'IAC Driver Bus 1'

View File

@ -2,7 +2,7 @@ import * as krill from './krill-parser.js';
import * as strudel from '@strudel/core/strudel.mjs';
import { addMiniLocations } from '../eval/shapeshifter.mjs';
const { pure, Pattern, Fraction, stack, slowcat, sequence, timeCat, silence } = strudel;
const { pure, Pattern, Fraction, stack, slowcat, sequence, timeCat, silence, reify } = strudel;
const applyOptions = (parent) => (pat, i) => {
const ast = parent.source_[i];
@ -159,14 +159,6 @@ Pattern.prototype.define('mini', mini, { composable: true });
Pattern.prototype.define('m', mini, { composable: true });
Pattern.prototype.define('h', h, { composable: true });
// TODO: move this to strudel?
export function reify(thing) {
if (thing?.constructor?.name === 'Pattern') {
return thing;
}
return pure(thing);
}
export function minify(thing) {
if (typeof thing === 'string') {
return mini(thing);

View File

@ -50,7 +50,31 @@ Pattern.prototype.tone = function (instrument) {
const onTrigger = (time, event) => {
let note;
let velocity = event.context?.velocity ?? 0.75;
switch (instrument.constructor.name) {
if (instrument instanceof PluckSynth) {
note = getPlayableNoteValue(event);
instrument.triggerAttack(note, time);
} else if (instrument instanceof NoiseSynth) {
instrument.triggerAttackRelease(event.duration, time); // noise has no value
} else if (instrument instanceof Piano) {
note = getPlayableNoteValue(event);
instrument.keyDown({ note, time, velocity });
instrument.keyUp({ note, time: time + event.duration, velocity });
} else if (instrument instanceof Sampler) {
note = getPlayableNoteValue(event);
instrument.triggerAttackRelease(note, event.duration, time, velocity);
} else if (instrument instanceof Players) {
if (!instrument.has(event.value)) {
throw new Error(`name "${event.value}" not defined for players`);
}
const player = instrument.player(event.value);
// velocity ?
player.start(time);
player.stop(time + event.duration);
} else {
note = getPlayableNoteValue(event);
instrument.triggerAttackRelease(note, event.duration, time, velocity);
}
/* switch (instrument.constructor.name) {
case 'PluckSynth':
note = getPlayableNoteValue(event);
instrument.triggerAttack(note, time);
@ -79,7 +103,7 @@ Pattern.prototype.tone = function (instrument) {
default:
note = getPlayableNoteValue(event);
instrument.triggerAttackRelease(note, event.duration, time, velocity);
}
} */
};
return event.setContext({ ...event.context, instrument, onTrigger });
});

1313
repl/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,6 +9,7 @@
"@testing-library/user-event": "^13.5.0",
"codemirror": "^5.65.2",
"events": "^3.3.0",
"gh-pages": "^3.2.3",
"react": "^17.0.2",
"react-codemirror2": "^7.2.1",
"react-dom": "^17.0.2",
@ -22,7 +23,10 @@
"test": "react-scripts test",
"eject": "react-scripts eject",
"tutorial": "parcel src/tutorial/index.html --no-cache",
"build-tutorial": "parcel build src/tutorial/index.html --dist-dir ../docs/tutorial --public-url /tutorial --no-optimize --no-scope-hoist --no-cache"
"build-tutorial": "parcel build src/tutorial/index.html --dist-dir ../docs/tutorial --public-url /tutorial --no-optimize --no-scope-hoist --no-cache",
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"static": "npx serve ./build"
},
"eslintConfig": {
"extends": [
@ -48,6 +52,7 @@
"autoprefixer": "^10.4.4",
"parcel": "^2.3.1",
"postcss": "^8.4.12",
"serve": "^13.0.2",
"tailwindcss": "^3.0.23"
}
}