Skip to main content

Note

The Note class represents a single musical note such as "D3", "G#4", "F-1", "Gb7", etc.

Note objects can be played back on a single channel by calling OutputChannel.playNote() or, on multiple channels of the same output, by calling Output.playNote().

The note has attack and release velocities set at 0.5 by default. These can be changed by passing in the appropriate option. It is also possible to set a system-wide default for attack and release velocities by using the WebMidi.defaults property.

If you prefer to work with raw MIDI values (0 to 127), you can use rawAttack and rawRelease to both get and set the values.

The note may have a duration. If it does, playback will be automatically stopped when the duration has elapsed by sending a "noteoff" event. By default, the duration is set to Infinity. In this case, it will never stop playing unless explicitly stopped by calling a method such as OutputChannel.stopNote(), Output.stopNote() or similar.

Since: 3.0.0

Constructorโ€‹

Creates a Note object.

Parameters

new Note(value)

ParameterTypeDefaultDescription
valuestring
number
The value used to create the note. If an identifier string is used, it must start with the note letter, optionally followed by an accidental and followed by the octave number ("C3", "G#4", "F-1", "Db7", etc.). If a number is used, it must be an integer between 0 and 127. In this case, middle C is considered to be C4 (note number 60).

Throws:

  • Error : Invalid note identifier
  • RangeError : Invalid name value
  • RangeError : Invalid accidental value
  • RangeError : Invalid octave value
  • RangeError : Invalid duration value
  • RangeError : Invalid attack value
  • RangeError : Invalid release value

Propertiesโ€‹

.accidentalโ€‹

Since: 3.0.0
Type: string

The accidental (#, ##, b or bb) of the note.

.attackโ€‹

Since: 3.0.0
Type: number

The attack velocity of the note as an integer between 0 and 1.

.durationโ€‹

Since: 3.0.0
Type: number

The duration of the note as a positive decimal number representing the number of milliseconds that the note should play for.

.identifierโ€‹

Since: 3.0.0
Type: string

The name, optional accidental and octave of the note, as a string.

.nameโ€‹

Since: 3.0.0
Type: string

The name (letter) of the note

.numberโ€‹

Since: 3.0.0
Type: number
Attributes: read-only

The MIDI number of the note (0 - 127). This number is derived from the note identifier using C4 as a reference for middle C.

.octaveโ€‹

Since: 3.0.0
Type: number

The octave of the note.

.rawAttackโ€‹

Since: 3.0.0
Type: number

The attack velocity of the note as a positive integer between 0 and 127.

.rawReleaseโ€‹

Since: 3.0.0
Type: number

The release velocity of the note as a positive integer between 0 and 127.

.releaseโ€‹

Since: 3.0.0
Type: number

The release velocity of the note as an integer between 0 and 1.


Methodsโ€‹

.getOffsetNumber(...)โ€‹

Returns a MIDI note number offset by octave and/or semitone. If the calculated value is less than 0, 0 will be returned. If the calculated value is more than 127, 127 will be returned. If an invalid value is supplied, 0 will be used.

Parameters

Signature: getOffsetNumber([octaveOffset], [semitoneOffset])

ParameterType(s)DefaultDescription
[octaveOffset]number
0An integer to offset the note number by octave.
[semitoneOffset]number
0An integer to offset the note number by semitone.

Return Value

Returns: number

An integer between 0 and 127