Class: StringyFi::Note

Inherits:
Object
  • Object
show all
Defined in:
lib/stringyfi/note.rb

Overview

Represents a musical note

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, octave = 4, alter = nil, duration = 1, duration_type = 'quarter') ⇒ Note

Returns a new instance of Note.



7
8
9
10
11
12
13
14
15
16
# File 'lib/stringyfi/note.rb', line 7

def initialize(name, octave = 4, alter = nil, duration = 1, duration_type = 'quarter')
  self.name = name.to_s.upcase
  self.octave = octave.to_s.to_i
  self.alter = begin
    alter.to_i
  rescue StandardError
    0
  end
  self.fractional_duration = calculate_fractional_duration(duration, duration_type)
end

Instance Attribute Details

#alterObject

Returns the value of attribute alter.



5
6
7
# File 'lib/stringyfi/note.rb', line 5

def alter
  @alter
end

#fractional_durationObject

Returns the value of attribute fractional_duration.



5
6
7
# File 'lib/stringyfi/note.rb', line 5

def fractional_duration
  @fractional_duration
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/stringyfi/note.rb', line 5

def name
  @name
end

#octaveObject

Returns the value of attribute octave.



5
6
7
# File 'lib/stringyfi/note.rb', line 5

def octave
  @octave
end

#tempoObject

Returns the value of attribute tempo.



5
6
7
# File 'lib/stringyfi/note.rb', line 5

def tempo
  @tempo
end

Instance Method Details

#<=>(other) ⇒ Object



96
97
98
# File 'lib/stringyfi/note.rb', line 96

def <=>(other)
  to_note_id <=> other.to_note_id
end

#calculate_fractional_duration(duration, duration_type) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/stringyfi/note.rb', line 22

def calculate_fractional_duration(duration, duration_type)
  divisor = {
    'half' => 2.0,
    'quarter' => 4.0,
    'eighth' => 8.0,
    '16th' => 16.0,
    '32nd' => 32.0
  }[duration_type] || 1.0
  duration / divisor
end

#inspectObject



92
93
94
# File 'lib/stringyfi/note.rb', line 92

def inspect
  "\"#{to_str}\""
end

#rest?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/stringyfi/note.rb', line 18

def rest?
  name == ''
end

#stringy_durations(shortest_fractional_duration) ⇒ Object

return [short, medium, long, very_long] repeats for the note TODO: scale medium, long, very_long durations correctly



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/stringyfi/note.rb', line 35

def stringy_durations(shortest_fractional_duration)
  time_units = (fractional_duration / shortest_fractional_duration).to_i
  if time_units >= 8
    [0, 0, 0, 1]
  elsif time_units >= 4
    [0, 0, 1, 0]
  elsif time_units >= 2
    [0, 1, 0, 0]
  else
    [1, 0, 0, 0]
  end
end

#to_note_idObject

Returns the note ID - unique for each frequency



84
85
86
# File 'lib/stringyfi/note.rb', line 84

def to_note_id
  "#{octave}:#{name}:#{alter}"
end

#to_note_nameObject

Returns the note name, regardless of octave



68
69
70
71
72
73
74
75
76
77
# File 'lib/stringyfi/note.rb', line 68

def to_note_name
  case alter
  when 1
    "#{name}#"
  when -1
    "#{name}b"
  else
    name
  end
end

#to_sObject



79
80
81
# File 'lib/stringyfi/note.rb', line 79

def to_s
  to_note_name
end

#to_strObject



88
89
90
# File 'lib/stringyfi/note.rb', line 88

def to_str
  to_note_id
end

#to_stringy(current_octave) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/stringyfi/note.rb', line 48

def to_stringy(current_octave)
  relative_octave = octave - current_octave + 1
  case alter
  when 1
    "#{name}#{relative_octave}S"
  when -1
    sharpy_name = {
      'B' => 'A',
      'E' => 'D',
      'A' => 'G',
      'D' => 'C',
      'G' => 'F'
    }[name]
    "#{sharpy_name}#{relative_octave}S"
  else
    "#{name}#{relative_octave}"
  end
end