An open API service indexing awesome lists of open source software.

https://github.com/danigb/pitch-parser

Music pitch parser for javascript
https://github.com/danigb/pitch-parser

Last synced: 11 months ago
JSON representation

Music pitch parser for javascript

Awesome Lists containing this project

README

          

# pitch-parser

[![Build Status](https://travis-ci.org/danigb/pitch-parser.svg?branch=master)](https://travis-ci.org/danigb/pitch-parser)
[![Code Climate](https://codeclimate.com/github/danigb/pitch-parser/badges/gpa.svg)](https://codeclimate.com/github/danigb/pitch-parser)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
[![npm version](https://badge.fury.io/js/pitch-parser.svg)](https://badge.fury.io/js/pitch-parser)

Music pitch parser for javascript.

Given a pitch string it returns a [pitch array](https://github.com/danigb/a-pitch), and given a pitch array it returns a pitch string:

```js
var pitch = require('pitch-parser')
pitch('C#4') // => [0, 2, 4]
pitch([0, 2, 4]) // => 'C#4'
pitch('Ebb3') // => [2, -2, 3]
pitch([2, -2, 3]) // => 'Ebb3'
pitch('A#') // => [5, 1, null]
pitch([5, 1, null]) // => 'A#'
```

The returned value is an array of 3 integers with the following form `[letter, accidentals, octave]` where:

- __letter__: a positive integer between 0 and 6 to represent C, D... to B
- __accidentals__: is an integer to represent pitch accidentals. 0 means no accidentals, negatives values are for flats and positives for sharps
- __octave__: (Optional) the octave number or null if not specified

## API









pitch(val) → {Array|String}





Converts pitches between strings and array notation


This functions parses a string in the form 'letter + accidentals [+ octave]'.
The letter can be upper or down case and the accidentals can be sharps #
flats b or double sharps x.


The pitch array notation is 3 integers is in the form [letter, accidentals, octave].


This function caches the result to get better performance. If for some
reason you don't want to cache, use pitch.parse and pitch.build



Parameters:

Name
Type
Description

val

String
|
Array

the pitch (can be a string or array)


Source:



Returns:


the converted val (string if it was an array,
and array if it was string)





Type


Array
|
String


Examples

pitch('C4') // => [0, 0, 4]

pitch([0, 0, 4]) // => 'C4'

 // parse

pitch('c2') // => [0, 0, 2]
pitch('F#') // => [4, 1, null] (no octave)

 // build

pitch([2, -1, 3]) // => 'Eb3'
pitch([6, -2, null]) // => 'Bbb'

 // return scientific notation

pitch(pitch('cbb')) // => 'Cbb'
pitch(pitch('fx')) // => 'F##'


*generated with [docme](https://github.com/thlorenz/docme)*

## License

MIT License