Skip to content

Commit

Permalink
using tagged template literal over function call
Browse files Browse the repository at this point in the history
  • Loading branch information
leomcelroy committed Oct 25, 2021
1 parent a1de3e3 commit 3c18a1a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 41 deletions.
82 changes: 59 additions & 23 deletions createMuse-tag.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { getLetters } from "./letters.js";
import { parse, tokenize } from "./parser.js";
import { compile } from "./compile.js";
import { compile as compileTemp } from "./compile.js";

const sleep = m => new Promise(r => setTimeout(r, m));

const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioCtx = new AudioContext();

export const museTag = (strs, ...vals) => { // should be able to insert array of beats
const museTag = (strs, ...vals) => { // should be able to insert array of beats
let result = "";
let refs = {}; // if val is function store in here
strs.forEach((str, i) => {
Expand All @@ -30,19 +30,64 @@ export const museTag = (strs, ...vals) => { // should be able to insert array of
}
})

console.log(result);
// console.log(result);

const toks = tokenize(result);
console.log(toks);
// console.log(toks);

const [ ast, remainder ] = parse(toks);
console.log(ast);
const compiled = compile(ast, refs);
console.log(compiled);
// console.log(ast);
const compiled = compileTemp(ast, refs);
// console.log(compiled);
return compiled;
}

export const createMuseTag = samples => (ops = {}) => {
class Muse {
constructor(samples, ops = {}) {
this.bpm = ops.bpm ?? 110;
this.volume = ops.volume ?? 100; // TODO

const type = ops.type ?? "sine";
const synthOptions = { type };

this.samples = { ...samples, ...getLetters(synthOptions) };
this.playing = false;
}

async play() {
const arr = museTag(...arguments);
// const arr = museTag(...arguments).map(({ value, duration }) => [value, duration]);

this.playing = true;

for (let i = 0; i < arr.length; i++) {
if (this.playing === false) continue;
let [ symbol, beats ] = arr[i];
dispatch("ADD_PLAYED", { symbol });
if (symbol === ";") await sleep(1000/this.bpm*60*beats);
else if (symbol in this.samples) this.samples[symbol](60*1000/this.bpm*beats, audioCtx);
}

// playing = false; // leave this out to call muse multiple times, stop is just a force stop

return this; // arr
}


stop() {
this.playing = false;
}

}

const createMuse = samples => (ops = {}) => {
const newMuse = new Muse(samples, ops);
dispatch("ADD_ACTIVE_MUSE", { newMuse })

return newMuse;
}

const createMuse2 = samples => (ops = {}) => {
const bpm = ops.bpm ?? 110;
const volume = ops.volume ?? 100; // TODO
const type = ops.type ?? "sine";
Expand All @@ -58,8 +103,8 @@ export const createMuseTag = samples => (ops = {}) => {

let playing = false;

async function play() { // run vs play?
const arr = museTag(...arguments);
const play = async (...args) => { // run vs play?
const arr = museTag(args);
// const arr = museTag(...arguments).map(({ value, duration }) => [value, duration]);

playing = true;
Expand All @@ -77,30 +122,21 @@ export const createMuseTag = samples => (ops = {}) => {
return arr;
}

function stop() { playing = false }

function compile() { return museTag(...arguments) }
const stop = async () => { playing = false }

// length, time

const museObj = { play, stop, compile }
const museObj = { play, stop }

dispatch("ADD_ACTIVE_MUSE", { newMuse: museObj })

return museObj;
}


export const createMuse = samples => (ops = {}) => {
const newMuse = new Muse(samples, ops);
dispatch("ADD_ACTIVE_MUSE", { newMuse })

return newMuse;
}

const length = x => x.reduce((acc, cur) => acc + (cur[0] == ";" ? cur[1] : 0), 0)


const compile = () => museTag(...arguments);
export { createMuse, compile, length };



Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { view } from "./view.js";
import { initialSamples } from "./samples.js";
import { init } from "./init.js";
import { play } from "./play.js";
import { createMuseTag } from "./createMuse-tag.js";
import { createMuse } from "./createMuse-tag.js";

import "./test.js";

Expand Down
1 change: 1 addition & 0 deletions letters.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async function playNote(frequency, duration, ctx, ops = {}) {
}

function getLetters(ops) {
console.log(ops);
const newLetters = {};
for (const k in letters) {
let hz = letters[k];
Expand Down
11 changes: 6 additions & 5 deletions play.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMuse as createMuseTemp } from "./Muse.js";
import { createMuseTag } from "./createMuse-tag.js";
// import { createMuse as createMuseTemp } from "./Muse.js";
import { createMuse, compile, length } from "./createMuse-tag.js";

function playSample(name, context, state) {
const audio = document.querySelector(`#${name}-audio`);
Expand All @@ -13,16 +13,17 @@ const createSamples = state => state.samples.reduce((acc, cur) => {
return acc;
}, {})

const createMuse = state => createMuseTemp(createSamples(state))

const bindKeys = state => obj => {
state.keyBindings = obj;
}

const makeIncluded = (state) => ({
createMuse: createMuse(state),
// createMuse: createMuse(state),
compile,
length,
bindKeys: bindKeys(state),
createMuseTag: createMuseTag(createSamples(state))
createMuse: createMuse(createSamples(state))
});

export function play(state) {
Expand Down
24 changes: 12 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { compile } from "./compile.js";
import { parse, tokenize } from "./parser.js";
import { museTag } from "./createMuse-tag.js";
// import { compile } from "./compile.js";
// import { parse, tokenize } from "./parser.js";
// import { museTag } from "./createMuse-tag.js";

const interpolated = museTag`;${() => ["a1", 2]} ^ 1`
console.log("interpolated", interpolated);
// const interpolated = museTag`;${() => ["a1", 2]} ^ 1`
// console.log("interpolated", interpolated);

const toks = tokenize("[a4 e4]<");
console.log("tokens:\n", toks)
const [ ast, remainder ] = parse(toks);
console.log("ast:\n", ast);
console.log("remainder:\n", remainder);
// const toks = tokenize("[a4 e4]<");
// console.log("tokens:\n", toks)
// const [ ast, remainder ] = parse(toks);
// console.log("ast:\n", ast);
// console.log("remainder:\n", remainder);

const result = compile(ast);
console.log(result);
// const result = compile(ast);
// console.log(result);

0 comments on commit 3c18a1a

Please sign in to comment.