Class: Music::Transcription::Program

Inherits:
Object
  • Object
show all
Includes:
Validatable
Defined in:
lib/music-transcription/model/program.rb,
lib/music-transcription/packing/program_packing.rb

Overview

Program defines markers (by starting note offset) and subprograms (list which markers are played).

Author:

  • James Tunnell

Instance Attribute Summary collapse

Attributes included from Validatable

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validatable

#invalid?, #valid?, #validatables, #validate

Constructor Details

#initialize(segments = []) ⇒ Program

Returns a new instance of Program.



13
14
15
# File 'lib/music-transcription/model/program.rb', line 13

def initialize segments = []
  @segments = segments
end

Instance Attribute Details

#segmentsObject

Returns the value of attribute segments.



11
12
13
# File 'lib/music-transcription/model/program.rb', line 11

def segments
  @segments
end

Class Method Details

.unpack(packing) ⇒ Object



11
12
13
14
# File 'lib/music-transcription/packing/program_packing.rb', line 11

def self.unpack packing
  segments = packing.map {|str| Segment.parse(str) }
  new segments
end

Instance Method Details

#==(other) ⇒ Object



26
27
28
# File 'lib/music-transcription/model/program.rb', line 26

def == other
  return other.respond_to?(:segments) && @segments == other.segments
end

#check_methodsObject



17
18
19
# File 'lib/music-transcription/model/program.rb', line 17

def check_methods
  [:ensure_increasing_segments, :ensure_nonnegative_segments]
end

#ensure_increasing_segmentsObject



39
40
41
42
43
44
# File 'lib/music-transcription/model/program.rb', line 39

def ensure_increasing_segments
  non_increasing = @segments.select {|seg| seg.first >= seg.last }
  if non_increasing.any?
    raise NonIncreasingError, "Non-increasing segments found #{non_increasing}"
  end
end

#ensure_nonnegative_segmentsObject



46
47
48
49
50
51
# File 'lib/music-transcription/model/program.rb', line 46

def ensure_nonnegative_segments
  negative = @segments.select {|seg| seg.first < 0 || seg.last < 0 }
  if negative.any?
    raise NegativeError, "Segments #{negative} have negative values"
  end
end

#include?(offset) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/music-transcription/model/program.rb', line 30

def include? offset
  @segments.each do |segment|
    if segment.include?(offset)
      return true
    end
  end
  return false
end

#lengthFloat

Returns the sum of all program segment lengths.

Returns:

  • (Float)

    the sum of all program segment lengths



22
23
24
# File 'lib/music-transcription/model/program.rb', line 22

def length
  segments.inject(0.0) { |length, segment| length + (segment.last - segment.first) }
end

#packObject



5
6
7
8
9
# File 'lib/music-transcription/packing/program_packing.rb', line 5

def pack
  @segments.map do |seg|
    seg.to_s
  end
end