Merge pull request #323 from tidalcycles/notes-without-octave

support notes without octave
This commit is contained in:
Felix Roos 2022-12-26 23:00:34 +01:00 committed by GitHub
commit 4fd4b0d713
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 6 deletions

View File

@ -33,9 +33,12 @@ describe('isNote', () => {
expect(isNote(note)).toBe(true); 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', () => { it('should not recognize invalid notes', () => {
expect(isNote('H5')).toBe(false); expect(isNote('H5')).toBe(false);
expect(isNote('C')).toBe(false);
expect(isNote('X')).toBe(false); expect(isNote('X')).toBe(false);
expect(isNote(1)).toBe(false); expect(isNote(1)).toBe(false);
}); });
@ -189,6 +192,7 @@ describe('parseNumeral', () => {
expect(parseNumeral(1.5)).toBe(1.5); expect(parseNumeral(1.5)).toBe(1.5);
}); });
it('should parse notes', () => { it('should parse notes', () => {
expect(parseNumeral('c')).toBe(48);
expect(parseNumeral('c4')).toBe(60); expect(parseNumeral('c4')).toBe(60);
expect(parseNumeral('c#4')).toBe(61); expect(parseNumeral('c#4')).toBe(61);
expect(parseNumeral('db4')).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 // 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) => { export const tokenizeNote = (note) => {
if (typeof note !== 'string') { if (typeof note !== 'string') {
return []; return [];
@ -19,7 +20,7 @@ export const tokenizeNote = (note) => {
// turns the given note into its midi number representation // turns the given note into its midi number representation
export const toMidi = (note) => { export const toMidi = (note) => {
const [pc, acc, oct] = 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 + '"');
} }

View File

@ -1,7 +1,7 @@
import escodegen from 'escodegen'; import escodegen from 'escodegen';
import { parse } from 'acorn'; import { parse } from 'acorn';
import { walk } from 'estree-walker'; import { walk } from 'estree-walker';
import { isNote } from '@strudel.cycles/core'; import { isNoteWithOctave } from '@strudel.cycles/core';
export function transpiler(input, options = {}) { export function transpiler(input, options = {}) {
const { wrapAsync = false, addReturn = true, simpleLocs = false } = options; const { wrapAsync = false, addReturn = true, simpleLocs = false } = options;
@ -25,11 +25,11 @@ export function transpiler(input, options = {}) {
this.skip(); this.skip();
return this.replace(miniWithLocation(value, node, simpleLocs)); 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(); this.skip();
return this.replace({ type: 'Literal', value: node.name }); return this.replace({ type: 'Literal', value: node.name });
} }
// TODO:
}, },
leave(node, parent, prop, index) {}, leave(node, parent, prop, index) {},
}); });