arirusso / unimidi

Realtime MIDI IO for Ruby
Other
255 stars 28 forks source link

Is #gets(*args) supposed to clear buffer? #39

Closed moonhouse closed 7 years ago

moonhouse commented 7 years ago

Reading the documentation I get the impression that #gets(*args) should clear the buffer while #gets_buffer(*args) doesn't clear the buffer since the documentation for the latter method mentions

Gets any messages in the buffer in the same format as Input#gets, without removing them from the buffer

However, when I use gets the buffer doesn't seem to be cleared.

require "unimidi"

input = UniMIDI::Input.gets

while true
  m = input.gets
  p m
  puts input.buffer.size
end

gives the following output:

Select a MIDI input...
0) AKAI PROFESSIONAL,LP APC MINI
> 0
[{:data=>[176, 52, 127], :timestamp=>11.573076248168945}]
1
[{:data=>[144, 35, 127], :timestamp=>4276.036977767944}]
2
[{:data=>[128, 35, 127], :timestamp=>4364.193677902222}]
3
[{:data=>[144, 28, 127], :timestamp=>4708.255052566528}]
4
[{:data=>[128, 28, 127], :timestamp=>4820.236682891846}]
5
[{:data=>[144, 44, 127], :timestamp=>5948.585033416748}]
6
[{:data=>[128, 44, 127], :timestamp=>6100.634813308716}]
7
[{:data=>[144, 44, 127], :timestamp=>6316.690921783447}]
8
[{:data=>[128, 44, 127], :timestamp=>6440.645933151245}]
9
[{:data=>[144, 28, 127], :timestamp=>6660.769701004028}]
10
[{:data=>[128, 28, 127], :timestamp=>6792.711019515991}]
11
[{:data=>[144, 21, 127], :timestamp=>6996.846914291382}]
12
[{:data=>[128, 21, 127], :timestamp=>7108.961820602417}]
13
[{:data=>[144, 37, 127], :timestamp=>7320.873022079468}]
14
[{:data=>[128, 37, 127], :timestamp=>7441.043853759766}]
15
[{:data=>[144, 36, 127], :timestamp=>7649.0068435668945}]
16
[{:data=>[128, 36, 127], :timestamp=>7721.003770828247}]
17
[{:data=>[144, 36, 127], :timestamp=>10141.655921936035}]
18
[{:data=>[128, 36, 127], :timestamp=>10245.80693244934}]
19

The buffer grows continously as more MIDI events come in despite being read by #gets. Since the documentation doesn't clearly state if the buffer should be cleared after calling the method but rather is implied by the description for #gets_buffer I am not sure if this is just unclear documentation or unintended behavior.

This is while running unimidi (0.4.6) on ruby 2.3.1p112 under Mac OS X Yosemite.

arirusso commented 7 years ago

Hi @moonhouse

Thanks for the clear post and example, is very helpful

This is an issue of the documentation not being clear

The Input#gets method should not clear the buffer. It returns the messages in the buffer that have been received since the last call to Input#gets.

On the other hand, the _Input#getsbuffer method returns all of the messages in the buffer since either the program was initialized or the buffer was explicitly cleared with _Input#clearbuffer

I'll follow up with a change to improve the documentation

arirusso commented 7 years ago

Changes here https://github.com/arirusso/unimidi/pull/40 and marking this resolved