Class: Wavify::Sequencer::NoteSequence

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Enumerable[Event]
Defined in:
lib/wavify/sequencer/note_sequence.rb,
sig/sequencer.rbs

Overview

Note sequence parser for note/rest/MIDI token notation.

Defined Under Namespace

Classes: Event

Constant Summary collapse

NOTE_OFFSETS =

Note-name to semitone offset lookup table.

{
  "C" => 0,
  "C#" => 1,
  "DB" => 1,
  "D" => 2,
  "D#" => 3,
  "EB" => 3,
  "E" => 4,
  "F" => 5,
  "F#" => 6,
  "GB" => 6,
  "G" => 7,
  "G#" => 8,
  "AB" => 8,
  "A" => 9,
  "A#" => 10,
  "BB" => 10,
  "B" => 11
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notation, default_octave: 4) ⇒ NoteSequence

Returns a new instance of NoteSequence.

Parameters:

  • notation (String)
  • default_octave: (Integer) (defaults to: 4)

Raises:



43
44
45
46
47
48
49
50
# File 'lib/wavify/sequencer/note_sequence.rb', line 43

def initialize(notation, default_octave: 4)
  raise InvalidNoteError, "note sequence notation must be String" unless notation.is_a?(String)

  @notation = notation.dup.freeze
  @default_octave = validate_default_octave!(default_octave)
  @events = parse_events(@notation).each(&:freeze).freeze
  freeze
end

Instance Attribute Details

#default_octaveInteger (readonly)

Returns the value of attribute default_octave.

Returns:

  • (Integer)


41
42
43
# File 'lib/wavify/sequencer/note_sequence.rb', line 41

def default_octave
  @default_octave
end

#eventsArray[Event] (readonly)

Returns the value of attribute events.

Returns:



41
42
43
# File 'lib/wavify/sequencer/note_sequence.rb', line 41

def events
  @events
end

#notationString (readonly)

Returns the value of attribute notation.

Returns:

  • (String)


41
42
43
# File 'lib/wavify/sequencer/note_sequence.rb', line 41

def notation
  @notation
end

Instance Method Details

#[](index) ⇒ Event?

Returns an event at the given index.

Parameters:

  • index (Integer)

Returns:



67
68
69
# File 'lib/wavify/sequencer/note_sequence.rb', line 67

def [](index)
  @events[index]
end

#eachNoteSequence #eachEnumerator[Event, NoteSequence]

Enumerates parsed events.

Overloads:

Yields:

  • (event)

Yield Parameters:

Returns:

  • (Enumerator)


57
58
59
60
61
# File 'lib/wavify/sequencer/note_sequence.rb', line 57

def each(&)
  return enum_for(:each) unless block_given?

  @events.each(&)
end

#lengthInteger Also known as: size

Returns number of parsed events.

Returns:

  • (Integer)

    number of parsed events



72
73
74
# File 'lib/wavify/sequencer/note_sequence.rb', line 72

def length
  @events.length
end

#midi_notesArray<Integer, nil>

Returns MIDI notes preserving rests as nil.

Returns:

  • (Array<Integer, nil>)

    MIDI notes preserving rests as nil



79
80
81
# File 'lib/wavify/sequencer/note_sequence.rb', line 79

def midi_notes
  @events.map(&:midi_note)
end

#note_eventsArray<Event>

Returns events excluding rests.

Returns:

  • (Array<Event>)

    events excluding rests



84
85
86
# File 'lib/wavify/sequencer/note_sequence.rb', line 84

def note_events
  @events.reject(&:rest?)
end