Class: Music::Performance::NoteSequence

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, stop, pitches, attacks) ⇒ NoteSequence

Returns a new instance of NoteSequence.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/music-performance/model/note_sequence.rb', line 41

def initialize start, stop, pitches, attacks
  if start >= stop
    raise ArgumentError, "start #{start} is not less than stop #{stop}"
  end
  
  if pitches.empty?
    raise ArgumentError, "no pitches given (at least one pitch is required at start offset)"
  end
  
  unless pitches.has_key?(start)
    raise ArgumentError, "no start pitch given"
  end
  
  pitches.keys.each do |offset|
    unless offset.between?(start,stop)
      raise ArgumentError, "pitch offset #{offset} is not between start #{start} and stop #{stop}"
    end
  end
  
  if attacks.empty?
    raise ArgumentError, "no attacks given (at least one is required at start offset)"
  end
  
  unless attacks.has_key?(start)
    raise ArgumentError, "no start attack given"
  end
  
  attacks.keys.each do |offset|
    unless offset.between?(start,stop)
      raise ArgumentError, "attack offset #{offset} is not between start #{start} and stop #{stop}"
    end
  end
  
  @start, @stop = start, stop
  @pitches, @attacks = pitches, attacks
end

Instance Attribute Details

#attacksObject (readonly)

Returns the value of attribute attacks.



40
41
42
# File 'lib/music-performance/model/note_sequence.rb', line 40

def attacks
  @attacks
end

#pitchesObject (readonly)

Returns the value of attribute pitches.



40
41
42
# File 'lib/music-performance/model/note_sequence.rb', line 40

def pitches
  @pitches
end

#startObject (readonly)

Returns the value of attribute start.



40
41
42
# File 'lib/music-performance/model/note_sequence.rb', line 40

def start
  @start
end

#stopObject (readonly)

Returns the value of attribute stop.



40
41
42
# File 'lib/music-performance/model/note_sequence.rb', line 40

def stop
  @stop
end

Class Method Details

.adjust_duration(duration, articulation) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/music-performance/model/note_sequence.rb', line 22

def self.adjust_duration duration, articulation
  x = duration
  y = Math.log2(x)

  case articulation
  when Music::Transcription::Articulations::TENUTO
    x
  when Music::Transcription::Articulations::PORTATO
    x / (1 + 2**(y-1))
  when Music::Transcription::Articulations::STACCATO
    x / (1 + 2**(y))
  when Music::Transcription::Articulations::STACCATISSIMO
    x / (1 + 2**(y+1))
  else
    x - (1/16.0)*(1/(1+2**(-y)))
  end
end

.from_elements(offset, elements) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/music-performance/model/note_sequence.rb', line 78

def self.from_elements offset, elements
  pitches = {}
  attacks = {}
  start = offset
  
  if elements.empty?
    raise ArgumentError, "no elements given"
  end
    
  last = elements.last
  skip_attack = false
  elements.each do |el|
    if skip_attack
      unless pitches.max[1] == el.pitch
        pitches[offset] = el.pitch
      end
    else
      pitches[offset] = el.pitch
      attacks[offset] = el.accented ? ACCENTED : UNACCENTED        
    end      
    skip_attack = el.slurred?
    
    unless el.equal?(last)
      offset += el.duration
    end
  end
  stop = offset + NoteSequence.adjust_duration(last.duration, last.articulation)
  
  new(start, stop, pitches, attacks)
end

Instance Method Details

#durationObject



109
# File 'lib/music-performance/model/note_sequence.rb', line 109

def duration; @stop - @start; end