项目作者: AtActionPark

项目描述 :
Javascript set of helpers for music theory - notes, intervals, chords and scales
高级语言: JavaScript
项目地址: git://github.com/AtActionPark/Pianissimo.git
创建时间: 2017-08-21T14:01:29Z
项目社区:https://github.com/AtActionPark/Pianissimo

开源协议:MIT License

下载


Pianissimo

Set of musical theory helpers for notes, intervals, chords and scales

Can be installed via nmp, or by copying the dist/pianissimo bundle file in your project

Create a pianissimo object

If installed via nmp, require Pianissimo first

  1. const pianissimo = require("pianissimo");

If the script is copied, the pianissimo object will be available globally

Examples

  1. const pianissimo = require("pianissimo");
  2. let note1 = pianissimo.note("C3");
  3. let note2 = note1.plusInterval("M9");
  4. note2.getName(); // D4
  5. let interval = pianissimo.interval(note2, "F#4");
  6. interval.invert();
  7. interval.getNotesName(); // ['F#4', D5]
  8. interval.getName(); // d6
  9. let note3 = interval.getNotes()[1]; // D5
  10. let scale = note3.toScale("locrian");
  11. scale.getNotesName(); // ['D5,'Eb5','F5','G5','Ab5','Bb5','C6']
  12. //or, chained, for decreased readability
  13. let scale = pianissimo
  14. .note("C3")
  15. .plusInterval("m9")
  16. .toInterval("F#4")
  17. .invert()
  18. .getNotes()[1]
  19. .toScale("locrian")
  20. .getNotesName();
  21. // ['D5,'Eb5','F5','G5','Ab5','Bb5','C6']
  22. let chord = pianissimo.chord("Cmaj7b9");
  23. chord.transpose("P5");
  24. chord.getNotesName(); // ['G3','Bb3','Db4','Fb4','Ab4']
  25. let chord2 = pianissimo.chord(["C3", "F#3", "G3", "D4"]);
  26. c.findBestName(); // GΔsus4'

Methods

  1. pianissimo.note(name); //returns a note object from a name or midi note number
  2. pianissimo.interval(name, order); //returns an interval object from a name and order (optional)
  3. pianissimo.interval(note1, note2); //returns an interval object from 2 notes
  4. pianissimo.scale(tonic, type, degree); //returns a scale object from a note, a scale name, and a degree (optional)
  5. pianissimo.chord(tonic, name); //returns a chord object from a note, and a chord name
  6. pianissimo.chord(name); //returns a chord object from a chord name
  7. pianissimo.randomNote(); //returns a random note object
  8. pianissimo.randomInterval(); //returns a random interval object
  9. pianissimo.randomScale(tonic); //returns a random scale object. The tonic is optional
  10. // and will be random if not specified
  11. pianissimo.setA4(frequency); //sets the frequency of A4 (default: 440Hz)

Notes

Creation:

Notes are object created through the .note(arg) method.
Argument must be a string of type ‘C3’, ‘C#2’, ‘Db’, ‘Solb4’, ‘fax’ … or a midi number

  1. let note = pianissimo.note("C#4");
  2. let note2 = pianissimo.note("Solbb2");
  3. let note3 = pianissimo.note("D"); //octave will default to 3
  4. let note4 = pianissimo.note("Gx4");
  5. let note4 = pianissimo.note(127); //midi ranges from 21 ('A0') to 127 ('G9')

Getters:

  1. note.getName(); // 'C#4'
  2. note.getRoot(); // 'C#'
  3. note.getRootName(); // 'C'
  4. note.getAlteration(); // '#'
  5. note.getOctave(); // 4
  6. note.getNotationType(); // letter (can be letter or name)
  7. note.getMidiNumber(); // 61

Methods:

.plusInterval(args) : adds an interval to the note, and returns the resulting note object.
Arguments can be an interval object, an interval name (order will be ascending) or an interval name and order

  1. note.plusInterval(interval); //ex: let note2 = note.plusInterval(intervalObject)
  2. note.plusInterval(intervalName); //ex: let note2 = note.plusInterval('P5')
  3. note.plusInterval(intervaName, order); //ex: let note2 = note.plusInterval('P5', 'descending')
  4. //ex: let note2 = note.plusInterval('P5', '-')

.toInterval(note) : returns a scale object built on the current note.
Arguments can be the name of the scale, and the degree to start on (optional)

  1. let note = pianissimo.note("C3");
  2. note.toInterval("G3"); //returns a P5 interval

.toScale(args) : returns a scale object built on the current note.
Arguments can be the name of the scale, and the degree to start on (optional)

  1. note.toScale(type); //ex: let scale = note.toScale('minor')
  2. note.toScale(type, degree); //ex: let scale = note.toScale('locrian',5)

Interval

Creation:

Interval are object created through the .interval(arg1,arg2) method.
The first way of creating an interval is by specifying the name and order.
Arg1 is the name of the interval (m3, P5,d18…)
Arg 2 is optional and is the order (ascending, descending, -, +)

  1. let interval1 = pianissimo.interval("m3", "descending");
  2. let interval2 = pianissimo.interval("d5"); //order will default to ascending
  3. let interval3 = pianissimo.interval("P8", "-");

The second way is by giving 2 notes.
Arg1 and Arg2 are 2 notes objects or note names

  1. let interval1 = pianissimo.interval("C3", "G3");
  2. let interval2 = pianissimo.interval(note1, note2);
  3. let interval3 = pianissimo.interval("C3", note2);
  4. let interval3 = pianissimo.interval(note1, "Sol#");

Intervals can also be created from a note object, like so:

Getters:

  1. interval.getName(); // 'm3'
  2. interval.getSemitones(); // 3
  3. interval.getOrder(); // 'ascending'
  4. interval.getNumber(); // 3
  5. interval.getQuality(); // m
  6. interval.getQualityText(); // minor
  7. interval.getNumberText(); // third
  8. interval.getNote1(); // undefined, or the note used to create the interval
  9. interval.getNote2(); // undefined, or the note used to create the interval
  10. interval.getNotes(); // returns [note1,note2]
  11. interval.getNotesName(); // returns [note1.getName(),note2.getName()]
  12. interval.getNotesFrequencies(); // returns [note1.getFrequency(),note2.getFrequency()]

Methods:

.invert() : returns the inverted interval. If it was defined with notes,

  1. let interval = new interval("m3").invert();
  2. interval.getName(); // M6
  3. let interval2 = new interval("C3", "G3").invert();
  4. interval.getName(); // M6
  5. interval.getNotesName(); //['G3', 'C4']

Scale

Creation:

Scales are object created through the .scale(tonic,type,degree) method.
Degree is optional, with default value 1, and used to start a scale on a different degree

  1. let scale1 = pianissimo.scale("C3", "minor"); //C minor
  2. let scale2 = pianissimo.scale("C3", "harmonicMinor", 5); //5th mode of the harmonic minor: C phrygian dominant

Scales can also be created from a note object:

  1. let note = pianissimo.note("C3");
  2. let scale = note.toScale("minor"); // equivalent to pianissimo.scale('C3','minor')

Getters:

  1. scale.getTonic(); // 'C3' (as a note object)
  2. scale.getName(); // 'C minor'
  3. scale.getType(); // 'minor'
  4. scale.getDegree(); // '1'
  5. scale.getNotes(); // will return an array of note objects
  6. scale.getNotesName(); // will return the name of the notes: ['C3','D3','Eb3','D3','G3','Ab3','Bb3','C4']
  7. scale.getNotesFrequencies(); // will return the frequencies of the notes: [130.81, 145.83, 155.56, 174.61, 196, 207.65, 233.08]

Methods:

.getChords() : returns an array of chords built on the scale, along with diatonic function.
Accepts an optional ‘number’ argument, to specify the number of notes per chord

  1. let note = pianissimo.note("C3");
  2. let scale = pianissimo.scale(note, "major");
  3. let chords = scale.getChords(3);
  4. console.log(chords);
  5. //[ Chord { chord: [ 'C3', 'E3', 'G3' ], name: 'C major tonic' },
  6. //Chord { chord: [ 'D3', 'F3', 'A3' ], name: 'C major supertonic' },
  7. //Chord { chord: [ 'E3', 'G3', 'B3' ], name: 'C major mediant' },
  8. //Chord { chord: [ 'F3', 'A3', 'C3' ], name: 'C major subdominant' },
  9. //Chord { chord: [ 'G3', 'B3', 'D4' ], name: 'C major dominant' },
  10. //Chord { chord: [ 'A3', 'C3', 'E4' ], name: 'C major submediant' },
  11. //Chord { chord: [ 'B3', 'D4', 'F4' ], name: 'C major leadingNote' } ]

Chord

Creation:

Chords are object usually created through the .chord(tonic,name) method.
The chord constructor will try to parse the name to build the chord on the tonic.
It should understand most of the usual symols (M,m,Minor,m7,ø,11,add, sus,+,o,dim,aug,…)

  1. let chord1 = pianissimo.chord("C3", "minor"); //C minor
  2. let chord2 = pianissimo.chord("C3", "ø"); //C half-diminished
  3. let chord2 = pianissimo.chord("C3", "m(b9b5b7b11)sus4"); //why not

Or by supplying only a full chord name

  1. let chord = pianissimo.chord("Sol#7b9"); //['Sol#3', 'Si#3' ,'Re#4', 'Fa##4', 'La4']

If thats the case, the octave can not be included in the name, and will be 3 by default

Ahords can also be created from a note object:

  1. let note = pianissimo.note("C3");
  2. let chord = note.toChord("minor"); // equivalent to pianissimo.chord('C3','minor')

Alternatively, chords can also be created by supplying an array of notes (and a optional name)

  1. let chord = pianissimo.chord(["C3", "F#4", "Bbb4"], "custom chord");

If no name is supplied, the chord creator will naively try to guess the name of the chord and set it

  1. let chord = pianissimo.chord(["C3", "Eb3", "Gb3", "Bb3"]); // name: Cdim7

Getters:

  1. chord.getTonic(); // 'C3' (as a note object)
  2. chord.getSymbols(); // 'minor'
  3. chord.getName(); // 'Cminor'
  4. chord.getNotes(); // will return an array of note objects
  5. chord.getNotesName(); // will return the name of the notes: ['C3','Eb3','Bb3']
  6. chord.getNotesFrequencies(); // will return the frequencies of the notes: [130.8, 155.56, 233.08]

Methods:

.transpose() : adds an interval to the chord, and returns the resulting chord object.
Arguments can be an interval object, an interval name (order will be ascending) or an interval name and order

  1. let chord = pianissimo.chord("C7b9");
  2. chord.transpose("P5");
  3. console.log(chords.getNotesName());
  4. //[ 'G3', 'B3', 'D4', 'F4', 'Ab4' ]

.invert(order) : will return an inverted version of the chord. Order will specify the number of inversions. Will default to 1 if no order is specified.

  1. let chord = pianissimo.chord("C7");
  2. chord.invert(1);
  3. console.log(chords.getNotesName());
  4. //[ 'E3', 'G3', 'Bb3', 'C4' ]

.findAlternateNames() : will return a list of possible names for the chord, along with notes order and intervals

  1. let chord = pianissimo.chord("C7");
  2. console.log(chords.findAlternateNames());
  3. //[ 'C7 - Notes: C3,E3,G3,Bb3 - Intervals: P1,M3,P5,m7',
  4. //'Edimadd♭6 - Notes: E3,G3,Bb3,C4 - Intervals: P1,m3,d5,m6',
  5. //'Gm6no5add4 - Notes: G3,Bb3,C4,E4 - Intervals: P1,m3,P4,M6',
  6. //'B6sus2no5add♯4 - Notes: Bb3,C4,E4,G4 - Intervals: P1,M2,A4,M6',
  7. //'Edim/C - Notes: C3,E3,G3,Bb3 - Intervals: P1,m3,d5',
  8. //'Gm6no5/C - Notes: C3,G3,Bb3,E4 - Intervals: P1,m3,M6',
  9. //'cant find name - Notes: C3,Bb3,E4,G4 - Intervals: P1,A4,M6' ]

.findBestName() : will return the shortest name from the possible nams list and set it.

  1. let c = pianissimo.chord(["C3", "F#3", "G3", "D4"]);
  2. console.log(c.findBestName());
  3. // GΔsus4'