Class: Music::Transcription::Note

Inherits:
Object
  • Object
show all
Includes:
Parseable, Validatable
Defined in:
lib/music-transcription/model/note.rb,
lib/music-transcription/parsing/convenience_methods.rb

Constant Summary collapse

DEFAULT_ARTICULATION =
Articulations::NORMAL
PARSER =
Parsing::NoteParser.new
CONVERSION_METHOD =
:to_note

Constants included from Parseable

Parseable::DEFAULT_SPLIT_PATTERN

Instance Attribute Summary collapse

Attributes included from Validatable

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parseable

included

Methods included from Validatable

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

Constructor Details

#initialize(duration, pitches = [], articulation: DEFAULT_ARTICULATION, accented: false, links: {}) ⇒ Note

Returns a new instance of Note.



14
15
16
17
18
19
20
21
22
23
# File 'lib/music-transcription/model/note.rb', line 14

def initialize duration, pitches = [], articulation: DEFAULT_ARTICULATION, accented: false, links: {}
  @duration = duration
  if !pitches.is_a? Enumerable
    pitches = [ pitches ]
  end
  @pitches = Set.new(pitches).sort
  @articulation = articulation
  @accented = accented
  @links = links
end

Instance Attribute Details

#accentedObject

Returns the value of attribute accented.



10
11
12
# File 'lib/music-transcription/model/note.rb', line 10

def accented
  @accented
end

#articulationObject

Returns the value of attribute articulation.



10
11
12
# File 'lib/music-transcription/model/note.rb', line 10

def articulation
  @articulation
end

#durationObject

Returns the value of attribute duration.



10
11
12
# File 'lib/music-transcription/model/note.rb', line 10

def duration
  @duration
end

Returns the value of attribute links.



9
10
11
# File 'lib/music-transcription/model/note.rb', line 9

def links
  @links
end

#pitchesObject (readonly)

Returns the value of attribute pitches.



9
10
11
# File 'lib/music-transcription/model/note.rb', line 9

def pitches
  @pitches
end

Class Method Details

.add_note_method(name, dur) ⇒ Object



100
101
102
103
104
# File 'lib/music-transcription/model/note.rb', line 100

def self.add_note_method(name, dur)
  self.class.send(:define_method,name.to_sym) do |pitches = [], articulation: DEFAULT_ARTICULATION, links: {}, accented: false|
    Note.new(dur, pitches, articulation: articulation, links: links, accented: accented)
  end
end

Instance Method Details

#==(other) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/music-transcription/model/note.rb', line 35

def == other
  return (@duration == other.duration) &&
  (self.pitches == other.pitches) &&
  (@links.to_a.sort == other.links.to_a.sort) &&
  (@articulation == other.articulation) &&
  (@accented == other.accented)
end

#check_methodsObject



25
26
27
# File 'lib/music-transcription/model/note.rb', line 25

def check_methods
  [ :ensure_positive_duration ]
end

#cloneObject



43
44
45
# File 'lib/music-transcription/model/note.rb', line 43

def clone
  Marshal.load(Marshal.dump(self))
end

#ensure_positive_durationObject



29
30
31
32
33
# File 'lib/music-transcription/model/note.rb', line 29

def ensure_positive_duration
  unless @duration > 0
    raise NonPositiveError, "duration #{@duration} is not positive"
  end
end

#stretch(ratio) ⇒ Object



59
60
61
# File 'lib/music-transcription/model/note.rb', line 59

def stretch ratio
  self.clone.stretch! ratio
end

#stretch!(ratio) ⇒ Object



63
64
65
66
# File 'lib/music-transcription/model/note.rb', line 63

def stretch! ratio
  @duration *= ratio
  return self
end

#to_sObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/music-transcription/model/note.rb', line 68

def to_s
  d = @duration.to_r
  if d.denominator == 1
    dur_str = "#{d.numerator}"
  elsif d.numerator == 1
    dur_str = "/#{d.denominator}"
  else
    dur_str = d.to_s
  end
  
  art_str = case @articulation
  when Articulations::SLUR then "="
  when Articulations::LEGATO then "|"
  when Articulations::TENUTO then "_"
  when Articulations::PORTATO then "%"
  when Articulations::STACCATO then "."
  when Articulations::STACCATISSIMO then "'"
  else ""
  end
  
  pitch_links_str = @pitches.map do |p|
    if @links.has_key?(p)
      p.to_s + @links[p].to_s
    else
      p.to_s
    end
  end.join(",")
  
  acc_str = @accented ? "!" : ""
  return dur_str + art_str + pitch_links_str + acc_str
end

#transpose(diff) ⇒ Object



47
48
49
# File 'lib/music-transcription/model/note.rb', line 47

def transpose diff
  self.clone.transpose! diff
end

#transpose!(diff) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/music-transcription/model/note.rb', line 51

def transpose! diff
  @pitches = @pitches.map {|pitch| pitch.transpose(diff) }
  @links = Hash[ @links.map do |k,v|
    [ k.transpose(diff), v.transpose(diff) ]
  end ]
  return self
end