UNPKG

music21j-port

Version:

A toolkit for computer-aided musicology, Javascript version

1,034 lines (859 loc) 27.9 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Music21j Source: music21/pitch.js</title> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css"> <link type="text/css" rel="stylesheet" href="styles/site.spruce.css"> </head> <body> <div class="container-fluid"> <div class="navbar navbar-fixed-top navbar-inverse"> <div class="navbar-inner"> <a class="brand" href="index.html">Music21j</a> <ul class="nav"> <li class="dropdown"> <a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="music21.html">music21</a> </li> <li> <a href="music21.articulations.html">music21.articulations</a> </li> <li> <a href="music21.audioSearch.html">music21.audioSearch</a> </li> <li> <a href="music21.base.html">music21.base</a> </li> <li> <a href="music21.beam.html">music21.beam</a> </li> <li> <a href="music21.chord.html">music21.chord</a> </li> <li> <a href="music21.clef.html">music21.clef</a> </li> <li> <a href="music21.common.html">music21.common</a> </li> <li> <a href="music21.duration.html">music21.duration</a> </li> <li> <a href="music21.dynamics.html">music21.dynamics</a> </li> <li> <a href="music21.expressions.html">music21.expressions</a> </li> <li> <a href="music21.fromPython.html">music21.fromPython</a> </li> <li> <a href="music21.instrument.html">music21.instrument</a> </li> <li> <a href="music21.interval.html">music21.interval</a> </li> <li> <a href="music21.key.html">music21.key</a> </li> <li> <a href="music21.keyboard.html">music21.keyboard</a> </li> <li> <a href="music21.meter.html">music21.meter</a> </li> <li> <a href="music21.miditools.html">music21.miditools</a> </li> <li> <a href="music21.note.html">music21.note</a> </li> <li> <a href="music21.pitch.html">music21.pitch</a> </li> <li> <a href="music21.prebase.html">music21.prebase</a> </li> <li> <a href="music21.renderOptions.html">music21.renderOptions</a> </li> <li> <a href="music21.roman.html">music21.roman</a> </li> <li> <a href="music21.scale.html">music21.scale</a> </li> <li> <a href="music21.sites.html">music21.sites</a> </li> <li> <a href="music21.stream.html">music21.stream</a> </li> <li> <a href="music21.tempo.html">music21.tempo</a> </li> <li> <a href="music21.tie.html">music21.tie</a> </li> <li> <a href="music21.tinyNotation.html">music21.tinyNotation</a> </li> <li> <a href="music21.vfShow.html">music21.vfShow</a> </li> <li> <a href="music21.webmidi.html">music21.webmidi</a> </li> </ul> </li> <li class="dropdown"> <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="music21.articulations.Accent.html">music21.articulations.Accent</a> </li> <li> <a href="music21.articulations.Articulation.html">music21.articulations.Articulation</a> </li> <li> <a href="music21.articulations.DynamicArticulation.html">music21.articulations.DynamicArticulation</a> </li> <li> <a href="music21.articulations.LengthArticulation.html">music21.articulations.LengthArticulation</a> </li> <li> <a href="music21.articulations.Marcato.html">music21.articulations.Marcato</a> </li> <li> <a href="music21.articulations.PitchArticulation.html">music21.articulations.PitchArticulation</a> </li> <li> <a href="music21.articulations.Spiccato.html">music21.articulations.Spiccato</a> </li> <li> <a href="music21.articulations.Staccatissimo.html">music21.articulations.Staccatissimo</a> </li> <li> <a href="music21.articulations.Staccato.html">music21.articulations.Staccato</a> </li> <li> <a href="music21.articulations.StrongAccent.html">music21.articulations.StrongAccent</a> </li> <li> <a href="music21.articulations.Tenuto.html">music21.articulations.Tenuto</a> </li> <li> <a href="music21.articulations.TimbreArticulation.html">music21.articulations.TimbreArticulation</a> </li> <li> <a href="music21.base.Music21Object.html">music21.base.Music21Object</a> </li> <li> <a href="music21.beam.Beam.html">music21.beam.Beam</a> </li> <li> <a href="music21.beam.Beams.html">music21.beam.Beams</a> </li> <li> <a href="music21.chord.Chord.html">music21.chord.Chord</a> </li> <li> <a href="music21.clef.AltoClef.html">music21.clef.AltoClef</a> </li> <li> <a href="music21.clef.BassClef.html">music21.clef.BassClef</a> </li> <li> <a href="music21.clef.Clef.html">music21.clef.Clef</a> </li> <li> <a href="music21.clef.MezzoSopranoClef.html">music21.clef.MezzoSopranoClef</a> </li> <li> <a href="music21.clef.PercussionClef.html">music21.clef.PercussionClef</a> </li> <li> <a href="music21.clef.SopranoClef.html">music21.clef.SopranoClef</a> </li> <li> <a href="music21.clef.TenorClef.html">music21.clef.TenorClef</a> </li> <li> <a href="music21.clef.Treble8vaClef.html">music21.clef.Treble8vaClef</a> </li> <li> <a href="music21.clef.Treble8vbClef.html">music21.clef.Treble8vbClef</a> </li> <li> <a href="music21.clef.TrebleClef.html">music21.clef.TrebleClef</a> </li> <li> <a href="music21.duration.Duration.html">music21.duration.Duration</a> </li> <li> <a href="music21.duration.Tuplet.html">music21.duration.Tuplet</a> </li> <li> <a href="music21.dynamics.Dynamic.html">music21.dynamics.Dynamic</a> </li> <li> <a href="music21.expressions.Expression.html">music21.expressions.Expression</a> </li> <li> <a href="music21.expressions.Fermata.html">music21.expressions.Fermata</a> </li> <li> <a href="music21.fromPython.Converter.html">music21.fromPython.Converter</a> </li> <li> <a href="music21.instrument.Instrument.html">music21.instrument.Instrument</a> </li> <li> <a href="music21.interval.ChromaticInterval.html">music21.interval.ChromaticInterval</a> </li> <li> <a href="music21.interval.DiatonicInterval.html">music21.interval.DiatonicInterval</a> </li> <li> <a href="music21.interval.GenericInterval.html">music21.interval.GenericInterval</a> </li> <li> <a href="music21.interval.Interval.html">music21.interval.Interval</a> </li> <li> <a href="music21.key.Key.html">music21.key.Key</a> </li> <li> <a href="music21.key.KeySignature.html">music21.key.KeySignature</a> </li> <li> <a href="music21.keyboard.BlackKey.html">music21.keyboard.BlackKey</a> </li> <li> <a href="music21.keyboard.Key.html">music21.keyboard.Key</a> </li> <li> <a href="music21.keyboard.Keyboard.html">music21.keyboard.Keyboard</a> </li> <li> <a href="music21.keyboard.WhiteKey.html">music21.keyboard.WhiteKey</a> </li> <li> <a href="music21.meter.TimeSignature.html">music21.meter.TimeSignature</a> </li> <li> <a href="music21.miditools.Event.html">music21.miditools.Event</a> </li> <li> <a href="music21.miditools.MidiPlayer.html">music21.miditools.MidiPlayer</a> </li> <li> <a href="music21.note.GeneralNote.html">music21.note.GeneralNote</a> </li> <li> <a href="music21.note.Lyric.html">music21.note.Lyric</a> </li> <li> <a href="music21.note.Note.html">music21.note.Note</a> </li> <li> <a href="music21.note.NotRest.html">music21.note.NotRest</a> </li> <li> <a href="music21.note.Rest.html">music21.note.Rest</a> </li> <li> <a href="music21.pitch.Accidental.html">music21.pitch.Accidental</a> </li> <li> <a href="music21.pitch.Pitch.html">music21.pitch.Pitch</a> </li> <li> <a href="music21.prebase.ProtoM21Object.html">music21.prebase.ProtoM21Object</a> </li> <li> <a href="music21.renderOptions.RenderOptions.html">music21.renderOptions.RenderOptions</a> </li> <li> <a href="music21.roman.RomanNumeral.html">music21.roman.RomanNumeral</a> </li> <li> <a href="music21.stream.Measure.html">music21.stream.Measure</a> </li> <li> <a href="music21.stream.Part.html">music21.stream.Part</a> </li> <li> <a href="music21.stream.Score.html">music21.stream.Score</a> </li> <li> <a href="music21.stream.Stream.html">music21.stream.Stream</a> </li> <li> <a href="music21.stream.Voice.html">music21.stream.Voice</a> </li> <li> <a href="music21.tempo.Metronome.html">music21.tempo.Metronome</a> </li> <li> <a href="music21.tie.Tie.html">music21.tie.Tie</a> </li> <li> <a href="music21.vfShow.Renderer.html">music21.vfShow.Renderer</a> </li> <li> <a href="music21.vfShow.RenderStack.html">music21.vfShow.RenderStack</a> </li> <li> <a href="Notation.html">Notation</a> </li> <li> <a href="Page.html">Page</a> </li> <li> <a href="Recorder.html">Recorder</a> </li> </ul> </li> <li class="dropdown"> <a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b class="caret"></b></a> <ul class="dropdown-menu "> <li> <a href="global.html#getM21attribute">getM21attribute</a> </li> <li> <a href="global.html#layout">layout</a> </li> <li> <a href="global.html#midiNumDiffFromFrequency">midiNumDiffFromFrequency</a> </li> <li> <a href="global.html#recorderWorkerJs">recorderWorkerJs</a> </li> <li> <a href="global.html#unpickler">unpickler</a> </li> <li> <a href="global.html#warnBanner">warnBanner</a> </li> </ul> </li> </ul> </div> </div> <div class="row-fluid"> <div class="span12"> <div id="main"> <h1 class="page-title">Source: music21/pitch.js</h1> <section> <article> <pre class="sunlight-highlight-javascript linenums">/** * music21j -- Javascript reimplementation of Core music21 features. * music21/pitch -- pitch routines * * Copyright (c) 2013-17, Michael Scott Cuthbert and cuthbertLab * Based on music21, Copyright (c) 2006–17, Michael Scott Cuthbert and cuthbertLab * */ import { Music21Exception } from './exceptions21.js'; import { prebase } from './prebase.js'; /** * pitch module. See {@link music21.pitch} namespace * * @exports music21/pitch */ /** * Pitch related objects and methods * * @namespace music21.pitch * @memberof music21 * @requires music21/prebase */ export const pitch = {}; /** * @class Accidental * @memberof music21.pitch * @param {string|number} accName - an accidental name * @extends music21.prebase.ProtoM21Object */ export class Accidental extends prebase.ProtoM21Object { constructor(accName) { super(); this._name = ''; this._alter = 0.0; this._modifier = ''; this._unicodeModifier = ''; this.displayType = 'normal'; // "normal", "always" supported currently this.displayStatus = undefined; // true, false, undefined this.set(accName); } /** * Sets a parameter of the accidental and updates name, alter, and modifier to suit. * * @memberof music21.pitch.Accidental * @param {number|string} accName - the name, number, or modifier to set * @returns {undefined} */ set(accName) { if (accName !== undefined &amp;&amp; accName.toLowerCase !== undefined) { accName = accName.toLowerCase(); } if ( accName === 'natural' || accName === 'n' || accName === 0 || accName === undefined ) { this._name = 'natural'; this._alter = 0.0; this._modifier = ''; this._unicodeModifier = '♮'; } else if (accName === 'sharp' || accName === '#' || accName === 1) { this._name = 'sharp'; this._alter = 1.0; this._modifier = '#'; this._unicodeModifier = '♯'; } else if ( accName === 'flat' || accName === '-' || accName === 'b' || accName === -1 ) { this._name = 'flat'; this._alter = -1.0; this._modifier = '-'; this._unicodeModifier = '♭'; } else if ( accName === 'double-flat' || accName === '--' || accName === -2 ) { this._name = 'double-flat'; this._alter = -2.0; this._modifier = '--'; this._unicodeModifier = '&amp;#x1d12b;'; } else if ( accName === 'double-sharp' || accName === '##' || accName === 2 ) { this._name = 'double-sharp'; this._alter = 2.0; this._modifier = '##'; this._unicodeModifier = '&amp;#x1d12a;'; } else if ( accName === 'triple-flat' || accName === '---' || accName === -3 ) { this._name = 'triple-flat'; this._alter = -3.0; this._modifier = '---'; this._unicodeModifier = '♭&amp;#x1d12b;'; } else if ( accName === 'triple-sharp' || accName === '###' || accName === 3 ) { this._name = 'triple-sharp'; this._alter = 3.0; this._modifier = '###'; this._unicodeModifier = '&amp;#x1d12a;'; } else if ( accName === 'quadruple-flat' || accName === '----' || accName === -4 ) { this._name = 'quadruple-flat'; this._alter = -4.0; this._modifier = '----'; this._unicodeModifier = '♭&amp;#x1d12b;'; } else if ( accName === 'quadruple-sharp' || accName === '####' || accName === 4 ) { this._name = 'quadruple-sharp'; this._alter = 4.0; this._modifier = '####'; this._unicodeModifier = '&amp;#x1d12a;'; } else { throw new Music21Exception('Accidental is not supported: ' + accName); } } /** * Return or set the name of the accidental ('flat', 'sharp', 'natural', etc.); * * When set, updates alter and modifier. * * @memberof music21.pitch.Accidental# * @var {string} name */ get name() { return this._name; } set name(n) { this.set(n); } /** * Return or set the alteration amount (-1.0 = flat; 1.0 = sharp; etc.) * * When set, updates name and modifier. * * @memberof music21.pitch.Accidental# * @var {number} alter */ get alter() { return this._alter; } set alter(alter) { this.set(alter); } /** * Return or set the modifier ('-', '#', '') * * When set, updates alter and name. * @memberof music21.pitch.Accidental# * @var {string} modifier */ get modifier() { return this._modifier; } set modifier(modifier) { this.set(modifier); } /** * Returns the modifier for vexflow ('b', '#', 'n') * * @memberof music21.pitch.Accidental# * @var {string} vexflowModifier * @readonly */ get vexflowModifier() { // todo -- rewrite with mapping. const m = this.modifier; if (m === '') { return 'n'; } else if (m === '#') { return '#'; } else if (m === '-') { return 'b'; } else if (m === '##') { return '##'; } else if (m === '--') { return 'bb'; } else if (m === '###') { return '###'; } else if (m === '---') { return 'bbb'; } else { throw new Music21Exception('Vexflow does not support: ' + m); } } /** * Returns the modifier in unicode or * for double and triple accidentals, as a hex escape * * @memberof music21.pitch.Accidental# * @var {string} unicodeModifier * @readonly */ get unicodeModifier() { return this._unicodeModifier; } } pitch.Accidental = Accidental; pitch.nameToMidi = { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 }; pitch.nameToSteps = { C: 0, D: 1, E: 2, F: 3, G: 4, A: 5, B: 6 }; pitch.stepsToName = ['C', 'D', 'E', 'F', 'G', 'A', 'B']; pitch.midiToName = [ 'C', 'C#', 'D', 'E-', 'E', 'F', 'F#', 'G', 'A-', 'A', 'B-', 'B', ]; /** * Pitch objects are found in {@link music21.note.Note} objects, and many other places. * * They do not have a {@link music21.duration.Duration} associated with them, so they * cannot be placed inside {@link music21.stream.Stream} objects. * * Valid pitch name formats are * - "C", "D', etc. ("B" = American B; "H" is not allowed) * - "C#", "C-" (C-flat; do not use "b" for flat), "C##", "C###", "C--" etc. * - Octave may be specified after the name + accidental: "C#4" etc. * - Octave can be arbitrarily high ("C10") but only as low as "C0" because "C-1" would be interpreted as C-flat octave 1; shift octave later for very low notes. * - If octave is not specified, the system will usually use octave 4, but might adjust according to context. If you do not like this behavior, give an octave always. * - Microtones are not supported in music21j (they are in music21p) * * @class Pitch * @memberof music21.pitch * @param {string} pn - name of the pitch, with or without octave, see above. * @extends music21.prebase.ProtoM21Object * @property {music21.pitch.Accidental|undefined} accidental - link to an accidental * @property {number} diatonicNoteNum - diatonic number of the pitch, where 29 = C4, C#4, C-4, etc.; 30 = D-4, D4, D#4, etc. updates other properties. * @property {number} midi - midi number of the pitch (C4 = 60); readonly. See {@link music21.pitch.Pitch#ps} for setable version. * @property {string} name - letter name of pitch + accidental modifier; e.g., B-flat = 'B-'; changes automatically w/ step and accidental * @property {string} nameWithOctave - letter name of pitch + accidental modifier + octave; changes automatically w/ step, accidental, and octave * @property {number} octave - number for the octave, where middle C = C4, and octaves change between B and C; default 4 * @property {number} ps - pitch space number, like midi number but floating point and w/ no restriction on range. C4 = 60.0 * @property {string} step - letter name for the pitch (C-G, A, B), without accidental; default 'C' */ export class Pitch extends prebase.ProtoM21Object { constructor(pn = 'C') { super(); this._step = 'C'; this._octave = 4; this._accidental = undefined; /* pn can be a nameWithOctave */ if (typeof pn === 'number') { if (pn &lt; 12) { pn += 60; // pitchClass } this.ps = pn; } else if (pn.match(/\d+/)) { this.nameWithOctave = pn; } else { this.name = pn; } } // N.B. cannot use transpose here, because of circular import. get step() { return this._step; } set step(s) { this._step = s; } get octave() { return this._octave; } set octave(o) { this._octave = o; } get accidental() { return this._accidental; } set accidental(a) { if (typeof a !== 'object' &amp;&amp; a !== undefined) { a = new pitch.Accidental(a); } this._accidental = a; } get name() { if (this.accidental === undefined) { return this.step; } else { return this.step + this.accidental.modifier; } } set name(nn) { this.step = nn.slice(0, 1).toUpperCase(); const tempAccidental = nn.slice(1); if (tempAccidental) { // not the empty string this.accidental = tempAccidental; // converts automatically } else { this.accidental = undefined; } } get nameWithOctave() { return this.name + this.octave.toString(); } set nameWithOctave(pn) { const storedOctave = pn.match(/\d+/); if (storedOctave !== undefined) { pn = pn.replace(/\d+/, ''); this.octave = parseInt(storedOctave); this.name = pn; } else { this.name = pn; } } get diatonicNoteNum() { return this.octave * 7 + pitch.nameToSteps[this.step] + 1; } set diatonicNoteNum(newDNN) { newDNN -= 1; // makes math easier this.octave = Math.floor(newDNN / 7); this.step = pitch.stepsToName[newDNN % 7]; } get frequency() { return 440 * Math.pow(2, (this.ps - 69) / 12); } get midi() { return Math.floor(this.ps); } get ps() { let accidentalAlter = 0; if (this.accidental !== undefined) { accidentalAlter = parseInt(this.accidental.alter); } return ( (this.octave + 1) * 12 + pitch.nameToMidi[this.step] + accidentalAlter ); } set ps(ps) { this.name = pitch.midiToName[ps % 12]; this.octave = Math.floor(ps / 12) - 1; } /** * @param {boolean} inPlace * @param {Int} directionInt -- -1 = down, 1 = up */ _getEnharmonicHelper(inPlace=false, directionInt) { // differs from Python version because // cannot import interval here. let octaveStored = true; if (this.octave === undefined) { octaveStored = false; } const p = this.clone(); p.diatonicNoteNum += directionInt; if (p.accidental === undefined) { p.accidental = new Accidental(0); } while (p.ps % 12 !== this.ps % 12) { // octaveless p.accidental.alter += -1 * directionInt; } if (!inPlace) { return p; } this.step = p.step; this.accidental = p.accidental; if (p.microtone === undefined) { this.microtone = p.microtone; } if (!octaveStored) { this.octave = undefined; } else { this.octave = p.octave; } return p; } getHigherEnharmonic(inPlace=false) { return this._getEnharmonicHelper(inPlace, 1); } getLowerEnharmonic(inPlace=false) { return this._getEnharmonicHelper(inPlace, -1); } /* TODO: isEnharmonic, getEnharmonic, getAllCommonEnharmonics */ /** * Returns the vexflow name for the pitch in the given clef. * * @memberof music21.pitch.Pitch# * @param {clef.Clef} clefObj - the active {@link music21.clef.Clef} object * @returns {String} - representation in vexflow */ vexflowName(clefObj) { // returns a vexflow Key name for this pitch. let tempPitch = this; if (clefObj !== undefined) { try { tempPitch = clefObj.convertPitchToTreble(this); } catch (e) { console.log(e, clefObj); } } let accidentalType = 'n'; if (this.accidental !== undefined) { accidentalType = this.accidental.vexflowModifier; } const outName = tempPitch.step + accidentalType + '/' + tempPitch.octave; return outName; } } pitch.Pitch = Pitch; </pre> </article> </section> </div> <div class="clearfix"></div> <footer> <span class="copyright"> Music21j, Copyright © 2013-2018 Michael Scott Cuthbert and cuthbertLab. </span> <br /> <span class="jsdoc-message"> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Jul 4th 2018 using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>. </span> </footer> </div> <br clear="both"> </div> </div> <!--<script src="scripts/sunlight.js"></script>--> <script src="scripts/docstrap.lib.js"></script> <script src="scripts/bootstrap-dropdown.js"></script> <script src="scripts/toc.js"></script> <script> $( function () { $( "[id*='$']" ).each( function () { var $this = $( this ); $this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) ); } ); $( "#toc" ).toc( { anchorName : function ( i, heading, prefix ) { return $( heading ).attr( "id" ) || ( prefix + i ); }, selectors : "h1,h2,h3,h4", showAndHide : false, scrollTo : "100px" } ); $( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" ); $( "#main span[id^='toc']" ).addClass( "toc-shim" ); $( '.dropdown-toggle' ).dropdown(); // $( ".tutorial-section pre, .readme-section pre" ).addClass( "sunlight-highlight-javascript" ).addClass( "linenums" ); $( ".tutorial-section pre, .readme-section pre" ).each( function () { var $this = $( this ); var example = $this.find( "code" ); exampleText = example.html(); var lang = /{@lang (.*?)}/.exec( exampleText ); if ( lang && lang[1] ) { exampleText = exampleText.replace( lang[0], "" ); example.html( exampleText ); lang = lang[1]; } else { lang = "javascript"; } if ( lang ) { $this .addClass( "sunlight-highlight-" + lang ) .addClass( "linenums" ) .html( example.html() ); } } ); Sunlight.highlightAll( { lineNumbers : true, showMenu : true, enableDoclinks : true } ); } ); </script> <!--Navigation and Symbol Display--> <!--Google Analytics--> </body> </html>