supper notes without octave

This commit is contained in:
Felix Roos 2022-12-23 23:01:07 +01:00
parent 823115c4ec
commit d1d9b37ec7
3 changed files with 11 additions and 6 deletions

View File

@ -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);

View File

@ -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 + '"');
}

View File

@ -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) {},
});