class Mass::Pitch
Represents the pitch of a given note, and calculates its correct MIDI value, leaving the Note
class more for calculating the proper duration and actually playing out the note.
Constants
- REQUIRED
A collection of attributes which are required for this object to be considered a valid pitch.
@type [Array<Symbol>]
- VALUES
A dictionary of MIDI note values that are substituted for a given String note value.
@type [Array<String>]
Attributes
Public Class Methods
Find a Pitch
by its given id
.
@param [String] by_id @return [Pitch] when the id is valid
# File lib/mass/pitch.rb, line 58 def self.find(by_id = nil) return if by_id.nil? new id: by_id end
@param [String] id - Identifier string of this pitch.
# File lib/mass/pitch.rb, line 43 def initialize(id: '') @id = id.to_s @name = @id.gsub(/\d/, '').to_s @octave = @id.gsub(/#{name}/, '').to_i @value = begin VALUES[name] rescue nil end end
Public Instance Methods
Use the id
parameter to define equivalence.
@return [Boolean] whether both pitches have the same ID.
# File lib/mass/pitch.rb, line 83 def ==(other) other.id == id end
The MIDI value of this pitch.
@return [Integer]
# File lib/mass/pitch.rb, line 76 def to_i value + octave_modifier end
Make sure the name
and octave
attributes have been correctly parsed out of the given id
.
@return [Boolean] whether the object is valid
# File lib/mass/pitch.rb, line 67 def valid? required_params.all? do |param| !param.nil? && (param != '') && (param != 0) end end
Private Instance Methods
The number by which the given scalar pitch gets modified to produce the correct MIDI note value for playback.
@private @return [Integer]
# File lib/mass/pitch.rb, line 100 def octave_modifier 12 * octave end
# File lib/mass/pitch.rb, line 89 def required_params REQUIRED.map do |param_name| send param_name end end