rawmidi 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +91 -0
- data/Rakefile +10 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/lib/rawmidi.rb +8 -0
- data/lib/rawmidi/api.rb +222 -0
- data/lib/rawmidi/card.rb +42 -0
- data/lib/rawmidi/device.rb +52 -0
- data/lib/rawmidi/input.rb +30 -0
- data/lib/rawmidi/output.rb +31 -0
- data/lib/rawmidi/version.rb +3 -0
- data/rawmidi.gemspec +29 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 641a8a579aadcd2a30cae88e0f3ec1a4e75e5461
|
4
|
+
data.tar.gz: ed4b243b85a9672b9de06f58255a3f2f9ad7e80c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f37825861dc04dca1598ec577c250329a25e9305214f577a8f4479aa96ace1fe6e1e1489953103a186adf5afbf31f278be44bcd47580c85ad94d7b3005fec95c
|
7
|
+
data.tar.gz: bf9079c3c7158d7ae09e4adfe1ff1b6c33eda884d568fc630737d9ea18d66d982596fd11c81e521a68182fb84f6dd83a347c248bc6ce1c3841621f41a6b35bdf
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at munshkr@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Damián Silvani
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# rawmidi-rb
|
2
|
+
|
3
|
+
ALSA RawMIDI bindings for Ruby
|
4
|
+
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
### List cards and devices
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
RawMIDI::Card.all
|
12
|
+
#=> [#<RawMIDI::Card:0x002af7bb0f26ec hw:0 "HDA Intel PCH">,
|
13
|
+
#<RawMIDI::Card:0x002af7bb0f269c hw:1 "CH345">]
|
14
|
+
|
15
|
+
card = RawMIDI::Card.new(1)
|
16
|
+
#=> #<RawMIDI::Card:0x002af7bb094b3c hw:1 "CH345">
|
17
|
+
|
18
|
+
card.outputs
|
19
|
+
#=> [#<RawMIDI::Output:0x002af7bb489760 hw:1,0,0 "CH345" closed>]
|
20
|
+
|
21
|
+
RawMIDI::Output.all
|
22
|
+
#=> [#<RawMIDI::Output:0x002af7bb489760 hw:1,0,0 "CH345" closed>]
|
23
|
+
```
|
24
|
+
|
25
|
+
### MIDI Output
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
out = RawMIDI::Output.all.first
|
29
|
+
out.open
|
30
|
+
|
31
|
+
out.write [0x90, 60, 100] # note on
|
32
|
+
|
33
|
+
sleep 1
|
34
|
+
|
35
|
+
out.write [0x90, 60, 0] # note off
|
36
|
+
out.close
|
37
|
+
```
|
38
|
+
|
39
|
+
### MIDI Input
|
40
|
+
|
41
|
+
*to do*
|
42
|
+
|
43
|
+
|
44
|
+
## Installation
|
45
|
+
|
46
|
+
*NOTE*: This is still incomplete, so I still haven't published it to Rubygems.
|
47
|
+
You'll need to clone the repository and install it with `rake install`, or use
|
48
|
+
Bundler.
|
49
|
+
|
50
|
+
|
51
|
+
Add this line to your application's Gemfile:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
gem 'rawmidi'
|
55
|
+
```
|
56
|
+
|
57
|
+
And then execute:
|
58
|
+
|
59
|
+
$ bundle
|
60
|
+
|
61
|
+
Or install it yourself as:
|
62
|
+
|
63
|
+
$ gem install rawmidi
|
64
|
+
|
65
|
+
|
66
|
+
## Development
|
67
|
+
|
68
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
69
|
+
`rake test` to run the tests. You can also run `bin/console` for an interactive
|
70
|
+
prompt that will allow you to experiment.
|
71
|
+
|
72
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
73
|
+
release a new version, update the version number in `version.rb`, and then run
|
74
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
75
|
+
git commits and tags, and push the `.gem` file to
|
76
|
+
[rubygems.org](https://rubygems.org).
|
77
|
+
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
Bug reports and pull requests are welcome on GitHub at
|
82
|
+
https://github.com/munshkr/rawmidi-rb. This project is intended to be a safe,
|
83
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
84
|
+
the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
85
|
+
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the [MIT
|
90
|
+
License](http://opensource.org/licenses/MIT).
|
91
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/rawmidi.rb
ADDED
data/lib/rawmidi/api.rb
ADDED
@@ -0,0 +1,222 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
|
3
|
+
module RawMIDI
|
4
|
+
module LibC
|
5
|
+
extend FFI::Library
|
6
|
+
|
7
|
+
ffi_lib FFI::Library::LIBC
|
8
|
+
|
9
|
+
# Needed for some functions that expects the user frees memory after use
|
10
|
+
attach_function :free, [:pointer], :void
|
11
|
+
end
|
12
|
+
|
13
|
+
module API
|
14
|
+
extend FFI::Library
|
15
|
+
|
16
|
+
ffi_lib 'libasound'
|
17
|
+
|
18
|
+
enum :snd_ctl_mode, [:default, :nonblock, :async, :readonly]
|
19
|
+
enum :snd_rawmidi_stream, [:output, :input]
|
20
|
+
|
21
|
+
MODE_FLAGS = {append: 0x1, nonblock: 0x2, sync: 0x4}
|
22
|
+
|
23
|
+
# snd_ctl
|
24
|
+
class SndCtl < FFI::Struct
|
25
|
+
layout :dl_handle, :pointer, # void*
|
26
|
+
:name, :pointer, # char*
|
27
|
+
:type, :pointer,
|
28
|
+
:ops, :pointer, # const snd_ctl_ops_t*
|
29
|
+
:private_data, :pointer, # void*
|
30
|
+
:nonblock, :ulong,
|
31
|
+
:poll_fd, :ulong,
|
32
|
+
:async_handlers, :ulong
|
33
|
+
end
|
34
|
+
|
35
|
+
# snd_rawmidi_info
|
36
|
+
class SndRawMIDIInfo < FFI::Struct
|
37
|
+
layout :device, :uint, # RO/WR (control): device number
|
38
|
+
:subdevice, :uint, # RO/WR (control): subdevice number
|
39
|
+
:stream, :int, # WR: stream
|
40
|
+
:card, :int, # R: card number
|
41
|
+
:flags, :uint, # SNDRV_RAWMIDI_INFO_XXXX
|
42
|
+
:id, [:uchar, 64], # ID (user selectable)
|
43
|
+
:name, [:uchar, 80], # name of device
|
44
|
+
:subname, [:uchar, 32], # name of active or selected subdevice
|
45
|
+
:subdevices_count, :uint,
|
46
|
+
:subdevices_avail, :uint,
|
47
|
+
:reserved, [:uchar, 64] # reserved for future use
|
48
|
+
end
|
49
|
+
|
50
|
+
# const char* snd_strerror(int error_number)
|
51
|
+
attach_function :snd_strerror, [:int], :string
|
52
|
+
|
53
|
+
# int snd_card_next(card&)
|
54
|
+
attach_function :snd_card_next, [:pointer], :int
|
55
|
+
# int snd_card_get_name(int card, char **name)
|
56
|
+
attach_function :snd_card_get_name, [:int, :pointer], :int
|
57
|
+
# int snd_card_get_longname(int card, char **name)
|
58
|
+
attach_function :snd_card_get_longname, [:int, :pointer], :int
|
59
|
+
|
60
|
+
# int snd_ctl_open(snd_ctl_t** ctl, const char* name, int mode)
|
61
|
+
attach_function :snd_ctl_open, [:pointer, :pointer, :snd_ctl_mode], :int
|
62
|
+
# int snd_ctl_close(snd_ctl_t* ctl)
|
63
|
+
attach_function :snd_ctl_close, [:pointer], :int
|
64
|
+
# int snd_ctl_rawmidi_next_device(snd_ctl_t* control, &device)
|
65
|
+
attach_function :snd_ctl_rawmidi_next_device, [:pointer, :pointer], :int
|
66
|
+
attach_function :snd_ctl_rawmidi_info, [:pointer, :pointer], :int
|
67
|
+
|
68
|
+
# int snd_rawmidi_open(snd_rawmidi_t** input, snd_rawmidi_t** output, const char* name, int mode)
|
69
|
+
attach_function :snd_rawmidi_open, [:pointer, :pointer, :string, :uint], :int
|
70
|
+
# int snd_rawmidi_close(snd_rawmidi_t* rawmidi)
|
71
|
+
attach_function :snd_rawmidi_close, [:pointer], :int
|
72
|
+
# int snd_rawmidi_write(snd_rawmidi_t* output, char* data, int datasize)
|
73
|
+
attach_function :snd_rawmidi_write, [:pointer, :pointer, :size_t], :ssize_t
|
74
|
+
# void snd_rawmidi_info_set_device(snd_rawmidi_info_t *obj, unsigned int val)
|
75
|
+
attach_function :snd_rawmidi_info_set_device, [:pointer, :uint], :void
|
76
|
+
# void snd_rawmidi_info_set_subdevice (snd_rawmidi_info_t *obj, unsigned int val)
|
77
|
+
attach_function :snd_rawmidi_info_set_subdevice, [:pointer, :uint], :void
|
78
|
+
# void snd_rawmidi_info_set_stream(snd_rawmidi_info_t *obj, snd_rawmidi_stream_t val)
|
79
|
+
attach_function :snd_rawmidi_info_set_stream, [:pointer, :snd_rawmidi_stream], :void
|
80
|
+
# unsigned int snd_rawmidi_info_get_subdevices_count(const snd_rawmidi_info_t *obj)
|
81
|
+
attach_function :snd_rawmidi_info_get_subdevices_count, [:pointer], :uint
|
82
|
+
# const char* snd_rawmidi_info_get_name(const snd_rawmidi_info_t *obj)
|
83
|
+
attach_function :snd_rawmidi_info_get_name, [:pointer], :string
|
84
|
+
|
85
|
+
|
86
|
+
module Card
|
87
|
+
def self.each_id
|
88
|
+
return enum_for(__method__) unless block_given?
|
89
|
+
|
90
|
+
card_p = FFI::MemoryPointer.new(:int).write_int(-1)
|
91
|
+
|
92
|
+
loop do
|
93
|
+
status = API.snd_card_next(card_p)
|
94
|
+
fail Error, API.snd_strerror(status) if status < 0
|
95
|
+
id = card_p.read_int
|
96
|
+
|
97
|
+
break if id < 0
|
98
|
+
yield id
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.with_control(card)
|
103
|
+
return enum_for(__method__, card) unless block_given?
|
104
|
+
|
105
|
+
ctl_pp = FFI::MemoryPointer.new(:pointer)
|
106
|
+
status = API.snd_ctl_open(ctl_pp, "hw:#{card}", :readonly)
|
107
|
+
if status < 0
|
108
|
+
fail Error, "cannot open control for card #{card}: #{API.snd_strerror(status)}"
|
109
|
+
end
|
110
|
+
|
111
|
+
ctl_p = ctl_pp.read_pointer
|
112
|
+
res = yield(ctl_p)
|
113
|
+
|
114
|
+
API.snd_ctl_close(ctl_p)
|
115
|
+
|
116
|
+
res
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.get_name(id)
|
120
|
+
name_pp = FFI::MemoryPointer.new(:pointer)
|
121
|
+
status = API.snd_card_get_name(id, name_pp)
|
122
|
+
fail Error, API.snd_strerror(status) if status < 0
|
123
|
+
|
124
|
+
name_p = name_pp.read_pointer
|
125
|
+
name = name_p.read_string
|
126
|
+
LibC.free(name_p)
|
127
|
+
|
128
|
+
name
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.get_longname(id)
|
132
|
+
name_pp = FFI::MemoryPointer.new(:pointer)
|
133
|
+
status = API.snd_card_get_longname(id, name_pp)
|
134
|
+
fail Error, API.snd_strerror(status) if status < 0
|
135
|
+
|
136
|
+
name_p = name_pp.read_pointer
|
137
|
+
name = name_p.read_string
|
138
|
+
LibC.free(name_p)
|
139
|
+
|
140
|
+
name
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
module Device
|
145
|
+
def self.each(card)
|
146
|
+
return enum_for(__method__, card) unless block_given?
|
147
|
+
|
148
|
+
Card.with_control(card) do |ctl_p|
|
149
|
+
device_p = FFI::MemoryPointer.new(:int).write_int(-1)
|
150
|
+
|
151
|
+
loop do
|
152
|
+
status = API.snd_ctl_rawmidi_next_device(ctl_p, device_p)
|
153
|
+
if status < 0
|
154
|
+
API.snd_ctl_close(ctl_p)
|
155
|
+
fail Error, "cannot determine device number: #{API.snd_strerror(status)}"
|
156
|
+
end
|
157
|
+
|
158
|
+
device = device_p.read_int
|
159
|
+
|
160
|
+
break if device < 0
|
161
|
+
|
162
|
+
info = subdevice_info(ctl_p, device)
|
163
|
+
yield device, info
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def self.subdevice_info(ctl_p, device, subdevice=0)
|
169
|
+
info_p = FFI::MemoryPointer.new(:char, SndRawMIDIInfo.size, true)
|
170
|
+
|
171
|
+
API.snd_rawmidi_info_set_device(info_p, device)
|
172
|
+
API.snd_rawmidi_info_set_subdevice(info_p, subdevice)
|
173
|
+
|
174
|
+
API.snd_rawmidi_info_set_stream(info_p, :input)
|
175
|
+
status = API.snd_ctl_rawmidi_info(ctl_p, info_p)
|
176
|
+
is_input = status >= 0
|
177
|
+
|
178
|
+
API.snd_rawmidi_info_set_stream(info_p, :output)
|
179
|
+
status = API.snd_ctl_rawmidi_info(ctl_p, info_p)
|
180
|
+
is_output = status >= 0
|
181
|
+
|
182
|
+
name = API.snd_rawmidi_info_get_name(info_p)
|
183
|
+
|
184
|
+
{name: name, input: is_input, output: is_output}
|
185
|
+
end
|
186
|
+
|
187
|
+
def self.open(handle_name, direction, mode)
|
188
|
+
midi_pp = FFI::MemoryPointer.new(:pointer)
|
189
|
+
mode_n = API::MODE_FLAGS[mode]
|
190
|
+
|
191
|
+
status = case direction
|
192
|
+
when :output
|
193
|
+
API.snd_rawmidi_open(nil, midi_pp, handle_name, mode_n)
|
194
|
+
when :input
|
195
|
+
API.snd_rawmidi_open(midi_pp, nil, handle_name, mode_n)
|
196
|
+
else
|
197
|
+
fail Error, 'invalid direction'
|
198
|
+
end
|
199
|
+
|
200
|
+
if status < 0
|
201
|
+
fail Error, "cannot open device: #{API.snd_strerror(status)}"
|
202
|
+
end
|
203
|
+
|
204
|
+
midi_pp.read_pointer
|
205
|
+
end
|
206
|
+
|
207
|
+
def self.write(midi_p, buffer)
|
208
|
+
buf_p = FFI::MemoryPointer.new(:char, buffer.size)
|
209
|
+
buf_p.write_array_of_char(buffer)
|
210
|
+
API.snd_rawmidi_write(midi_p, buf_p, buffer.size)
|
211
|
+
end
|
212
|
+
|
213
|
+
def self.close(midi_p)
|
214
|
+
status = API.snd_rawmidi_close(midi_p)
|
215
|
+
if status < 0
|
216
|
+
fail Error, "cannot close device: #{API.snd_strerror(status)}"
|
217
|
+
end
|
218
|
+
true
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
data/lib/rawmidi/card.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rawmidi/api'
|
2
|
+
require 'rawmidi/input'
|
3
|
+
require 'rawmidi/output'
|
4
|
+
|
5
|
+
module RawMIDI
|
6
|
+
class Card
|
7
|
+
attr_reader :id, :name
|
8
|
+
|
9
|
+
def self.all
|
10
|
+
API::Card.each_id.map { |id| new(id) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(id)
|
14
|
+
@id = id
|
15
|
+
@name = API::Card.get_name(id)
|
16
|
+
end
|
17
|
+
|
18
|
+
def handle_name
|
19
|
+
"hw:#{@id}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def inputs
|
23
|
+
API::Device.each(@id).select { |_, info| info[:input] }.map do |id, info|
|
24
|
+
Input.new(self, id, name: info[:name])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def outputs
|
29
|
+
API::Device.each(@id).select { |_, info| info[:output] }.map do |id, info|
|
30
|
+
Output.new(self, id, name: info[:name])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def longname
|
35
|
+
API::Card.get_longname(@id)
|
36
|
+
end
|
37
|
+
|
38
|
+
def inspect
|
39
|
+
"#<#{self.class.name}:#{"0x%014x" % object_id} #{handle_name} #{@name.inspect}>"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module RawMIDI
|
2
|
+
module Device
|
3
|
+
attr_reader :id, :card, :name
|
4
|
+
|
5
|
+
def initialize(card_or_id, id, **info)
|
6
|
+
@card = card_or_id.is_a?(Card) ? card_or_id : Card.new(card_or_id)
|
7
|
+
@id = id
|
8
|
+
|
9
|
+
info = get_subdevice_info if info.empty?
|
10
|
+
@name = info[:name]
|
11
|
+
@input = info[:input]
|
12
|
+
@output = info[:output]
|
13
|
+
end
|
14
|
+
|
15
|
+
def handle_name
|
16
|
+
"#{@card.handle_name},#{@id},0"
|
17
|
+
end
|
18
|
+
|
19
|
+
def inspect
|
20
|
+
"#<#{self.class.name}:#{"0x%014x" % object_id} #{handle_name} #{@name.inspect} #{open? ? 'open' : 'closed'}>"
|
21
|
+
end
|
22
|
+
|
23
|
+
def open
|
24
|
+
fail 'already open' if @midi_p
|
25
|
+
@midi_p = API::Device.open(handle_name, direction, :sync)
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
29
|
+
def close
|
30
|
+
return if closed?
|
31
|
+
API::Device.close(@midi_p)
|
32
|
+
@midi_p = nil
|
33
|
+
true
|
34
|
+
end
|
35
|
+
|
36
|
+
def open?
|
37
|
+
!!@midi_p
|
38
|
+
end
|
39
|
+
|
40
|
+
def closed?
|
41
|
+
!open?
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def get_subdevice_info
|
47
|
+
API::Card.with_control(@card.id) do |ctl_p|
|
48
|
+
API::Device.subdevice_info(ctl_p, @id)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rawmidi/device'
|
2
|
+
require 'rawmidi/api'
|
3
|
+
|
4
|
+
module RawMIDI
|
5
|
+
class Input
|
6
|
+
include Device
|
7
|
+
|
8
|
+
def self.all
|
9
|
+
Card.all.flat_map(&:inputs)
|
10
|
+
end
|
11
|
+
|
12
|
+
def input?
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
def output?
|
17
|
+
false
|
18
|
+
end
|
19
|
+
|
20
|
+
def read
|
21
|
+
fail NotImplementedError
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def direction
|
27
|
+
:input
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rawmidi/device'
|
2
|
+
require 'rawmidi/api'
|
3
|
+
|
4
|
+
module RawMIDI
|
5
|
+
class Output
|
6
|
+
include Device
|
7
|
+
|
8
|
+
def self.all
|
9
|
+
Card.all.flat_map(&:outputs)
|
10
|
+
end
|
11
|
+
|
12
|
+
def input?
|
13
|
+
false
|
14
|
+
end
|
15
|
+
|
16
|
+
def output?
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
def write(buffer)
|
21
|
+
fail 'device is closed' if closed?
|
22
|
+
API::Device.write(@midi_p, buffer)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def direction
|
28
|
+
:output
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/rawmidi.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rawmidi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rawmidi"
|
8
|
+
spec.version = RawMIDI::VERSION
|
9
|
+
spec.authors = ["Damián Silvani"]
|
10
|
+
spec.email = ["munshkr@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby FFI binding for ALSA RawMIDI}
|
13
|
+
spec.homepage = "https://github.com/munshkr/rawmidi-rb"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features|playground)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
26
|
+
spec.add_development_dependency 'pry'
|
27
|
+
|
28
|
+
spec.add_dependency 'ffi'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rawmidi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Damián Silvani
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: ffi
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- munshkr@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- CODE_OF_CONDUCT.md
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/rawmidi.rb
|
100
|
+
- lib/rawmidi/api.rb
|
101
|
+
- lib/rawmidi/card.rb
|
102
|
+
- lib/rawmidi/device.rb
|
103
|
+
- lib/rawmidi/input.rb
|
104
|
+
- lib/rawmidi/output.rb
|
105
|
+
- lib/rawmidi/version.rb
|
106
|
+
- rawmidi.gemspec
|
107
|
+
homepage: https://github.com/munshkr/rawmidi-rb
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.5.1
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: Ruby FFI binding for ALSA RawMIDI
|
131
|
+
test_files: []
|