This commit is contained in:
Felix Roos 2022-02-06 21:00:29 +01:00
parent 27be90358f
commit b91eeb72f0
6 changed files with 1832 additions and 38 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,6 @@
{
"imports": {
"@tonaljs/tonal": "./@tonaljs/tonal.js",
"fraction.js": "./fractionjs.js",
"react": "./react.js",
"react-dom": "./react-dom.js",

8
docs/dist/App.js vendored
View File

@ -5,10 +5,10 @@ import cx from "./cx.js";
import * as Tone from "../_snowpack/pkg/tone.js";
import useCycle from "./useCycle.js";
import * as tunes from "./tunes.js";
import _mini from "./mini.js";
const {tetris, tetrisMini} = tunes;
import * as krill from "./parse.js";
const {tetris, tetrisMini, tetrisHaskell} = tunes;
const {sequence, pure, reify, slowcat, fastcat, cat, stack, silence} = strudel;
const mini = _mini;
const {mini, h} = krill;
const parse = (code) => eval(code);
const synth = new Tone.PolySynth().toDestination();
synth.set({
@ -18,7 +18,7 @@ synth.set({
}
});
function App() {
const [code, setCode] = useState(tetrisMini);
const [code, setCode] = useState(tetrisHaskell);
const [log, setLog] = useState("");
const logBox = useRef();
const [error, setError] = useState();

27
docs/dist/mini.js vendored
View File

@ -1,27 +0,0 @@
import * as krill from "../_snowpack/link/repl/krill-parser.js";
import * as strudel from "../_snowpack/link/strudel.js";
const {sequence, stack, silence} = strudel;
export function patternifyAST(ast) {
switch (ast.type_) {
case "pattern":
if (ast.arguments_.alignment === "v") {
return stack(...ast.source_.map(patternifyAST));
}
return sequence(...ast.source_.map(patternifyAST));
case "element":
if (ast.source_ === "~") {
return silence;
}
if (typeof ast.source_ !== "object") {
return ast.source_;
}
return patternifyAST(ast.source_);
}
}
export default (...strings) => {
const pattern = sequence(...strings.map((str) => {
const ast = krill.parse(`"${str}"`);
return patternifyAST(ast);
}));
return pattern;
};

81
docs/dist/parse.js vendored Normal file
View File

@ -0,0 +1,81 @@
import * as krill from "../_snowpack/link/repl/krill-parser.js";
import * as strudel from "../_snowpack/link/strudel.js";
import {Scale, Note, Interval} from "../_snowpack/pkg/@tonaljs/tonal.js";
const {sequence, stack, silence, Fraction, pure} = strudel;
function reify(thing) {
if (thing?.constructor?.name === "Pattern") {
return thing;
}
return pure(thing);
}
const applyOptions = (parent) => (pat, i) => {
const ast = parent.source_[i];
const options = ast.options_;
const operator = options?.operator;
if (operator) {
switch (operator.type_) {
case "stretch":
const speed = new Fraction(operator.arguments_.amount).inverse().valueOf();
return reify(pat).fast(speed);
}
console.warn(`operator "${operator.type_}" not implemented`);
}
const unimplemented = Object.keys(options || {}).filter((key) => key !== "operator");
if (unimplemented.length) {
console.warn(`option${unimplemented.length > 1 ? "s" : ""} ${unimplemented.map((o) => `"${o}"`).join(", ")} not implemented`);
}
return pat;
};
export function patternifyAST(ast) {
switch (ast.type_) {
case "pattern":
const children = ast.source_.map(patternifyAST).map(applyOptions(ast));
if (ast.arguments_.alignment === "v") {
return stack(...children);
}
return sequence(...children);
case "element":
if (ast.source_ === "~") {
return silence;
}
if (typeof ast.source_ !== "object") {
return ast.source_;
}
return patternifyAST(ast.source_);
case "stretch":
return patternifyAST(ast.source_).slow(ast.arguments_.amount);
case "scale":
let [tonic, scale] = Scale.tokenize(ast.arguments_.scale);
const intervals = Scale.get(scale).intervals;
const pattern = patternifyAST(ast.source_);
tonic = tonic || "C4";
console.log("tonic", tonic);
return pattern.fmap((step) => {
step = Number(step);
if (isNaN(step)) {
console.warn(`scale step "${step}" not a number`);
return step;
}
const octaves = Math.floor(step / intervals.length);
const mod = (n, m) => n < 0 ? mod(n + m, m) : n % m;
const index = mod(step, intervals.length);
const interval = Interval.add(intervals[index], Interval.fromSemitones(octaves * 12));
return Note.transpose(tonic, interval || "1P");
});
default:
console.warn(`node type "${ast.type_}" not implemented -> returning silence`);
return silence;
}
}
export const mini = (...strings) => {
const pattern = sequence(...strings.map((str) => {
const ast = krill.parse(`"${str}"`);
return patternifyAST(ast);
}));
return pattern;
};
export const h = (string) => {
const ast = krill.parse(string);
console.log("ast", ast);
return patternifyAST(ast);
};

31
docs/dist/tunes.js vendored
View File

@ -53,14 +53,31 @@ export const tetrisMini = `mini(\`[[e5 [b4 c5] d5 [c5 b4]]
[e5 [~ c5] e5 [d5 c5]]
[b4 [b4 c5] d5 e5]
[c5 a4 a4 ~]],
[[e2 e3 e2 e3 e2 e3 e2 e3]
[a2 a3 a2 a3 a2 a3 a2 a3]
[g#2 g#3 g#2 g#3 e2 e3 e2 e3]
[[e2 e3]*4]
[[a2 a3]*4]
[[g#2 g#3]*2 [e2 e3]*2]
[a2 a3 a2 a3 a2 a3 b1 c2]
[d2 d3 d2 d3 d2 d3 d2 d3]
[c2 c3 c2 c3 c2 c3 c2 c3]
[b1 b2 b1 b2 e2 e3 e2 e3]
[a1 a2 a1 a2 a1 a2 a1 a2]]\`)._slow(16);
[[d2 d3]*4]
[[c2 c3]*4]
[[b1 b2]*2 [e2 e3]*2]
[[a1 a2]*4]\`)._slow(16);
`;
export const tetrisHaskell = `h(\`slow 16 $ "[[e5 [b4 c5] d5 [c5 b4]]
[a4 [a4 c5] e5 [d5 c5]]
[b4 [~ c5] d5 e5]
[c5 a4 a4 ~]
[[~ d5] [~ f5] a5 [g5 f5]]
[e5 [~ c5] e5 [d5 c5]]
[b4 [b4 c5] d5 e5]
[c5 a4 a4 ~]],
[[e2 e3]*4]
[[a2 a3]*4]
[[g#2 g#3]*2 [e2 e3]*2]
[a2 a3 a2 a3 a2 a3 b1 c2]
[[d2 d3]*4]
[[c2 c3]*4]
[[b1 b2]*2 [e2 e3]*2]
[[a1 a2]*4]"\`)
`;
export const spanish = `slowcat(
stack('c4','eb4','g4'),