-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
297 additions
and
11 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
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,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,78 @@ | ||
# @tonaljs/time-signature  [](https://www.npmjs.com/package/@tonaljs/time-signature) | ||
|
||
> Functions to parse time signatures | ||
## Usage | ||
|
||
ES6: | ||
|
||
```js | ||
import { TimeSignature } from "@tonaljs/tonal"; | ||
``` | ||
|
||
node: | ||
|
||
```js | ||
const { TimeSignature } = require("@tonaljs/tonal"); | ||
``` | ||
|
||
single module: | ||
|
||
```js | ||
import TimeSignature from "@tonaljs/time-signature"; | ||
``` | ||
|
||
## API | ||
|
||
#### `TimeSignature.names() => string[]` | ||
|
||
Return a list of most most frequently-used time signatures: | ||
|
||
```js | ||
TimeSignature.names(); | ||
``` | ||
|
||
#### `TimeSignature.get(name: string | [number, number]) // => object` | ||
|
||
Get a time signature: | ||
|
||
```js | ||
TimeSignature.get("3/4"); // => | ||
// { | ||
// empty: false, | ||
// name: "3/4", | ||
// upper: 3, | ||
// lower: 4, | ||
// type: "simple", | ||
// additive: [] | ||
// }; | ||
``` | ||
|
||
`type` can be `simple`, `compound` or `regular` | ||
|
||
Additive signatures are accepted: | ||
|
||
```js | ||
TimeSignature.get("3+2+3/8"); // => | ||
// { | ||
// empty: false, | ||
// name: '3+2+3/8', | ||
// type: 'irregular', | ||
// upper: 8, | ||
// lower: 8, | ||
// additive: [ 3, 2, 3 ] | ||
// } | ||
``` | ||
|
||
Arrays can be passed as arguments: | ||
|
||
```js | ||
TimeSignature.get([3, 4]); | ||
TimeSignature.get(["3", "4"]); | ||
TimeSignature.get(["3+2+3", "8"]); | ||
``` | ||
|
||
## References | ||
|
||
- https://en.wikipedia.org/wiki/Time_signature | ||
- https://en.wikipedia.org/wiki/Metre_(music) |
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,102 @@ | ||
// TYPES: PARSING | ||
export type TimeSignatureLiteral = string | [number, number] | [string, string]; | ||
type ParsedTimeSignature = [number | number[], number]; | ||
|
||
// TYPES: PROPERTIES | ||
export type ValidTimeSignature = { | ||
readonly empty: false; | ||
readonly name: string; | ||
readonly upper: number | number[]; | ||
readonly lower: number; | ||
readonly type: "simple" | "compound" | "irregular"; | ||
readonly additive: number[]; | ||
}; | ||
|
||
export type InvalidTimeSignature = { | ||
readonly empty: true; | ||
readonly name: ""; | ||
readonly upper: undefined; | ||
readonly lower: undefined; | ||
readonly type: undefined; | ||
readonly additive: []; | ||
}; | ||
|
||
export type TimeSignature = ValidTimeSignature | InvalidTimeSignature; | ||
|
||
// CONSTANTS | ||
const NONE: InvalidTimeSignature = { | ||
empty: true, | ||
name: "", | ||
upper: undefined, | ||
lower: undefined, | ||
type: undefined, | ||
additive: [] | ||
}; | ||
|
||
const NAMES = ["4/4", "3/4", "2/4", "2/2", "12/8", "9/8", "6/8", "3/8"]; | ||
|
||
// PUBLIC API | ||
|
||
export function names() { | ||
return NAMES.slice(); | ||
} | ||
|
||
const REGEX = /^(\d?\d(?:\+\d)*)\/(\d)$/; | ||
const CACHE = new Map<TimeSignatureLiteral, TimeSignature>(); | ||
|
||
export function get(literal: TimeSignatureLiteral): TimeSignature { | ||
const cached = CACHE.get(literal); | ||
if (cached) { | ||
return cached; | ||
} | ||
|
||
const ts = build(parse(literal)); | ||
CACHE.set(literal, ts); | ||
return ts; | ||
} | ||
|
||
export function parse(literal: TimeSignatureLiteral): ParsedTimeSignature { | ||
if (typeof literal === "string") { | ||
const [_, up, low] = REGEX.exec(literal) || []; | ||
return parse([up, low]); | ||
} | ||
|
||
const [up, down] = literal; | ||
const denominator = +down; | ||
if (typeof up === "number") { | ||
return [up, denominator]; | ||
} | ||
|
||
const list = up.split("+").map(n => +n); | ||
return list.length === 1 ? [list[0], denominator] : [list, denominator]; | ||
} | ||
|
||
export default { names, parse, get }; | ||
|
||
// PRIVATE | ||
|
||
function build([up, down]: ParsedTimeSignature): TimeSignature { | ||
const upper = Array.isArray(up) ? up.reduce((a, b) => a + b, 0) : up; | ||
const lower = down; | ||
if (upper === 0 || lower === 0) { | ||
return NONE; | ||
} | ||
|
||
const name = Array.isArray(up) ? `${up.join("+")}/${down}` : `${up}/${down}`; | ||
const additive = Array.isArray(up) ? up : []; | ||
const type = | ||
lower === 4 || lower === 2 | ||
? "simple" | ||
: lower === 8 && upper % 3 === 0 | ||
? "compound" | ||
: "irregular"; | ||
|
||
return { | ||
empty: false, | ||
name, | ||
type, | ||
upper, | ||
lower, | ||
additive | ||
}; | ||
} |
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,26 @@ | ||
{ | ||
"name": "@tonaljs/time-signature", | ||
"version": "3.6.0", | ||
"description": "Musical time signatures", | ||
"scripts": { | ||
"build": "rollup -c=../../rollup.config.js" | ||
}, | ||
"keywords": [ | ||
"time signature", | ||
"time meter", | ||
"metre", | ||
"music", | ||
"theory" | ||
], | ||
"main": "dist/index.es5.js", | ||
"module": "dist/index.esnext.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"types": "dist/time-signature/index.d.ts", | ||
"author": "[email protected]", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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,49 @@ | ||
import TimeSignature from "./index"; | ||
|
||
describe("time-signature", () => { | ||
test("get", () => { | ||
expect(TimeSignature.get("4/4")).toEqual({ | ||
empty: false, | ||
name: "4/4", | ||
type: "simple", | ||
upper: 4, | ||
lower: 4, | ||
additive: [] | ||
}); | ||
}); | ||
|
||
test("get invalid", () => { | ||
expect(TimeSignature.get("0/0").empty).toBe(true); | ||
}); | ||
|
||
test("simple", () => { | ||
expect(TimeSignature.get("4/4").type).toEqual("simple"); | ||
expect(TimeSignature.get("3/4").type).toEqual("simple"); | ||
expect(TimeSignature.get("2/4").type).toEqual("simple"); | ||
expect(TimeSignature.get("2/2").type).toEqual("simple"); | ||
}); | ||
test("compound", () => { | ||
expect(TimeSignature.get("3/8").type).toEqual("compound"); | ||
expect(TimeSignature.get("6/8").type).toEqual("compound"); | ||
expect(TimeSignature.get("9/8").type).toEqual("compound"); | ||
expect(TimeSignature.get("12/8").type).toEqual("compound"); | ||
}); | ||
|
||
test("irregular", () => { | ||
expect(TimeSignature.get("2+3+3/8").type).toEqual("irregular"); | ||
expect(TimeSignature.get("3+2+2/8").type).toEqual("irregular"); | ||
}); | ||
|
||
test("names", () => { | ||
expect(TimeSignature.names()).toEqual([ | ||
"4/4", | ||
"3/4", | ||
"2/4", | ||
"2/2", | ||
"12/8", | ||
"9/8", | ||
"6/8", | ||
"3/8" | ||
]); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -34,7 +34,8 @@ | |
"@tonaljs/range": "^3.5.0", | ||
"@tonaljs/roman-numeral": "^3.5.0", | ||
"@tonaljs/scale": "^3.5.1", | ||
"@tonaljs/scale-type": "^3.5.0" | ||
"@tonaljs/scale-type": "^3.5.0", | ||
"@tonaljs/time-signature": "^3.5.0" | ||
}, | ||
"author": "[email protected]", | ||
"license": "MIT", | ||
|
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
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