Class: Musicality::NoteSequence

Inherits:
Object
  • Object
show all
Defined in:
lib/musicality/performance/model/note_sequence.rb

Defined Under Namespace

Classes: Element

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(offset, separation, elements = []) ⇒ NoteSequence

Returns a new instance of NoteSequence.



24
25
26
27
28
# File 'lib/musicality/performance/model/note_sequence.rb', line 24

def initialize offset, separation, elements = []
  @offset = offset
  @separation = separation
  @elements = elements
end

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



23
24
25
# File 'lib/musicality/performance/model/note_sequence.rb', line 23

def elements
  @elements
end

#offsetObject Also known as: start

Returns the value of attribute offset.



23
24
25
# File 'lib/musicality/performance/model/note_sequence.rb', line 23

def offset
  @offset
end

#separationObject

Returns the value of attribute separation.



23
24
25
# File 'lib/musicality/performance/model/note_sequence.rb', line 23

def separation
  @separation
end

Class Method Details

.adjust_duration(duration, separation) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/musicality/performance/model/note_sequence.rb', line 6

def self.adjust_duration duration, separation
  function = case separation
  when Separation::TENUTO
    DurationFunctions::TENUTO_DURATION
  when Separation::PORTATO
    DurationFunctions::PORTATO_DURATION
  when Separation::STACCATO
    DurationFunctions::STACCATO_DURATION
  when Separation::STACCATISSIMO
    DurationFunctions::STACCATISSIMO_DURATION
  else
    DurationFunctions::NORMAL_DURATION
  end

  function.at(duration)
end

Instance Method Details

#durationObject



66
67
68
# File 'lib/musicality/performance/model/note_sequence.rb', line 66

def duration
  stop - offset
end

#first_pitchObject



74
75
76
# File 'lib/musicality/performance/model/note_sequence.rb', line 74

def first_pitch
  elements.first.pitch
end

#full_durationObject



70
71
72
# File 'lib/musicality/performance/model/note_sequence.rb', line 70

def full_duration
  @elements.map {|el| el.duration }.inject(0,:+)
end

#last_attackObject



82
83
84
# File 'lib/musicality/performance/model/note_sequence.rb', line 82

def last_attack
  elements.last.attack
end

#last_pitchObject



78
79
80
# File 'lib/musicality/performance/model/note_sequence.rb', line 78

def last_pitch
  elements.last.pitch
end

#offsetsObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/musicality/performance/model/note_sequence.rb', line 51

def offsets
  raise "contains no elements" if elements.empty?

  off = @offset
  elements.map do |e|
    x = off
    off += e.duration
    x
  end
end

#simplify!Object

any consecutive elements with the same pitch and no attack will be combined



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/musicality/performance/model/note_sequence.rb', line 31

def simplify!
  return if @elements.none?

  prev_element = @elements[0]
  idx = 1

  while idx < @elements.size
    element = @elements[idx]
    if (element.pitch == prev_element.pitch) && (element.attack == Attack::NONE)
      prev_element.duration += element.duration
      @elements.delete_at(idx)
    else
      prev_element = element
      idx += 1
    end
  end
end

#stopObject



62
63
64
# File 'lib/musicality/performance/model/note_sequence.rb', line 62

def stop
  offsets.last + NoteSequence.adjust_duration(elements.last.duration, separation)
end