midinous 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,100 @@
1
+ # Copyright (C) 2019 James "Nornec" Ratliff
2
+ #
3
+ # This file is part of Midinous
4
+ #
5
+ # Midinous is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # Midinous is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with Midinous. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+ class GuiListener < MIDIEye::Listener
19
+
20
+ GUI_LISTEN_INTERVAL = 1.0 / 10
21
+
22
+ def gui_listen_loop
23
+ loop do
24
+ poll
25
+ @event.trigger_enqueued
26
+ sleep(GUI_LISTEN_INTERVAL)
27
+ end
28
+ end
29
+
30
+ def gui_listen
31
+ @listener = Thread.new do
32
+ begin
33
+ gui_listen_loop
34
+ rescue Exception => exception
35
+ Thread.main.raise(exception)
36
+ end
37
+ end
38
+ @listener.abort_on_exception = true
39
+ true
40
+ end
41
+ end
42
+
43
+ class Proc_Midi
44
+ attr_accessor :out_list, :in_list
45
+ attr_reader :out_id, :in_id, :in, :midi_in
46
+ def initialize(oid,iid)
47
+ @out_id = oid
48
+ @out_list = UniMIDI::Output.all
49
+ @out = UniMIDI::Output.use(@out_id)
50
+
51
+ @in_id = iid
52
+ @in = UniMIDI::Input.use(@in_id)
53
+ @in_list = UniMIDI::Input.all
54
+ @midi_in = GuiListener.new(@in)
55
+ @midi_in.listen_for(:class => [MIDIMessage::NoteOn,MIDIMessage::NoteOff]) {|e| Pl.set_note(e[:message].note.clamp(0,127))}
56
+ @midi_in.gui_listen
57
+ end
58
+
59
+ #Select the output device
60
+ def sel_out(id)
61
+ @out = UniMIDI::Output.use(id)
62
+ @out_id = id
63
+ end
64
+
65
+ #Select the input device
66
+ def sel_in(id)
67
+ @in = UniMIDI::Input.use(id)
68
+ @in_id = id
69
+ end
70
+
71
+ #Sends a note to an instrument
72
+ def note_send(channel,note,velocity)
73
+ @out.puts(0x90+channel-1,note,velocity)
74
+ end
75
+
76
+ #Release a note. Does not require a duration. Is called when a release signal is received.
77
+ def note_rlse(channel,note)
78
+ @out.puts(0x80+channel-1,note,0x00)
79
+ end
80
+
81
+ end
82
+
83
+ class NoteSender
84
+ attr_reader :note, :chan, :vel
85
+ def initialize(note,chan,vel)
86
+ @note = note
87
+ @chan = chan
88
+ @vel = vel
89
+ end
90
+
91
+ def play
92
+ Pm.note_send(@chan,@note,@vel)
93
+ end
94
+ def stop
95
+ Pm.note_rlse(@chan,@note)
96
+ end
97
+
98
+ end
99
+
100
+ Pm = Proc_Midi.new(0,0)