Class: StringyFi::Note

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

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
# File 'lib/stringyfi/note.rb', line 7

def initialize(name, octave=4, alter=nil, duration=1, duration_type="quarter")
  self.name = "#{name}".upcase
  self.octave = "#{octave}".to_i
  self.alter = alter.to_i rescue 0
  self.fractional_duration = calculate_fractional_duration(duration, duration_type)
end

Instance Attribute Details

#alterObject

Returns the value of attribute alter.



3
4
5
# File 'lib/stringyfi/note.rb', line 3

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.



3
4
5
# File 'lib/stringyfi/note.rb', line 3

def name
  @name
end

#octaveObject

Returns the value of attribute octave.



3
4
5
# File 'lib/stringyfi/note.rb', line 3

def octave
  @octave
end

#tempoObject

Returns the value of attribute tempo.



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

def tempo
  @tempo
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#calculate_fractional_duration(duration, duration_type) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/stringyfi/note.rb', line 18

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



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

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

#rest?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/stringyfi/note.rb', line 14

def rest?
  name == ""
end

#stringy_durations(shortest_fractional_duration) ⇒ Object

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



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/stringyfi/note.rb', line 31

def stringy_durations(shortest_fractional_duration)
  time_units = (fractional_duration / shortest_fractional_duration).to_i
  case
  when time_units >= 8
    [0, 0, 0, 1]
  when time_units >= 4
    [0, 0, 1, 0]
  when 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



81
82
83
# File 'lib/stringyfi/note.rb', line 81

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

#to_note_nameObject

Returns the note name, regardless of octave



65
66
67
68
69
70
71
72
73
74
# File 'lib/stringyfi/note.rb', line 65

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

#to_sObject



76
77
78
# File 'lib/stringyfi/note.rb', line 76

def to_s
  to_note_name
end

#to_strObject



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

def to_str
  to_note_id
end

#to_stringy(current_octave) ⇒ Object



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

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