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
# File 'lib/music-transcription/model/note.rb', line 14

def initialize duration, pitches = [], articulation: DEFAULT_ARTICULATION, accented: false, links: {}
  @duration = duration
  @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



97
98
99
100
101
# File 'lib/music-transcription/model/note.rb', line 97

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



32
33
34
35
36
37
38
# File 'lib/music-transcription/model/note.rb', line 32

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



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

def check_methods
  [ :ensure_positive_duration ]
end

#cloneObject



40
41
42
# File 'lib/music-transcription/model/note.rb', line 40

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

#ensure_positive_durationObject



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

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

#stretch(ratio) ⇒ Object



56
57
58
# File 'lib/music-transcription/model/note.rb', line 56

def stretch ratio
  self.clone.stretch! ratio
end

#stretch!(ratio) ⇒ Object



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

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

#to_sObject



65
66
67
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
# File 'lib/music-transcription/model/note.rb', line 65

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



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

def transpose diff
  self.clone.transpose! diff
end

#transpose!(diff) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/music-transcription/model/note.rb', line 48

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