collavoce 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/collavoce.rb CHANGED
@@ -1,3 +1,14 @@
1
+ module Collavoce
2
+ def self.device_name
3
+ @device_name
4
+ end
5
+
6
+ def self.device_name=(device_name)
7
+ @device_name = device_name
8
+ end
9
+ end
10
+
1
11
  require 'collavoce/note'
2
12
  require 'collavoce/voice'
3
13
  require 'collavoce/tracker'
14
+ require 'collavoce/sigint'
@@ -1,6 +1,7 @@
1
1
  module Collavoce
2
2
  class Note
3
3
  class UnparsableError < RuntimeError; end
4
+ ShortMessage = Java::javax.sound.midi.ShortMessage
4
5
 
5
6
  attr_accessor :value
6
7
  attr_accessor :duration
@@ -83,5 +84,31 @@ module Collavoce
83
84
  def ==(other)
84
85
  value == other.value && duration == other.duration
85
86
  end
87
+
88
+ def on(receiver, channel)
89
+ @receiver = receiver
90
+ @channel = channel
91
+
92
+ message = ShortMessage.new
93
+ message.set_message(ShortMessage::NOTE_ON, @channel, value, 127);
94
+ @receiver.send(message, 0)
95
+ end
96
+
97
+ def off
98
+ message = ShortMessage.new
99
+ message.set_message(ShortMessage::NOTE_OFF, @channel, value, 127);
100
+ @receiver.send(message, 0)
101
+ end
102
+
103
+ def play(receiver, channel, bar_duration)
104
+ sleep_duration = bar_duration * duration
105
+ if value
106
+ on(receiver, channel)
107
+ sleep sleep_duration
108
+ off
109
+ else
110
+ sleep sleep_duration
111
+ end
112
+ end
86
113
  end
87
114
  end
@@ -0,0 +1,13 @@
1
+ module Collavoce
2
+ def self.stop
3
+ @running = false
4
+ end
5
+
6
+ def self.running
7
+ @running.nil? || @running
8
+ end
9
+ end
10
+
11
+ trap("INT") do
12
+ Collavoce.stop
13
+ end
@@ -1,10 +1,7 @@
1
1
  module Collavoce
2
2
  class Voice
3
- include Java
4
-
5
- MidiSystem = javax.sound.midi.MidiSystem
6
- ShortMessage = javax.sound.midi.ShortMessage
7
-
3
+ MidiSystem = Java::javax.sound.midi.MidiSystem
4
+
8
5
  attr_accessor :notes
9
6
 
10
7
  def self.notes(notes)
@@ -18,7 +15,7 @@ module Collavoce
18
15
  def self.new(options = {})
19
16
  super({:notes => @notes, :channel => @channel}.merge(options))
20
17
  end
21
-
18
+
22
19
  def initialize(options = {})
23
20
  @channel = (options.delete(:channel) || 1) - 1
24
21
  @notes = options.delete(:notes).map { |n| Note.new(n) }
@@ -26,38 +23,45 @@ module Collavoce
26
23
  @bar_duration = (60.to_f / @bpm) * 4
27
24
  end
28
25
 
26
+ def output_devices
27
+ devices = MidiSystem.get_midi_device_info.to_a.map do |info|
28
+ MidiSystem.get_midi_device(info)
29
+ end
30
+ devices.select do |device|
31
+ device.get_max_receivers != 0
32
+ end
33
+ end
34
+
29
35
  def device
30
- all = MidiSystem.get_midi_device_info.to_a
31
- possible = all.select { |i| i.get_name == "Bus 1" }
32
- devices = possible.map { |i| MidiSystem.get_midi_device(i) }
33
- device = devices.select { |d| d.get_max_receivers != 0 }.first
34
- device.open
35
- device
36
+ name = Collavoce.device_name
37
+ selected_device = output_devices.detect do |device|
38
+ device.get_device_info.get_name == name
39
+ end
40
+
41
+ if !selected_device
42
+ raise "Couldn't find device called #{name}" if name
43
+ raise "No output devices available" if output_devices.empty?
44
+ selected_device = output_devices.first
45
+ $stderr.puts "INFO: Sending notes to #{selected_device.get_device_info.get_name}"
46
+ end
47
+
48
+ selected_device.open
49
+ selected_device
36
50
  end
37
-
51
+
38
52
  def receiver
39
53
  return @receiver if @receiver
40
54
  @receiver = device.get_receiver
41
55
  end
42
-
56
+
43
57
  def send_note(note, channel)
44
- duration = @bar_duration * note.duration
45
- if note.value
46
- noteon = ShortMessage.new
47
- noteon.set_message(ShortMessage::NOTE_ON, channel, note.value, 127);
48
- noteoff = ShortMessage.new
49
- noteoff.set_message(ShortMessage::NOTE_OFF, channel, note.value, 127);
50
- receiver.send(noteon, 0)
51
- sleep duration
52
- receiver.send(noteoff, 0)
53
- else
54
- sleep duration
55
- end
58
+ note.play(receiver, channel, @bar_duration)
56
59
  end
57
-
60
+
58
61
  def play(this_many = 1)
59
62
  this_many.times do
60
63
  @notes.each do |note|
64
+ break unless Collavoce.running
61
65
  send_note(note, @channel)
62
66
  end
63
67
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mat Schaffer
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-03 00:00:00 -05:00
17
+ date: 2011-01-20 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -53,6 +53,7 @@ files:
53
53
  - README.rdoc
54
54
  - lib/collavoce.rb
55
55
  - lib/collavoce/note.rb
56
+ - lib/collavoce/sigint.rb
56
57
  - lib/collavoce/tracker.rb
57
58
  - lib/collavoce/voice.rb
58
59
  has_rdoc: true