class Mystic::Note

Overview

Represents a note, consisting of a note name (letter and accidental) and octave

To create a Note:

# Middle C
Note.new("C4")

# Middle C. Octave defaults to 4 if not provided
Note.new("C")

# Middle C
# In the case of accidentals, will default to using sharps.
Note.from_midi(60)

# The A above middle C
# This is used internally and may be less useful for most end users.
# See `Coords` for more information on Coordinate representation of pitches
Note.from_coords(Coords.new(-1, 3))

Defined in:

mystic/note.cr

Constant Summary

ALL_PITCHES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
BASE_OCTAVE = 4

Coordinates assume starting from C4

LETTER_PITCH_CLASSES = {C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11}
NAME_PATTERN = "(?<letter>[abcdefgABCDEFG])(?<accidental>[b♭𝄫]+|[#♯x𝄪]*)"

Regex pattern for a note name

PITCH_COORDS = {C: Coords.new(0, 0), D: Coords.new(-1, 2), E: Coords.new(-2, 4), F: Coords.new(1, -1), G: Coords.new(0, 1), A: Coords.new(-1, 3), B: Coords.new(-2, 5)}
PITCHES = ["C", "D", "E", "F", "G", "A", "B"]

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.from_coords(coords : Coords) : self #

Returns a note corresponding to a given Coords


[View source]
def self.new(s : String) #

Returns a note corresponding to a given string representation


[View source]

Class Method Detail

def self.from_midi(i : Int32) #

Returns a note corresponding to a given midi value


[View source]

Instance Method Detail

def +(interval : Interval) : Note #

[View source]
def -(interval : Interval) : Note #

[View source]
def -(other : self) : Interval #

[View source]
def <(other : self) : Bool #

[View source]
def <=>(other : self) : Int32 #

Note: this compares notes as ordered on a staff rather than by pitch.

For example, a Cx4 < Db4 even though Cx4 sounds higher.


[View source]
def ==(other : self) : Bool #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


[View source]
def >(other : self) : Bool #

[View source]
def accidental : String #

[View source]
def accidental_offset : Int32 #

[View source]
def chroma : Int32 #

Returns the numerical pitch class (0-11)


[View source]
def coords : Coords #

Returns Coords representation


[View source]
def frequency(tuning = 440.0) : Float64 #

Returns the frequency in Hz


[View source]
def letter : String #

[View source]
def midi : Int32 #

Returns the midi value


[View source]
def name : String #

[View source]
def octave : Int32 #

[View source]
def pitch_class : PitchClass #

Returns the PitchClass


[View source]
def to_s(io : IO) : Nil #
Description copied from class Reference

Appends a short String representation of this object which includes its class name and its object address.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).to_s # => #<Person:0x10a199f20>

[View source]