Compare commits

..

4 Commits

Author SHA1 Message Date
Felix Roos 14184993d0 Merge remote-tracking branch 'origin/main' into fix-scale-offset 2023-02-09 19:06:42 +01:00
Felix Roos b92d1d09a2 tonleiter experiment 2023-02-03 19:43:10 +01:00
Felix Roos c79b2653e7 revert change for now
(because tonal degree does not support floats)
2023-02-03 19:42:35 +01:00
Felix Roos 554b72c916 fix scale degrees
fixes https://github.com/tidalcycles/strudel/issues/378
2023-02-02 22:11:56 +01:00
250 changed files with 10924 additions and 25595 deletions
-2
View File
@@ -16,5 +16,3 @@ vite.config.js
**/*.tsx **/*.tsx
**/*.ts **/*.ts
**/*.json **/*.json
**/dev-dist
**/dist
-62
View File
@@ -1,62 +0,0 @@
name: Tauri Builder
on:
push:
tags:
- "v*"
workflow_dispatch:
jobs:
release:
strategy:
fail-fast: false
matrix:
platform: [macos-latest, ubuntu-latest, windows-latest]
include:
- os: ubuntu-latest
rust_target: x86_64-unknown-linux-gnu
- os: macos-latest
rust_target: x86_64-apple-darwin
- os: macos-latest
rust_target: aarch64-apple-darwin
- os: windows-latest
rust_target: x86_64-pc-windows-msvc
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 8.6.2
- name: Node.js setup
uses: actions/setup-node@v3
with:
node-version: latest
# node-version-file: '.nvmrc'
- name: Install Rust (Stable)
run:
curl https://sh.rustup.rs -sSf | sh -s -- -y
- name: Install dependencies (ubuntu only)
if: matrix.platform == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -y libgtk-3-dev webkit2gtk-4.0 libappindicator3-dev librsvg2-dev patchelf
- name: Install app dependencies from lockfile and build web
run: pnpm install
- name: Build the app
uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# tauri-action replaces \_\_VERSION\_\_ with the app version
tagName: ${{ github.ref_name }}
releaseName: "Strudel v__VERSION__"
releaseBody: |
See the assets to download this version and install.
releaseDraft: true
prerelease: false
-4
View File
@@ -40,7 +40,3 @@ repl/stats.html
coverage coverage
public/icons/apple-splash-* public/icons/apple-splash-*
dev-dist dev-dist
Dirt-Samples
tidal-drum-machines
webaudiofontdata
src-tauri/target
-1
View File
@@ -9,4 +9,3 @@ packages/xen/tunejs.js
paper paper
pnpm-lock.yaml pnpm-lock.yaml
pnpm-workspace.yaml pnpm-workspace.yaml
**/dev-dist
+1 -1
View File
@@ -7,7 +7,7 @@
"jsxSingleQuote": false, "jsxSingleQuote": false,
"trailingComma": "all", "trailingComma": "all",
"bracketSpacing": true, "bracketSpacing": true,
"bracketSameLine": false, "jsxBracketSameLine": false,
"arrowParens": "always", "arrowParens": "always",
"proseWrap": "preserve", "proseWrap": "preserve",
"htmlWhitespaceSensitivity": "css", "htmlWhitespaceSensitivity": "css",
+2 -31
View File
@@ -31,7 +31,7 @@ Use one of the Communication Channels listed above.
## Improve the Docs ## Improve the Docs
If you find some weak spots in the [docs](https://strudel.tidalcycles.org/workshop/getting-started/), If you find some weak spots in the [docs](https://strudel.tidalcycles.org/learn/getting-started),
you can edit each file directly on github via the "Edit this page" link located in the right sidebar. you can edit each file directly on github via the "Edit this page" link located in the right sidebar.
## Propose a Feature ## Propose a Feature
@@ -82,10 +82,6 @@ Please report any problems you've had with the setup instructions!
## Code Style ## Code Style
To make sure the code changes only where it should, we are using prettier to unify the code style. To make sure the code changes only where it should, we are using prettier to unify the code style.
- You can format all files at once by running `pnpm prettier` from the project root
- Run `pnpm format-check` from the project root to check if all files are well formatted
If you use VSCode, you can If you use VSCode, you can
1. install [the prettier extension](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) 1. install [the prettier extension](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
@@ -93,24 +89,6 @@ If you use VSCode, you can
3. Choose "Configure Default Formatter..." 3. Choose "Configure Default Formatter..."
4. Select prettier 4. Select prettier
## ESLint
To prevent unwanted runtime errors, this project uses [eslint](https://eslint.org/).
- You can check for lint errors by running `pnpm lint`
There are also eslint extensions / plugins for most editors.
## Running Tests
- Run all tests with `pnpm test`
- Run all tests with UI using `pnpm test-ui`
## Running all CI Checks
When opening a PR, the CI runner will automatically check the code style and eslint, as well as run all tests.
You can run the same check with `pnpm check`
## Package Workflow ## Package Workflow
The project is split into multiple [packages](https://github.com/tidalcycles/strudel/tree/main/packages) with independent versioning. The project is split into multiple [packages](https://github.com/tidalcycles/strudel/tree/main/packages) with independent versioning.
@@ -123,14 +101,7 @@ To publish all packages that have been changed since the last release, run:
```sh ```sh
npm login npm login
npx lerna publish
# this will increment all the versions in package.json files of non private packages to selected versions
npx lerna version --no-private
# publish all packages inside /packages using pnpm! don't use lerna to publish!!
pnpm --filter "./packages/**" publish --dry-run
# the last command was only a dry-run, make sure everything looks ok, if yes, run the same command without flag
``` ```
To manually publish a single package, increase the version in the `package.json`, then run `pnpm publish`. To manually publish a single package, increase the version in the `package.json`, then run `pnpm publish`.
+2 -2
View File
@@ -14,8 +14,8 @@ An experiment in making a [Tidal](https://github.com/tidalcycles/tidal/) using w
After cloning the project, you can run the REPL locally: After cloning the project, you can run the REPL locally:
```bash ```bash
pnpm run setup npm run setup
pnpm run repl npm run repl
``` ```
## Using Strudel In Your Project ## Using Strudel In Your Project
+5 -2
View File
@@ -20,8 +20,11 @@ Example: <https://felixroos.github.io/strudel/swatch/>
### 6. edit `website/astro.config.mjs` to use site: `https://<your-username>.github.io` and base `/strudel`, like this ### 6. edit `website/astro.config.mjs` to use site: `https://<your-username>.github.io` and base `/strudel`, like this
```js ```js
const site = 'https://<your-username>.github.io'; export default defineConfig({
const base = '/strudel'; /* ... rest of config ... */
site: 'https://<your-username>.github.io',
base: '/strudel',
});
``` ```
### 7. commit & push the changes ### 7. commit & push the changes
+14 -13
View File
@@ -18,6 +18,7 @@
"build": "npm run prebuild && cd website && npm run build", "build": "npm run prebuild && cd website && npm run build",
"preview": "cd website && npm run preview", "preview": "cd website && npm run preview",
"osc": "cd packages/osc && npm run server", "osc": "cd packages/osc && npm run server",
"deploy": "NODE_DEBUG=gh-pages gh-pages -d out",
"jsdoc": "jsdoc packages/ -c jsdoc.config.json", "jsdoc": "jsdoc packages/ -c jsdoc.config.json",
"jsdoc-json": "jsdoc packages/ --template ./node_modules/jsdoc-json --destination doc.json -c jsdoc.config.json", "jsdoc-json": "jsdoc packages/ --template ./node_modules/jsdoc-json --destination doc.json -c jsdoc.config.json",
"lint": "eslint . --ext mjs,js --quiet", "lint": "eslint . --ext mjs,js --quiet",
@@ -45,28 +46,28 @@
}, },
"homepage": "https://strudel.tidalcycles.org", "homepage": "https://strudel.tidalcycles.org",
"dependencies": { "dependencies": {
"dependency-tree": "^9.0.0",
"vitest": "^0.25.7",
"@strudel.cycles/core": "workspace:*", "@strudel.cycles/core": "workspace:*",
"@strudel.cycles/mini": "workspace:*", "@strudel.cycles/mini": "workspace:*",
"@strudel.cycles/tonal": "workspace:*", "@strudel.cycles/tonal": "workspace:*",
"@strudel.cycles/transpiler": "workspace:*", "@strudel.cycles/transpiler": "workspace:*",
"@strudel.cycles/webaudio": "workspace:*", "@strudel.cycles/webaudio": "workspace:*",
"@strudel.cycles/xen": "workspace:*", "@strudel.cycles/xen": "workspace:*"
"acorn": "^8.8.1",
"dependency-tree": "^9.0.0"
}, },
"devDependencies": { "devDependencies": {
"@tauri-apps/cli": "^1.4.0", "@vitest/ui": "^0.25.7",
"@vitest/ui": "^0.28.0", "c8": "^7.12.0",
"canvas": "^2.11.2", "canvas": "^2.11.0",
"eslint": "^8.39.0", "eslint": "^8.28.0",
"eslint-plugin-import": "^2.27.5", "eslint-plugin-import": "^2.27.5",
"events": "^3.3.0", "events": "^3.3.0",
"jsdoc": "^4.0.2", "gh-pages": "^4.0.0",
"jsdoc": "^3.6.10",
"jsdoc-json": "^2.0.2", "jsdoc-json": "^2.0.2",
"jsdoc-to-markdown": "^8.0.0", "jsdoc-to-markdown": "^7.1.1",
"lerna": "^6.6.1", "lerna": "^4.0.0",
"prettier": "^2.8.8", "prettier": "^2.8.1",
"rollup-plugin-visualizer": "^5.8.1", "rollup-plugin-visualizer": "^5.8.1"
"vitest": "^0.28.0"
} }
} }
-3
View File
@@ -1,3 +0,0 @@
# @strudel/codemirror
This package contains helpers and extensions to use codemirror6. See [vite-vanilla-repl-cm6](../core/examples/vite-vanilla-repl-cm6/main.js) as an example of using it.
-199
View File
@@ -1,199 +0,0 @@
import { EditorState } from '@codemirror/state';
import { EditorView, keymap, Decoration, lineNumbers, highlightActiveLineGutter } from '@codemirror/view';
import { defaultKeymap } from '@codemirror/commands';
import { syntaxHighlighting, defaultHighlightStyle } from '@codemirror/language';
import { javascript } from '@codemirror/lang-javascript';
import { StateField, StateEffect } from '@codemirror/state';
import { oneDark } from './themes/one-dark';
import { repl, Drawer } from '@strudel.cycles/core';
// https://codemirror.net/docs/guide/
export function initEditor({ initialCode = '', onChange, onEvaluate, onStop, theme = oneDark, root }) {
let state = EditorState.create({
doc: initialCode,
extensions: [
theme,
javascript(),
lineNumbers(),
highlightField,
highlightActiveLineGutter(),
syntaxHighlighting(defaultHighlightStyle),
keymap.of(defaultKeymap),
flashField,
EditorView.updateListener.of((v) => onChange(v)),
keymap.of([
{
key: 'Ctrl-Enter',
run: () => onEvaluate(),
},
{
key: 'Ctrl-.',
run: () => onStop(),
},
]),
],
});
return new EditorView({
state,
parent: root,
});
}
//
// highlighting
//
export const setHighlights = StateEffect.define();
export const highlightField = StateField.define({
create() {
return Decoration.none;
},
update(highlights, tr) {
try {
for (let e of tr.effects) {
if (e.is(setHighlights)) {
const { haps } = e.value;
const marks =
haps
.map((hap) =>
(hap.context.locations || []).map(({ start, end }) => {
// const color = hap.context.color || e.value.color || '#FFCA28';
let from = tr.newDoc.line(start.line).from + start.column;
let to = tr.newDoc.line(end.line).from + end.column;
const l = tr.newDoc.length;
if (from > l || to > l) {
return; // dont mark outside of range, as it will throw an error
}
const mark = Decoration.mark({
attributes: { style: `outline: 2px solid #FFCA28;` },
});
return mark.range(from, to);
}),
)
.flat()
.filter(Boolean) || [];
highlights = Decoration.set(marks, true);
}
}
return highlights;
} catch (err) {
// console.warn('highlighting error', err);
return Decoration.set([]);
}
},
provide: (f) => EditorView.decorations.from(f),
});
// helper to simply trigger highlighting for given haps
export function highlightHaps(view, haps) {
view.dispatch({ effects: setHighlights.of({ haps }) });
}
//
// flash
//
export const setFlash = StateEffect.define();
const flashField = StateField.define({
create() {
return Decoration.none;
},
update(flash, tr) {
try {
for (let e of tr.effects) {
if (e.is(setFlash)) {
if (e.value) {
const mark = Decoration.mark({ attributes: { style: `background-color: #FFCA2880` } });
flash = Decoration.set([mark.range(0, tr.newDoc.length)]);
} else {
flash = Decoration.set([]);
}
}
}
return flash;
} catch (err) {
console.warn('flash error', err);
return flash;
}
},
provide: (f) => EditorView.decorations.from(f),
});
export const flash = (view, ms = 200) => {
view.dispatch({ effects: setFlash.of(true) });
setTimeout(() => {
view.dispatch({ effects: setFlash.of(false) });
}, ms);
};
export class StrudelMirror {
constructor(options) {
const { root, initialCode = '', onDraw, drawTime = [-2, 2], prebake, ...replOptions } = options;
this.code = initialCode;
this.drawer = new Drawer((haps, time) => {
const currentFrame = haps.filter((hap) => time >= hap.whole.begin && time <= hap.endClipped);
this.highlight(currentFrame);
onDraw?.(haps, time, currentFrame);
}, drawTime);
const prebaked = prebake();
prebaked.then(async () => {
if (!onDraw) {
return;
}
const { scheduler, evaluate } = await this.repl;
// draw first frame instantly
prebaked.then(async () => {
await evaluate(this.code, false);
this.drawer.invalidate(scheduler);
onDraw?.(this.drawer.visibleHaps, 0, []);
});
});
this.repl = repl({
...replOptions,
onToggle: async (started) => {
replOptions?.onToggle?.(started);
const { scheduler } = await this.repl;
if (started) {
this.drawer.start(scheduler);
} else {
this.drawer.stop();
}
},
beforeEval: async () => {
await prebaked;
},
afterEval: (options) => {
replOptions?.afterEval?.(options);
this.drawer.invalidate();
},
});
this.editor = initEditor({
root,
initialCode,
onChange: (v) => {
this.code = v.state.doc.toString();
},
onEvaluate: () => this.evaluate(),
onStop: () => this.stop(),
});
}
async evaluate() {
const { evaluate } = await this.repl;
this.flash();
await evaluate(this.code);
}
async stop() {
const { scheduler } = await this.repl;
scheduler.stop();
}
flash(ms) {
flash(this.editor, ms);
}
highlight(haps) {
highlightHaps(this.editor, haps);
}
}
-47
View File
@@ -1,47 +0,0 @@
{
"name": "@strudel/codemirror",
"version": "0.8.4",
"description": "Codemirror Extensions for Strudel",
"main": "codemirror.mjs",
"publishConfig": {
"main": "dist/index.js",
"module": "dist/index.mjs"
},
"scripts": {
"build": "vite build",
"prepublishOnly": "npm run build"
},
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/tidalcycles/strudel.git"
},
"keywords": [
"tidalcycles",
"strudel",
"pattern",
"livecoding",
"algorave"
],
"author": "Felix Roos <flix91@gmail.com>",
"contributors": [
"Alex McLean <alex@slab.org>"
],
"license": "AGPL-3.0-or-later",
"bugs": {
"url": "https://github.com/tidalcycles/strudel/issues"
},
"homepage": "https://github.com/tidalcycles/strudel#readme",
"dependencies": {
"@codemirror/commands": "^6.2.4",
"@codemirror/lang-javascript": "^6.1.7",
"@codemirror/language": "^6.6.0",
"@codemirror/state": "^6.2.0",
"@codemirror/view": "^6.10.0",
"@lezer/highlight": "^1.1.4",
"@strudel.cycles/core": "workspace:*"
},
"devDependencies": {
"vite": "^4.3.3"
}
}
-139
View File
@@ -1,139 +0,0 @@
import { EditorView } from '@codemirror/view';
import { HighlightStyle, syntaxHighlighting } from '@codemirror/language';
import { tags as t } from '@lezer/highlight';
// Using https://github.com/one-dark/vscode-one-dark-theme/ as reference for the colors
const chalky = '#e5c07b',
coral = '#e06c75',
cyan = '#56b6c2',
invalid = '#ffffff',
ivory = '#abb2bf',
stone = '#7d8799', // Brightened compared to original to increase contrast
malibu = '#61afef',
sage = '#98c379',
whiskey = '#d19a66',
violet = '#c678dd',
darkBackground = '#21252b',
highlightBackground = '#2c313a',
background = '#282c34',
tooltipBackground = '#353a42',
selection = '#3E4451',
cursor = '#528bff';
/// The colors used in the theme, as CSS color strings.
export const color = {
chalky,
coral,
cyan,
invalid,
ivory,
stone,
malibu,
sage,
whiskey,
violet,
darkBackground,
highlightBackground,
background,
tooltipBackground,
selection,
cursor,
};
/// The editor theme styles for One Dark.
export const oneDarkTheme = EditorView.theme(
{
'&': {
color: ivory,
backgroundColor: background,
},
'.cm-content': {
caretColor: cursor,
},
'.cm-cursor, .cm-dropCursor': { borderLeftColor: cursor },
'&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection':
{ backgroundColor: selection },
'.cm-panels': { backgroundColor: darkBackground, color: ivory },
'.cm-panels.cm-panels-top': { borderBottom: '2px solid black' },
'.cm-panels.cm-panels-bottom': { borderTop: '2px solid black' },
'.cm-searchMatch': {
backgroundColor: '#72a1ff59',
outline: '1px solid #457dff',
},
'.cm-searchMatch.cm-searchMatch-selected': {
backgroundColor: '#6199ff2f',
},
'.cm-activeLine': { backgroundColor: '#6699ff0b' },
'.cm-selectionMatch': { backgroundColor: '#aafe661a' },
'&.cm-focused .cm-matchingBracket, &.cm-focused .cm-nonmatchingBracket': {
backgroundColor: '#bad0f847',
},
'.cm-gutters': {
backgroundColor: background,
color: stone,
border: 'none',
},
'.cm-activeLineGutter': {
backgroundColor: highlightBackground,
},
'.cm-foldPlaceholder': {
backgroundColor: 'transparent',
border: 'none',
color: '#ddd',
},
'.cm-tooltip': {
border: 'none',
backgroundColor: tooltipBackground,
},
'.cm-tooltip .cm-tooltip-arrow:before': {
borderTopColor: 'transparent',
borderBottomColor: 'transparent',
},
'.cm-tooltip .cm-tooltip-arrow:after': {
borderTopColor: tooltipBackground,
borderBottomColor: tooltipBackground,
},
'.cm-tooltip-autocomplete': {
'& > ul > li[aria-selected]': {
backgroundColor: highlightBackground,
color: ivory,
},
},
},
{ dark: true },
);
/// The highlighting style for code in the One Dark theme.
export const oneDarkHighlightStyle = HighlightStyle.define([
{ tag: t.keyword, color: violet },
{ tag: [t.name, t.deleted, t.character, t.propertyName, t.macroName], color: coral },
{ tag: [t.function(t.variableName), t.labelName], color: malibu },
{ tag: [t.color, t.constant(t.name), t.standard(t.name)], color: whiskey },
{ tag: [t.definition(t.name), t.separator], color: ivory },
{ tag: [t.typeName, t.className, t.number, t.changed, t.annotation, t.modifier, t.self, t.namespace], color: chalky },
{ tag: [t.operator, t.operatorKeyword, t.url, t.escape, t.regexp, t.link, t.special(t.string)], color: cyan },
{ tag: [t.meta, t.comment], color: stone },
{ tag: t.strong, fontWeight: 'bold' },
{ tag: t.emphasis, fontStyle: 'italic' },
{ tag: t.strikethrough, textDecoration: 'line-through' },
{ tag: t.link, color: stone, textDecoration: 'underline' },
{ tag: t.heading, fontWeight: 'bold', color: coral },
{ tag: [t.atom, t.bool, t.special(t.variableName)], color: whiskey },
{ tag: [t.processingInstruction, t.string, t.inserted], color: sage },
{ tag: t.invalid, color: invalid },
]);
/// Extension to enable the One Dark theme (both the editor theme and
/// the highlight style).
export const oneDark = [oneDarkTheme, syntaxHighlighting(oneDarkHighlightStyle)];
-19
View File
@@ -1,19 +0,0 @@
import { defineConfig } from 'vite';
import { dependencies } from './package.json';
import { resolve } from 'path';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [],
build: {
lib: {
entry: resolve(__dirname, 'codemirror.mjs'),
formats: ['es', 'cjs'],
fileName: (ext) => ({ es: 'index.mjs', cjs: 'index.js' }[ext]),
},
rollupOptions: {
external: [...Object.keys(dependencies)],
},
target: 'esnext',
},
});
+1 -2
View File
@@ -1,5 +1,4 @@
import { Pattern, getDrawContext, silence, register, pure } from './index.mjs'; import { controls, Pattern, getDrawContext, silence, register, pure } from './index.mjs';
import controls from './controls.mjs'; // do not import from index.mjs as it breaks for some reason..
const { createParams } = controls; const { createParams } = controls;
let clearColor = '#22222210'; let clearColor = '#22222210';
+315 -300
View File
File diff suppressed because it is too large Load Diff
+17 -23
View File
@@ -10,49 +10,44 @@ import { logger } from './logger.mjs';
export class Cyclist { export class Cyclist {
constructor({ interval, onTrigger, onToggle, onError, getTime, latency = 0.1 }) { constructor({ interval, onTrigger, onToggle, onError, getTime, latency = 0.1 }) {
this.started = false; this.started = false;
this.cps = 1; this.cps = 1; // TODO
this.lastTick = 0; // absolute time when last tick (clock callback) happened this.phase = 0;
this.lastBegin = 0; // query begin of last tick this.getTime = getTime;
this.lastEnd = 0; // query end of last tick
this.getTime = getTime; // get absolute time
this.onToggle = onToggle; this.onToggle = onToggle;
this.latency = latency; // fixed trigger time offset this.latency = latency;
const round = (x) => Math.round(x * 1000) / 1000; const round = (x) => Math.round(x * 1000) / 1000;
this.clock = createClock( this.clock = createClock(
getTime, getTime,
// called slightly before each cycle
(phase, duration, tick) => { (phase, duration, tick) => {
if (tick === 0) { if (tick === 0) {
this.origin = phase; this.origin = phase;
} }
const begin = round(phase - this.origin);
this.phase = begin - latency;
const end = round(begin + duration);
const time = getTime();
try { try {
const time = getTime(); const haps = this.pattern.queryArc(begin, end); // get Haps
const begin = this.lastEnd;
this.lastBegin = begin;
const end = round(begin + duration * this.cps);
this.lastEnd = end;
const haps = this.pattern.queryArc(begin, end);
const tickdeadline = phase - time; // time left till phase begins
this.lastTick = time + tickdeadline;
haps.forEach((hap) => { haps.forEach((hap) => {
if (hap.part.begin.equals(hap.whole.begin)) { if (hap.part.begin.equals(hap.whole.begin)) {
const deadline = (hap.whole.begin - begin) / this.cps + tickdeadline + latency; const deadline = hap.whole.begin + this.origin - time + latency;
const duration = hap.duration / this.cps; const duration = hap.duration * 1;
onTrigger?.(hap, deadline, duration, this.cps); onTrigger?.(hap, deadline, duration);
} }
}); });
} catch (e) { } catch (e) {
logger(`[cyclist] error: ${e.message}`); logger(`[cyclist] error: ${e.message}`);
onError?.(e); onError?.(e);
} }
}, }, // called slightly before each cycle
interval, // duration of each cycle interval, // duration of each cycle
); );
} }
getPhase() {
return this.getTime() - this.origin - this.latency;
}
now() { now() {
const secondsSinceLastTick = this.getTime() - this.lastTick - this.clock.duration; return this.getTime() - this.origin + this.clock.minLatency;
return this.lastBegin + secondsSinceLastTick * this.cps; // + this.clock.minLatency;
} }
setStarted(v) { setStarted(v) {
this.started = v; this.started = v;
@@ -74,7 +69,6 @@ export class Cyclist {
stop() { stop() {
logger('[cyclist] stop'); logger('[cyclist] stop');
this.clock.stop(); this.clock.stop();
this.lastEnd = 0;
this.setStarted(false); this.setStarted(false);
} }
setPattern(pat, autostart = false) { setPattern(pat, autostart = false) {
+2 -96
View File
@@ -1,6 +1,6 @@
/* /*
draw.mjs - <short description TODO> draw.mjs - <short description TODO>
Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/strudel/blob/main/packages/core/draw.mjs> Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/strudel/blob/main/packages/tone/draw.mjs>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
@@ -13,7 +13,7 @@ export const getDrawContext = (id = 'test-canvas') => {
canvas.id = id; canvas.id = id;
canvas.width = window.innerWidth; canvas.width = window.innerWidth;
canvas.height = window.innerHeight; canvas.height = window.innerHeight;
canvas.style = 'pointer-events:none;width:100%;height:100%;position:fixed;top:0;left:0'; canvas.style = 'pointer-events:none;width:100%;height:100%;position:fixed;top:0;left:0;z-index:5';
document.body.prepend(canvas); document.body.prepend(canvas);
} }
return canvas.getContext('2d'); return canvas.getContext('2d');
@@ -65,97 +65,3 @@ Pattern.prototype.onPaint = function (onPaint) {
this.context = { onPaint }; this.context = { onPaint };
return this; return this;
}; };
// const round = (x) => Math.round(x * 1000) / 1000;
// encapsulates starting and stopping animation frames
export class Framer {
constructor(onFrame, onError) {
this.onFrame = onFrame;
this.onError = onError;
}
start() {
const self = this;
let frame = requestAnimationFrame(function updateHighlights(time) {
try {
self.onFrame(time);
} catch (err) {
self.onError(err);
}
frame = requestAnimationFrame(updateHighlights);
});
self.cancel = () => {
cancelAnimationFrame(frame);
};
}
stop() {
if (this.cancel) {
this.cancel();
}
}
}
// syncs animation frames to a cyclist scheduler
// see vite-vanilla-repl-cm6 for an example
export class Drawer {
constructor(onDraw, drawTime) {
let [lookbehind, lookahead] = drawTime; // e.g. [-2, 2]
lookbehind = Math.abs(lookbehind);
this.visibleHaps = [];
this.lastFrame = null;
this.drawTime = drawTime;
this.framer = new Framer(
() => {
if (!this.scheduler) {
console.warn('Drawer: no scheduler');
return;
}
// calculate current frame time (think right side of screen for pianoroll)
const phase = this.scheduler.now() + lookahead;
// first frame just captures the phase
if (this.lastFrame === null) {
this.lastFrame = phase;
return;
}
// query haps from last frame till now. take last 100ms max
const haps = this.scheduler.pattern.queryArc(Math.max(this.lastFrame, phase - 1 / 10), phase);
this.lastFrame = phase;
this.visibleHaps = (this.visibleHaps || [])
// filter out haps that are too far in the past (think left edge of screen for pianoroll)
.filter((h) => h.whole.end >= phase - lookbehind - lookahead)
// add new haps with onset (think right edge bars scrolling in)
.concat(haps.filter((h) => h.hasOnset()));
const time = phase - lookahead;
onDraw(this.visibleHaps, time, this);
},
(err) => {
console.warn('draw error', err);
},
);
}
invalidate(scheduler = this.scheduler) {
if (!scheduler) {
return;
}
this.scheduler = scheduler;
const t = scheduler.now();
let [_, lookahead] = this.drawTime;
const [begin, end] = [Math.max(t, 0), t + lookahead + 0.1];
// remove all future haps
this.visibleHaps = this.visibleHaps.filter((h) => h.whole.begin < t);
// query future haps
const futureHaps = scheduler.pattern.queryArc(begin, end); // +0.1 = workaround for weird holes in query..
// append future haps
this.visibleHaps = this.visibleHaps.concat(futureHaps);
}
start(scheduler) {
this.scheduler = scheduler;
this.invalidate();
this.framer.start();
}
stop() {
if (this.framer) {
this.framer.stop();
}
}
}
+7 -1
View File
@@ -11,9 +11,15 @@ This program is free software: you can redistribute it and/or modify it under th
*/ */
import { Pattern, timeCat, register, silence } from './pattern.mjs'; import { Pattern, timeCat, register, silence } from './pattern.mjs';
import { rotate, flatten, splitAt, zipWith } from './util.mjs'; import { rotate, flatten } from './util.mjs';
import Fraction from './fraction.mjs'; import Fraction from './fraction.mjs';
const splitAt = function (index, value) {
return [value.slice(0, index), value.slice(index)];
};
const zipWith = (f, xs, ys) => xs.map((n, i) => f(n, ys[i]));
const left = function (n, x) { const left = function (n, x) {
const [ons, offs] = n; const [ons, offs] = n;
const [xs, ys] = x; const [xs, ys] = x;
+8
View File
@@ -6,7 +6,12 @@ This program is free software: you can redistribute it and/or modify it under th
import { isPattern } from './index.mjs'; import { isPattern } from './index.mjs';
let scoped = false;
export const evalScope = async (...args) => { export const evalScope = async (...args) => {
if (scoped) {
console.warn('evalScope was called more than once.');
}
scoped = true;
const results = await Promise.allSettled(args); const results = await Promise.allSettled(args);
const modules = results.filter((result) => result.status === 'fulfilled').map((r) => r.value); const modules = results.filter((result) => result.status === 'fulfilled').map((r) => r.value);
results.forEach((result, i) => { results.forEach((result, i) => {
@@ -37,6 +42,9 @@ function safeEval(str, options = {}) {
} }
export const evaluate = async (code, transpiler) => { export const evaluate = async (code, transpiler) => {
if (!scoped) {
await evalScope(); // at least scope Pattern.prototype.boostrap
}
if (transpiler) { if (transpiler) {
code = transpiler(code); // transform syntactically correct js code to semantically usable code code = transpiler(code); // transform syntactically correct js code to semantically usable code
} }
@@ -1,24 +0,0 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
@@ -1,8 +0,0 @@
# vite-vanilla-repl-cm6
This folder demonstrates how to set up a strudel repl using vite and vanilla JS + codemirror. Run it using:
```sh
npm i
npm run dev
```
@@ -1,22 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite Vanilla Strudel REPL</title>
</head>
<body>
<main>
<nav>
<button id="play">eval</button>
<button id="stop">stop</button>
</nav>
<div class="container">
<div id="editor"></div>
<div id="output"></div>
</div>
<canvas id="roll"></canvas>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
@@ -1,39 +0,0 @@
import { StrudelMirror } from '@strudel/codemirror';
import { funk42 } from './tunes';
import { drawPianoroll, evalScope, controls } from '@strudel.cycles/core';
import './style.css';
import { initAudioOnFirstClick } from '@strudel.cycles/webaudio';
import { transpiler } from '@strudel.cycles/transpiler';
import { getAudioContext, webaudioOutput, registerSynthSounds } from '@strudel.cycles/webaudio';
import { registerSoundfonts } from '@strudel.cycles/soundfonts';
// init canvas
const canvas = document.getElementById('roll');
canvas.width = canvas.width * 2;
canvas.height = canvas.height * 2;
const drawContext = canvas.getContext('2d');
const drawTime = [-2, 2]; // time window of drawn haps
const editor = new StrudelMirror({
defaultOutput: webaudioOutput,
getTime: () => getAudioContext().currentTime,
transpiler,
root: document.getElementById('editor'),
initialCode: funk42,
drawTime,
onDraw: (haps, time) => drawPianoroll({ haps, time, ctx: drawContext, drawTime, fold: 0 }),
prebake: async () => {
initAudioOnFirstClick(); // needed to make the browser happy (don't await this here..)
const loadModules = evalScope(
controls,
import('@strudel.cycles/core'),
import('@strudel.cycles/mini'),
import('@strudel.cycles/tonal'),
import('@strudel.cycles/webaudio'),
);
await Promise.all([loadModules, registerSynthSounds(), registerSoundfonts()]);
},
});
document.getElementById('play').addEventListener('click', () => editor.evaluate());
document.getElementById('stop').addEventListener('click', () => editor.stop());
@@ -1,23 +0,0 @@
{
"name": "vite-vanilla-repl-cm6",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"vite": "^4.3.2"
},
"dependencies": {
"@strudel/codemirror": "workspace:*",
"@strudel.cycles/core": "workspace:*",
"@strudel.cycles/mini": "workspace:*",
"@strudel.cycles/soundfonts": "workspace:*",
"@strudel.cycles/tonal": "workspace:*",
"@strudel.cycles/transpiler": "workspace:*",
"@strudel.cycles/webaudio": "workspace:*"
}
}
@@ -1,31 +0,0 @@
body,
html {
margin: 0;
height: 100%;
background: #282c34;
}
main {
height: 100%;
display: flex;
flex-direction: column;
}
.container {
flex-grow: 1;
max-height: 100%;
position: relative;
overflow: auto;
}
#editor {
overflow: auto;
}
.cm-editor {
height: 100%;
}
#roll {
height: 300px;
}
@@ -1,112 +0,0 @@
export const bumpStreet = `// froos - "22 bump street", licensed with CC BY-NC-SA 4.0
await samples('github:felixroos/samples/main')
await samples('https://strudel.tidalcycles.org/tidal-drum-machines.json', 'github:ritchse/tidal-drum-machines/main/machines/')
"<[0,<6 7 9>,13,<17 20 22 26>]!2>/2"
// make it 22 edo
.fmap(v => Math.pow(2,v/22))
// mess with the base frequency
.mul("<300 [300@3 200]>/8").freq()
.layer(
// chords
x=>x.div(freq(2)).s("flute").euclidLegato("<3 2>",8)
.shape(.4).lpf(sine.range(800,4000).slow(8)),
// adlibs
x=>x.arp("{0 3 2 [1 3]}%1.5")
.s('xylo').mul(freq(2))
.delay(.5).delayfeedback(.4).juxBy(.5, rev)
.hpf(sine.range(200,3000).slow(8)),
// bass
x=>x.arp("[0 [2 1?]](5,8)").s('sawtooth').div(freq(4))
.lpf(sine.range(400,2000).slow(8)).lpq(8).shape(.4)
.off(1/8, x=>x.mul(freq(2)).degradeBy(.5)).gain(.3)
).clip(1).release(.2)
.stack(
// drums
s("bd sd:<2 1>, [~ hh]*2, [~ rim]").bank('RolandTR909')
.off(1/8, x=>x.speed(2).gain(.4)).sometimes(ply(2)).gain(.8)
.mask("<0@4 1@12>/4")
.reset("<x@15 [x(3,8) x*[4 8]]>")
// wait for it...
).fast(2/3)
//.crush(6) // remove "//" if you dare`;
export const trafficFlam = `// froos - "traffic flam", licensed with CC BY-NC-SA 4.0
await samples('github:felixroos/samples/main')
await samples('https://strudel.tidalcycles.org/tidal-drum-machines.json', 'github:ritchse/tidal-drum-machines/main/machines/')
addVoicings('hip', {
m11: ['2M 3m 4P 7m'],
'^7#11': ['3M 4A 5P 7M'],
}, ['C4', 'C6'])
stack(
stack(
"<Bbm11 A^7#11>/2".voicings('hip').note()
.s("gm_epiano1:2")
.arp("[<[0 1 2 3] [3 2 1 0]> ~@5]/2")
.release(2).late(.25).lpf(2000),
"<Bb1 A1>/2".note().s('gm_acoustic_bass'),
n("<0 2 3>(3,8)".off(1/8, add(4)))
.scale("<Bb4:minor A4:lydian>/2")
.s('gm_electric_guitar_jazz')
.decay(sine.range(.05, .2).slow(32)).sustain(0)
.delay(.5).lpf(sine.range(100,5000).slow(64))
.gain(.7).room(.5).pan(sine.range(0,1).slow(11))
).add(perlin.range(0,.25).note()),
stack(
s("bd:1(3,8) rim").bank('RolandTR707').slow(2).room("<0 <.1 .6>>")
.when("<0@7 1>",x=>x.echoWith(3, .0625, (x,i) => x.speed(1+i*.24))),
s("rim*4").end(.05).bank('RolandTR808').speed(.8).room(.2)
)
)
.late("[0 .05]*2").late(12)
`;
export const funk42 = `// froos - how to funk in 42 lines of code
// adapted from "how to funk in two minutes" by marc rebillet https://www.youtube.com/watch?v=3vBwRfQbXkg
// thanks to peach for the transcription: https://www.youtube.com/watch?v=8eiPXvIgda4
await samples('github:felixroos/samples/main')
await samples('https://strudel.tidalcycles.org/tidal-drum-machines.json', 'github:ritchse/tidal-drum-machines/main/machines/')
setcps(.5)
let drums = stack(
s("bd*2, ~ sd").bank('RolandTR707').room("0 .1"),
s("hh*4").begin(.2).release(.02).end(.25).release(.02)
.gain(.3).bank('RolandTR707').late(.02).room(.5),
//s("shaker_small").struct("[x x*2]*2").speed(".8,.9").release(.02)
).fast(2)
let wurli = note(\`<
[[a2,g3,[b3 c4],e4] ~ [g3,c4,e4](3,8)@4 ~@2]!3
[[e2,e3,a3,b3,e4]@3 [e2,e3,ab3,b3,e4]@5]>\`)
.s("gm_epiano1:5").decay(.2).sustain("<[1 0@7]!3 1>")
.gain("<[.8@2 .4@14]!3 .7>").room(.3)
let organ = note("<[~@3 [a3,d4,f#4]@2 [[a3,c4,e4]@2 ~] ~@2]!3 ~>".add(12))
.s("gm_percussive_organ:2").gain(.6).lpf(1800).pan(.2).room(.3);
let clav = note(\`<
[~@3 a2 [g3,[b3 c4],e4]@2 ~ a2 [g3,b3,e4] ~@2 [g3,c4,e4] ~@4]!3
[~@3 e3 [[a3 b3],c3,e3]@2 ~ e2 [e3,a3]@3 [b3,e3] ~@2 [b3,e3]@2]>\`)
.s("gm_clavinet:1").decay("<.25!3 [.25 .4]>").sustain(0)
.gain(.7).pan(.8).room(.2);
let bass = note(\`<
[a1 [~ [g2 a2]] [g1 g#1] [a1 [g2 a2]]]
[a1 [~ [g2 a2]] [e3 d3] [c3 [g3 a3]]]
[a1 [~ [g2 a2]] [g1 g#1] [a1 [g2 a2]]]
[e2@6 e1@5 e1 [[d2 e3] g1]@4]
>\`).s("gm_electric_bass_pick:1").release(.1)
stack(
drums
,wurli
,organ
,clav
,bass
)`;
@@ -6,3 +6,5 @@ This folder demonstrates how to set up a strudel repl using vite and vanilla JS.
npm i npm i
npm run dev npm run dev
``` ```
or view it [live on githack](https://rawcdn.githack.com/tidalcycles/strudel/5fb36acb046ead7cd6ad3cd10f532e7f585f536a/packages/core/examples/vite-vanilla-repl/dist/index.html)
@@ -10,7 +10,7 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"devDependencies": { "devDependencies": {
"vite": "^4.3.3" "vite": "^3.2.0"
}, },
"dependencies": { "dependencies": {
"@strudel.cycles/core": "workspace:*", "@strudel.cycles/core": "workspace:*",
+1 -18
View File
@@ -32,11 +32,7 @@ export class Hap {
} }
get duration() { get duration() {
return this.whole.end.sub(this.whole.begin).mul(typeof this.value?.clip === 'number' ? this.value?.clip : 1); return this.whole.end.sub(this.whole.begin);
}
get endClipped() {
return this.whole.begin.add(this.duration);
} }
wholeOrPart() { wholeOrPart() {
@@ -130,19 +126,6 @@ export class Hap {
setContext(context) { setContext(context) {
return new Hap(this.whole, this.part, this.value, context); return new Hap(this.whole, this.part, this.value, context);
} }
ensureObjectValue() {
/* if (isNote(hap.value)) {
// supports primitive hap values that look like notes
hap.value = { note: hap.value };
} */
if (typeof this.value !== 'object') {
throw new Error(
`expected hap.value to be an object, but got "${this.value}". Hint: append .note() or .s() to the end`,
'error',
);
}
}
} }
export default Hap; export default Hap;
-2
View File
@@ -18,13 +18,11 @@ export * from './util.mjs';
export * from './speak.mjs'; export * from './speak.mjs';
export * from './evaluate.mjs'; export * from './evaluate.mjs';
export * from './repl.mjs'; export * from './repl.mjs';
export * from './cyclist.mjs';
export * from './logger.mjs'; export * from './logger.mjs';
export * from './time.mjs'; export * from './time.mjs';
export * from './draw.mjs'; export * from './draw.mjs';
export * from './animate.mjs'; export * from './animate.mjs';
export * from './pianoroll.mjs'; export * from './pianoroll.mjs';
export * from './spiral.mjs';
export * from './ui.mjs'; export * from './ui.mjs';
export { default as drawLine } from './drawLine.mjs'; export { default as drawLine } from './drawLine.mjs';
export { default as gist } from './gist.js'; export { default as gist } from './gist.js';
+3 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/core", "name": "@strudel.cycles/core",
"version": "0.8.2", "version": "0.6.8",
"description": "Port of Tidal Cycles to JavaScript", "description": "Port of Tidal Cycles to JavaScript",
"main": "index.mjs", "main": "index.mjs",
"type": "module", "type": "module",
@@ -35,7 +35,7 @@
}, },
"gitHead": "0e26d4e741500f5bae35b023608f062a794905c2", "gitHead": "0e26d4e741500f5bae35b023608f062a794905c2",
"devDependencies": { "devDependencies": {
"vite": "^4.3.3", "vite": "^3.2.2",
"vitest": "^0.28.0" "vitest": "^0.25.7"
} }
} }
+32 -194
View File
@@ -27,7 +27,6 @@ export class Pattern {
* Create a pattern. As an end user, you will most likely not create a Pattern directly. * Create a pattern. As an end user, you will most likely not create a Pattern directly.
* *
* @param {function} query - The function that maps a {@link State} to an array of {@link Hap}. * @param {function} query - The function that maps a {@link State} to an array of {@link Hap}.
* @noAutocomplete
*/ */
constructor(query) { constructor(query) {
this.query = query; this.query = query;
@@ -52,7 +51,6 @@ export class Pattern {
/** /**
* see {@link Pattern#withValue} * see {@link Pattern#withValue}
* @noAutocomplete
*/ */
fmap(func) { fmap(func) {
return this.withValue(func); return this.withValue(func);
@@ -64,7 +62,6 @@ export class Pattern {
* pattern of functions. * pattern of functions.
* @param {Function} whole_func * @param {Function} whole_func
* @param {Function} func * @param {Function} func
* @noAutocomplete
* @returns Pattern * @returns Pattern
*/ */
appWhole(whole_func, pat_val) { appWhole(whole_func, pat_val) {
@@ -100,7 +97,6 @@ export class Pattern {
* are not the same but do intersect, the resulting hap has a timespan of the * are not the same but do intersect, the resulting hap has a timespan of the
* intersection. This applies to both the part and the whole timespan. * intersection. This applies to both the part and the whole timespan.
* @param {Pattern} pat_val * @param {Pattern} pat_val
* @noAutocomplete
* @returns Pattern * @returns Pattern
*/ */
appBoth(pat_val) { appBoth(pat_val) {
@@ -121,7 +117,6 @@ export class Pattern {
* are preserved from the pattern of functions (often referred to as the left * are preserved from the pattern of functions (often referred to as the left
* hand or inner pattern). * hand or inner pattern).
* @param {Pattern} pat_val * @param {Pattern} pat_val
* @noAutocomplete
* @returns Pattern * @returns Pattern
*/ */
appLeft(pat_val) { appLeft(pat_val) {
@@ -152,7 +147,6 @@ export class Pattern {
* pattern of values, i.e. structure is preserved from the right hand/outer * pattern of values, i.e. structure is preserved from the right hand/outer
* pattern. * pattern.
* @param {Pattern} pat_val * @param {Pattern} pat_val
* @noAutocomplete
* @returns Pattern * @returns Pattern
*/ */
appRight(pat_val) { appRight(pat_val) {
@@ -338,7 +332,6 @@ export class Pattern {
* const haps = pattern.queryArc(0, 1) * const haps = pattern.queryArc(0, 1)
* console.log(haps) * console.log(haps)
* silence * silence
* @noAutocomplete
*/ */
queryArc(begin, end) { queryArc(begin, end) {
try { try {
@@ -354,7 +347,6 @@ export class Pattern {
* some calculations easier to express, as all haps are then constrained to * some calculations easier to express, as all haps are then constrained to
* happen within a cycle. * happen within a cycle.
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
splitQueries() { splitQueries() {
const pat = this; const pat = this;
@@ -369,7 +361,6 @@ export class Pattern {
* timespan before passing it to the original pattern. * timespan before passing it to the original pattern.
* @param {Function} func the function to apply * @param {Function} func the function to apply
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
withQuerySpan(func) { withQuerySpan(func) {
return new Pattern((state) => this.query(state.withSpan(func))); return new Pattern((state) => this.query(state.withSpan(func)));
@@ -391,7 +382,6 @@ export class Pattern {
* begin and end time of the query timespan. * begin and end time of the query timespan.
* @param {Function} func the function to apply * @param {Function} func the function to apply
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
withQueryTime(func) { withQueryTime(func) {
return new Pattern((state) => this.query(state.withSpan((span) => span.withTime(func)))); return new Pattern((state) => this.query(state.withSpan((span) => span.withTime(func))));
@@ -403,7 +393,6 @@ export class Pattern {
* present, `whole` timespans). * present, `whole` timespans).
* @param {Function} func * @param {Function} func
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
withHapSpan(func) { withHapSpan(func) {
return new Pattern((state) => this.query(state).map((hap) => hap.withSpan(func))); return new Pattern((state) => this.query(state).map((hap) => hap.withSpan(func)));
@@ -414,7 +403,6 @@ export class Pattern {
* begin and end time of the hap timespans. * begin and end time of the hap timespans.
* @param {Function} func the function to apply * @param {Function} func the function to apply
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
withHapTime(func) { withHapTime(func) {
return this.withHapSpan((span) => span.withTime(func)); return this.withHapSpan((span) => span.withTime(func));
@@ -424,7 +412,6 @@ export class Pattern {
* Returns a new pattern with the given function applied to the list of haps returned by every query. * Returns a new pattern with the given function applied to the list of haps returned by every query.
* @param {Function} func * @param {Function} func
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
withHaps(func) { withHaps(func) {
return new Pattern((state) => func(this.query(state))); return new Pattern((state) => func(this.query(state)));
@@ -434,7 +421,6 @@ export class Pattern {
* As with {@link Pattern#withHaps}, but applies the function to every hap, rather than every list of haps. * As with {@link Pattern#withHaps}, but applies the function to every hap, rather than every list of haps.
* @param {Function} func * @param {Function} func
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
withHap(func) { withHap(func) {
return this.withHaps((haps) => haps.map(func)); return this.withHaps((haps) => haps.map(func));
@@ -444,7 +430,6 @@ export class Pattern {
* Returns a new pattern with the context field set to every hap set to the given value. * Returns a new pattern with the context field set to every hap set to the given value.
* @param {*} context * @param {*} context
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
setContext(context) { setContext(context) {
return this.withHap((hap) => hap.setContext(context)); return this.withHap((hap) => hap.setContext(context));
@@ -454,7 +439,6 @@ export class Pattern {
* Returns a new pattern with the given function applied to the context field of every hap. * Returns a new pattern with the given function applied to the context field of every hap.
* @param {Function} func * @param {Function} func
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
withContext(func) { withContext(func) {
return this.withHap((hap) => hap.setContext(func(hap.context))); return this.withHap((hap) => hap.setContext(func(hap.context)));
@@ -463,7 +447,6 @@ export class Pattern {
/** /**
* Returns a new pattern with the context field of every hap set to an empty object. * Returns a new pattern with the context field of every hap set to an empty object.
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
stripContext() { stripContext() {
return this.withHap((hap) => hap.setContext({})); return this.withHap((hap) => hap.setContext({}));
@@ -475,7 +458,6 @@ export class Pattern {
* @param {Number} start * @param {Number} start
* @param {Number} end * @param {Number} end
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
withLocation(start, end) { withLocation(start, end) {
const location = { const location = {
@@ -518,7 +500,6 @@ export class Pattern {
* Returns a new Pattern, which only returns haps that meet the given test. * Returns a new Pattern, which only returns haps that meet the given test.
* @param {Function} hap_test - a function which returns false for haps to be removed from the pattern * @param {Function} hap_test - a function which returns false for haps to be removed from the pattern
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
filterHaps(hap_test) { filterHaps(hap_test) {
return new Pattern((state) => this.query(state).filter(hap_test)); return new Pattern((state) => this.query(state).filter(hap_test));
@@ -529,7 +510,6 @@ export class Pattern {
* inside haps. * inside haps.
* @param {Function} value_test * @param {Function} value_test
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
filterValues(value_test) { filterValues(value_test) {
return new Pattern((state) => this.query(state).filter((hap) => value_test(hap.value))); return new Pattern((state) => this.query(state).filter((hap) => value_test(hap.value)));
@@ -539,7 +519,6 @@ export class Pattern {
* Returns a new pattern, with haps containing undefined values removed from * Returns a new pattern, with haps containing undefined values removed from
* query results. * query results.
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
removeUndefineds() { removeUndefineds() {
return this.filterValues((val) => val != undefined); return this.filterValues((val) => val != undefined);
@@ -550,7 +529,6 @@ export class Pattern {
* with an onset is one with a `whole` timespan that begins at the same time * with an onset is one with a `whole` timespan that begins at the same time
* as its `part` timespan. * as its `part` timespan.
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
onsetsOnly() { onsetsOnly() {
// Returns a new pattern that will only return haps where the start // Returns a new pattern that will only return haps where the start
@@ -563,7 +541,6 @@ export class Pattern {
* Returns a new pattern, with 'continuous' haps (those without 'whole' * Returns a new pattern, with 'continuous' haps (those without 'whole'
* timespans) removed from query results. * timespans) removed from query results.
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
discreteOnly() { discreteOnly() {
// removes continuous haps that don't have a 'whole' timespan // removes continuous haps that don't have a 'whole' timespan
@@ -573,7 +550,6 @@ export class Pattern {
/** /**
* Combines adjacent haps with the same value and whole. Only * Combines adjacent haps with the same value and whole. Only
* intended for use in tests. * intended for use in tests.
* @noAutocomplete
*/ */
defragmentHaps() { defragmentHaps() {
// remove continuous haps // remove continuous haps
@@ -628,7 +604,6 @@ export class Pattern {
* @param {Boolean} with_context - set to true, otherwise the context field * @param {Boolean} with_context - set to true, otherwise the context field
* will be stripped from the resulting haps. * will be stripped from the resulting haps.
* @returns [Hap] * @returns [Hap]
* @noAutocomplete
*/ */
firstCycle(with_context = false) { firstCycle(with_context = false) {
var self = this; var self = this;
@@ -640,7 +615,6 @@ export class Pattern {
/** /**
* Accessor for a list of values returned by querying the first cycle. * Accessor for a list of values returned by querying the first cycle.
* @noAutocomplete
*/ */
get firstCycleValues() { get firstCycleValues() {
return this.firstCycle().map((hap) => hap.value); return this.firstCycle().map((hap) => hap.value);
@@ -648,7 +622,6 @@ export class Pattern {
/** /**
* More human-readable version of the {@link Pattern#firstCycleValues} accessor. * More human-readable version of the {@link Pattern#firstCycleValues} accessor.
* @noAutocomplete
*/ */
get showFirstCycle() { get showFirstCycle() {
return this.firstCycle().map( return this.firstCycle().map(
@@ -660,7 +633,6 @@ export class Pattern {
* Returns a new pattern, which returns haps sorted in temporal order. Mainly * Returns a new pattern, which returns haps sorted in temporal order. Mainly
* of use when comparing two patterns for equality, in tests. * of use when comparing two patterns for equality, in tests.
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
sortHapsByPart() { sortHapsByPart() {
return this.withHaps((haps) => return this.withHaps((haps) =>
@@ -840,7 +812,7 @@ export class Pattern {
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// functions relating to chords/patterns of lists/lists of patterns // functions relating to chords/patterns of lists
// returns Array<Hap[]> where each list of haps satisfies eq // returns Array<Hap[]> where each list of haps satisfies eq
function groupHapsBy(eq, haps) { function groupHapsBy(eq, haps) {
@@ -889,35 +861,6 @@ Pattern.prototype.arp = function (pat) {
return this.arpWith((haps) => pat.fmap((i) => haps[i % haps.length])); return this.arpWith((haps) => pat.fmap((i) => haps[i % haps.length]));
}; };
/*
* Takes a time duration followed by one or more patterns, and shifts the given patterns in time, so they are
* distributed equally over the given time duration. They are then combined with the pattern 'weave' is called on, after it has been stretched out (i.e. slowed down by) the time duration.
* @name weave
* @memberof Pattern
* @example pan(saw).weave(4, s("bd(3,8)"), s("~ sd"))
* @example n("0 1 2 3 4 5 6 7").weave(8, s("bd(3,8)"), s("~ sd"))
addToPrototype('weave', function (t, ...pats) {
return this.weaveWith(t, ...pats.map((x) => set.out(x)));
});
*/
/*
* Like 'weave', but accepts functions rather than patterns, which are applied to the pattern.
* @name weaveWith
* @memberof Pattern
addToPrototype('weaveWith', function (t, ...funcs) {
const pat = this;
const l = funcs.length;
t = Fraction(t);
if (l == 0) {
return silence;
}
return stack(...funcs.map((func, i) => pat.inside(t, func).early(Fraction(i).div(l))))._slow(t);
});
*/
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// compose matrix functions // compose matrix functions
@@ -1127,8 +1070,6 @@ function _composeOp(a, b, func) {
export const polyrhythm = stack; export const polyrhythm = stack;
export const pr = stack; export const pr = stack;
export const pm = polymeter;
// methods that create patterns, which are added to patternified Pattern methods // methods that create patterns, which are added to patternified Pattern methods
// TODO: remove? this is only used in old transpiler (shapeshifter) // TODO: remove? this is only used in old transpiler (shapeshifter)
Pattern.prototype.factories = { Pattern.prototype.factories = {
@@ -1162,7 +1103,6 @@ export const silence = new Pattern(() => []);
* @returns {Pattern} * @returns {Pattern}
* @example * @example
* pure('e4') // "e4" * pure('e4') // "e4"
* @noAutocomplete
*/ */
export function pure(value) { export function pure(value) {
function query(state) { function query(state) {
@@ -1286,20 +1226,6 @@ export function timeCat(...timepats) {
return stack(...pats); return stack(...pats);
} }
/**
* Allows to arrange multiple patterns together over multiple cycles.
* Takes a variable number of arrays with two elements specifying the number of cycles and the pattern to use.
*
* @return {Pattern}
* @example
* arrange([4, "<c a f e>(3,8)"],[2, "<g a>(5,8)"]).note()
*/
export function arrange(...sections) {
const total = sections.reduce((sum, [cycles]) => sum + cycles, 0);
sections = sections.map(([cycles, section]) => [cycles, section.fast(cycles)]);
return timeCat(...sections).slow(total);
}
export function fastcat(...pats) { export function fastcat(...pats) {
return slowcat(...pats)._fast(pats.length); return slowcat(...pats)._fast(pats.length);
} }
@@ -1376,6 +1302,11 @@ export function polymeter(...args) {
return polymeterSteps(0, ...args); return polymeterSteps(0, ...args);
} }
// alias
export function pm(...args) {
polymeter(...args);
}
export const mask = curry((a, b) => reify(b).mask(a)); export const mask = curry((a, b) => reify(b).mask(a));
export const struct = curry((a, b) => reify(b).struct(a)); export const struct = curry((a, b) => reify(b).struct(a));
export const superimpose = curry((a, b) => reify(b).superimpose(...a)); export const superimpose = curry((a, b) => reify(b).superimpose(...a));
@@ -1412,48 +1343,41 @@ export const func = curry((a, b) => reify(b).func(a));
* *
* @param {string} name name of the function * @param {string} name name of the function
* @param {function} func function with 1 or more params, where last is the current pattern * @param {function} func function with 1 or more params, where last is the current pattern
* @noAutocomplete
* *
*/ */
export function register(name, func, patternify = true) { export function register(name, func) {
if (Array.isArray(name)) { if (Array.isArray(name)) {
const result = {}; const result = {};
for (const name_item of name) { for (const name_item of name) {
result[name_item] = register(name_item, func, patternify); result[name_item] = register(name_item, func);
} }
return result; return result;
} }
const arity = func.length; const arity = func.length;
var pfunc; // the patternified function var pfunc; // the patternified function
if (patternify) { pfunc = function (...args) {
pfunc = function (...args) { args = args.map(reify);
args = args.map(reify); const pat = args[args.length - 1];
const pat = args[args.length - 1]; if (arity === 1) {
if (arity === 1) { return func(pat);
return func(pat); }
} const [left, ...right] = args.slice(0, -1);
const [left, ...right] = args.slice(0, -1); let mapFn = (...args) => {
let mapFn = (...args) => { // make sure to call func with the correct argument count
// make sure to call func with the correct argument count // args.length is expected to be <= arity-1
// args.length is expected to be <= arity-1 // so we set undefined args explicitly undefined
// so we set undefined args explicitly undefined Array(arity - 1)
Array(arity - 1) .fill()
.fill() .map((_, i) => args[i] ?? undefined);
.map((_, i) => args[i] ?? undefined); return func(...args, pat);
return func(...args, pat);
};
mapFn = curry(mapFn, null, arity - 1);
return right.reduce((acc, p) => acc.appLeft(p), left.fmap(mapFn)).innerJoin();
}; };
} else { mapFn = curry(mapFn, null, arity - 1);
pfunc = function (...args) { return right.reduce((acc, p) => acc.appLeft(p), left.fmap(mapFn)).innerJoin();
args = args.map(reify); };
return func(...args);
};
}
Pattern.prototype[name] = function (...args) { Pattern.prototype[name] = function (...args) {
args = args.map(reify);
// For methods that take a single argument (plus 'this'), allow // For methods that take a single argument (plus 'this'), allow
// multiple arguments but sequence them // multiple arguments but sequence them
if (arity === 2 && args.length !== 1) { if (arity === 2 && args.length !== 1) {
@@ -1461,7 +1385,6 @@ export function register(name, func, patternify = true) {
} else if (arity !== args.length + 1) { } else if (arity !== args.length + 1) {
throw new Error(`.${name}() expects ${arity - 1} inputs but got ${args.length}.`); throw new Error(`.${name}() expects ${arity - 1} inputs but got ${args.length}.`);
} }
args = args.map(reify);
return pfunc(...args, this); return pfunc(...args, this);
}; };
@@ -1525,17 +1448,15 @@ export const ceil = register('ceil', function (pat) {
* Assumes a numerical pattern, containing unipolar values in the range 0 .. * Assumes a numerical pattern, containing unipolar values in the range 0 ..
* 1. Returns a new pattern with values scaled to the bipolar range -1 .. 1 * 1. Returns a new pattern with values scaled to the bipolar range -1 .. 1
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
export const toBipolar = register('toBipolar', function (pat) { export const toBipolar = register('toBipolar', function (pat) {
return pat.fmap((x) => x * 2 - 1); return pat.fmap((x) => x * 2 - 1);
}); });
/** /**
* Assumes a numerical pattern, containing bipolar values in the range -1 .. 1 * Assumes a numerical pattern, containing bipolar values in the range -1 ..
* Returns a new pattern with values scaled to the unipolar range 0 .. 1 * 1. Returns a new pattern with values scaled to the unipolar range 0 .. 1
* @returns Pattern * @returns Pattern
* @noAutocomplete
*/ */
export const fromBipolar = register('fromBipolar', function (pat) { export const fromBipolar = register('fromBipolar', function (pat) {
return pat.fmap((x) => (x + 1) / 2); return pat.fmap((x) => (x + 1) / 2);
@@ -1582,24 +1503,6 @@ export const range2 = register('range2', function (min, max, pat) {
return pat.fromBipolar()._range(min, max); return pat.fromBipolar()._range(min, max);
}); });
/**
* Allows dividing numbers via list notation using ":".
* Returns a new pattern with just numbers.
* @name ratio
* @memberof Pattern
* @returns Pattern
* @example
* ratio("1, 5:4, 3:2").mul(110).freq().s("piano").slow(2)
*/
export const ratio = register('ratio', (pat) =>
pat.fmap((v) => {
if (!Array.isArray(v)) {
return v;
}
return v.slice(1).reduce((acc, n) => acc / n, v[0]);
}),
);
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Structural and temporal transformations // Structural and temporal transformations
@@ -1695,9 +1598,6 @@ export const ply = register('ply', function (factor, pat) {
* s("<bd sd> hh").fast(2) // s("[<bd sd> hh]*2") * s("<bd sd> hh").fast(2) // s("[<bd sd> hh]*2")
*/ */
export const { fast, density } = register(['fast', 'density'], function (factor, pat) { export const { fast, density } = register(['fast', 'density'], function (factor, pat) {
if (factor === 0) {
return silence;
}
factor = Fraction(factor); factor = Fraction(factor);
const fastQuery = pat.withQueryTime((t) => t.mul(factor)); const fastQuery = pat.withQueryTime((t) => t.mul(factor));
return fastQuery.withHapTime((t) => t.div(factor)); return fastQuery.withHapTime((t) => t.div(factor));
@@ -1724,9 +1624,6 @@ export const hurry = register('hurry', function (r, pat) {
* s("<bd sd> hh").slow(2) // s("[<bd sd> hh]/2") * s("<bd sd> hh").slow(2) // s("[<bd sd> hh]/2")
*/ */
export const { slow, sparsity } = register(['slow', 'sparsity'], function (factor, pat) { export const { slow, sparsity } = register(['slow', 'sparsity'], function (factor, pat) {
if (factor === 0) {
return silence;
}
return pat._fast(Fraction(1).div(factor)); return pat._fast(Fraction(1).div(factor));
}); });
@@ -2053,7 +1950,7 @@ export const jux = register('jux', function (func, pat) {
* @example * @example
* "<0 [2 4]>" * "<0 [2 4]>"
* .echoWith(4, 1/8, (p,n) => p.add(n*2)) * .echoWith(4, 1/8, (p,n) => p.add(n*2))
* .scale('C minor').note().clip(.2) * .scale('C minor').note().legato(.2)
*/ */
export const { echoWith, echowith, stutWith, stutwith } = register( export const { echoWith, echowith, stutWith, stutwith } = register(
['echoWith', 'echowith', 'stutWith', 'stutwith'], ['echoWith', 'echowith', 'stutWith', 'stutwith'],
@@ -2203,7 +2100,6 @@ export const velocity = register('velocity', function (velocity, pat) {
/** /**
* *
* Multiplies the hap duration with the given factor. * Multiplies the hap duration with the given factor.
* With samples, `clip` might be a better function to use ([more info](https://github.com/tidalcycles/strudel/pull/598))
* @name legato * @name legato
* @memberof Pattern * @memberof Pattern
* @example * @example
@@ -2266,65 +2162,7 @@ const _loopAt = function (factor, pat, cps = 1) {
.slow(factor); .slow(factor);
}; };
/* const { loopAt, loopat } = register(['loopAt', 'loopat'], function (factor, pat) {
* Chops samples into the given number of slices, triggering those slices with a given pattern of slice numbers.
* @name slice
* @memberof Pattern
* @returns Pattern
* @example
* await samples('github:tidalcycles/Dirt-Samples/master')
* s("breaks165").slice(8, "0 1 <2 2*2> 3 [4 0] 5 6 7".every(3, rev)).slow(1.5)
*/
export const slice = register(
'slice',
function (npat, ipat, opat) {
return npat.innerBind((n) =>
ipat.outerBind((i) =>
opat.outerBind((o) => {
// If it's not an object, assume it's a string and make it a 's' control parameter
o = o instanceof Object ? o : { s: o };
// Remember we must stay pure and avoid editing the object directly
const toAdd = { begin: i / n, end: (i + 1) / n, _slices: n };
return pure({ ...toAdd, ...o });
}),
),
);
},
false, // turns off auto-patternification
);
/*
* Works the same as slice, but changes the playback speed of each slice to match the duration of its step.
* @name splice
* @memberof Pattern
* @returns Pattern
* @example
* await samples('github:tidalcycles/Dirt-Samples/master')
* s("breaks165").splice(8, "0 1 [2 3 0]@2 3 0@2 7").hurry(0.65)
*/
export const splice = register(
'splice',
function (npat, ipat, opat) {
const sliced = slice(npat, ipat, opat);
return sliced.withHap(function (hap) {
return hap.withValue((v) => ({
...{
speed: (1 / v._slices / hap.whole.duration) * (v.speed || 1),
unit: 'c',
},
...v,
}));
});
},
false, // turns off auto-patternification
);
// this function will be redefined in repl.mjs to use the correct cps value.
// It is still here to work in cases where repl.mjs is not used
export const { loopAt, loopat } = register(['loopAt', 'loopat'], function (factor, pat) {
return _loopAt(factor, pat, 1); return _loopAt(factor, pat, 1);
}); });
@@ -2341,6 +2179,6 @@ export const { loopAt, loopat } = register(['loopAt', 'loopat'], function (facto
* s("rhodes").loopAtCps(4,1.5).cps(1.5) * s("rhodes").loopAtCps(4,1.5).cps(1.5)
*/ */
// TODO - global cps clock // TODO - global cps clock
export const { loopAtCps, loopatcps } = register(['loopAtCps', 'loopatcps'], function (factor, cps, pat) { const { loopAtCps, loopatcps } = register(['loopAtCps', 'loopatcps'], function (factor, cps, pat) {
return _loopAt(factor, pat, cps); return _loopAt(factor, pat, cps);
}); });
+8 -39
View File
@@ -4,7 +4,7 @@ Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/st
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { Pattern, noteToMidi, getDrawContext, freqToMidi, isNote } from './index.mjs'; import { Pattern, toMidi, getDrawContext, freqToMidi, isNote } from './index.mjs';
const scale = (normalized, min, max) => normalized * (max - min) + min; const scale = (normalized, min, max) => normalized * (max - min) + min;
const getValue = (e) => { const getValue = (e) => {
@@ -18,7 +18,7 @@ const getValue = (e) => {
} }
note = note ?? n; note = note ?? n;
if (typeof note === 'string') { if (typeof note === 'string') {
return noteToMidi(note); return toMidi(note);
} }
if (typeof note === 'number') { if (typeof note === 'number') {
return note; return note;
@@ -50,7 +50,6 @@ Pattern.prototype.pianoroll = function ({
timeframe: timeframeProp, timeframe: timeframeProp,
fold = 0, fold = 0,
vertical = 0, vertical = 0,
labels = 0,
} = {}) { } = {}) {
const ctx = getDrawContext(); const ctx = getDrawContext();
const w = ctx.canvas.width; const w = ctx.canvas.width;
@@ -83,12 +82,12 @@ Pattern.prototype.pianoroll = function ({
ctx.fillRect(0, 0, w, h); ctx.fillRect(0, 0, w, h);
} }
const inFrame = (event) => const inFrame = (event) =>
(!hideNegative || event.whole.begin >= 0) && event.whole.begin <= t + to && event.endClipped >= t + from; (!hideNegative || event.whole.begin >= 0) && event.whole.begin <= t + to && event.whole.end >= t + from;
events.filter(inFrame).forEach((event) => { events.filter(inFrame).forEach((event) => {
const isActive = event.whole.begin <= t && event.endClipped > t; const isActive = event.whole.begin <= t && event.whole.end > t;
ctx.fillStyle = event.context?.color || inactive; ctx.fillStyle = event.context?.color || inactive;
ctx.strokeStyle = event.context?.color || active; ctx.strokeStyle = event.context?.color || active;
ctx.globalAlpha = event.context.velocity ?? event.value?.gain ?? 1; ctx.globalAlpha = event.context.velocity ?? 1;
const timePx = scale((event.whole.begin - (flipTime ? to : from)) / timeExtent, ...timeRange); const timePx = scale((event.whole.begin - (flipTime ? to : from)) / timeExtent, ...timeRange);
let durationPx = scale(event.duration / timeExtent, 0, timeAxis); let durationPx = scale(event.duration / timeExtent, 0, timeAxis);
const value = getValue(event); const value = getValue(event);
@@ -115,14 +114,6 @@ Pattern.prototype.pianoroll = function ({
]; ];
} }
isActive ? ctx.strokeRect(...coords) : ctx.fillRect(...coords); isActive ? ctx.strokeRect(...coords) : ctx.fillRect(...coords);
if (labels) {
const label = event.value.note ?? event.value.s + (event.value.n ? `:${event.value.n}` : '');
ctx.font = `${barThickness * 0.75}px monospace`;
ctx.strokeStyle = 'black';
ctx.fillStyle = isActive ? 'white' : 'black';
ctx.textBaseline = 'top';
ctx.fillText(label, ...coords);
}
}); });
ctx.globalAlpha = 1; // reset! ctx.globalAlpha = 1; // reset!
const playheadPosition = scale(-from / timeExtent, ...timeRange); const playheadPosition = scale(-from / timeExtent, ...timeRange);
@@ -190,7 +181,6 @@ export function pianoroll({
timeframe: timeframeProp, timeframe: timeframeProp,
fold = 0, fold = 0,
vertical = 0, vertical = 0,
labels = false,
ctx, ctx,
} = {}) { } = {}) {
const w = ctx.canvas.width; const w = ctx.canvas.width;
@@ -250,7 +240,7 @@ export function pianoroll({
const color = event.value?.color || event.context?.color; const color = event.value?.color || event.context?.color;
ctx.fillStyle = color || inactive; ctx.fillStyle = color || inactive;
ctx.strokeStyle = color || active; ctx.strokeStyle = color || active;
ctx.globalAlpha = event.context.velocity ?? event.value?.gain ?? 1; ctx.globalAlpha = event.context.velocity ?? 1;
const timePx = scale((event.whole.begin - (flipTime ? to : from)) / timeExtent, ...timeRange); const timePx = scale((event.whole.begin - (flipTime ? to : from)) / timeExtent, ...timeRange);
let durationPx = scale(event.duration / timeExtent, 0, timeAxis); let durationPx = scale(event.duration / timeExtent, 0, timeAxis);
const value = getValue(event); const value = getValue(event);
@@ -277,14 +267,6 @@ export function pianoroll({
]; ];
} }
isActive ? ctx.strokeRect(...coords) : ctx.fillRect(...coords); isActive ? ctx.strokeRect(...coords) : ctx.fillRect(...coords);
if (labels) {
const label = event.value.note ?? event.value.s + (event.value.n ? `:${event.value.n}` : '');
ctx.font = `${barThickness * 0.75}px monospace`;
ctx.strokeStyle = 'black';
ctx.fillStyle = isActive ? 'white' : 'black';
ctx.textBaseline = 'top';
ctx.fillText(label, ...coords);
}
}); });
ctx.globalAlpha = 1; // reset! ctx.globalAlpha = 1; // reset!
const playheadPosition = scale(-from / timeExtent, ...timeRange); const playheadPosition = scale(-from / timeExtent, ...timeRange);
@@ -302,7 +284,7 @@ export function pianoroll({
return this; return this;
} }
export function getDrawOptions(drawTime, options = {}) { function getOptions(drawTime, options = {}) {
let [lookbehind, lookahead] = drawTime; let [lookbehind, lookahead] = drawTime;
lookbehind = Math.abs(lookbehind); lookbehind = Math.abs(lookbehind);
const cycles = lookahead + lookbehind; const cycles = lookahead + lookbehind;
@@ -311,18 +293,5 @@ export function getDrawOptions(drawTime, options = {}) {
} }
Pattern.prototype.punchcard = function (options) { Pattern.prototype.punchcard = function (options) {
return this.onPaint((ctx, time, haps, drawTime) => return this.onPaint((ctx, time, haps, drawTime) => pianoroll({ ctx, time, haps, ...getOptions(drawTime, options) }));
pianoroll({ ctx, time, haps, ...getDrawOptions(drawTime, options) }),
);
}; };
/* Pattern.prototype.pianoroll = function (options) {
return this.onPaint((ctx, time, haps, drawTime) =>
pianoroll({ ctx, time, haps, ...getDrawOptions(drawTime, { fold: 0, ...options }) }),
);
}; */
export function drawPianoroll(options) {
const { drawTime, ...rest } = options;
pianoroll({ ...getDrawOptions(drawTime), ...rest });
}
+18 -53
View File
@@ -2,8 +2,6 @@ import { Cyclist } from './cyclist.mjs';
import { evaluate as _evaluate } from './evaluate.mjs'; import { evaluate as _evaluate } from './evaluate.mjs';
import { logger } from './logger.mjs'; import { logger } from './logger.mjs';
import { setTime } from './time.mjs'; import { setTime } from './time.mjs';
import { evalScope } from './evaluate.mjs';
import { register } from './pattern.mjs';
export function repl({ export function repl({
interval, interval,
@@ -19,26 +17,36 @@ export function repl({
}) { }) {
const scheduler = new Cyclist({ const scheduler = new Cyclist({
interval, interval,
onTrigger: getTrigger({ defaultOutput, getTime }), onTrigger: async (hap, deadline, duration) => {
try {
if (!hap.context.onTrigger || !hap.context.dominantTrigger) {
await defaultOutput(hap, deadline, duration);
}
if (hap.context.onTrigger) {
const cps = 1;
// call signature of output / onTrigger is different...
await hap.context.onTrigger(getTime() + deadline, hap, getTime(), cps);
}
} catch (err) {
logger(`[cyclist] error: ${err.message}`, 'error');
}
},
onError: onSchedulerError, onError: onSchedulerError,
getTime, getTime,
onToggle, onToggle,
}); });
const setPattern = (pattern, autostart = true) => {
pattern = editPattern?.(pattern) || pattern;
scheduler.setPattern(pattern, autostart);
};
setTime(() => scheduler.now()); // TODO: refactor? setTime(() => scheduler.now()); // TODO: refactor?
const evaluate = async (code, autostart = true) => { const evaluate = async (code, autostart = true) => {
if (!code) { if (!code) {
throw new Error('no code to evaluate'); throw new Error('no code to evaluate');
} }
try { try {
await beforeEval?.({ code }); beforeEval?.({ code });
let { pattern } = await _evaluate(code, transpiler); let { pattern } = await _evaluate(code, transpiler);
logger(`[eval] code updated`); logger(`[eval] code updated`);
setPattern(pattern, autostart); pattern = editPattern?.(pattern) || pattern;
scheduler.setPattern(pattern, autostart);
afterEval?.({ code, pattern }); afterEval?.({ code, pattern });
return pattern; return pattern;
} catch (err) { } catch (err) {
@@ -50,48 +58,5 @@ export function repl({
const stop = () => scheduler.stop(); const stop = () => scheduler.stop();
const start = () => scheduler.start(); const start = () => scheduler.start();
const pause = () => scheduler.pause(); const pause = () => scheduler.pause();
const setCps = (cps) => scheduler.setCps(cps); return { scheduler, evaluate, start, stop, pause };
const setCpm = (cpm) => scheduler.setCps(cpm / 60);
// the following functions use the cps value, which is why they are defined here..
const loopAt = register('loopAt', (cycles, pat) => {
return pat.loopAtCps(cycles, scheduler.cps);
});
const fit = register('fit', (pat) =>
pat.withHap((hap) =>
hap.withValue((v) => ({
...v,
speed: scheduler.cps / hap.whole.duration, // overwrite speed completely?
unit: 'c',
})),
),
);
evalScope({
loopAt,
fit,
setCps,
setcps: setCps,
setCpm,
setcpm: setCpm,
});
return { scheduler, evaluate, start, stop, pause, setCps, setPattern };
} }
export const getTrigger =
({ getTime, defaultOutput }) =>
async (hap, deadline, duration, cps) => {
try {
if (!hap.context.onTrigger || !hap.context.dominantTrigger) {
await defaultOutput(hap, deadline, duration, cps);
}
if (hap.context.onTrigger) {
// call signature of output / onTrigger is different...
await hap.context.onTrigger(getTime() + deadline, hap, getTime(), cps);
}
} catch (err) {
logger(`[cyclist] error: ${err.message}`, 'error');
}
};
+1 -1
View File
@@ -27,7 +27,7 @@ export const isaw2 = isaw.toBipolar();
* *
* @return {Pattern} * @return {Pattern}
* @example * @example
* "c3 [eb3,g3] g2 [g3,bb3]".note().clip(saw.slow(4)) * "c3 [eb3,g3] g2 [g3,bb3]".legato(saw.slow(4)).note()
* @example * @example
* saw.range(0,8).segment(8).scale('C major').slow(4).note() * saw.range(0,8).segment(8).scale('C major').slow(4).note()
* *
-117
View File
@@ -1,117 +0,0 @@
import { Pattern } from './index.mjs';
// polar coords -> xy
function fromPolar(angle, radius, cx, cy) {
const radians = ((angle - 90) * Math.PI) / 180;
return [cx + Math.cos(radians) * radius, cy + Math.sin(radians) * radius];
}
const xyOnSpiral = (angle, margin, cx, cy, rotate = 0) => fromPolar((angle + rotate) * 360, margin * angle, cx, cy); // TODO: logSpiral
// draw spiral / segment of spiral
function spiralSegment(options) {
let {
ctx,
from = 0,
to = 3,
margin = 50,
cx = 100,
cy = 100,
rotate = 0,
thickness = margin / 2,
color = '#0000ff30',
cap = 'round',
stretch = 1,
fromOpacity = 1,
toOpacity = 1,
} = options;
from *= stretch;
to *= stretch;
rotate *= stretch;
ctx.lineWidth = thickness;
ctx.lineCap = cap;
ctx.strokeStyle = color;
ctx.globalAlpha = fromOpacity;
ctx.beginPath();
let [sx, sy] = xyOnSpiral(from, margin, cx, cy, rotate);
ctx.moveTo(sx, sy);
const increment = 1 / 60;
let angle = from;
while (angle <= to) {
const [x, y] = xyOnSpiral(angle, margin, cx, cy, rotate);
//ctx.lineWidth = angle*thickness;
ctx.globalAlpha = ((angle - from) / (to - from)) * toOpacity;
ctx.lineTo(x, y);
angle += increment;
}
ctx.stroke();
}
Pattern.prototype.spiral = function (options = {}) {
const {
stretch = 1,
size = 80,
thickness = size / 2,
cap = 'butt', // round butt squar,
inset = 3, // start angl,
playheadColor = '#ffffff90',
playheadLength = 0.02,
playheadThickness = thickness,
padding = 0,
steady = 1,
inactiveColor = '#ffffff20',
colorizeInactive = 0,
fade = true,
// logSpiral = true,
} = options;
function spiral({ ctx, time, haps, drawTime }) {
ctx.clearRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight);
const [cx, cy] = [ctx.canvas.width / 2, ctx.canvas.height / 2];
const settings = {
margin: size / stretch,
cx,
cy,
stretch,
cap,
thickness,
};
const playhead = {
...settings,
thickness: playheadThickness,
from: inset - playheadLength,
to: inset,
color: playheadColor,
};
const [min] = drawTime;
const rotate = steady * time;
haps.forEach((hap) => {
const isActive = hap.whole.begin <= time && hap.endClipped > time;
const from = hap.whole.begin - time + inset;
const to = hap.endClipped - time + inset - padding;
const { color } = hap.context;
const opacity = fade ? 1 - Math.abs((hap.whole.begin - time) / min) : 1;
spiralSegment({
ctx,
...settings,
from,
to,
rotate,
color: colorizeInactive || isActive ? color : inactiveColor,
fromOpacity: opacity,
toOpacity: opacity,
});
});
spiralSegment({
ctx,
...playhead,
rotate,
});
}
return this.onPaint((ctx, time, haps, drawTime) => spiral({ ctx, time, haps, drawTime }));
};
-28
View File
@@ -1,28 +0,0 @@
/*
controls.test.mjs - <short description TODO>
Copyright (C) 2023 Strudel contributors - see <https://github.com/tidalcycles/strudel/blob/main/packages/core/test/controls.test.mjs>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import controls from '../controls.mjs';
import { mini } from '../../mini/mini.mjs';
import { describe, it, expect } from 'vitest';
describe('controls', () => {
it('should support controls', () => {
expect(controls.s('bd').firstCycleValues).toEqual([{ s: 'bd' }]);
});
it('should support compound controls', () => {
expect(controls.s(mini('bd:3')).firstCycleValues).toEqual([{ s: 'bd', n: 3 }]);
expect(controls.s(mini('bd:3 sd:4:1.4')).firstCycleValues).toEqual([
{ s: 'bd', n: 3 },
{ s: 'sd', n: 4, gain: 1.4 },
]);
});
it('should support ignore extra elements in compound controls', () => {
expect(controls.s(mini('bd:3:0.4 sd:4:0.5:3:17')).firstCycleValues).toEqual([
{ s: 'bd', n: 3, gain: 0.4 },
{ s: 'sd', n: 4, gain: 0.5 },
]);
});
});
-44
View File
@@ -959,48 +959,4 @@ describe('Pattern', () => {
sameFirst(s('a', 'b').hurry(2), s('a', 'b').fast(2).speed(2)); sameFirst(s('a', 'b').hurry(2), s('a', 'b').fast(2).speed(2));
}); });
}); });
/*describe('composable functions', () => {
it('Can compose functions', () => {
sameFirst(sequence(3, 4).fast(2).rev().fast(2), fast(2).rev().fast(2)(sequence(3, 4)));
});
it('Can compose by method chaining operators with controls', () => {
sameFirst(s('bd').apply(set.n(3).fast(2)), s('bd').set.n(3).fast(2));
});
it('Can compose by method chaining operators and alignments with controls', () => {
sameFirst(s('bd').apply(set.in.n(3).fast(2)), s('bd').set.n(3).fast(2));
// sameFirst(s('bd').apply(set.squeeze.n(3).fast(2)), s('bd').set.squeeze.n(3).fast(2));
});
});
describe('weave', () => {
it('Can distribute patterns along a pattern', () => {
sameFirst(n(0, 1).weave(2, s('bd', silence), s(silence, 'sd')), sequence(s('bd').n(0), s('sd').n(1)));
});
});
*/
describe('slice', () => {
it('Can slice a sample', () => {
sameFirst(
s('break').slice(4, sequence(0, 1, 2, 3)),
sequence(
{ begin: 0, end: 0.25, s: 'break', _slices: 4 },
{ begin: 0.25, end: 0.5, s: 'break', _slices: 4 },
{ begin: 0.5, end: 0.75, s: 'break', _slices: 4 },
{ begin: 0.75, end: 1, s: 'break', _slices: 4 },
),
);
});
});
describe('splice', () => {
it('Can splice a sample', () => {
sameFirst(
s('break').splice(4, sequence(0, 1, 2, 3)),
sequence(
{ begin: 0, end: 0.25, s: 'break', _slices: 4, unit: 'c', speed: 1 },
{ begin: 0.25, end: 0.5, s: 'break', _slices: 4, unit: 'c', speed: 1 },
{ begin: 0.5, end: 0.75, s: 'break', _slices: 4, unit: 'c', speed: 1 },
{ begin: 0.75, end: 1, s: 'break', _slices: 4, unit: 'c', speed: 1 },
),
);
});
});
}); });
-40
View File
@@ -1,40 +0,0 @@
/*test for issue 302 support alternative solmization types */
import { sol2note } from '../util.mjs';
import { test } from 'vitest';
import assert from 'assert';
test('solmization - letters', () => {
const result = sol2note(60, 'letters');
const expected = 'C4';
assert.equal(result, expected);
});
test('solmization - solfeggio', () => {
const result = sol2note(60, 'solfeggio');
const expected = 'Do4';
assert.equal(result, expected);
});
test('solmization - indian', () => {
const result = sol2note(60, 'indian');
const expected = 'Sa4';
assert.equal(result, expected);
});
test('solmization - german', () => {
const result = sol2note(60, 'german');
const expected = 'C4';
assert.equal(result, expected);
});
test('solmization - byzantine', () => {
const result = sol2note(60, 'byzantine');
const expected = 'Ni4';
assert.equal(result, expected);
});
test('solmization - japanese', () => {
const result = sol2note(60, 'japanese');
const expected = 'I4';
assert.equal(result, expected);
});
+17 -17
View File
@@ -8,8 +8,8 @@ import { pure } from '../pattern.mjs';
import { import {
isNote, isNote,
tokenizeNote, tokenizeNote,
noteToMidi, toMidi,
midiToFreq, fromMidi,
freqToMidi, freqToMidi,
_mod, _mod,
compose, compose,
@@ -75,27 +75,27 @@ describe('isNote', () => {
expect(tokenizeNote(123)).toStrictEqual([]); expect(tokenizeNote(123)).toStrictEqual([]);
}); });
}); });
describe('noteToMidi', () => { describe('toMidi', () => {
it('should turn notes into midi', () => { it('should turn notes into midi', () => {
expect(noteToMidi('A4')).toEqual(69); expect(toMidi('A4')).toEqual(69);
expect(noteToMidi('C4')).toEqual(60); expect(toMidi('C4')).toEqual(60);
expect(noteToMidi('Db4')).toEqual(61); expect(toMidi('Db4')).toEqual(61);
expect(noteToMidi('C3')).toEqual(48); expect(toMidi('C3')).toEqual(48);
expect(noteToMidi('Cb3')).toEqual(47); expect(toMidi('Cb3')).toEqual(47);
expect(noteToMidi('Cbb3')).toEqual(46); expect(toMidi('Cbb3')).toEqual(46);
expect(noteToMidi('C#3')).toEqual(49); expect(toMidi('C#3')).toEqual(49);
expect(noteToMidi('C#3')).toEqual(49); expect(toMidi('C#3')).toEqual(49);
expect(noteToMidi('C##3')).toEqual(50); expect(toMidi('C##3')).toEqual(50);
}); });
it('should throw an error when given a non-note', () => { it('should throw an error when given a non-note', () => {
expect(() => noteToMidi('Q')).toThrowError(`not a note: "Q"`); expect(() => toMidi('Q')).toThrowError(`not a note: "Q"`);
expect(() => noteToMidi('Z')).toThrowError(`not a note: "Z"`); expect(() => toMidi('Z')).toThrowError(`not a note: "Z"`);
}); });
}); });
describe('midiToFreq', () => { describe('fromMidi', () => {
it('should turn midi into frequency', () => { it('should turn midi into frequency', () => {
expect(midiToFreq(69)).toEqual(440); expect(fromMidi(69)).toEqual(440);
expect(midiToFreq(57)).toEqual(220); expect(fromMidi(57)).toEqual(220);
}); });
}); });
describe('freqToMidi', () => { describe('freqToMidi', () => {
+14 -81
View File
@@ -6,12 +6,12 @@ This program is free software: you can redistribute it and/or modify it under th
// returns true if the given string is a note // returns true if the given string is a note
export const isNoteWithOctave = (name) => /^[a-gA-G][#bs]*[0-9]$/.test(name); export const isNoteWithOctave = (name) => /^[a-gA-G][#bs]*[0-9]$/.test(name);
export const isNote = (name) => /^[a-gA-G][#bsf]*[0-9]?$/.test(name); export const isNote = (name) => /^[a-gA-G][#bs]*[0-9]?$/.test(name);
export const tokenizeNote = (note) => { export const tokenizeNote = (note) => {
if (typeof note !== 'string') { if (typeof note !== 'string') {
return []; return [];
} }
const [pc, acc = '', oct] = note.match(/^([a-gA-G])([#bsf]*)([0-9])?$/)?.slice(1) || []; const [pc, acc = '', oct] = note.match(/^([a-gA-G])([#bs]*)([0-9])?$/)?.slice(1) || [];
if (!pc) { if (!pc) {
return []; return [];
} }
@@ -19,16 +19,16 @@ export const tokenizeNote = (note) => {
}; };
// turns the given note into its midi number representation // turns the given note into its midi number representation
export const noteToMidi = (note) => { export const toMidi = (note) => {
const [pc, acc, oct = 3] = tokenizeNote(note); const [pc, acc, oct = 3] = tokenizeNote(note);
if (!pc) { if (!pc) {
throw new Error('not a note: "' + note + '"'); throw new Error('not a note: "' + note + '"');
} }
const chroma = { c: 0, d: 2, e: 4, f: 5, g: 7, a: 9, b: 11 }[pc.toLowerCase()]; const chroma = { c: 0, d: 2, e: 4, f: 5, g: 7, a: 9, b: 11 }[pc.toLowerCase()];
const offset = acc?.split('').reduce((o, char) => o + { '#': 1, b: -1, s: 1, f: -1 }[char], 0) || 0; const offset = acc?.split('').reduce((o, char) => o + { '#': 1, b: -1, s: 1 }[char], 0) || 0;
return (Number(oct) + 1) * 12 + chroma + offset; return (Number(oct) + 1) * 12 + chroma + offset;
}; };
export const midiToFreq = (n) => { export const fromMidi = (n) => {
return Math.pow(2, (n - 69) / 12) * 440; return Math.pow(2, (n - 69) / 12) * 440;
}; };
@@ -45,7 +45,7 @@ export const valueToMidi = (value, fallbackValue) => {
return freqToMidi(freq); return freqToMidi(freq);
} }
if (typeof note === 'string') { if (typeof note === 'string') {
return noteToMidi(note); return toMidi(note);
} }
if (typeof note === 'number') { if (typeof note === 'number') {
return note; return note;
@@ -58,23 +58,20 @@ export const valueToMidi = (value, fallbackValue) => {
/** /**
* @deprecated does not appear to be referenced or invoked anywhere in the codebase * @deprecated does not appear to be referenced or invoked anywhere in the codebase
* @noAutocomplete
*/ */
export const getFreq = (noteOrMidi) => { export const getFreq = (noteOrMidi) => {
if (typeof noteOrMidi === 'number') { if (typeof noteOrMidi === 'number') {
return midiToFreq(noteOrMidi); return fromMidi(noteOrMidi);
} }
return midiToFreq(noteToMidi(noteOrMidi)); return fromMidi(toMidi(noteOrMidi));
}; };
const pcs = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'];
/** /**
* @deprecated only used in workshop (first-notes) * @deprecated does not appear to be referenced or invoked anywhere in the codebase
* @noAutocomplete
*/ */
export const midi2note = (n) => { export const midi2note = (n) => {
const oct = Math.floor(n / 12) - 1; const oct = Math.floor(n / 12) - 1;
const pc = pcs[n % 12]; const pc = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'][n % 12];
return pc + oct; return pc + oct;
}; };
@@ -92,7 +89,7 @@ export const getPlayableNoteValue = (hap) => {
} }
// if value is number => interpret as midi number as long as its not marked as frequency // if value is number => interpret as midi number as long as its not marked as frequency
if (typeof note === 'number' && context.type !== 'frequency') { if (typeof note === 'number' && context.type !== 'frequency') {
note = midiToFreq(hap.value); note = fromMidi(hap.value);
} else if (typeof note === 'number' && context.type === 'frequency') { } else if (typeof note === 'number' && context.type === 'frequency') {
note = hap.value; // legacy workaround.. will be removed in the future note = hap.value; // legacy workaround.. will be removed in the future
} else if (typeof note !== 'string' || !isNote(note)) { } else if (typeof note !== 'string' || !isNote(note)) {
@@ -111,9 +108,9 @@ export const getFrequency = (hap) => {
return getFreq(value.note || value.n || value.value); return getFreq(value.note || value.n || value.value);
} }
if (typeof value === 'number' && context.type !== 'frequency') { if (typeof value === 'number' && context.type !== 'frequency') {
value = midiToFreq(hap.value); value = fromMidi(hap.value);
} else if (typeof value === 'string' && isNote(value)) { } else if (typeof value === 'string' && isNote(value)) {
value = midiToFreq(noteToMidi(hap.value)); value = fromMidi(toMidi(hap.value));
} else if (typeof value !== 'number') { } else if (typeof value !== 'number') {
throw new Error('not a note or frequency: ' + value); throw new Error('not a note or frequency: ' + value);
} }
@@ -171,7 +168,7 @@ export function parseNumeral(numOrString) {
return asNumber; return asNumber;
} }
if (isNote(numOrString)) { if (isNote(numOrString)) {
return noteToMidi(numOrString); return toMidi(numOrString);
} }
throw new Error(`cannot parse as numeral: "${numOrString}"`); throw new Error(`cannot parse as numeral: "${numOrString}"`);
} }
@@ -207,67 +204,3 @@ export function parseFractional(numOrString) {
} }
export const fractionalArgs = (fn) => mapArgs(fn, parseFractional); export const fractionalArgs = (fn) => mapArgs(fn, parseFractional);
export const splitAt = function (index, value) {
return [value.slice(0, index), value.slice(index)];
};
export const zipWith = (f, xs, ys) => xs.map((n, i) => f(n, ys[i]));
export const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
/* solmization, not used yet */
const solfeggio = ['Do', 'Reb', 'Re', 'Mib', 'Mi', 'Fa', 'Solb', 'Sol', 'Lab', 'La', 'Sib', 'Si']; /*solffegio notes*/
const indian = [
'Sa',
'Re',
'Ga',
'Ma',
'Pa',
'Dha',
'Ni',
]; /*indian musical notes, seems like they do not use flats or sharps*/
const german = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Hb', 'H']; /*german & dutch musical notes*/
const byzantine = [
'Ni',
'Pab',
'Pa',
'Voub',
'Vou',
'Ga',
'Dib',
'Di',
'Keb',
'Ke',
'Zob',
'Zo',
]; /*byzantine musical notes*/
const japanese = [
'I',
'Ro',
'Ha',
'Ni',
'Ho',
'He',
'To',
]; /*traditional japanese musical notes, seems like they do not use falts or sharps*/
const english = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'];
export const sol2note = (n, notation = 'letters') => {
const pc =
notation === 'solfeggio'
? solfeggio /*check if its is any of the following*/
: notation === 'indian'
? indian
: notation === 'german'
? german
: notation === 'byzantine'
? byzantine
: notation === 'japanese'
? japanese
: english; /*if not use standard version*/
const note = pc[n % 12]; /*calculating the midi value to the note*/
const oct = Math.floor(n / 12) - 1;
return note + oct;
};
+1 -1
View File
@@ -44,6 +44,6 @@ function createClock(
}; };
const getPhase = () => phase; const getPhase = () => phase;
// setCallback // setCallback
return { setDuration, start, stop, pause, duration, interval, getPhase, minLatency }; return { setDuration, start, stop, pause, duration, getPhase, minLatency };
} }
export default createClock; export default createClock;
+3 -1
View File
@@ -28,7 +28,9 @@ export const csound = register('csound', (instrument, pat) => {
logger('[csound] not loaded yet', 'warning'); logger('[csound] not loaded yet', 'warning');
return; return;
} }
hap.ensureObjectValue(); if (typeof hap.value !== 'object') {
throw new Error('csound only support objects as hap values');
}
let { gain = 0.8 } = hap.value; let { gain = 0.8 } = hap.value;
gain *= 0.2; gain *= 0.2;
+3 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/csound", "name": "@strudel.cycles/csound",
"version": "0.8.0", "version": "0.6.2",
"description": "csound bindings for strudel", "description": "csound bindings for strudel",
"main": "index.mjs", "main": "index.mjs",
"publishConfig": { "publishConfig": {
@@ -32,11 +32,11 @@
}, },
"homepage": "https://github.com/tidalcycles/strudel#readme", "homepage": "https://github.com/tidalcycles/strudel#readme",
"dependencies": { "dependencies": {
"@csound/browser": "6.18.7", "@csound/browser": "6.18.5",
"@strudel.cycles/core": "workspace:*", "@strudel.cycles/core": "workspace:*",
"@strudel.cycles/webaudio": "workspace:*" "@strudel.cycles/webaudio": "workspace:*"
}, },
"devDependencies": { "devDependencies": {
"vite": "^4.3.3" "vite": "^3.2.2"
} }
} }
+3 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/eval", "name": "@strudel.cycles/eval",
"version": "0.8.0", "version": "0.6.2",
"description": "Code evaluator for strudel", "description": "Code evaluator for strudel",
"main": "index.mjs", "main": "index.mjs",
"publishConfig": { "publishConfig": {
@@ -44,7 +44,7 @@
}, },
"devDependencies": { "devDependencies": {
"@strudel.cycles/mini": "workspace:*", "@strudel.cycles/mini": "workspace:*",
"vite": "^4.3.3", "vite": "^3.2.2",
"vitest": "^0.28.0" "vitest": "^0.25.7"
} }
} }
+16 -31
View File
@@ -5,9 +5,8 @@ This program is free software: you can redistribute it and/or modify it under th
*/ */
import * as _WebMidi from 'webmidi'; import * as _WebMidi from 'webmidi';
import { Pattern, isPattern, logger } from '@strudel.cycles/core'; import { Pattern, isPattern, isNote, getPlayableNoteValue, logger } from '@strudel.cycles/core';
import { getAudioContext } from '@strudel.cycles/webaudio'; import { getAudioContext } from '@strudel.cycles/webaudio';
import { noteToMidi } from '@strudel.cycles/core';
// if you use WebMidi from outside of this package, make sure to import that instance: // if you use WebMidi from outside of this package, make sure to import that instance:
export const { WebMidi } = _WebMidi; export const { WebMidi } = _WebMidi;
@@ -64,7 +63,7 @@ function getDevice(output, outputs) {
} }
// Pattern.prototype.midi = function (output: string | number, channel = 1) { // Pattern.prototype.midi = function (output: string | number, channel = 1) {
Pattern.prototype.midi = function (output) { Pattern.prototype.midi = function (output, channel = 1) {
if (!supportsMidi()) { if (!supportsMidi()) {
throw new Error(`🎹 WebMidi is not enabled. Supported Browsers: https://caniuse.com/?search=webmidi`); throw new Error(`🎹 WebMidi is not enabled. Supported Browsers: https://caniuse.com/?search=webmidi`);
} }
@@ -91,6 +90,11 @@ Pattern.prototype.midi = function (output) {
); );
} }
return this.onTrigger((time, hap) => { return this.onTrigger((time, hap) => {
let note = getPlayableNoteValue(hap);
const velocity = hap.context?.velocity ?? 0.9;
if (!isNote(note)) {
throw new Error('not a note: ' + note);
}
if (!midiReady) { if (!midiReady) {
return; return;
} }
@@ -102,34 +106,15 @@ Pattern.prototype.midi = function (output) {
.join(' | ')}`, .join(' | ')}`,
); );
} }
hap.ensureObjectValue(); // console.log('midi', value, output);
const timingOffset = WebMidi.time - getAudioContext().currentTime * 1000;
// calculate time
const timingOffset = WebMidi.time - getAudioContext().getOutputTimestamp().contextTime * 1000;
time = time * 1000 + timingOffset; time = time * 1000 + timingOffset;
// const inMs = '+' + (time - Tone.getContext().currentTime) * 1000;
// destructure value // await enableWebMidi()
const { note, nrpnn, nrpv, ccn, ccv, midichan = 1 } = hap.value; device.playNote(note, channel, {
const velocity = hap.context?.velocity ?? 0.9; // TODO: refactor velocity time,
const duration = hap.duration.valueOf() * 1000 - 5; duration: hap.duration.valueOf() * 1000 - 5,
attack: velocity,
if (note) { });
const midiNumber = noteToMidi(note);
device.playNote(midiNumber, midichan, {
time,
duration,
attack: velocity,
});
}
if (ccv && ccn) {
if (typeof ccv !== 'number' || ccv < 0 || ccv > 1) {
throw new Error('expected ccv to be a number between 0 and 1');
}
if (!['string', 'number'].includes(typeof ccn)) {
throw new Error('expected ccn to be a number or a string');
}
const scaled = Math.round(ccv * 127);
device.sendControlChange(ccn, scaled, midichan, { time });
}
}); });
}; };
+3 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/midi", "name": "@strudel.cycles/midi",
"version": "0.8.0", "version": "0.6.0",
"description": "Midi API for strudel", "description": "Midi API for strudel",
"main": "index.mjs", "main": "index.mjs",
"publishConfig": { "publishConfig": {
@@ -31,9 +31,9 @@
"dependencies": { "dependencies": {
"@strudel.cycles/core": "workspace:*", "@strudel.cycles/core": "workspace:*",
"@strudel.cycles/webaudio": "workspace:*", "@strudel.cycles/webaudio": "workspace:*",
"webmidi": "^3.1.5" "webmidi": "^3.0.21"
}, },
"devDependencies": { "devDependencies": {
"vite": "^4.3.3" "vite": "^3.2.2"
} }
} }
+134 -171
View File
@@ -1,4 +1,4 @@
// Generated by Peggy 3.0.2. // Generated by Peggy 2.0.1.
// //
// https://peggyjs.org/ // https://peggyjs.org/
@@ -43,19 +43,16 @@ peg$SyntaxError.prototype.format = function(sources) {
} }
} }
var s = this.location.start; var s = this.location.start;
var offset_s = (this.location.source && (typeof this.location.source.offset === "function")) var loc = this.location.source + ":" + s.line + ":" + s.column;
? this.location.source.offset(s)
: s;
var loc = this.location.source + ":" + offset_s.line + ":" + offset_s.column;
if (src) { if (src) {
var e = this.location.end; var e = this.location.end;
var filler = peg$padEnd("", offset_s.line.toString().length, ' '); var filler = peg$padEnd("", s.line.toString().length, ' ');
var line = src[s.line - 1]; var line = src[s.line - 1];
var last = s.line === e.line ? e.column : line.length + 1; var last = s.line === e.line ? e.column : line.length + 1;
var hatLen = (last - s.column) || 1; var hatLen = (last - s.column) || 1;
str += "\n --> " + loc + "\n" str += "\n --> " + loc + "\n"
+ filler + " |\n" + filler + " |\n"
+ offset_s.line + " | " + line + "\n" + s.line + " | " + line + "\n"
+ filler + " | " + peg$padEnd("", s.column - 1, ' ') + filler + " | " + peg$padEnd("", s.column - 1, ' ')
+ peg$padEnd("", hatLen, "^"); + peg$padEnd("", hatLen, "^");
} else { } else {
@@ -185,21 +182,21 @@ function peg$parse(input, options) {
var peg$c8 = "#"; var peg$c8 = "#";
var peg$c9 = "^"; var peg$c9 = "^";
var peg$c10 = "_"; var peg$c10 = "_";
var peg$c11 = "["; var peg$c11 = ":";
var peg$c12 = "]"; var peg$c12 = "[";
var peg$c13 = "{"; var peg$c13 = "]";
var peg$c14 = "}"; var peg$c14 = "{";
var peg$c15 = "%"; var peg$c15 = "}";
var peg$c16 = "<"; var peg$c16 = "%";
var peg$c17 = ">"; var peg$c17 = "<";
var peg$c18 = "@"; var peg$c18 = ">";
var peg$c19 = "!"; var peg$c19 = "@";
var peg$c20 = "("; var peg$c20 = "!";
var peg$c21 = ")"; var peg$c21 = "(";
var peg$c22 = "/"; var peg$c22 = ")";
var peg$c23 = "*"; var peg$c23 = "/";
var peg$c24 = "?"; var peg$c24 = "*";
var peg$c25 = ":"; var peg$c25 = "?";
var peg$c26 = "struct"; var peg$c26 = "struct";
var peg$c27 = "target"; var peg$c27 = "target";
var peg$c28 = "euclid"; var peg$c28 = "euclid";
@@ -240,21 +237,21 @@ function peg$parse(input, options) {
var peg$e15 = peg$literalExpectation("#", false); var peg$e15 = peg$literalExpectation("#", false);
var peg$e16 = peg$literalExpectation("^", false); var peg$e16 = peg$literalExpectation("^", false);
var peg$e17 = peg$literalExpectation("_", false); var peg$e17 = peg$literalExpectation("_", false);
var peg$e18 = peg$literalExpectation("[", false); var peg$e18 = peg$literalExpectation(":", false);
var peg$e19 = peg$literalExpectation("]", false); var peg$e19 = peg$literalExpectation("[", false);
var peg$e20 = peg$literalExpectation("{", false); var peg$e20 = peg$literalExpectation("]", false);
var peg$e21 = peg$literalExpectation("}", false); var peg$e21 = peg$literalExpectation("{", false);
var peg$e22 = peg$literalExpectation("%", false); var peg$e22 = peg$literalExpectation("}", false);
var peg$e23 = peg$literalExpectation("<", false); var peg$e23 = peg$literalExpectation("%", false);
var peg$e24 = peg$literalExpectation(">", false); var peg$e24 = peg$literalExpectation("<", false);
var peg$e25 = peg$literalExpectation("@", false); var peg$e25 = peg$literalExpectation(">", false);
var peg$e26 = peg$literalExpectation("!", false); var peg$e26 = peg$literalExpectation("@", false);
var peg$e27 = peg$literalExpectation("(", false); var peg$e27 = peg$literalExpectation("!", false);
var peg$e28 = peg$literalExpectation(")", false); var peg$e28 = peg$literalExpectation("(", false);
var peg$e29 = peg$literalExpectation("/", false); var peg$e29 = peg$literalExpectation(")", false);
var peg$e30 = peg$literalExpectation("*", false); var peg$e30 = peg$literalExpectation("/", false);
var peg$e31 = peg$literalExpectation("?", false); var peg$e31 = peg$literalExpectation("*", false);
var peg$e32 = peg$literalExpectation(":", false); var peg$e32 = peg$literalExpectation("?", false);
var peg$e33 = peg$literalExpectation("struct", false); var peg$e33 = peg$literalExpectation("struct", false);
var peg$e34 = peg$literalExpectation("target", false); var peg$e34 = peg$literalExpectation("target", false);
var peg$e35 = peg$literalExpectation("euclid", false); var peg$e35 = peg$literalExpectation("euclid", false);
@@ -282,37 +279,36 @@ function peg$parse(input, options) {
var peg$f8 = function(p, s, r) { return x => x.options_['ops'].push({ type_: "bjorklund", arguments_ :{ pulse: p, step:s, rotation:r }}) }; var peg$f8 = function(p, s, r) { return x => x.options_['ops'].push({ type_: "bjorklund", arguments_ :{ pulse: p, step:s, rotation:r }}) };
var peg$f9 = function(a) { return x => x.options_['ops'].push({ type_: "stretch", arguments_ :{ amount:a, type: 'slow' }}) }; var peg$f9 = function(a) { return x => x.options_['ops'].push({ type_: "stretch", arguments_ :{ amount:a, type: 'slow' }}) };
var peg$f10 = function(a) { return x => x.options_['ops'].push({ type_: "stretch", arguments_ :{ amount:a, type: 'fast' }}) }; var peg$f10 = function(a) { return x => x.options_['ops'].push({ type_: "stretch", arguments_ :{ amount:a, type: 'fast' }}) };
var peg$f11 = function(a) { return x => x.options_['ops'].push({ type_: "degradeBy", arguments_ :{ amount:a, seed: seed++ } }) }; var peg$f11 = function(a) { return x => x.options_['ops'].push({ type_: "degradeBy", arguments_ :{ amount:a } }) };
var peg$f12 = function(s) { return x => x.options_['ops'].push({ type_: "tail", arguments_ :{ element:s } }) }; var peg$f12 = function(s, ops) { const result = new ElementStub(s, {ops: [], weight: 1, reps: 1});
var peg$f13 = function(s, ops) { const result = new ElementStub(s, {ops: [], weight: 1, reps: 1});
for (const op of ops) { for (const op of ops) {
op(result); op(result);
} }
return result; return result;
}; };
var peg$f14 = function(s) { return new PatternStub(s, 'fastcat'); }; var peg$f13 = function(s) { return new PatternStub(s, 'fastcat'); };
var peg$f15 = function(tail) { return { alignment: 'stack', list: tail }; }; var peg$f14 = function(tail) { return { alignment: 'stack', list: tail }; };
var peg$f16 = function(tail) { return { alignment: 'rand', list: tail, seed: seed++ }; }; var peg$f15 = function(tail) { return { alignment: 'rand', list: tail }; };
var peg$f17 = function(head, tail) { if (tail && tail.list.length > 0) { return new PatternStub([head, ...tail.list], tail.alignment, tail.seed); } else { return head; } }; var peg$f16 = function(head, tail) { if (tail && tail.list.length > 0) { return new PatternStub([head, ...tail.list], tail.alignment); } else { return head; } };
var peg$f18 = function(head, tail) { return new PatternStub(tail ? [head, ...tail.list] : [head], 'polymeter'); }; var peg$f17 = function(head, tail) { return new PatternStub(tail ? [head, ...tail.list] : [head], 'polymeter'); };
var peg$f19 = function(sc) { return sc; }; var peg$f18 = function(sc) { return sc; };
var peg$f20 = function(s) { return { name: "struct", args: { mini:s }}}; var peg$f19 = function(s) { return { name: "struct", args: { mini:s }}};
var peg$f21 = function(s) { return { name: "target", args : { name:s}}}; var peg$f20 = function(s) { return { name: "target", args : { name:s}}};
var peg$f22 = function(p, s, r) { return { name: "bjorklund", args :{ pulse: p, step:parseInt(s) }}}; var peg$f21 = function(p, s, r) { return { name: "bjorklund", args :{ pulse: p, step:parseInt(s) }}};
var peg$f23 = function(a) { return { name: "stretch", args :{ amount: a}}}; var peg$f22 = function(a) { return { name: "stretch", args :{ amount: a}}};
var peg$f24 = function(a) { return { name: "shift", args :{ amount: "-"+a}}}; var peg$f23 = function(a) { return { name: "shift", args :{ amount: "-"+a}}};
var peg$f25 = function(a) { return { name: "shift", args :{ amount: a}}}; var peg$f24 = function(a) { return { name: "shift", args :{ amount: a}}};
var peg$f26 = function(a) { return { name: "stretch", args :{ amount: "1/"+a}}}; var peg$f25 = function(a) { return { name: "stretch", args :{ amount: "1/"+a}}};
var peg$f27 = function(s) { return { name: "scale", args :{ scale: s.join("")}}}; var peg$f26 = function(s) { return { name: "scale", args :{ scale: s.join("")}}};
var peg$f28 = function(s, v) { return v}; var peg$f27 = function(s, v) { return v};
var peg$f29 = function(s, ss) { ss.unshift(s); return new PatternStub(ss, 'slowcat'); }; var peg$f28 = function(s, ss) { ss.unshift(s); return new PatternStub(ss, 'slowcat'); };
var peg$f30 = function(sg) {return sg}; var peg$f29 = function(sg) {return sg};
var peg$f31 = function(o, soc) { return new OperatorStub(o.name,o.args,soc)}; var peg$f30 = function(o, soc) { return new OperatorStub(o.name,o.args,soc)};
var peg$f32 = function(sc) { return sc }; var peg$f31 = function(sc) { return sc };
var peg$f33 = function(c) { return c }; var peg$f32 = function(c) { return c };
var peg$f34 = function(v) { return new CommandStub("setcps", { value: v})}; var peg$f33 = function(v) { return new CommandStub("setcps", { value: v})};
var peg$f35 = function(v) { return new CommandStub("setcps", { value: (v/120/2)})}; var peg$f34 = function(v) { return new CommandStub("setcps", { value: (v/120/2)})};
var peg$f36 = function() { return new CommandStub("hush")}; var peg$f35 = function() { return new CommandStub("hush")};
var peg$currPos = 0; var peg$currPos = 0;
var peg$savedPos = 0; var peg$savedPos = 0;
var peg$posDetailsCache = [{ line: 1, column: 1 }]; var peg$posDetailsCache = [{ line: 1, column: 1 }];
@@ -425,11 +421,11 @@ function peg$parse(input, options) {
} }
} }
function peg$computeLocation(startPos, endPos, offset) { function peg$computeLocation(startPos, endPos) {
var startPosDetails = peg$computePosDetails(startPos); var startPosDetails = peg$computePosDetails(startPos);
var endPosDetails = peg$computePosDetails(endPos); var endPosDetails = peg$computePosDetails(endPos);
var res = { return {
source: peg$source, source: peg$source,
start: { start: {
offset: startPos, offset: startPos,
@@ -442,11 +438,6 @@ function peg$parse(input, options) {
column: endPosDetails.column column: endPosDetails.column
} }
}; };
if (offset && peg$source && (typeof peg$source.offset === "function")) {
res.start = peg$source.offset(res.start);
res.end = peg$source.offset(res.end);
}
return res;
} }
function peg$fail(expected) { function peg$fail(expected) {
@@ -857,6 +848,15 @@ function peg$parse(input, options) {
s0 = peg$FAILED; s0 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e17); } if (peg$silentFails === 0) { peg$fail(peg$e17); }
} }
if (s0 === peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 58) {
s0 = peg$c11;
peg$currPos++;
} else {
s0 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e18); }
}
}
} }
} }
} }
@@ -899,11 +899,11 @@ function peg$parse(input, options) {
s0 = peg$currPos; s0 = peg$currPos;
s1 = peg$parsews(); s1 = peg$parsews();
if (input.charCodeAt(peg$currPos) === 91) { if (input.charCodeAt(peg$currPos) === 91) {
s2 = peg$c11; s2 = peg$c12;
peg$currPos++; peg$currPos++;
} else { } else {
s2 = peg$FAILED; s2 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e18); } if (peg$silentFails === 0) { peg$fail(peg$e19); }
} }
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
s3 = peg$parsews(); s3 = peg$parsews();
@@ -911,11 +911,11 @@ function peg$parse(input, options) {
if (s4 !== peg$FAILED) { if (s4 !== peg$FAILED) {
s5 = peg$parsews(); s5 = peg$parsews();
if (input.charCodeAt(peg$currPos) === 93) { if (input.charCodeAt(peg$currPos) === 93) {
s6 = peg$c12; s6 = peg$c13;
peg$currPos++; peg$currPos++;
} else { } else {
s6 = peg$FAILED; s6 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e19); } if (peg$silentFails === 0) { peg$fail(peg$e20); }
} }
if (s6 !== peg$FAILED) { if (s6 !== peg$FAILED) {
s7 = peg$parsews(); s7 = peg$parsews();
@@ -943,11 +943,11 @@ function peg$parse(input, options) {
s0 = peg$currPos; s0 = peg$currPos;
s1 = peg$parsews(); s1 = peg$parsews();
if (input.charCodeAt(peg$currPos) === 123) { if (input.charCodeAt(peg$currPos) === 123) {
s2 = peg$c13; s2 = peg$c14;
peg$currPos++; peg$currPos++;
} else { } else {
s2 = peg$FAILED; s2 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e20); } if (peg$silentFails === 0) { peg$fail(peg$e21); }
} }
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
s3 = peg$parsews(); s3 = peg$parsews();
@@ -955,11 +955,11 @@ function peg$parse(input, options) {
if (s4 !== peg$FAILED) { if (s4 !== peg$FAILED) {
s5 = peg$parsews(); s5 = peg$parsews();
if (input.charCodeAt(peg$currPos) === 125) { if (input.charCodeAt(peg$currPos) === 125) {
s6 = peg$c14; s6 = peg$c15;
peg$currPos++; peg$currPos++;
} else { } else {
s6 = peg$FAILED; s6 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e21); } if (peg$silentFails === 0) { peg$fail(peg$e22); }
} }
if (s6 !== peg$FAILED) { if (s6 !== peg$FAILED) {
s7 = peg$parsepolymeter_steps(); s7 = peg$parsepolymeter_steps();
@@ -990,11 +990,11 @@ function peg$parse(input, options) {
s0 = peg$currPos; s0 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 37) { if (input.charCodeAt(peg$currPos) === 37) {
s1 = peg$c15; s1 = peg$c16;
peg$currPos++; peg$currPos++;
} else { } else {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e22); } if (peg$silentFails === 0) { peg$fail(peg$e23); }
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parseslice(); s2 = peg$parseslice();
@@ -1019,11 +1019,11 @@ function peg$parse(input, options) {
s0 = peg$currPos; s0 = peg$currPos;
s1 = peg$parsews(); s1 = peg$parsews();
if (input.charCodeAt(peg$currPos) === 60) { if (input.charCodeAt(peg$currPos) === 60) {
s2 = peg$c16; s2 = peg$c17;
peg$currPos++; peg$currPos++;
} else { } else {
s2 = peg$FAILED; s2 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e23); } if (peg$silentFails === 0) { peg$fail(peg$e24); }
} }
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
s3 = peg$parsews(); s3 = peg$parsews();
@@ -1031,11 +1031,11 @@ function peg$parse(input, options) {
if (s4 !== peg$FAILED) { if (s4 !== peg$FAILED) {
s5 = peg$parsews(); s5 = peg$parsews();
if (input.charCodeAt(peg$currPos) === 62) { if (input.charCodeAt(peg$currPos) === 62) {
s6 = peg$c17; s6 = peg$c18;
peg$currPos++; peg$currPos++;
} else { } else {
s6 = peg$FAILED; s6 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e24); } if (peg$silentFails === 0) { peg$fail(peg$e25); }
} }
if (s6 !== peg$FAILED) { if (s6 !== peg$FAILED) {
s7 = peg$parsews(); s7 = peg$parsews();
@@ -1088,9 +1088,6 @@ function peg$parse(input, options) {
s0 = peg$parseop_replicate(); s0 = peg$parseop_replicate();
if (s0 === peg$FAILED) { if (s0 === peg$FAILED) {
s0 = peg$parseop_degrade(); s0 = peg$parseop_degrade();
if (s0 === peg$FAILED) {
s0 = peg$parseop_tail();
}
} }
} }
} }
@@ -1105,11 +1102,11 @@ function peg$parse(input, options) {
s0 = peg$currPos; s0 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 64) { if (input.charCodeAt(peg$currPos) === 64) {
s1 = peg$c18; s1 = peg$c19;
peg$currPos++; peg$currPos++;
} else { } else {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e25); } if (peg$silentFails === 0) { peg$fail(peg$e26); }
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parsenumber(); s2 = peg$parsenumber();
@@ -1133,11 +1130,11 @@ function peg$parse(input, options) {
s0 = peg$currPos; s0 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 33) { if (input.charCodeAt(peg$currPos) === 33) {
s1 = peg$c19; s1 = peg$c20;
peg$currPos++; peg$currPos++;
} else { } else {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e26); } if (peg$silentFails === 0) { peg$fail(peg$e27); }
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parsenumber(); s2 = peg$parsenumber();
@@ -1161,11 +1158,11 @@ function peg$parse(input, options) {
s0 = peg$currPos; s0 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 40) { if (input.charCodeAt(peg$currPos) === 40) {
s1 = peg$c20; s1 = peg$c21;
peg$currPos++; peg$currPos++;
} else { } else {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e27); } if (peg$silentFails === 0) { peg$fail(peg$e28); }
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parsews(); s2 = peg$parsews();
@@ -1189,11 +1186,11 @@ function peg$parse(input, options) {
} }
s12 = peg$parsews(); s12 = peg$parsews();
if (input.charCodeAt(peg$currPos) === 41) { if (input.charCodeAt(peg$currPos) === 41) {
s13 = peg$c21; s13 = peg$c22;
peg$currPos++; peg$currPos++;
} else { } else {
s13 = peg$FAILED; s13 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e28); } if (peg$silentFails === 0) { peg$fail(peg$e29); }
} }
if (s13 !== peg$FAILED) { if (s13 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
@@ -1227,11 +1224,11 @@ function peg$parse(input, options) {
s0 = peg$currPos; s0 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 47) { if (input.charCodeAt(peg$currPos) === 47) {
s1 = peg$c22; s1 = peg$c23;
peg$currPos++; peg$currPos++;
} else { } else {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e29); } if (peg$silentFails === 0) { peg$fail(peg$e30); }
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parseslice(); s2 = peg$parseslice();
@@ -1255,11 +1252,11 @@ function peg$parse(input, options) {
s0 = peg$currPos; s0 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 42) { if (input.charCodeAt(peg$currPos) === 42) {
s1 = peg$c23; s1 = peg$c24;
peg$currPos++; peg$currPos++;
} else { } else {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e30); } if (peg$silentFails === 0) { peg$fail(peg$e31); }
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parseslice(); s2 = peg$parseslice();
@@ -1283,11 +1280,11 @@ function peg$parse(input, options) {
s0 = peg$currPos; s0 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 63) { if (input.charCodeAt(peg$currPos) === 63) {
s1 = peg$c24; s1 = peg$c25;
peg$currPos++; peg$currPos++;
} else { } else {
s1 = peg$FAILED; s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e31); } if (peg$silentFails === 0) { peg$fail(peg$e32); }
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parsenumber(); s2 = peg$parsenumber();
@@ -1304,34 +1301,6 @@ function peg$parse(input, options) {
return s0; return s0;
} }
function peg$parseop_tail() {
var s0, s1, s2;
s0 = peg$currPos;
if (input.charCodeAt(peg$currPos) === 58) {
s1 = peg$c25;
peg$currPos++;
} else {
s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e32); }
}
if (s1 !== peg$FAILED) {
s2 = peg$parseslice();
if (s2 !== peg$FAILED) {
peg$savedPos = s0;
s0 = peg$f12(s2);
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
return s0;
}
function peg$parseslice_with_ops() { function peg$parseslice_with_ops() {
var s0, s1, s2, s3; var s0, s1, s2, s3;
@@ -1345,7 +1314,7 @@ function peg$parse(input, options) {
s3 = peg$parseslice_op(); s3 = peg$parseslice_op();
} }
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f13(s1, s2); s0 = peg$f12(s1, s2);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1370,7 +1339,7 @@ function peg$parse(input, options) {
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s1 = peg$f14(s1); s1 = peg$f13(s1);
} }
s0 = s1; s0 = s1;
@@ -1419,7 +1388,7 @@ function peg$parse(input, options) {
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s1 = peg$f15(s1); s1 = peg$f14(s1);
} }
s0 = s1; s0 = s1;
@@ -1468,7 +1437,7 @@ function peg$parse(input, options) {
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s1 = peg$f16(s1); s1 = peg$f15(s1);
} }
s0 = s1; s0 = s1;
@@ -1489,7 +1458,7 @@ function peg$parse(input, options) {
s2 = null; s2 = null;
} }
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f17(s1, s2); s0 = peg$f16(s1, s2);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1509,7 +1478,7 @@ function peg$parse(input, options) {
s2 = null; s2 = null;
} }
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f18(s1, s2); s0 = peg$f17(s1, s2);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1519,20 +1488,18 @@ function peg$parse(input, options) {
} }
function peg$parsemini() { function peg$parsemini() {
var s0, s1, s2, s3, s4, s5, s6; var s0, s1, s2, s3, s4;
s0 = peg$currPos; s0 = peg$currPos;
s1 = peg$parsews(); s1 = peg$parsews();
s2 = peg$parsequote(); s2 = peg$parsequote();
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
s3 = peg$parsews(); s3 = peg$parsestack_or_choose();
s4 = peg$parsestack_or_choose(); if (s3 !== peg$FAILED) {
if (s4 !== peg$FAILED) { s4 = peg$parsequote();
s5 = peg$parsews(); if (s4 !== peg$FAILED) {
s6 = peg$parsequote();
if (s6 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f19(s4); s0 = peg$f18(s3);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1594,7 +1561,7 @@ function peg$parse(input, options) {
s3 = peg$parsemini_or_operator(); s3 = peg$parsemini_or_operator();
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f20(s3); s0 = peg$f19(s3);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1627,7 +1594,7 @@ function peg$parse(input, options) {
s5 = peg$parsequote(); s5 = peg$parsequote();
if (s5 !== peg$FAILED) { if (s5 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f21(s4); s0 = peg$f20(s4);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1672,7 +1639,7 @@ function peg$parse(input, options) {
s7 = null; s7 = null;
} }
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f22(s3, s5, s7); s0 = peg$f21(s3, s5, s7);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1705,7 +1672,7 @@ function peg$parse(input, options) {
s3 = peg$parsenumber(); s3 = peg$parsenumber();
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f23(s3); s0 = peg$f22(s3);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1734,7 +1701,7 @@ function peg$parse(input, options) {
s3 = peg$parsenumber(); s3 = peg$parsenumber();
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f24(s3); s0 = peg$f23(s3);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1763,7 +1730,7 @@ function peg$parse(input, options) {
s3 = peg$parsenumber(); s3 = peg$parsenumber();
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f25(s3); s0 = peg$f24(s3);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1792,7 +1759,7 @@ function peg$parse(input, options) {
s3 = peg$parsenumber(); s3 = peg$parsenumber();
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f26(s3); s0 = peg$f25(s3);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1834,7 +1801,7 @@ function peg$parse(input, options) {
s5 = peg$parsequote(); s5 = peg$parsequote();
if (s5 !== peg$FAILED) { if (s5 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f27(s4); s0 = peg$f26(s4);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -1909,11 +1876,11 @@ function peg$parse(input, options) {
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
s2 = peg$parsews(); s2 = peg$parsews();
if (input.charCodeAt(peg$currPos) === 91) { if (input.charCodeAt(peg$currPos) === 91) {
s3 = peg$c11; s3 = peg$c12;
peg$currPos++; peg$currPos++;
} else { } else {
s3 = peg$FAILED; s3 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e18); } if (peg$silentFails === 0) { peg$fail(peg$e19); }
} }
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
s4 = peg$parsews(); s4 = peg$parsews();
@@ -1926,7 +1893,7 @@ function peg$parse(input, options) {
s9 = peg$parsemini_or_operator(); s9 = peg$parsemini_or_operator();
if (s9 !== peg$FAILED) { if (s9 !== peg$FAILED) {
peg$savedPos = s7; peg$savedPos = s7;
s7 = peg$f28(s5, s9); s7 = peg$f27(s5, s9);
} else { } else {
peg$currPos = s7; peg$currPos = s7;
s7 = peg$FAILED; s7 = peg$FAILED;
@@ -1943,7 +1910,7 @@ function peg$parse(input, options) {
s9 = peg$parsemini_or_operator(); s9 = peg$parsemini_or_operator();
if (s9 !== peg$FAILED) { if (s9 !== peg$FAILED) {
peg$savedPos = s7; peg$savedPos = s7;
s7 = peg$f28(s5, s9); s7 = peg$f27(s5, s9);
} else { } else {
peg$currPos = s7; peg$currPos = s7;
s7 = peg$FAILED; s7 = peg$FAILED;
@@ -1955,15 +1922,15 @@ function peg$parse(input, options) {
} }
s7 = peg$parsews(); s7 = peg$parsews();
if (input.charCodeAt(peg$currPos) === 93) { if (input.charCodeAt(peg$currPos) === 93) {
s8 = peg$c12; s8 = peg$c13;
peg$currPos++; peg$currPos++;
} else { } else {
s8 = peg$FAILED; s8 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e19); } if (peg$silentFails === 0) { peg$fail(peg$e20); }
} }
if (s8 !== peg$FAILED) { if (s8 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f29(s5, s6); s0 = peg$f28(s5, s6);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -2009,7 +1976,7 @@ function peg$parse(input, options) {
s4 = peg$parsecomment(); s4 = peg$parsecomment();
} }
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f30(s1); s0 = peg$f29(s1);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -2031,7 +1998,7 @@ function peg$parse(input, options) {
s5 = peg$parsemini_or_operator(); s5 = peg$parsemini_or_operator();
if (s5 !== peg$FAILED) { if (s5 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f31(s1, s5); s0 = peg$f30(s1, s5);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -2056,7 +2023,7 @@ function peg$parse(input, options) {
s1 = peg$parsemini_or_operator(); s1 = peg$parsemini_or_operator();
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s1 = peg$f32(s1); s1 = peg$f31(s1);
} }
s0 = s1; s0 = s1;
if (s0 === peg$FAILED) { if (s0 === peg$FAILED) {
@@ -2089,7 +2056,7 @@ function peg$parse(input, options) {
if (s2 !== peg$FAILED) { if (s2 !== peg$FAILED) {
s3 = peg$parsews(); s3 = peg$parsews();
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f33(s2); s0 = peg$f32(s2);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -2114,7 +2081,7 @@ function peg$parse(input, options) {
s3 = peg$parsenumber(); s3 = peg$parsenumber();
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f34(s3); s0 = peg$f33(s3);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -2143,7 +2110,7 @@ function peg$parse(input, options) {
s3 = peg$parsenumber(); s3 = peg$parsenumber();
if (s3 !== peg$FAILED) { if (s3 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s0 = peg$f35(s3); s0 = peg$f34(s3);
} else { } else {
peg$currPos = s0; peg$currPos = s0;
s0 = peg$FAILED; s0 = peg$FAILED;
@@ -2169,7 +2136,7 @@ function peg$parse(input, options) {
} }
if (s1 !== peg$FAILED) { if (s1 !== peg$FAILED) {
peg$savedPos = s0; peg$savedPos = s0;
s1 = peg$f36(); s1 = peg$f35();
} }
s0 = s1; s0 = s1;
@@ -2195,13 +2162,10 @@ function peg$parse(input, options) {
this.location_ = location(); this.location_ = location();
} }
var PatternStub = function(source, alignment, seed) var PatternStub = function(source, alignment)
{ {
this.type_ = "pattern"; this.type_ = "pattern";
this.arguments_ = { alignment: alignment }; this.arguments_ = { alignment : alignment};
if (seed !== undefined) {
this.arguments_.seed = seed;
}
this.source_ = source; this.source_ = source;
} }
@@ -2227,7 +2191,6 @@ function peg$parse(input, options) {
this.options_ = options; this.options_ = options;
} }
var seed = 0;
peg$result = peg$startRuleFunction(); peg$result = peg$startRuleFunction();
+8 -15
View File
@@ -19,13 +19,10 @@ This program is free software: you can redistribute it and/or modify it under th
this.location_ = location(); this.location_ = location();
} }
var PatternStub = function(source, alignment, seed) var PatternStub = function(source, alignment)
{ {
this.type_ = "pattern"; this.type_ = "pattern";
this.arguments_ = { alignment: alignment }; this.arguments_ = { alignment : alignment};
if (seed !== undefined) {
this.arguments_.seed = seed;
}
this.source_ = source; this.source_ = source;
} }
@@ -51,7 +48,6 @@ This program is free software: you can redistribute it and/or modify it under th
this.options_ = options; this.options_ = options;
} }
var seed = 0;
} }
start = statement start = statement
@@ -100,7 +96,7 @@ quote = '"' / "'"
// ------------------ steps and cycles --------------------------- // ------------------ steps and cycles ---------------------------
// single step definition (e.g bd) // single step definition (e.g bd)
step_char = [0-9a-zA-Z~] / "-" / "#" / "." / "^" / "_" step_char = [0-9a-zA-Z~] / "-" / "#" / "." / "^" / "_" / ":"
step = ws chars:step_char+ ws { return new AtomStub(chars.join("")) } step = ws chars:step_char+ ws { return new AtomStub(chars.join("")) }
// define a sub cycle e.g. [1 2, 3 [4]] // define a sub cycle e.g. [1 2, 3 [4]]
@@ -123,7 +119,7 @@ slice = step / sub_cycle / polymeter / slow_sequence
// slice modifier affects the timing/size of a slice (e.g. [a b c]@3) // slice modifier affects the timing/size of a slice (e.g. [a b c]@3)
// at this point, we assume we can represent them as regular sequence operators // at this point, we assume we can represent them as regular sequence operators
slice_op = op_weight / op_bjorklund / op_slow / op_fast / op_replicate / op_degrade / op_tail slice_op = op_weight / op_bjorklund / op_slow / op_fast / op_replicate / op_degrade
op_weight = "@" a:number op_weight = "@" a:number
{ return x => x.options_['weight'] = a } { return x => x.options_['weight'] = a }
@@ -141,10 +137,7 @@ op_fast = "*"a:slice
{ return x => x.options_['ops'].push({ type_: "stretch", arguments_ :{ amount:a, type: 'fast' }}) } { return x => x.options_['ops'].push({ type_: "stretch", arguments_ :{ amount:a, type: 'fast' }}) }
op_degrade = "?"a:number? op_degrade = "?"a:number?
{ return x => x.options_['ops'].push({ type_: "degradeBy", arguments_ :{ amount:a, seed: seed++ } }) } { return x => x.options_['ops'].push({ type_: "degradeBy", arguments_ :{ amount:a } }) }
op_tail = ":" s:slice
{ return x => x.options_['ops'].push({ type_: "tail", arguments_ :{ element:s } }) }
// a slice with an modifier applied i.e [bd@4 sd@3]@2 hh] // a slice with an modifier applied i.e [bd@4 sd@3]@2 hh]
slice_with_ops = s:slice ops:slice_op* slice_with_ops = s:slice ops:slice_op*
@@ -166,12 +159,12 @@ stack_tail = tail:(comma @sequence)+
// a choose is a series of pipe-separated sequence, one of which is // a choose is a series of pipe-separated sequence, one of which is
// chosen at random, each cycle // chosen at random, each cycle
choose_tail = tail:(pipe @sequence)+ choose_tail = tail:(pipe @sequence)+
{ return { alignment: 'rand', list: tail, seed: seed++ }; } { return { alignment: 'rand', list: tail }; }
// if the stack contains only one element, we don't create a stack but return the // if the stack contains only one element, we don't create a stack but return the
// underlying element // underlying element
stack_or_choose = head:sequence tail:(stack_tail / choose_tail)? stack_or_choose = head:sequence tail:(stack_tail / choose_tail)?
{ if (tail && tail.list.length > 0) { return new PatternStub([head, ...tail.list], tail.alignment, tail.seed); } else { return head; } } { if (tail && tail.list.length > 0) { return new PatternStub([head, ...tail.list], tail.alignment); } else { return head; } }
polymeter_stack = head:sequence tail:stack_tail? polymeter_stack = head:sequence tail:stack_tail?
{ return new PatternStub(tail ? [head, ...tail.list] : [head], 'polymeter'); } { return new PatternStub(tail ? [head, ...tail.list] : [head], 'polymeter'); }
@@ -182,7 +175,7 @@ polymeter_stack = head:sequence tail:stack_tail?
// Experimental haskellish parser begins // Experimental haskellish parser begins
// mini-notation = a quoted stack // mini-notation = a quoted stack
mini = ws quote ws sc:stack_or_choose ws quote mini = ws quote sc:stack_or_choose quote
{ return sc; } { return sc; }
// ------------------ operators --------------------------- // ------------------ operators ---------------------------
+30 -16
View File
@@ -7,13 +7,17 @@ This program is free software: you can redistribute it and/or modify it under th
import * as krill from './krill-parser.js'; import * as krill from './krill-parser.js';
import * as strudel from '@strudel.cycles/core'; import * as strudel from '@strudel.cycles/core';
const randOffset = 0.0003; /* var _seedState = 0;
const randOffset = 0.0002;
function _nextSeed() {
return _seedState++;
} */
const applyOptions = (parent, code) => (pat, i) => { const applyOptions = (parent, code) => (pat, i) => {
const ast = parent.source_[i]; const ast = parent.source_[i];
const options = ast.options_; const options = ast.options_;
const ops = options?.ops; const ops = options?.ops;
if (ops) { if (ops) {
for (const op of ops) { for (const op of ops) {
switch (op.type_) { switch (op.type_) {
@@ -39,14 +43,27 @@ const applyOptions = (parent, code) => (pat, i) => {
break; break;
} }
case 'degradeBy': { case 'degradeBy': {
pat = strudel // TODO: find out what is right here
.reify(pat) // example:
._degradeByWith(strudel.rand.early(randOffset * op.arguments_.seed), op.arguments_.amount ?? 0.5); /*
break; stack(
} s("hh*8").degrade(),
case 'tail': { s("[ht*8]?")
const friend = patternifyAST(op.arguments_.element, code); )
pat = pat.fmap((a) => (b) => Array.isArray(a) ? [...a, b] : [a, b]).appLeft(friend); */
// above example will only be in sync when _degradeBy is used...
// it also seems that the nextSeed will create undeterministic behaviour
// as it uses a global _seedState. This is probably the reason for
// https://github.com/tidalcycles/strudel/issues/245
// this is how it was:
/*
return strudel.reify(pat)._degradeByWith(
strudel.rand.early(randOffset * _nextSeed()).segment(1),
op.arguments_.amount ?? 0.5,
);
*/
pat = strudel.reify(pat).degradeBy(op.arguments_.amount === null ? 0.5 : op.arguments_.amount);
break; break;
} }
default: { default: {
@@ -91,7 +108,9 @@ export function patternifyAST(ast, code) {
return strudel.stack(...aligned); return strudel.stack(...aligned);
} }
if (alignment === 'rand') { if (alignment === 'rand') {
return strudel.chooseInWith(strudel.rand.early(randOffset * ast.arguments_.seed).segment(1), children); // https://github.com/tidalcycles/strudel/issues/245#issuecomment-1345406422
// return strudel.chooseInWith(strudel.rand.early(randOffset * _nextSeed()).segment(1), children);
return strudel.chooseCycles(...children);
} }
const weightedChildren = ast.source_.some((child) => !!child.options_?.weight); const weightedChildren = ast.source_.some((child) => !!child.options_?.weight);
if (!weightedChildren && alignment === 'slowcat') { if (!weightedChildren && alignment === 'slowcat') {
@@ -190,8 +209,3 @@ export function minify(thing) {
} }
return strudel.reify(thing); return strudel.reify(thing);
} }
// calling this function will cause patterns to parse strings as mini notation by default
export function miniAllStrings() {
strudel.setStringParser(mini);
}
+4 -4
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/mini", "name": "@strudel.cycles/mini",
"version": "0.8.2", "version": "0.6.0",
"description": "Mini notation for strudel", "description": "Mini notation for strudel",
"main": "index.mjs", "main": "index.mjs",
"type": "module", "type": "module",
@@ -35,8 +35,8 @@
"@strudel.cycles/core": "workspace:*" "@strudel.cycles/core": "workspace:*"
}, },
"devDependencies": { "devDependencies": {
"peggy": "^3.0.2", "peggy": "^2.0.1",
"vite": "^4.3.3", "vite": "^3.2.2",
"vitest": "^0.28.0" "vitest": "^0.25.7"
} }
} }
+3 -20
View File
@@ -140,22 +140,7 @@ describe('mini', () => {
expect(haps.length < 230).toBe(true); expect(haps.length < 230).toBe(true);
// 'Had too many cycles remaining after degradeBy 0.8'); // 'Had too many cycles remaining after degradeBy 0.8');
}); });
it('supports multiple independent uses of the random choice operator ("|")', () => { /*it('supports the random choice operator ("|") with nesting', () => {
const numCycles = 1000;
const values = mini('[a|b] [a|b]')
.queryArc(0, numCycles)
.map((e) => e.value);
const observed = { aa: 0, ab: 0, ba: 0, bb: 0 };
for (let i = 0; i < values.length; i += 2) {
const chunk = values.slice(i, i + 2);
observed[chunk.join('')]++;
}
for (const count of Object.values(observed)) {
// Should fall within 99% confidence interval for binomial with p=0.25.
expect(215 <= count && count <= 286).toBe(true);
}
});
it('supports the random choice operator ("|") with nesting', () => {
const numCycles = 900; const numCycles = 900;
const haps = mini('a | [b | c] | [d | e | f]').queryArc(0, numCycles); const haps = mini('a | [b | c] | [d | e | f]').queryArc(0, numCycles);
// Should have about 1/3 a, 1/6 each of b | c, and 1/9 each of d | e | f. // Should have about 1/3 a, 1/6 each of b | c, and 1/9 each of d | e | f.
@@ -180,8 +165,6 @@ describe('mini', () => {
// 15.086 is the chisq for 5 degrees of freedom at 99%, so for 99% of uniformly-distributed // 15.086 is the chisq for 5 degrees of freedom at 99%, so for 99% of uniformly-distributed
// PRNG, this test should succeed // PRNG, this test should succeed
expect(chisq <= 15.086).toBe(true); expect(chisq <= 15.086).toBe(true);
}); // assert(chisq <= 15.086, chisq + ' was expected to be less than 15.086 under chi-squared test');
it('supports lists', () => { });*/
expect(minV('a:b c:d:[e:f] g')).toEqual([['a', 'b'], ['c', 'd', ['e', 'f']], 'g']);
});
}); });
+3 -5
View File
@@ -39,16 +39,14 @@ let startedAt = -1;
/** /**
* *
* Sends each hap as an OSC message, which can be picked up by SuperCollider or any other OSC-enabled software. * Sends each hap as an OSC message, which can be picked up by SuperCollider or any other OSC-enabled software.
* For more info, read [MIDI & OSC in the docs](https://strudel.tidalcycles.org/learn/input-output)
* *
* @name osc * @name osc
* @memberof Pattern * @memberof Pattern
* @returns Pattern * @returns Pattern
*/ */
Pattern.prototype.osc = function () { Pattern.prototype.osc = async function () {
return this.onTrigger(async (time, hap, currentTime, cps = 1) => { const osc = await connect();
hap.ensureObjectValue(); return this.onTrigger((time, hap, currentTime, cps = 1) => {
const osc = await connect();
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
+3 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/osc", "name": "@strudel.cycles/osc",
"version": "0.8.0", "version": "0.6.0",
"description": "OSC messaging for strudel", "description": "OSC messaging for strudel",
"main": "osc.mjs", "main": "osc.mjs",
"publishConfig": { "publishConfig": {
@@ -40,7 +40,7 @@
"osc-js": "^2.4.0" "osc-js": "^2.4.0"
}, },
"devDependencies": { "devDependencies": {
"pkg": "^5.8.1", "pkg": "^5.7.0",
"vite": "^4.3.3" "vite": "^3.2.2"
} }
} }
+14 -33
View File
@@ -13,43 +13,24 @@ npm i @strudel.cycles/react
Here is a minimal example of how to set up a MiniRepl: Here is a minimal example of how to set up a MiniRepl:
```jsx ```jsx
import * as React from 'react';
import '@strudel.cycles/react/dist/style.css';
import { MiniRepl } from '@strudel.cycles/react';
import { evalScope, controls } from '@strudel.cycles/core'; import { evalScope, controls } from '@strudel.cycles/core';
import { samples, initAudioOnFirstClick } from '@strudel.cycles/webaudio'; import { MiniRepl } from '@strudel.cycles/react';
import { prebake } from '../repl/src/prebake.mjs';
async function prebake() { evalScope(
await samples( controls,
'https://strudel.tidalcycles.org/tidal-drum-machines.json', import('@strudel.cycles/core'),
'github:ritchse/tidal-drum-machines/main/machines/' import('@strudel.cycles/tonal'),
); import('@strudel.cycles/mini'),
await samples( import('@strudel.cycles/webaudio'),
'https://strudel.tidalcycles.org/EmuSP12.json', /* probably import other strudel packages */
'https://strudel.tidalcycles.org/EmuSP12/' );
);
}
async function init() { prebake();
await evalScope(
controls,
import('@strudel.cycles/core'),
import('@strudel.cycles/mini'),
import('@strudel.cycles/webaudio'),
import('@strudel.cycles/tonal')
);
await prebake();
initAudioOnFirstClick();
}
if (typeof window !== 'undefined') { export function Repl({ tune }) {
init(); return <MiniRepl tune={tune} hideOutsideView={true} />;
}
export default function App() {
return <MiniRepl tune={`s("bd sd,hh*4")`} />;
} }
``` ```
- Open [example on stackblitz](https://stackblitz.com/edit/react-ts-saaair?file=tune.tsx,App.tsx) For a more sophisticated example, check out the [nano-repl](./examples/nano-repl/)!
- Also check out the [nano-repl](./examples/nano-repl/) for a more sophisticated example
@@ -1,15 +0,0 @@
# nano-repl
this is an example of how to create a repl with strudel and react.
## Usage
after cloning the strudel repo:
```sh
pnpm i
cd packages/react/examples/nano-repl
pnpm dev
```
you should now have a repl running at `http://localhost:5173/`
@@ -15,18 +15,17 @@
"@strudel.cycles/osc": "workspace:*", "@strudel.cycles/osc": "workspace:*",
"@strudel.cycles/mini": "workspace:*", "@strudel.cycles/mini": "workspace:*",
"@strudel.cycles/transpiler": "workspace:*", "@strudel.cycles/transpiler": "workspace:*",
"@strudel.cycles/soundfonts": "workspace:*",
"@strudel.cycles/webaudio": "workspace:*", "@strudel.cycles/webaudio": "workspace:*",
"@strudel.cycles/tonal": "workspace:*", "@strudel.cycles/tonal": "workspace:*",
"@strudel.cycles/react": "workspace:*" "@strudel.cycles/react": "workspace:*"
}, },
"devDependencies": { "devDependencies": {
"@types/react": "^18.2.0", "@types/react": "^18.0.17",
"@types/react-dom": "^18.2.1", "@types/react-dom": "^18.0.6",
"@vitejs/plugin-react": "^4.0.0", "@vitejs/plugin-react": "^2.0.1",
"autoprefixer": "^10.4.14", "autoprefixer": "^10.4.8",
"postcss": "^8.4.23", "postcss": "^8.4.16",
"tailwindcss": "^3.3.2", "tailwindcss": "^3.1.8",
"vite": "^4.3.3" "vite": "^3.0.7"
} }
} }
+18 -30
View File
@@ -1,34 +1,22 @@
import { controls, evalScope } from '@strudel.cycles/core'; import { controls, evalScope } from '@strudel.cycles/core';
import { CodeMirror, useHighlighting, useKeydown, useStrudel, flash } from '@strudel.cycles/react'; import { CodeMirror, useHighlighting, useKeydown, useStrudel, flash } from '@strudel.cycles/react';
import { import { getAudioContext, initAudioOnFirstClick, panic, webaudioOutput } from '@strudel.cycles/webaudio';
getAudioContext,
initAudioOnFirstClick,
panic,
webaudioOutput,
registerSynthSounds,
} from '@strudel.cycles/webaudio';
import { registerSoundfonts } from '@strudel.cycles/soundfonts';
import { useCallback, useState } from 'react'; import { useCallback, useState } from 'react';
import './style.css'; import './style.css';
// import { prebake } from '../../../../../repl/src/prebake.mjs'; // import { prebake } from '../../../../../repl/src/prebake.mjs';
initAudioOnFirstClick(); initAudioOnFirstClick();
async function init() { // TODO: only import stuff when play is pressed?
// TODO: only import stuff when play is pressed? evalScope(
const loadModules = evalScope( controls,
controls, import('@strudel.cycles/core'),
import('@strudel.cycles/core'), import('@strudel.cycles/tonal'),
import('@strudel.cycles/tonal'), import('@strudel.cycles/mini'),
import('@strudel.cycles/mini'), import('@strudel.cycles/xen'),
import('@strudel.cycles/xen'), import('@strudel.cycles/webaudio'),
import('@strudel.cycles/webaudio'), import('@strudel.cycles/osc'),
import('@strudel.cycles/osc'), );
);
await Promise.all([loadModules, registerSynthSounds(), registerSoundfonts()]);
}
init();
const defaultTune = `samples({ const defaultTune = `samples({
bd: ['bd/BT0AADA.wav','bd/BT0AAD0.wav','bd/BT0A0DA.wav','bd/BT0A0D3.wav','bd/BT0A0D0.wav','bd/BT0A0A7.wav'], bd: ['bd/BT0AADA.wav','bd/BT0AAD0.wav','bd/BT0A0DA.wav','bd/BT0A0D3.wav','bd/BT0A0D0.wav','bd/BT0A0A7.wav'],
@@ -43,7 +31,7 @@ stack(
.off(1/8,x=>x.add(12).degradeBy(.5)) // random octave jumps .off(1/8,x=>x.add(12).degradeBy(.5)) // random octave jumps
.add(perlin.range(0,.5)) // random pitch variation .add(perlin.range(0,.5)) // random pitch variation
.superimpose(add(.05)) // add second, slightly detuned voice .superimpose(add(.05)) // add second, slightly detuned voice
.note() // wrap in "note" .n() // wrap in "n"
.decay(.15).sustain(0) // make each note of equal length .decay(.15).sustain(0) // make each note of equal length
.s('sawtooth') // waveform .s('sawtooth') // waveform
.gain(.4) // turn down .gain(.4) // turn down
@@ -52,7 +40,7 @@ stack(
,"<Am7!3 <Em7 E7b13 Em7 Ebm7b5>>".voicings('lefthand') // chords ,"<Am7!3 <Em7 E7b13 Em7 Ebm7b5>>".voicings('lefthand') // chords
.superimpose(x=>x.add(.04)) // add second, slightly detuned voice .superimpose(x=>x.add(.04)) // add second, slightly detuned voice
.add(perlin.range(0,.5)) // random pitch variation .add(perlin.range(0,.5)) // random pitch variation
.note() // wrap in "n" .n() // wrap in "n"
.s('square') // waveform .s('square') // waveform
.gain(.16) // turn down .gain(.16) // turn down
.cutoff(500) // fixed cutoff .cutoff(500) // fixed cutoff
@@ -61,7 +49,7 @@ stack(
,"a4 c5 <e6 a6>".struct("x(5,8)") ,"a4 c5 <e6 a6>".struct("x(5,8)")
.superimpose(x=>x.add(.04)) // add second, slightly detuned voice .superimpose(x=>x.add(.04)) // add second, slightly detuned voice
.add(perlin.range(0,.5)) // random pitch variation .add(perlin.range(0,.5)) // random pitch variation
.note() // wrap in "note" .n() // wrap in "n"
.decay(.1).sustain(0) // make notes short .decay(.1).sustain(0) // make notes short
.s('triangle') // waveform .s('triangle') // waveform
.degradeBy(perlin.range(0,.5)) // randomly controlled random removal :) .degradeBy(perlin.range(0,.5)) // randomly controlled random removal :)
@@ -78,7 +66,7 @@ function App() {
const [code, setCode] = useState(defaultTune); const [code, setCode] = useState(defaultTune);
const [view, setView] = useState(); const [view, setView] = useState();
// const [code, setCode] = useState(`"c3".note().slow(2)`); // const [code, setCode] = useState(`"c3".note().slow(2)`);
const { scheduler, evaluate, schedulerError, evalError, isDirty, activeCode, pattern, started } = useStrudel({ const { scheduler, evaluate, schedulerError, evalError, isDirty, activeCode, pattern } = useStrudel({
code, code,
defaultOutput: webaudioOutput, defaultOutput: webaudioOutput,
getTime, getTime,
@@ -87,8 +75,8 @@ function App() {
useHighlighting({ useHighlighting({
view, view,
pattern, pattern,
active: started && !activeCode?.includes('strudel disable-highlighting'), active: !activeCode?.includes('strudel disable-highlighting'),
getTime: () => scheduler.now(), getTime: () => scheduler.getPhase(),
}); });
const error = evalError || schedulerError; const error = evalError || schedulerError;
@@ -115,7 +103,7 @@ function App() {
} }
} }
}, },
[scheduler, evaluate, view, code], [scheduler, evaluate, view],
), ),
); );
return ( return (
@@ -2,18 +2,6 @@
@tailwind components; @tailwind components;
@tailwind utilities; @tailwind utilities;
:root {
--background: #222;
--lineBackground: #22222250;
--foreground: #fff;
--caret: #ffcc00;
--selection: rgba(128, 203, 196, 0.5);
--selectionMatch: #036dd626;
--lineHighlight: #00000050;
--gutterBackground: transparent;
--gutterForeground: #8a919966;
}
body { body {
background: #123; background: #123;
} }
@@ -6,23 +6,9 @@ This program is free software: you can redistribute it and/or modify it under th
module.exports = { module.exports = {
// TODO: find out if leaving out tutorial path works now // TODO: find out if leaving out tutorial path works now
content: ['./src/**/*.{js,jsx,ts,tsx}', '../../src/**/*.{html,js,jsx,md,mdx,ts,tsx}'], content: ['./src/**/*.{js,jsx,ts,tsx}'],
theme: { theme: {
extend: { extend: {},
colors: {
// codemirror-theme settings
background: 'var(--background)',
lineBackground: 'var(--lineBackground)',
foreground: 'var(--foreground)',
caret: 'var(--caret)',
selection: 'var(--selection)',
selectionMatch: 'var(--selectionMatch)',
gutterBackground: 'var(--gutterBackground)',
gutterForeground: 'var(--gutterForeground)',
gutterBorder: 'var(--gutterBorder)',
lineHighlight: 'var(--lineHighlight)',
},
},
}, },
plugins: [], plugins: [],
}; };
+17 -20
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/react", "name": "@strudel.cycles/react",
"version": "0.8.0", "version": "0.6.0",
"description": "React components for strudel", "description": "React components for strudel",
"main": "src/index.js", "main": "src/index.js",
"publishConfig": { "publishConfig": {
@@ -32,33 +32,30 @@
}, },
"homepage": "https://github.com/tidalcycles/strudel#readme", "homepage": "https://github.com/tidalcycles/strudel#readme",
"dependencies": { "dependencies": {
"@codemirror/autocomplete": "^6.6.0", "@codemirror/lang-javascript": "^6.1.1",
"@codemirror/lang-javascript": "^6.1.7",
"@codemirror/state": "^6.2.0", "@codemirror/state": "^6.2.0",
"@codemirror/view": "^6.10.0", "@codemirror/view": "^6.7.3",
"@lezer/highlight": "^1.1.4", "@lezer/highlight": "^1.1.3",
"@replit/codemirror-emacs": "^6.0.1",
"@replit/codemirror-vim": "^6.0.14",
"@strudel.cycles/core": "workspace:*", "@strudel.cycles/core": "workspace:*",
"@strudel.cycles/transpiler": "workspace:*", "@strudel.cycles/transpiler": "workspace:*",
"@strudel.cycles/webaudio": "workspace:*", "@strudel.cycles/webaudio": "workspace:*",
"@uiw/codemirror-themes": "^4.19.16", "@uiw/codemirror-themes": "^4.12.4",
"@uiw/react-codemirror": "^4.19.16", "@uiw/react-codemirror": "^4.12.4",
"react-hook-inview": "^4.5.0" "react-hook-inview": "^4.5.0"
}, },
"peerDependencies": { "peerDependencies": {
"react": "^18.2.0", "react": "^17.0.2",
"react-dom": "^18.2.0" "react-dom": "^17.0.2"
}, },
"devDependencies": { "devDependencies": {
"@types/react": "^18.2.0", "@types/react": "^17.0.2",
"@types/react-dom": "^18.2.1", "@types/react-dom": "^17.0.2",
"@vitejs/plugin-react": "^4.0.0", "@vitejs/plugin-react": "^2.2.0",
"autoprefixer": "^10.4.14", "autoprefixer": "^10.4.7",
"postcss": "^8.4.23", "postcss": "^8.4.18",
"react": "^18.2.0", "react": "^17.0.2",
"react-dom": "^18.2.0", "react-dom": "^17.0.2",
"tailwindcss": "^3.3.2", "tailwindcss": "^3.0.24",
"vite": "^4.3.3" "vite": "^3.2.2"
} }
} }
+2
View File
@@ -8,6 +8,8 @@ evalScope(
import('@strudel.cycles/core'), import('@strudel.cycles/core'),
import('@strudel.cycles/tonal'), import('@strudel.cycles/tonal'),
import('@strudel.cycles/mini'), import('@strudel.cycles/mini'),
import('@strudel.cycles/midi'),
import('@strudel.cycles/xen'),
import('@strudel.cycles/webaudio'), import('@strudel.cycles/webaudio'),
); );
@@ -1,77 +0,0 @@
import { createRoot } from 'react-dom/client';
import jsdoc from '../../../../doc.json';
const getDocLabel = (doc) => doc.name || doc.longname;
const getInnerText = (html) => {
var div = document.createElement('div');
div.innerHTML = html;
return div.textContent || div.innerText || '';
};
export function Autocomplete({ doc }) {
return (
<div className="prose dark:prose-invert max-h-[400px] overflow-auto">
<h3 className="pt-0 mt-0">{getDocLabel(doc)}</h3>
<div dangerouslySetInnerHTML={{ __html: doc.description }} />
<ul>
{doc.params?.map(({ name, type, description }, i) => (
<li key={i}>
{name} : {type.names?.join(' | ')} {description ? <> - {getInnerText(description)}</> : ''}
</li>
))}
</ul>
<div>
{doc.examples?.map((example, i) => (
<div key={i}>
<pre
className="cursor-pointer"
onMouseDown={(e) => {
console.log('ola!');
navigator.clipboard.writeText(example);
e.stopPropagation();
}}
>
{example}
</pre>
</div>
))}
</div>
</div>
);
}
const jsdocCompletions = jsdoc.docs
.filter(
(doc) =>
getDocLabel(doc) &&
!getDocLabel(doc).startsWith('_') &&
!['package'].includes(doc.kind) &&
!['superdirtOnly', 'noAutocomplete'].some((tag) => doc.tags?.find((t) => t.originalTitle === tag)),
)
// https://codemirror.net/docs/ref/#autocomplete.Completion
.map((doc) /*: Completion */ => ({
label: getDocLabel(doc),
// detail: 'xxx', // An optional short piece of information to show (with a different style) after the label.
info: () => {
const node = document.createElement('div');
// if Autocomplete is non-interactive, it could also be rendered at build time..
// .. using renderToStaticMarkup
createRoot(node).render(<Autocomplete doc={doc} />);
return node;
},
type: 'function', // https://codemirror.net/docs/ref/#autocomplete.Completion.type
}));
export const strudelAutocomplete = (context /* : CompletionContext */) => {
let word = context.matchBefore(/\w*/);
if (word.from == word.to && !context.explicit) return null;
return {
from: word.from,
options: jsdocCompletions,
/* options: [
{ label: 'match', type: 'keyword' },
{ label: 'hello', type: 'variable', info: '(World)' },
{ label: 'magic', type: 'text', apply: '⠁⭒*.✩.*⭒⠁', detail: 'macro' },
], */
};
};
+10 -62
View File
@@ -1,15 +1,11 @@
import React, { useMemo } from 'react'; import React from 'react';
import _CodeMirror from '@uiw/react-codemirror'; import _CodeMirror from '@uiw/react-codemirror';
import { EditorView, Decoration } from '@codemirror/view'; import { EditorView, Decoration } from '@codemirror/view';
import { StateField, StateEffect } from '@codemirror/state'; import { StateField, StateEffect } from '@codemirror/state';
import { javascript, javascriptLanguage } from '@codemirror/lang-javascript'; import { javascript } from '@codemirror/lang-javascript';
import strudelTheme from '../themes/strudel-theme'; import strudelTheme from '../themes/strudel-theme';
import './style.css'; import './style.css';
import { useCallback } from 'react'; import { useCallback } from 'react';
import { autocompletion } from '@codemirror/autocomplete';
import { strudelAutocomplete } from './Autocomplete';
import { vim } from '@replit/codemirror-vim';
import { emacs } from '@replit/codemirror-emacs';
export const setFlash = StateEffect.define(); export const setFlash = StateEffect.define();
const flashField = StateField.define({ const flashField = StateField.define({
@@ -53,24 +49,19 @@ const highlightField = StateField.define({
try { try {
for (let e of tr.effects) { for (let e of tr.effects) {
if (e.is(setHighlights)) { if (e.is(setHighlights)) {
const { haps } = e.value;
const marks = const marks =
haps e.value
.map((hap) => .map((hap) =>
(hap.context.locations || []).map(({ start, end }) => { (hap.context.locations || []).map(({ start, end }) => {
const color = hap.context.color || e.value.color; const color = hap.context.color || '#FFCA28';
let from = tr.newDoc.line(start.line).from + start.column; let from = tr.newDoc.line(start.line).from + start.column;
let to = tr.newDoc.line(end.line).from + end.column; let to = tr.newDoc.line(end.line).from + end.column;
const l = tr.newDoc.length; const l = tr.newDoc.length;
if (from > l || to > l) { if (from > l || to > l) {
return; // dont mark outside of range, as it will throw an error return; // dont mark outside of range, as it will throw an error
} }
let mark; // const mark = Decoration.mark({ attributes: { style: `outline: 1px solid ${color}` } });
if (color) { const mark = Decoration.mark({ attributes: { style: `outline: 1.5px solid ${color};` } });
mark = Decoration.mark({ attributes: { style: `outline: 2px solid ${color};` } });
} else {
mark = Decoration.mark({ attributes: { class: `outline outline-2 outline-foreground` } });
}
return mark.range(from, to); return mark.range(from, to);
}), }),
) )
@@ -88,37 +79,21 @@ const highlightField = StateField.define({
provide: (f) => EditorView.decorations.from(f), provide: (f) => EditorView.decorations.from(f),
}); });
const staticExtensions = [javascript(), highlightField, flashField]; const extensions = [javascript(), strudelTheme, highlightField, flashField];
export default function CodeMirror({ export default function CodeMirror({ value, onChange, onViewChanged, onSelectionChange, options, editorDidMount }) {
value,
onChange,
onViewChanged,
onSelectionChange,
theme,
keybindings,
isLineNumbersDisplayed,
isAutoCompletionEnabled,
isLineWrappingEnabled,
fontSize = 18,
fontFamily = 'monospace',
options,
editorDidMount,
}) {
const handleOnChange = useCallback( const handleOnChange = useCallback(
(value) => { (value) => {
onChange?.(value); onChange?.(value);
}, },
[onChange], [onChange],
); );
const handleOnCreateEditor = useCallback( const handleOnCreateEditor = useCallback(
(view) => { (view) => {
onViewChanged?.(view); onViewChanged?.(view);
}, },
[onViewChanged], [onViewChanged],
); );
const handleOnUpdate = useCallback( const handleOnUpdate = useCallback(
(viewUpdate) => { (viewUpdate) => {
if (viewUpdate.selectionSet && onSelectionChange) { if (viewUpdate.selectionSet && onSelectionChange) {
@@ -127,43 +102,16 @@ export default function CodeMirror({
}, },
[onSelectionChange], [onSelectionChange],
); );
const extensions = useMemo(() => {
let _extensions = [...staticExtensions];
let bindings = {
vim,
emacs,
};
if (bindings[keybindings]) {
_extensions.push(bindings[keybindings]());
}
if (isAutoCompletionEnabled) {
_extensions.push(javascriptLanguage.data.of({ autocomplete: strudelAutocomplete }));
} else {
_extensions.push(autocompletion({ override: [] }));
}
if (isLineWrappingEnabled) {
_extensions.push(EditorView.lineWrapping);
}
return _extensions;
}, [keybindings, isAutoCompletionEnabled, isLineWrappingEnabled]);
return ( return (
<div style={{ fontSize, fontFamily }} className="w-full"> <>
<_CodeMirror <_CodeMirror
value={value} value={value}
theme={theme || strudelTheme}
onChange={handleOnChange} onChange={handleOnChange}
onCreateEditor={handleOnCreateEditor} onCreateEditor={handleOnCreateEditor}
onUpdate={handleOnUpdate} onUpdate={handleOnUpdate}
extensions={extensions} extensions={extensions}
basicSetup={{ lineNumbers: isLineNumbersDisplayed }}
/> />
</div> </>
); );
} }
+47 -91
View File
@@ -7,35 +7,18 @@ import useHighlighting from '../hooks/useHighlighting.mjs';
import useStrudel from '../hooks/useStrudel.mjs'; import useStrudel from '../hooks/useStrudel.mjs';
import CodeMirror6, { flash } from './CodeMirror6'; import CodeMirror6, { flash } from './CodeMirror6';
import { Icon } from './Icon'; import { Icon } from './Icon';
import styles from './MiniRepl.module.css';
import './style.css'; import './style.css';
import { logger } from '@strudel.cycles/core'; import { logger } from '@strudel.cycles/core';
import useEvent from '../hooks/useEvent.mjs';
import useKeydown from '../hooks/useKeydown.mjs';
const getTime = () => getAudioContext().currentTime; const getTime = () => getAudioContext().currentTime;
export function MiniRepl({ export function MiniRepl({ tune, hideOutsideView = false, enableKeyboard, drawTime, punchcard, canvasHeight = 200 }) {
tune,
hideOutsideView = false,
enableKeyboard,
onTrigger,
drawTime,
punchcard,
punchcardLabels,
onPaint,
canvasHeight = 200,
fontSize = 18,
fontFamily,
hideHeader = false,
theme,
keybindings,
isLineNumbersDisplayed,
}) {
drawTime = drawTime || (punchcard ? [0, 4] : undefined); drawTime = drawTime || (punchcard ? [0, 4] : undefined);
const evalOnMount = !!drawTime; const evalOnMount = !!drawTime;
const drawContext = useCallback( const drawContext = useCallback(
punchcard ? (canvasId) => document.querySelector('#' + canvasId)?.getContext('2d') : null, !!drawTime ? (canvasId) => document.querySelector('#' + canvasId)?.getContext('2d') : null,
[punchcard], [drawTime],
); );
const { const {
code, code,
@@ -55,18 +38,7 @@ export function MiniRepl({
} = useStrudel({ } = useStrudel({
initialCode: tune, initialCode: tune,
defaultOutput: webaudioOutput, defaultOutput: webaudioOutput,
editPattern: (pat, id) => { editPattern: (pat) => (punchcard ? pat.punchcard() : pat),
//pat = pat.withContext((ctx) => ({ ...ctx, id }));
if (onTrigger) {
pat = pat.onTrigger(onTrigger, false);
}
if (onPaint) {
pat = pat.onPaint(onPaint);
} else if (punchcard) {
pat = pat.punchcard({ labels: punchcardLabels });
}
return pat;
},
getTime, getTime,
evalOnMount, evalOnMount,
drawContext, drawContext,
@@ -91,26 +63,25 @@ export function MiniRepl({
getTime: () => scheduler.now(), getTime: () => scheduler.now(),
}); });
// keyboard shortcuts // set active pattern on ctrl+enter
useKeydown( useLayoutEffect(() => {
useCallback( if (enableKeyboard) {
async (e) => { const handleKeyPress = async (e) => {
if (view?.hasFocus) { if (e.ctrlKey || e.altKey) {
if (e.ctrlKey || e.altKey) { if (e.code === 'Enter') {
if (e.code === 'Enter') { e.preventDefault();
e.preventDefault(); flash(view);
flash(view); await activateCode();
await activateCode(); } else if (e.code === 'Period') {
} else if (e.key === '.' || e.code === 'Period') { stop();
stop(); e.preventDefault();
e.preventDefault();
}
} }
} }
}, };
[activateCode, stop, view], window.addEventListener('keydown', handleKeyPress, true);
), return () => window.removeEventListener('keydown', handleKeyPress, true);
); }
}, [enableKeyboard, pattern, code, evaluate, stop, view]);
const [log, setLog] = useState([]); const [log, setLog] = useState([]);
useLogger( useLogger(
@@ -120,54 +91,29 @@ export function MiniRepl({
// const logId = data?.pattern?.meta?.id; // const logId = data?.pattern?.meta?.id;
if (logId === replId) { if (logId === replId) {
setLog((l) => { setLog((l) => {
return l.concat([e.detail]).slice(-8); return l.concat([e.detail]).slice(-10);
}); });
} }
}, []), }, []),
); );
return ( return (
<div className="overflow-hidden rounded-t-md bg-background border border-lineHighlight" ref={ref}> <div className={styles.container} ref={ref}>
{!hideHeader && ( <div className={styles.header}>
<div className="flex justify-between bg-lineHighlight"> <div className={styles.buttons}>
<div className="flex"> <button className={cx(styles.button, started ? 'animate-pulse' : '')} onClick={() => togglePlay()}>
<button <Icon type={started ? 'stop' : 'play'} />
className={cx( </button>
'cursor-pointer w-16 flex items-center justify-center p-1 border-r border-lineHighlight text-foreground bg-lineHighlight hover:bg-background', <button className={cx(isDirty ? styles.button : styles.buttonDisabled)} onClick={() => activateCode()}>
started ? 'animate-pulse' : '', <Icon type="refresh" />
)} </button>
onClick={() => togglePlay()}
>
<Icon type={started ? 'stop' : 'play'} />
</button>
<button
className={cx(
'w-16 flex items-center justify-center p-1 text-foreground border-lineHighlight bg-lineHighlight',
isDirty ? 'text-foreground hover:bg-background cursor-pointer' : 'opacity-50 cursor-not-allowed',
)}
onClick={() => activateCode()}
>
<Icon type="refresh" />
</button>
</div>
</div> </div>
)} {error && <div className={styles.error}>{error.message}</div>}
<div className="overflow-auto relative">
{show && (
<CodeMirror6
value={code}
onChange={setCode}
onViewChanged={setView}
theme={theme}
fontFamily={fontFamily}
fontSize={fontSize}
keybindings={keybindings}
isLineNumbersDisplayed={isLineNumbersDisplayed}
/>
)}
{error && <div className="text-right p-1 text-md text-red-200">{error.message}</div>}
</div> </div>
{punchcard && ( <div className={styles.body}>
{show && <CodeMirror6 value={code} onChange={setCode} onViewChanged={setView} />}
</div>
{drawTime && (
<canvas <canvas
id={canvasId} id={canvasId}
className="w-full pointer-events-none" className="w-full pointer-events-none"
@@ -194,3 +140,13 @@ export function MiniRepl({
function useLogger(onTrigger) { function useLogger(onTrigger) {
useEvent(logger.key, onTrigger); useEvent(logger.key, onTrigger);
} }
// TODO: dedupe
function useEvent(name, onTrigger, useCapture = false) {
useEffect(() => {
document.addEventListener(name, onTrigger, useCapture);
return () => {
document.removeEventListener(name, onTrigger, useCapture);
};
}, [onTrigger]);
}
@@ -0,0 +1,27 @@
.container {
@apply rounded-md overflow-hidden bg-[#222222];
}
.header {
@apply flex justify-between bg-slate-700 border-t border-slate-500;
}
.buttons {
@apply flex;
}
.button {
@apply cursor-pointer w-16 flex items-center justify-center p-1 bg-slate-700 border-r border-slate-500 text-white hover:bg-slate-600;
}
.buttonDisabled {
@apply w-16 flex items-center justify-center p-1 bg-slate-600 text-slate-400 cursor-not-allowed;
}
.error {
@apply text-right p-1 text-sm text-red-200;
}
.body {
@apply overflow-auto relative;
}
+3 -19
View File
@@ -1,30 +1,14 @@
:root {
--background: #222;
--lineBackground: #22222299;
--foreground: #fff;
--caret: #ffcc00;
--selection: rgba(128, 203, 196, 0.5);
--selectionMatch: #036dd626;
--lineHighlight: #00000050;
--gutterBackground: transparent;
--gutterForeground: #8a919966;
}
.cm-editor { .cm-editor {
background-color: transparent !important; background-color: transparent !important;
height: 100%; height: 100%;
z-index: 11; z-index: 11;
} font-size: 18px;
.cm-theme {
width: 100%;
height: 100%;
} }
.cm-theme-light { .cm-theme-light {
width: 100%; width: 100%;
} }
footer { .cm-line > * {
z-index: 0 !important; background: #00000095;
} }
-12
View File
@@ -1,12 +0,0 @@
import { useEffect } from 'react';
function useEvent(name, onTrigger, useCapture = false) {
useEffect(() => {
document.addEventListener(name, onTrigger, useCapture);
return () => {
document.removeEventListener(name, onTrigger, useCapture);
};
}, [onTrigger]);
}
export default useEvent;
+5 -6
View File
@@ -1,6 +1,5 @@
import { useEffect, useRef } from 'react'; import { useEffect, useRef } from 'react';
import { setHighlights } from '../components/CodeMirror6'; import { setHighlights } from '../components/CodeMirror6';
const round = (x) => Math.round(x * 1000) / 1000;
function useHighlighting({ view, pattern, active, getTime }) { function useHighlighting({ view, pattern, active, getTime }) {
const highlights = useRef([]); const highlights = useRef([]);
@@ -15,14 +14,14 @@ function useHighlighting({ view, pattern, active, getTime }) {
// force min framerate of 10 fps => fixes crash on tab refocus, where lastEnd could be far away // force min framerate of 10 fps => fixes crash on tab refocus, where lastEnd could be far away
// see https://github.com/tidalcycles/strudel/issues/108 // see https://github.com/tidalcycles/strudel/issues/108
const begin = Math.max(lastEnd.current ?? audioTime, audioTime - 1 / 10, -0.01); // negative time seems buggy const begin = Math.max(lastEnd.current ?? audioTime, audioTime - 1 / 10, -0.01); // negative time seems buggy
const span = [round(begin), round(audioTime + 1 / 60)]; const span = [begin, audioTime + 1 / 60];
lastEnd.current = span[1]; lastEnd.current = span[1];
highlights.current = highlights.current.filter((hap) => hap.endClipped > audioTime); // keep only highlights that are still active highlights.current = highlights.current.filter((hap) => hap.whole.end > audioTime); // keep only highlights that are still active
const haps = pattern.queryArc(...span).filter((hap) => hap.hasOnset()); const haps = pattern.queryArc(...span).filter((hap) => hap.hasOnset());
highlights.current = highlights.current.concat(haps); // add potential new onsets highlights.current = highlights.current.concat(haps); // add potential new onsets
view.dispatch({ effects: setHighlights.of({ haps: highlights.current }) }); // highlight all still active + new active haps view.dispatch({ effects: setHighlights.of(highlights.current) }); // highlight all still active + new active haps
} catch (err) { } catch (err) {
view.dispatch({ effects: setHighlights.of({ haps: [] }) }); view.dispatch({ effects: setHighlights.of([]) });
} }
frame = requestAnimationFrame(updateHighlights); frame = requestAnimationFrame(updateHighlights);
}); });
@@ -31,7 +30,7 @@ function useHighlighting({ view, pattern, active, getTime }) {
}; };
} else { } else {
highlights.current = []; highlights.current = [];
view.dispatch({ effects: setHighlights.of({ haps: [] }) }); view.dispatch({ effects: setHighlights.of([]) });
} }
} }
}, [pattern, active, view]); }, [pattern, active, view]);
+10 -9
View File
@@ -10,6 +10,7 @@ function useStrudel({
getTime, getTime,
evalOnMount = false, evalOnMount = false,
initialCode = '', initialCode = '',
autolink = false,
beforeEval, beforeEval,
afterEval, afterEval,
editPattern, editPattern,
@@ -29,11 +30,10 @@ function useStrudel({
const [pattern, setPattern] = useState(); const [pattern, setPattern] = useState();
const [started, setStarted] = useState(false); const [started, setStarted] = useState(false);
const isDirty = code !== activeCode; const isDirty = code !== activeCode;
//const shouldPaint = useCallback((pat) => !!(pat?.context?.onPaint && drawContext), [drawContext]); const shouldPaint = useCallback((pat) => !!(pat?.context?.onPaint && drawContext), [drawContext]);
const shouldPaint = useCallback((pat) => !!pat?.context?.onPaint, []);
// TODO: make sure this hook reruns when scheduler.started changes // TODO: make sure this hook reruns when scheduler.started changes
const { scheduler, evaluate, start, stop, pause, setCps } = useMemo( const { scheduler, evaluate, start, stop, pause } = useMemo(
() => () =>
repl({ repl({
interval, interval,
@@ -47,17 +47,19 @@ function useStrudel({
drawContext, drawContext,
transpiler, transpiler,
editPattern, editPattern,
beforeEval: async ({ code }) => { beforeEval: ({ code }) => {
setCode(code); setCode(code);
await beforeEval?.(); beforeEval?.();
}, },
afterEval: (res) => { afterEval: ({ pattern: _pattern, code }) => {
const { pattern: _pattern, code } = res;
setActiveCode(code); setActiveCode(code);
setPattern(_pattern); setPattern(_pattern);
setEvalError(); setEvalError();
setSchedulerError(); setSchedulerError();
afterEval?.(res); if (autolink) {
window.location.hash = '#' + encodeURIComponent(btoa(code));
}
afterEval?.();
}, },
onToggle: (v) => { onToggle: (v) => {
setStarted(v); setStarted(v);
@@ -154,7 +156,6 @@ function useStrudel({
stop, stop,
pause, pause,
togglePlay, togglePlay,
setCps,
}; };
} }
+4 -6
View File
@@ -1,11 +1,9 @@
// import 'tailwindcss/tailwind.css'; // import 'tailwindcss/tailwind.css';
export { default as CodeMirror, flash } from './components/CodeMirror6'; // !SSR export { default as CodeMirror, flash } from './components/CodeMirror6';
export * from './components/MiniRepl'; // !SSR export * from './components/MiniRepl';
export { default as useHighlighting } from './hooks/useHighlighting'; // !SSR export { default as useHighlighting } from './hooks/useHighlighting';
export { default as useStrudel } from './hooks/useStrudel'; // !SSR
export { default as usePostMessage } from './hooks/usePostMessage'; export { default as usePostMessage } from './hooks/usePostMessage';
export { default as useStrudel } from './hooks/useStrudel';
export { default as useKeydown } from './hooks/useKeydown'; export { default as useKeydown } from './hooks/useKeydown';
export { default as useEvent } from './hooks/useEvent';
export { default as strudelTheme } from './themes/strudel-theme';
export { default as cx } from './cx'; export { default as cx } from './cx';
+3 -2
View File
@@ -1,9 +1,10 @@
import React from 'react'; import React from 'react';
import ReactDOM from 'react-dom';
import App from './App'; import App from './App';
import { createRoot } from 'react-dom/client';
createRoot(document.getElementById('root')).render( ReactDOM.render(
<React.StrictMode> <React.StrictMode>
<App /> <App />
</React.StrictMode>, </React.StrictMode>,
document.getElementById('root'),
); );
-39
View File
@@ -1,39 +0,0 @@
import { tags as t } from '@lezer/highlight';
import { createTheme } from '@uiw/codemirror-themes';
export const settings = {
background: '#9bbc0f',
foreground: '#0f380f', // whats that?
caret: '#0f380f',
selection: '#306230',
selectionMatch: '#ffffff26',
lineHighlight: '#8bac0f',
lineBackground: '#9bbc0f50',
//lineBackground: 'transparent',
gutterBackground: 'transparent',
gutterForeground: '#0f380f',
light: true,
};
export default createTheme({
theme: 'light',
settings,
styles: [
{ tag: t.keyword, color: '#0f380f' },
{ tag: t.operator, color: '#0f380f' },
{ tag: t.special(t.variableName), color: '#0f380f' },
{ tag: t.typeName, color: '#0f380f' },
{ tag: t.atom, color: '#0f380f' },
{ tag: t.number, color: '#0f380f' },
{ tag: t.definition(t.variableName), color: '#0f380f' },
{ tag: t.string, color: '#0f380f' },
{ tag: t.special(t.string), color: '#0f380f' },
{ tag: t.comment, color: '#0f380f' },
{ tag: t.variableName, color: '#0f380f' },
{ tag: t.tagName, color: '#0f380f' },
{ tag: t.bracket, color: '#0f380f' },
{ tag: t.meta, color: '#0f380f' },
{ tag: t.attributeName, color: '#0f380f' },
{ tag: t.propertyName, color: '#0f380f' },
{ tag: t.className, color: '#0f380f' },
{ tag: t.invalid, color: '#0f380f' },
],
});
-37
View File
@@ -1,37 +0,0 @@
import { tags as t } from '@lezer/highlight';
import { createTheme } from '@uiw/codemirror-themes';
export const settings = {
background: 'black',
foreground: 'white', // whats that?
caret: 'white',
selection: '#ffffff20',
selectionMatch: '#036dd626',
lineHighlight: '#ffffff10',
lineBackground: '#00000050',
gutterBackground: 'transparent',
gutterForeground: '#8a919966',
};
export default createTheme({
theme: 'dark',
settings,
styles: [
{ tag: t.keyword, color: 'white' },
{ tag: t.operator, color: 'white' },
{ tag: t.special(t.variableName), color: 'white' },
{ tag: t.typeName, color: 'white' },
{ tag: t.atom, color: 'white' },
{ tag: t.number, color: 'white' },
{ tag: t.definition(t.variableName), color: 'white' },
{ tag: t.string, color: 'white' },
{ tag: t.special(t.string), color: 'white' },
{ tag: t.comment, color: 'white' },
{ tag: t.variableName, color: 'white' },
{ tag: t.tagName, color: 'white' },
{ tag: t.bracket, color: 'white' },
{ tag: t.meta, color: 'white' },
{ tag: t.attributeName, color: 'white' },
{ tag: t.propertyName, color: 'white' },
{ tag: t.className, color: 'white' },
{ tag: t.invalid, color: 'white' },
],
});
-40
View File
@@ -1,40 +0,0 @@
import { tags as t } from '@lezer/highlight';
import { createTheme } from '@uiw/codemirror-themes';
export const settings = {
background: '#051DB5',
lineBackground: '#051DB550',
foreground: 'white', // whats that?
caret: 'white',
selection: 'rgba(128, 203, 196, 0.5)',
selectionMatch: '#036dd626',
// lineHighlight: '#8a91991a', // original
lineHighlight: '#00000050',
gutterBackground: 'transparent',
// gutterForeground: '#8a919966',
gutterForeground: '#8a919966',
};
export default createTheme({
theme: 'dark',
settings,
styles: [
{ tag: t.keyword, color: 'white' },
{ tag: t.operator, color: 'white' },
{ tag: t.special(t.variableName), color: 'white' },
{ tag: t.typeName, color: 'white' },
{ tag: t.atom, color: 'white' },
{ tag: t.number, color: 'white' },
{ tag: t.definition(t.variableName), color: 'white' },
{ tag: t.string, color: 'white' },
{ tag: t.special(t.string), color: 'white' },
{ tag: t.comment, color: 'white' },
{ tag: t.variableName, color: 'white' },
{ tag: t.tagName, color: 'white' },
{ tag: t.bracket, color: 'white' },
{ tag: t.meta, color: 'white' },
{ tag: t.attributeName, color: 'white' },
{ tag: t.propertyName, color: 'white' },
{ tag: t.className, color: 'white' },
{ tag: t.invalid, color: 'white' },
],
});
-36
View File
@@ -1,36 +0,0 @@
import { tags as t } from '@lezer/highlight';
import { createTheme } from '@uiw/codemirror-themes';
export const settings = {
background: 'black',
foreground: '#41FF00', // whats that?
caret: '#41FF00',
selection: '#ffffff20',
selectionMatch: '#036dd626',
lineHighlight: '#ffffff10',
gutterBackground: 'transparent',
gutterForeground: '#8a919966',
};
export default createTheme({
theme: 'dark',
settings,
styles: [
{ tag: t.keyword, color: '#41FF00' },
{ tag: t.operator, color: '#41FF00' },
{ tag: t.special(t.variableName), color: '#41FF00' },
{ tag: t.typeName, color: '#41FF00' },
{ tag: t.atom, color: '#41FF00' },
{ tag: t.number, color: '#41FF00' },
{ tag: t.definition(t.variableName), color: '#41FF00' },
{ tag: t.string, color: '#41FF00' },
{ tag: t.special(t.string), color: '#41FF00' },
{ tag: t.comment, color: '#41FF00' },
{ tag: t.variableName, color: '#41FF00' },
{ tag: t.tagName, color: '#41FF00' },
{ tag: t.bracket, color: '#41FF00' },
{ tag: t.meta, color: '#41FF00' },
{ tag: t.attributeName, color: '#41FF00' },
{ tag: t.propertyName, color: '#41FF00' },
{ tag: t.className, color: '#41FF00' },
{ tag: t.invalid, color: '#41FF00' },
],
});
-38
View File
@@ -1,38 +0,0 @@
import { tags as t } from '@lezer/highlight';
import { createTheme } from '@uiw/codemirror-themes';
export const settings = {
background: 'white',
foreground: 'black', // whats that?
caret: 'black',
selection: 'rgba(128, 203, 196, 0.5)',
selectionMatch: '#ffffff26',
lineHighlight: '#cccccc50',
lineBackground: '#ffffff50',
gutterBackground: 'transparent',
gutterForeground: 'black',
light: true,
};
export default createTheme({
theme: 'light',
settings,
styles: [
{ tag: t.keyword, color: 'black' },
{ tag: t.operator, color: 'black' },
{ tag: t.special(t.variableName), color: 'black' },
{ tag: t.typeName, color: 'black' },
{ tag: t.atom, color: 'black' },
{ tag: t.number, color: 'black' },
{ tag: t.definition(t.variableName), color: 'black' },
{ tag: t.string, color: 'black' },
{ tag: t.special(t.string), color: 'black' },
{ tag: t.comment, color: 'black' },
{ tag: t.variableName, color: 'black' },
{ tag: t.tagName, color: 'black' },
{ tag: t.bracket, color: 'black' },
{ tag: t.meta, color: 'black' },
{ tag: t.attributeName, color: 'black' },
{ tag: t.propertyName, color: 'black' },
{ tag: t.className, color: 'black' },
{ tag: t.invalid, color: 'black' },
],
});
+1 -15
View File
@@ -1,21 +1,7 @@
module.exports = { module.exports = {
content: ['./src/**/*.{js,jsx,ts,tsx}'], content: ['./src/**/*.{js,jsx,ts,tsx}'],
theme: { theme: {
extend: { extend: {},
colors: {
// codemirror-theme settings
background: 'var(--background)',
lineBackground: 'var(--lineBackground)',
foreground: 'var(--foreground)',
caret: 'var(--caret)',
selection: 'var(--selection)',
selectionMatch: 'var(--selectionMatch)',
gutterBackground: 'var(--gutterBackground)',
gutterForeground: 'var(--gutterForeground)',
gutterBorder: 'var(--gutterBorder)',
lineHighlight: 'var(--lineHighlight)',
},
},
}, },
plugins: [], plugins: [],
corePlugins: { corePlugins: {
+2 -2
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/serial", "name": "@strudel.cycles/serial",
"version": "0.8.0", "version": "0.6.0",
"description": "Webserial API for strudel", "description": "Webserial API for strudel",
"main": "serial.mjs", "main": "serial.mjs",
"publishConfig": { "publishConfig": {
@@ -32,6 +32,6 @@
"@strudel.cycles/core": "workspace:*" "@strudel.cycles/core": "workspace:*"
}, },
"devDependencies": { "devDependencies": {
"vite": "^4.3.3" "vite": "^3.2.2"
} }
} }
+7 -46
View File
@@ -1,6 +1,4 @@
import { noteToMidi, freqToMidi } from '@strudel.cycles/core'; import { toMidi } from '@strudel.cycles/core';
import { getAudioContext, registerSound, getEnvelope } from '@strudel.cycles/webaudio';
import gm from './gm.mjs';
let loadCache = {}; let loadCache = {};
async function loadFont(name) { async function loadFont(name) {
@@ -10,6 +8,7 @@ async function loadFont(name) {
const load = async () => { const load = async () => {
// TODO: make soundfont source configurable // TODO: make soundfont source configurable
const url = `https://felixroos.github.io/webaudiofontdata/sound/${name}.js`; const url = `https://felixroos.github.io/webaudiofontdata/sound/${name}.js`;
console.log('load font', name, url);
const preset = await fetch(url).then((res) => res.text()); const preset = await fetch(url).then((res) => res.text());
let [_, data] = preset.split('={'); let [_, data] = preset.split('={');
return eval('{' + data); return eval('{' + data);
@@ -18,24 +17,15 @@ async function loadFont(name) {
return loadCache[name]; return loadCache[name];
} }
export async function getFontBufferSource(name, value, ac) { export async function getFontBufferSource(name, pitch, ac) {
let { note = 'c3', freq } = value; if (typeof pitch === 'string') {
let midi; pitch = toMidi(pitch);
if (freq) {
midi = freqToMidi(freq);
} else if (typeof note === 'string') {
midi = noteToMidi(note);
} else if (typeof note === 'number') {
midi = note;
} else {
throw new Error(`unexpected "note" type "${typeof note}"`);
} }
const { buffer, zone } = await getFontPitch(name, pitch, ac);
const { buffer, zone } = await getFontPitch(name, midi, ac);
const src = ac.createBufferSource(); const src = ac.createBufferSource();
src.buffer = buffer; src.buffer = buffer;
const baseDetune = zone.originalPitch - 100.0 * zone.coarseTune - zone.fineTune; const baseDetune = zone.originalPitch - 100.0 * zone.coarseTune - zone.fineTune;
const playbackRate = 1.0 * Math.pow(2, (100.0 * midi - baseDetune) / 1200.0); const playbackRate = 1.0 * Math.pow(2, (100.0 * pitch - baseDetune) / 1200.0);
// src detune? // src detune?
src.playbackRate.value = playbackRate; src.playbackRate.value = playbackRate;
const loop = zone.loopStart > 1 && zone.loopStart < zone.loopEnd; const loop = zone.loopStart > 1 && zone.loopStart < zone.loopEnd;
@@ -124,32 +114,3 @@ async function getBuffer(zone, audioContext) {
} }
} }
} }
export function registerSoundfonts() {
Object.entries(gm).forEach(([name, fonts]) => {
registerSound(
name,
async (time, value, onended) => {
const { n = 0 } = value;
const { attack = 0.001, decay = 0.001, sustain = 1, release = 0.001 } = value;
const font = fonts[n % fonts.length];
const ctx = getAudioContext();
const bufferSource = await getFontBufferSource(font, value, ctx);
bufferSource.start(time);
const { node: envelope, stop: releaseEnvelope } = getEnvelope(attack, decay, sustain, release, 0.3, time);
bufferSource.connect(envelope);
const stop = (releaseTime) => {
bufferSource.stop(releaseTime + release);
releaseEnvelope(releaseTime);
};
bufferSource.onended = () => {
bufferSource.disconnect();
envelope.disconnect();
onended();
};
return { node: envelope, stop };
},
{ type: 'soundfont', prebake: true, fonts },
);
});
}
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -1,6 +1,6 @@
import { getFontBufferSource, registerSoundfonts } from './fontloader.mjs'; import { getFontBufferSource } from './fontloader.mjs';
import * as soundfontList from './list.mjs'; import * as soundfontList from './list.mjs';
import { startPresetNote } from 'sfumato'; import { startPresetNote } from 'sfumato';
import { loadSoundfont } from './sfumato.mjs'; import { loadSoundfont } from './sfumato.mjs';
export { loadSoundfont, startPresetNote, getFontBufferSource, soundfontList, registerSoundfonts }; export { loadSoundfont, startPresetNote, getFontBufferSource, soundfontList };
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/soundfonts", "name": "@strudel.cycles/soundfonts",
"version": "0.8.2", "version": "0.6.0",
"description": "Soundsfont support for strudel", "description": "Soundsfont support for strudel",
"main": "index.mjs", "main": "index.mjs",
"publishConfig": { "publishConfig": {
@@ -36,7 +36,7 @@
"soundfont2": "^0.4.0" "soundfont2": "^0.4.0"
}, },
"devDependencies": { "devDependencies": {
"node-fetch": "^3.3.1", "node-fetch": "^3.2.6",
"vite": "^4.3.3" "vite": "^3.2.2"
} }
} }
+3 -27
View File
@@ -1,5 +1,5 @@
import { Pattern, getPlayableNoteValue, noteToMidi } from '@strudel.cycles/core'; import { Pattern, getPlayableNoteValue, toMidi } from '@strudel.cycles/core';
import { getAudioContext, registerSound } from '@strudel.cycles/webaudio'; import { getAudioContext } from '@strudel.cycles/webaudio';
import { loadSoundfont as _loadSoundfont, startPresetNote } from 'sfumato'; import { loadSoundfont as _loadSoundfont, startPresetNote } from 'sfumato';
Pattern.prototype.soundfont = function (sf, n = 0) { Pattern.prototype.soundfont = function (sf, n = 0) {
@@ -8,7 +8,7 @@ Pattern.prototype.soundfont = function (sf, n = 0) {
const note = getPlayableNoteValue(h); const note = getPlayableNoteValue(h);
const preset = sf.presets[n % sf.presets.length]; const preset = sf.presets[n % sf.presets.length];
const deadline = ctx.currentTime + t - ct; const deadline = ctx.currentTime + t - ct;
const args = [ctx, preset, noteToMidi(note), deadline]; const args = [ctx, preset, toMidi(note), deadline];
const stop = startPresetNote(...args); const stop = startPresetNote(...args);
stop(deadline + h.duration); stop(deadline + h.duration);
}); });
@@ -21,29 +21,5 @@ export function loadSoundfont(url) {
} }
const sf = _loadSoundfont(url); const sf = _loadSoundfont(url);
soundfontCache.set(url, sf); soundfontCache.set(url, sf);
/*sf.then((font) => {
font.presets.forEach((preset) => {
console.log('preset', preset.header.name);
registerSound(
preset.header.name.replaceAll(' ', '_'),
(time, value, onended) => {
const ctx = getAudioContext();
let { note } = value; // freq ?
const p = font.presets.find((p) => p.header.name === preset.header.name);
if (!p) {
throw new Error('preset not found');
}
const deadline = time; // - ctx.currentTime;
const args = [ctx, p, noteToMidi(note), deadline];
const stop = startPresetNote(...args);
return { node: undefined, stop };
},
{ type: 'soundfont' },
);
});
//console.log('f', f);
});*/
return sf; return sf;
} }
+4 -4
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/tonal", "name": "@strudel.cycles/tonal",
"version": "0.8.2", "version": "0.6.0",
"description": "Tonal functions for strudel", "description": "Tonal functions for strudel",
"main": "index.mjs", "main": "index.mjs",
"publishConfig": { "publishConfig": {
@@ -34,10 +34,10 @@
"@strudel.cycles/core": "workspace:*", "@strudel.cycles/core": "workspace:*",
"@tonaljs/tonal": "^4.7.2", "@tonaljs/tonal": "^4.7.2",
"chord-voicings": "^0.0.1", "chord-voicings": "^0.0.1",
"webmidi": "^3.1.5" "webmidi": "^3.0.21"
}, },
"devDependencies": { "devDependencies": {
"vite": "^4.3.3", "vite": "^3.2.2",
"vitest": "^0.28.0" "vitest": "^0.25.7"
} }
} }
-15
View File
@@ -9,7 +9,6 @@ This program is free software: you can redistribute it and/or modify it under th
import '../tonal.mjs'; // need to import this to add prototypes import '../tonal.mjs'; // need to import this to add prototypes
import { pure, controls, seq } from '@strudel.cycles/core'; import { pure, controls, seq } from '@strudel.cycles/core';
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from 'vitest';
import { mini } from '../../mini/mini.mjs';
const { n } = controls; const { n } = controls;
describe('tonal', () => { describe('tonal', () => {
@@ -31,18 +30,4 @@ describe('tonal', () => {
.firstCycleValues.map((h) => h.note), .firstCycleValues.map((h) => h.note),
).toEqual(['C3', 'D3', 'E3']); ).toEqual(['C3', 'D3', 'E3']);
}); });
it('scale with colon', () => {
expect(
n(0, 1, 2)
.scale('C:major')
.firstCycleValues.map((h) => h.note),
).toEqual(['C3', 'D3', 'E3']);
});
it('scale with mininotation colon', () => {
expect(
n(0, 1, 2)
.scale(mini('C:major'))
.firstCycleValues.map((h) => h.note),
).toEqual(['C3', 'D3', 'E3']);
});
}); });
+11
View File
@@ -0,0 +1,11 @@
import { describe, it, expect } from 'vitest';
import { Note } from '../tonleiter.mjs';
describe('tonleiter', () => {
it('Should tokenize notes', () => {
expect(Note.tokenize('c')).toEqual(['c', '']);
expect(Note.tokenize('C')).toEqual(['C', '']);
expect(Note.tokenize('c#')).toEqual(['c', '#']);
expect(Note.tokenize('Bb')).toEqual(['B', 'b']);
});
});
+9 -15
View File
@@ -123,40 +123,34 @@ export const scaleTranspose = register('scaleTranspose', function (offset /* : n
/** /**
* Turns numbers into notes in the scale (zero indexed). Also sets scale for other scale operations, like {@link Pattern#scaleTranspose}. * Turns numbers into notes in the scale (zero indexed). Also sets scale for other scale operations, like {@link Pattern#scaleTranspose}.
* *
* A scale consists of a root note (e.g. `c4`, `c`, `f#`, `bb4`) followed by semicolon (':') and then a [scale type](https://github.com/tonaljs/tonal/blob/main/packages/scale-type/data.ts). * The scale name has the form "TO? N" wher
* *
* The root note defaults to octave 3, if no octave number is given. * - T = Tonic
* - O = Octave (optional, defaults to 3)
* - N = Name of scale, available names can be found [here](https://github.com/tonaljs/tonal/blob/main/packages/scale-type/data.ts).
* *
* @memberof Pattern * @memberof Pattern
* @name scale * @name scale
* @param {string} scale Name of scale * @param {string} scale Name of scale
* @returns Pattern * @returns Pattern
* @example * @example
* "0 2 4 6 4 2".scale("C2:major").note()
* @example
* "0 2 4 6 4 2" * "0 2 4 6 4 2"
* .scale("C2:<major minor>") * .scale(seq('C2 major', 'C2 minor').slow(2))
* .note() * .note()
* @example
* "0 1 2 3 4 5 6 7".rev().scale("C2:<major minor>").note()
* .s("folkharp")
*/ */
export const scale = register('scale', function (scale, pat) { export const scale = register('scale', function (scale /* : string */, pat) {
// Supports ':' list syntax in mininotation
if (Array.isArray(scale)) {
scale = scale.flat().join(' ');
}
return pat.withHap((hap) => { return pat.withHap((hap) => {
const isObject = typeof hap.value === 'object'; const isObject = typeof hap.value === 'object';
let note = isObject ? hap.value.n : hap.value; let note = isObject ? hap.value.n : hap.value;
const asNumber = Number(note); const asNumber = Number(note);
if (!isNaN(asNumber)) { if (!isNaN(asNumber)) {
// TODO: worth keeping for supporting ':' in (non-mininotation) strings?
scale = scale.replaceAll(':', ' ');
let [tonic, scaleName] = Scale.tokenize(scale); let [tonic, scaleName] = Scale.tokenize(scale);
const { pc, oct = 3 } = Note.get(tonic); const { pc, oct = 3 } = Note.get(tonic);
note = scaleOffset(pc + ' ' + scaleName, asNumber, pc + oct); note = scaleOffset(pc + ' ' + scaleName, asNumber, pc + oct);
/* const scaleWIthOctave = `${pc + oct} ${scaleName}`;
const degree = Scale.degrees(scaleWIthOctave);
note = degree(asNumber + (asNumber >= 0 ? 1 : 0)); */
} }
return hap.withValue(() => (isObject ? { ...hap.value, note } : note)).setContext({ ...hap.context, scale }); return hap.withValue(() => (isObject ? { ...hap.value, note } : note)).setContext({ ...hap.context, scale });
}); });
+71
View File
@@ -0,0 +1,71 @@
const steps = [1, 0, 2, 0, 3, 4, 0, 5, 0, 6, 0, 7];
const notes = ['C', '', 'D', '', 'E', 'F', '', 'G', '', 'A', '', 'B'];
const noteLetters = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
const accidentalOffset = (accidentals) => {
return accidentals.split('#').length - accidentals.split('b').length;
};
const accidentalString = (offset) => {
if (offset < 0) {
return 'b'.repeat(-offset);
}
if (offset > 0) {
return '#'.repeat(offset);
}
return '';
};
export const Step = {
tokenize(step) {
const [accidentals, stepNumber] = step.match(/^([#b]*)([1-9]+)$/).slice(1);
return [accidentals, parseInt(stepNumber)];
},
accidentals(step) {
return accidentalOffset(Step.tokenize(step)[0]);
},
};
Step.tokenize('#11');
Step.tokenize('b13');
Step.tokenize('bb6');
Step.accidentals('3');
Step.accidentals('b3');
export const Note = {
// TODO: support octave numbers
tokenize(note) {
return [note[0], note.slice(1)];
},
accidentals(note) {
return accidentalOffset(this.tokenize(note)[1]);
},
};
Note.tokenize('C##');
Note.accidentals('C#');
Note.accidentals('C##');
Note.accidentals('Eb');
Note.accidentals('Bbb');
// TODO: support octave numbers
function transpose(note, step) {
// example: E, 3
const stepNumber = Step.tokenize(step)[1]; // 3
const noteLetter = Note.tokenize(note)[0]; // E
const noteIndex = noteLetters.indexOf(noteLetter); // 2 "E is C+2"
const targetNote = noteLetters[(noteIndex + stepNumber - 1) % 8]; // G "G is a third above E"
const rootIndex = notes.indexOf(noteLetter); // 4 "E is 4 semitones above C"
const targetIndex = notes.indexOf(targetNote); // 7 "G is 7 semitones above C"
const indexOffset = targetIndex - rootIndex; // 3 (E to G is normally a 3 semitones)
const stepIndex = steps.indexOf(stepNumber); // 4 ("3" is normally 4 semitones)
const offsetAccidentals = accidentalString(Step.accidentals(step) + Note.accidentals(note) + stepIndex - indexOffset); // "we need to add a # to to the G to make it a major third from E"
return [targetNote, offsetAccidentals].join('');
}
transpose('F#', '3');
transpose('C', '3');
transpose('D', '3');
transpose('E', '3');
transpose('Eb', '3');
transpose('Ebb', '3');
+1 -44
View File
@@ -6,53 +6,10 @@ This program is free software: you can redistribute it and/or modify it under th
import { stack, register } from '@strudel.cycles/core'; import { stack, register } from '@strudel.cycles/core';
import _voicings from 'chord-voicings'; import _voicings from 'chord-voicings';
const { dictionaryVoicing, minTopNoteDiff } = _voicings.default || _voicings; // parcel module resolution fuckup const { dictionaryVoicing, minTopNoteDiff, lefthand } = _voicings.default || _voicings; // parcel module resolution fuckup
const lefthand = {
m7: ['3m 5P 7m 9M', '7m 9M 10m 12P'],
7: ['3M 6M 7m 9M', '7m 9M 10M 13M'],
'^7': ['3M 5P 7M 9M', '7M 9M 10M 12P'],
69: ['3M 5P 6A 9M'],
m7b5: ['3m 5d 7m 8P', '7m 8P 10m 12d'],
'7b9': ['3M 6m 7m 9m', '7m 9m 10M 13m'],
'7b13': ['3M 6m 7m 9m', '7m 9m 10M 13m'],
o7: ['1P 3m 5d 6M', '5d 6M 8P 10m'],
'7#11': ['7m 9M 11A 13A'],
'7#9': ['3M 7m 9A'],
mM7: ['3m 5P 7M 9M', '7M 9M 10m 12P'],
m6: ['3m 5P 6M 9M', '6M 9M 10m 12P'],
};
const guidetones = {
m7: ['3m 7m', '7m 10m'],
m9: ['3m 7m', '7m 10m'],
7: ['3M 7m', '7m 10M'],
'^7': ['3M 7M', '7M 10M'],
'^9': ['3M 7M', '7M 10M'],
69: ['3M 6M'],
6: ['3M 6M', '6M 10M'],
m7b5: ['3m 7m', '7m 10m'],
'7b9': ['3M 7m', '7m 10M'],
'7b13': ['3M 7m', '7m 10M'],
o7: ['3m 6M', '6M 10m'],
'7#11': ['3M 7m', '7m 10M'],
'7#9': ['3M 7m', '7m 10M'],
mM7: ['3m 7M', '7M 10m'],
m6: ['3m 6M', '6M 10m'],
};
const triads = {
'': ['1P 3M 5P', '3M 5P 8P', '5P 8P 10M'],
M: ['1P 3M 5P', '3M 5P 8P', '5P 8P 10M'],
m: ['1P 3m 5P', '3m 5P 8P', '5P 8P 10m'],
o: ['1P 3m 5d', '3m 5d 8P', '5d 8P 10m'],
aug: ['1P 3m 5A', '3m 5A 8P', '5A 8P 10m'],
};
export const voicingRegistry = { export const voicingRegistry = {
lefthand: { dictionary: lefthand, range: ['F3', 'A4'] }, lefthand: { dictionary: lefthand, range: ['F3', 'A4'] },
triads: { dictionary: triads },
guidetones: { dictionary: guidetones },
}; };
export const setVoicingRange = (name, range) => addVoicings(name, voicingRegistry[name].dictionary, range); export const setVoicingRange = (name, range) => addVoicings(name, voicingRegistry[name].dictionary, range);
+3 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/tone", "name": "@strudel.cycles/tone",
"version": "0.8.0", "version": "0.6.0",
"description": "Tone.js API for strudel", "description": "Tone.js API for strudel",
"main": "index.mjs", "main": "index.mjs",
"type": "module", "type": "module",
@@ -34,7 +34,7 @@
"tone": "^14.7.77" "tone": "^14.7.77"
}, },
"devDependencies": { "devDependencies": {
"vite": "^4.3.3", "vite": "^3.2.2",
"vitest": "^0.28.0" "vitest": "^0.25.7"
} }
} }
+3 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "@strudel.cycles/transpiler", "name": "@strudel.cycles/transpiler",
"version": "0.8.2", "version": "0.6.0",
"description": "Transpiler for strudel user code. Converts syntactically correct but semantically meaningless JS into evaluatable strudel code.", "description": "Transpiler for strudel user code. Converts syntactically correct but semantically meaningless JS into evaluatable strudel code.",
"main": "index.mjs", "main": "index.mjs",
"publishConfig": { "publishConfig": {
@@ -36,7 +36,7 @@
"estree-walker": "^3.0.1" "estree-walker": "^3.0.1"
}, },
"devDependencies": { "devDependencies": {
"vite": "^4.3.3", "vite": "^3.2.2",
"vitest": "^0.28.0" "vitest": "^0.25.8"
} }
} }
-82
View File
@@ -1,82 +0,0 @@
# @strudel/web
This package provides an easy to use bundle of multiple strudel packages for the web.
## Usage
Save this code as a `.html` file and double click it:
```html
<!DOCTYPE html>
<button id="play">play</button>
<button id="stop">stop</button>
<script type="module">
import { initStrudel } from 'https://cdn.skypack.dev/@strudel/web@0.8.2';
initStrudel();
document.getElementById('play').addEventListener('click', () => note('<c a f e>(3,8)').play());
document.getElementById('stop').addEventListener('click', () => hush());
</script>
```
With the help of [skypack](https://www.skypack.dev/), you don't need a bundler nor a server.
As soon as you call `initStrudel()`, all strudel functions are made available.
In this case, we are using the `note` function to create a pattern.
To actually play the pattern, you have to append `.play()` to the end.
Note: Due to the [Autoplay policy](https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Best_practices#autoplay_policy), you can only play audio in a browser after a click event.
### Via npm
If you're using a bundler, you can install the package via `npm i @strudel/web`, then just import it like:
```js
import { initStrudel } from '@strudel/web';
```
The rest of the code should be the same. Check out [vite](https://vitejs.dev/) for a good bundler / dev server.
### Loading samples
By default, no external samples are loaded, but you can add them like this:
```js
initStrudel({
prebake: () => samples('github:tidalcycles/Dirt-Samples/master'),
});
document.getElementById('play').addEventListener('click',
() => s("bd sd").play()
)
```
You can learn [more about the `samples` function here](https://strudel.tidalcycles.org/learn/samples#loading-custom-samples).
### Evaluating Code
Instead of creating patterns directly in JS, you might also want to take in user input and turn that into a pattern.
This is called evaluation: Taking a piece of code and executing it on the fly.
To do that, you can use the `evaluate` function:
```js
initStrudel();
document.getElementById('play').addEventListener('click',
() => evaluate('note("c a f e").jux(rev)')
);
document.getElementById('play').addEventListener('stop',
() => hush()
);
```
### Double vs Single Quotes
There is a tiny difference between the [Strudel REPL](https://strudel.tidalcycles.org/) and `@strudel/web`.
In the REPL you can use 'single quotes' for regular JS strings and "double quotes" for mini notation patterns.
In `@strudel/web`, it does not matter which types of quotes you're using.
There will probably be an escapte hatch for that in the future.
## More Examples
Check out the examples folder for more examples, both using plain html and vite!
-10
View File
@@ -1,10 +0,0 @@
<!DOCTYPE html>
<button id="play">play</button>
<button id="stop">stop</button>
<script type="module">
import { initStrudel } from 'https://cdn.skypack.dev/@strudel/web@0.8.2';
initStrudel();
document.getElementById('play').addEventListener('click', () => evaluate('note("c a f e").jux(rev)'));
document.getElementById('play').addEventListener('stop', () => hush());
</script>
@@ -1,16 +0,0 @@
<!DOCTYPE html>
<button id="a">A</button>
<button id="b">B</button>
<button id="c">C</button>
<button id="stop">stop</button>
<script type="module">
import { initStrudel } from 'https://cdn.skypack.dev/@strudel/web@0.8.2';
initStrudel({
prebake: () => samples('github:tidalcycles/Dirt-Samples/master'),
});
const click = (id, action) => document.getElementById(id).addEventListener('click', action);
click('a', () => evaluate(`s('bd,jvbass(3,8)').jux(rev)`));
click('b', () => s('bd*2,hh(3,4),jvbass(5,8,1)').jux(rev).play());
click('c', () => s('bd*2,hh(3,4),jvbass:[0 4](5,8,1)').jux(rev).stack(s('~ sd')).play());
click('stop', () => hush());
</script>
-10
View File
@@ -1,10 +0,0 @@
<!DOCTYPE html>
<button id="play">play</button>
<button id="stop">stop</button>
<script type="module">
import { initStrudel } from 'https://cdn.skypack.dev/@strudel/web@0.8.2';
initStrudel();
document.getElementById('play').addEventListener('click', () => note('<c a f e>(3,8)').play());
document.getElementById('stop').addEventListener('click', () => hush());
</script>
@@ -1,24 +0,0 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
@@ -1,28 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="https://strudel.tidalcycles.org/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@strudel/web REPL Example</title>
</head>
<body>
<div id="app"></div>
<button id="a">A</button>
<button id="b">B</button>
<button id="c">C</button>
<button id="stop">stop</button>
<script type="module">
import { initStrudel } from '@strudel/web';
initStrudel({
prebake: () => samples('github:tidalcycles/Dirt-Samples/master'),
});
const click = (id, action) => document.getElementById(id).addEventListener('click', action);
click('a', () => evaluate(`s('bd,jvbass(3,8)').jux(rev)`));
click('b', () => s('bd*2,hh(3,4),jvbass(5,8,1)').jux(rev).play());
click('c', () => s('bd*2,hh(3,4),jvbass:[0 4](5,8,1)').jux(rev).stack(s('~ sd')).play());
click('stop', () => hush());
</script>
</body>
</html>

Some files were not shown because too many files have changed in this diff Show More