From d1d9b37ec764dd7377754d88e72996ef887beb82 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Fri, 23 Dec 2022 23:01:07 +0100 Subject: [PATCH] supper notes without octave --- packages/core/test/util.test.mjs | 6 +++++- packages/core/util.mjs | 5 +++-- packages/transpiler/transpiler.mjs | 6 +++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/core/test/util.test.mjs b/packages/core/test/util.test.mjs index 9ebccee3..4a79192b 100644 --- a/packages/core/test/util.test.mjs +++ b/packages/core/test/util.test.mjs @@ -33,9 +33,12 @@ describe('isNote', () => { expect(isNote(note)).toBe(true); }); }); + it('should recognize notes without octave', () => { + expect(isNote('C')).toBe(true); + expect(isNote('Bb')).toBe(true); + }); it('should not recognize invalid notes', () => { expect(isNote('H5')).toBe(false); - expect(isNote('C')).toBe(false); expect(isNote('X')).toBe(false); expect(isNote(1)).toBe(false); }); @@ -189,6 +192,7 @@ describe('parseNumeral', () => { expect(parseNumeral(1.5)).toBe(1.5); }); it('should parse notes', () => { + expect(parseNumeral('c')).toBe(48); expect(parseNumeral('c4')).toBe(60); expect(parseNumeral('c#4')).toBe(61); expect(parseNumeral('db4')).toBe(61); diff --git a/packages/core/util.mjs b/packages/core/util.mjs index d8a7a233..19ff7c12 100644 --- a/packages/core/util.mjs +++ b/packages/core/util.mjs @@ -5,7 +5,8 @@ This program is free software: you can redistribute it and/or modify it under th */ // returns true if the given string is a note -export const isNote = (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][#bs]*[0-9]?$/.test(name); export const tokenizeNote = (note) => { if (typeof note !== 'string') { return []; @@ -19,7 +20,7 @@ export const tokenizeNote = (note) => { // turns the given note into its midi number representation export const toMidi = (note) => { - const [pc, acc, oct] = tokenizeNote(note); + const [pc, acc, oct = 3] = tokenizeNote(note); if (!pc) { throw new Error('not a note: "' + note + '"'); } diff --git a/packages/transpiler/transpiler.mjs b/packages/transpiler/transpiler.mjs index a0b24512..fee9d542 100644 --- a/packages/transpiler/transpiler.mjs +++ b/packages/transpiler/transpiler.mjs @@ -1,7 +1,7 @@ import escodegen from 'escodegen'; import { parse } from 'acorn'; import { walk } from 'estree-walker'; -import { isNote } from '@strudel.cycles/core'; +import { isNoteWithOctave } from '@strudel.cycles/core'; export function transpiler(input, options = {}) { const { wrapAsync = false, addReturn = true, simpleLocs = false } = options; @@ -25,11 +25,11 @@ export function transpiler(input, options = {}) { this.skip(); return this.replace(miniWithLocation(value, node, simpleLocs)); } - if (node.type === 'Identifier' && isNote(node.name)) { + // TODO: remove pseudo note variables? + if (node.type === 'Identifier' && isNoteWithOctave(node.name)) { this.skip(); return this.replace({ type: 'Literal', value: node.name }); } - // TODO: }, leave(node, parent, prop, index) {}, });