Class: Collavoce::Note

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

Defined Under Namespace

Classes: UnparsableError

Constant Summary collapse

ShortMessage =
Java::javax.sound.midi.ShortMessage
NOTES =
{
  "C" => 60,
  "D" => 62,
  "E" => 64,
  "F" => 65,
  "G" => 67,
  "A" => 69,
  "B" => 71,
}
DIVISIONS =
{
  "w" => 1,
  "h" => 1.to_f / 2,
  "q" => 1.to_f / 4,
  "e" => 1.to_f / 8,
  "s" => 1.to_f / 16,
  "t" => 1.to_f / 32
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(note) ⇒ Note

Returns a new instance of Note.



28
29
30
31
32
33
34
35
# File 'lib/collavoce/note.rb', line 28

def initialize(note)
  case note
  when String
    init_from_string(note)
  when Note
    init_from_note(note)
  end
end

Instance Attribute Details

#durationObject

Returns the value of attribute duration.



7
8
9
# File 'lib/collavoce/note.rb', line 7

def duration
  @duration
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/collavoce/note.rb', line 6

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  value == other.value && duration == other.duration
end

#augObject



72
73
74
75
76
# File 'lib/collavoce/note.rb', line 72

def aug
  copy = Note.new(self)
  copy.aug!
  copy
end

#aug!Object



64
65
66
# File 'lib/collavoce/note.rb', line 64

def aug!
  @value += 1
end

#dimObject



78
79
80
81
82
# File 'lib/collavoce/note.rb', line 78

def dim
  copy = Note.new(self)
  copy.dim!
  copy
end

#dim!Object



68
69
70
# File 'lib/collavoce/note.rb', line 68

def dim!
  @value -= 1
end

#init_from_note(note) ⇒ Object



59
60
61
62
# File 'lib/collavoce/note.rb', line 59

def init_from_note(note)
  @value    = note.value
  @duration = note.duration
end

#init_from_string(note) ⇒ Object

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/collavoce/note.rb', line 37

def init_from_string(note)
  match = note.match(/^([ABCDEFGR])([#]*)?([b]*)?(\d)?([whqest]*)/)

  raise UnparsableError.new("Couldn't parse note: #{note}") unless match

  if match[5].empty?
    @duration = DIVISIONS["q"]
  else
    @duration = 0
    match[5].each_char do |c|
      @duration += DIVISIONS[c]
    end
  end

  if base_value = NOTES[match[1]]
    offset = (match[2] || "").length
    offset -= (match[3] || "").length
    octave = (match[4] || "4").to_i
    @value = base_value + ((octave - 4) * 12) + offset
  end
end

#offObject



97
98
99
100
101
# File 'lib/collavoce/note.rb', line 97

def off
  message = ShortMessage.new
  message.set_message(ShortMessage::NOTE_OFF, @channel, value, 127);
  @receiver.send(message, 0)
end

#on(receiver, channel) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/collavoce/note.rb', line 88

def on(receiver, channel)
  @receiver = receiver
  @channel = channel

  message = ShortMessage.new
  message.set_message(ShortMessage::NOTE_ON, @channel, value, 127);
  @receiver.send(message, 0)
end

#play(receiver, channel, bar_duration) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/collavoce/note.rb', line 103

def play(receiver, channel, bar_duration)
  sleep_duration = bar_duration * duration
  if value
    on(receiver, channel)
    sleep sleep_duration
    off
  else
    sleep sleep_duration
  end
end