-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: move named to pitch * feat: add notation-interval * feat: extract interval * chore: rename to pitch-interval * feat: extract pitch-note * chore: fix pitch-interval documentation * feat: add pitch-distance * chore: changeset * chore: add typedoc * feat: create notation-scientific * fix: readme badges * chore: notation-scientific not yet ready to publish * chore: bump version * chore: deps * chore: code cleanup
- Loading branch information
Showing
46 changed files
with
996 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ compiled | |
.awcache | ||
.rpt2_cache | ||
.turbo | ||
tmp/ | ||
tmp/ | ||
typedoc/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
import { isNamedPitch } from "@tonaljs/pitch"; | ||
|
||
export * from "@tonaljs/pitch"; | ||
export * from "./src/distance"; | ||
export * from "./src/interval"; | ||
export * from "./src/named"; | ||
export * from "./src/note"; | ||
export * from "./src/utils"; | ||
export * from "@tonaljs/pitch-distance"; | ||
export * from "@tonaljs/pitch-interval"; | ||
export * from "@tonaljs/pitch-note"; | ||
|
||
export const fillStr = (s: string, n: number) => Array(Math.abs(n) + 1).join(s); | ||
|
||
export function deprecate< | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
ResultFn extends (this: any, ...newArgs: any[]) => ReturnType<ResultFn>, | ||
>(original: string, alternative: string, fn: ResultFn) { | ||
return function (this: unknown, ...args: unknown[]): ReturnType<ResultFn> { | ||
// tslint:disable-next-line | ||
console.warn(`${original} is deprecated. Use ${alternative}.`); | ||
return fn.apply(this, args); | ||
}; | ||
} | ||
|
||
export const isNamed = deprecate("isNamed", "isNamedPitch", isNamedPitch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { interval, isNamed, note } from "../index"; | ||
import { interval, isNamedPitch, note } from "../index"; | ||
|
||
describe("@tonaljs/core", () => { | ||
test("isNamed", () => { | ||
expect(isNamed(note("C4"))).toBe(true); | ||
expect(isNamed(interval("P4"))).toBe(true); | ||
expect(isNamed(note("X"))).toBe(true); | ||
test("isNamedPitch", () => { | ||
expect(isNamedPitch(note("C4"))).toBe(true); | ||
expect(isNamedPitch(interval("P4"))).toBe(true); | ||
expect(isNamedPitch(note("X"))).toBe(true); | ||
|
||
expect(isNamed(undefined)).toBe(false); | ||
expect(isNamed(null)).toBe(false); | ||
expect(isNamed("")).toBe(false); | ||
expect(isNamedPitch(undefined)).toBe(false); | ||
expect(isNamedPitch(null)).toBe(false); | ||
expect(isNamedPitch("")).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @tonaljs/notation-scientific |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 danigb | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# @tonaljs/notation-scientific ![tonal](https://img.shields.io/badge/@tonaljs-notation_scientific-yellow.svg?style=flat-square) [![npm version](https://img.shields.io/npm/v/@tonaljs/notation-scientific.svg?style=flat-square)](https://www.npmjs.com/package/@tonaljs/notation-scientific) | ||
|
||
> Parse notes in scientific notation | ||
## Usage | ||
|
||
⚠️ It's probably you don't need to use this package directly. Use [tonal-note](/packages/note) instead. | ||
|
||
```js | ||
import { parse, name, tokenize } from "@tonaljs/notation-scientific"; | ||
|
||
parse("cb4"); // => { step: 0, oct: 4, alt: -1 } | ||
``` | ||
|
||
## API | ||
|
||
### `tokenize(name: string) => [string, string, string, string]` | ||
|
||
Given a note name in [scientific notation] it returns an array with the different parts: | ||
|
||
```js | ||
tokenize("Abb4 major"); // => ["", "A", "bb", "4", "major"] | ||
``` | ||
|
||
### `parse(name: string) => { step, alt, oct? } | undefined` | ||
|
||
Given a note name, it returns an object with the following properties: | ||
|
||
- step (number): the letter number (0..6) | ||
- alt (number): the accidental number (..., -1 = 'b', 0 = '', 1 = '#', ...) | ||
- oct (number?): the optional octave | ||
|
||
### `name(parsed) => string` | ||
|
||
Given the result of `parse` it returns the name. | ||
|
||
```js | ||
name({ step: 1, oct: 2, alt: 1 }); // => "D#1" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
type ScientificPitch = { | ||
step: number; | ||
alt: number; | ||
oct?: number; | ||
}; | ||
|
||
const fillStr = (s: string, n: number) => Array(Math.abs(n) + 1).join(s); | ||
|
||
const altToAcc = (alt: number): string => | ||
alt < 0 ? fillStr("b", -alt) : fillStr("#", alt); | ||
|
||
/** | ||
* Given a note literal (a note name or a note object), returns the Note object | ||
* @example | ||
* note('Bb4') // => { name: "Bb4", midi: 70, chroma: 10, ... } | ||
*/ | ||
|
||
type NoteTokens = [string, string, string, string]; | ||
|
||
const REGEX = /^([a-gA-G]?)(#{1,}|b{1,}|x{1,}|)(-?\d*)\s*(.*)$/; | ||
|
||
/** | ||
* @private | ||
*/ | ||
export function tokenize(str: string): NoteTokens { | ||
const m = REGEX.exec(str) as string[]; | ||
return [m[1].toUpperCase(), m[2].replace(/x/g, "##"), m[3], m[4]]; | ||
} | ||
|
||
export function parse(noteName: string): ScientificPitch | null { | ||
const tokens = tokenize(noteName); | ||
if (tokens[0] === "" || tokens[3] !== "") { | ||
return null; | ||
} | ||
|
||
const letter = tokens[0]; | ||
const acc = tokens[1]; | ||
const octStr = tokens[2]; | ||
|
||
const step = (letter.charCodeAt(0) + 3) % 7; | ||
const alt = acc[0] === "b" ? -acc.length : acc.length; | ||
const oct = octStr.length ? +octStr : undefined; | ||
|
||
return { | ||
alt, | ||
oct, | ||
step, | ||
}; | ||
} | ||
|
||
export function name(props: ScientificPitch): string { | ||
const { step, alt, oct } = props; | ||
const letter = "CDEFGAB".charAt(step); | ||
if (!letter) { | ||
return ""; | ||
} | ||
|
||
const pc = letter + altToAcc(alt); | ||
return oct || oct === 0 ? pc + oct : pc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@tonaljs/notation-scientific", | ||
"version": "4.8.0", | ||
"private": true, | ||
"description": "Parse intervals in shorthand notation", | ||
"keywords": [ | ||
"note", | ||
"music", | ||
"theory", | ||
"interval", | ||
"shorthand notation" | ||
], | ||
"dependencies": {}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"author": "[email protected]", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"build": "tsup index.ts --sourcemap --dts --format esm,cjs" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { name, parse, tokenize } from "./index"; | ||
|
||
describe("Scientific Notation", () => { | ||
test("tokenize", () => { | ||
expect(tokenize("Cbb5 major")).toEqual(["C", "bb", "5", "major"]); | ||
expect(tokenize("Ax")).toEqual(["A", "##", "", ""]); | ||
expect(tokenize("CM")).toEqual(["C", "", "", "M"]); | ||
expect(tokenize("maj7")).toEqual(["", "", "", "maj7"]); | ||
expect(tokenize("")).toEqual(["", "", "", ""]); | ||
expect(tokenize("bb")).toEqual(["B", "b", "", ""]); | ||
expect(tokenize("##")).toEqual(["", "##", "", ""]); | ||
}); | ||
|
||
test("parse", () => { | ||
expect(parse("C3")).toEqual({ alt: 0, oct: 3, step: 0 }); | ||
expect(parse("A4")).toEqual({ alt: 0, oct: 4, step: 5 }); | ||
expect(parse("D#")).toEqual({ alt: 1, oct: undefined, step: 1 }); | ||
expect(parse("D##")).toEqual({ alt: 2, oct: undefined, step: 1 }); | ||
expect(parse("D###")).toEqual({ alt: 3, oct: undefined, step: 1 }); | ||
expect(parse("eb")).toEqual({ alt: -1, oct: undefined, step: 2 }); | ||
expect(parse("ebbb")).toEqual({ alt: -3, oct: undefined, step: 2 }); | ||
expect(parse("ebbb3")).toEqual({ alt: -3, oct: 3, step: 2 }); | ||
expect(parse("ebbb-3")).toEqual({ alt: -3, oct: -3, step: 2 }); | ||
expect(parse("ebbbbbb-3")).toEqual({ alt: -6, oct: -3, step: 2 }); | ||
expect(parse("bb")).toEqual(parse("Bb")); | ||
}); | ||
|
||
test("name", () => { | ||
expect(name({ step: 1, alt: -1 })).toBe("Db"); | ||
expect(name({ step: 2, alt: 1 })).toBe("E#"); | ||
expect(name({ step: 2, alt: 1, oct: 4 })).toBe("E#4"); | ||
expect(name({ step: 5, alt: 0 })).toBe("A"); | ||
expect(name({ step: 5, alt: 0, oct: 2 })).toBe("A2"); | ||
expect(name({ step: -1, alt: 0 })).toBe(""); | ||
expect(name({ step: 8, alt: 0 })).toBe(""); | ||
}); | ||
}); |
Oops, something went wrong.