midi-nibbler 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +24 -16
  2. data/lib/nibbler/nibbler.rb +59 -11
  3. data/lib/nibbler.rb +1 -1
  4. metadata +2 -2
data/README.rdoc CHANGED
@@ -20,13 +20,13 @@ Parse MIDI Messages
20
20
 
21
21
  Enter a message piece by piece
22
22
 
23
- p nibbler.parse("90")
23
+ nibbler.parse("90")
24
24
  nil
25
25
 
26
- p nibbler.parse("40")
26
+ nibbler.parse("40")
27
27
  nil
28
28
 
29
- p nibbler.parse("40")
29
+ nibbler.parse("40")
30
30
  # #<MIDIMessage::NoteOn:0x98c9818
31
31
  # @channel=0,
32
32
  # @data=[64, 100],
@@ -38,7 +38,7 @@ Enter a message piece by piece
38
38
 
39
39
  Enter a message all at once
40
40
 
41
- p nibbler.parse("904040")
41
+ nibbler.parse("904040")
42
42
 
43
43
  # #<MIDIMessage::NoteOn:0x98c9818
44
44
  # @channel=0,
@@ -51,53 +51,61 @@ Enter a message all at once
51
51
 
52
52
  Use bytes
53
53
 
54
- p nibbler.parse(0x90, 0x40, 0x40)
54
+ nibbler.parse(0x90, 0x40, 0x40)
55
55
  #<MIDIMessage::NoteOn:0x98c9818 ...>
56
56
 
57
57
  You can use nibbles in string format
58
58
 
59
- p nibbler.parse("9", "0", "4", "0", "4", "0")
59
+ nibbler.parse("9", "0", "4", "0", "4", "0")
60
60
  #<MIDIMessage::NoteOn:0x98c9818 ...>
61
61
 
62
62
  Interchange the different types
63
63
 
64
- p nibbler.parse("9", "0", 0x40, 64)
64
+ nibbler.parse("9", "0", 0x40, 64)
65
65
  #<MIDIMessage::NoteOn:0x98c9818 ...>
66
66
 
67
67
  Use running status
68
68
 
69
- p nibbler.parse(0x40, 64)
69
+ nibbler.parse(0x40, 64)
70
70
  #<MIDIMessage::NoteOn:0x98c9818 ...>
71
71
 
72
72
  Look at the messages we've parsed
73
73
 
74
- p nibbler.messages
74
+ nibbler.messages
75
75
  [#<MIDIMessage::NoteOn:0x98c9804 ...>
76
76
  #<MIDIMessage::NoteOn:0x98c9811 ...>]
77
77
 
78
78
  Add an incomplete message
79
79
 
80
- p nibbler.parse("9")
81
- p nibbler.parse("40")
80
+ nibbler.parse("9")
81
+ nibbler.parse("40")
82
82
 
83
83
  See progress
84
84
 
85
- p nibbler.buffer
85
+ nibbler.buffer
86
86
  ["9", "4", "0"]
87
87
 
88
- p nibbler.buffer_hex
88
+ nibbler.buffer_s
89
89
  "940"
90
90
 
91
91
  Pass in a timestamp
92
92
 
93
- p nibbler.parse("904040", :timestamp => Time.now.to_i)
94
- # { :messages=> #<MIDIMessage::NoteOn:0x92f4564 ..>, :timestamp=>1304488440 }
93
+ nibbler.parse("904040", :timestamp => Time.now.to_i)
94
+ # { :messages=> #<MIDIMessage::NoteOn:0x92f4564 ..>, :timestamp=>1304488440 }
95
+
96
+ Bind events
97
+
98
+ nibbler.when({ :class => MIDIMessage::NoteOn }) { puts "bark" }
99
+ nibbler.parse("904040")
100
+
101
+ "bark"
102
+ #<MIDIMessage::NoteOn:0x98c9818 ...>
95
103
 
96
104
  Nibbler defaults to generate {midi-message}[http://github.com/arirusso/midi-message] objects, but it is also possible to use {midilib}[https://github.com/jimm/midilib]
97
105
 
98
106
  Nibbler.new(:message_lib => :midilib)
99
107
 
100
- p nibbler.parse("9", "0", 0x40, "40")
108
+ nibbler.parse("9", "0", 0x40, "40")
101
109
  "0: ch 00 on 40 40"
102
110
 
103
111
  == Author
@@ -7,7 +7,8 @@ module Nibbler
7
7
 
8
8
  extend Forwardable
9
9
 
10
- attr_reader :messages,
10
+ attr_reader :callbacks,
11
+ :messages,
11
12
  :processed,
12
13
  :rejected
13
14
 
@@ -20,7 +21,8 @@ module Nibbler
20
21
  def_delegator :clear_messages, :messages, :clear
21
22
 
22
23
  def initialize(options = {}, &block)
23
- @processed, @rejected, @messages = [], [], []
24
+ @timestamps = options[:timestamps] || false
25
+ @callbacks, @processed, @rejected, @messages = [], [], [], []
24
26
  @parser = Parser.new(options)
25
27
  @typefilter = HexCharArrayFilter.new
26
28
  block.call unless block.nil?
@@ -30,9 +32,10 @@ module Nibbler
30
32
  @messages | @fragmented_messages
31
33
  end
32
34
 
33
- def buffer_hex
35
+ def buffer_s
34
36
  buffer.join
35
37
  end
38
+ alias_method :buffer_hex, :buffer_s
36
39
 
37
40
  def clear_buffer
38
41
  buffer.clear
@@ -41,24 +44,69 @@ module Nibbler
41
44
  def clear_messages
42
45
  @messages.clear
43
46
  end
47
+
48
+ def use_timestamps
49
+ @messages = @messages.map do |m|
50
+ { :messages => m, :timestamp => nil }
51
+ end
52
+ @timestamps = true
53
+ end
44
54
 
45
55
  def parse(*a)
46
- options = a.last.kind_of?(Hash) ? a.pop : nil
56
+ a.compact!
57
+ options = a.last.kind_of?(Hash) ? a.pop : nil
58
+ timestamp = options[:timestamp] if !options.nil? && !options[:timestamp].nil?
59
+ use_timestamps if !timestamp.nil? && !@timestamps
47
60
  queue = @typefilter.process(a)
48
61
  result = @parser.process(queue)
49
- @messages += result[:messages]
62
+ record_message(result[:messages], timestamp)
63
+ handle_events(result[:messages]) unless @callbacks.empty?
50
64
  @processed += result[:processed]
51
65
  @rejected += result[:rejected]
52
- #@buffer = result[:remaining]
66
+ get_parse_output(result[:messages], options)
67
+ end
68
+
69
+ def when(hash, &proc)
70
+ if proc.nil?
71
+ warn "callback must have proc"
72
+ return false
73
+ end
74
+ hash[:proc] = proc
75
+ @callbacks << hash
76
+ true
77
+ end
78
+ alias_method :sees, :when
79
+
80
+ private
81
+
82
+ def handle_events(messages)
83
+ @callbacks.each do |cb|
84
+ messages.each do |msg|
85
+ match = true
86
+ cb.each do |key, val|
87
+ match = false if !key.eql?(:proc) && !msg.send(key).eql?(val)
88
+ end
89
+ cb[:proc].call(msg) if match
90
+ end
91
+ end
92
+ end
93
+
94
+ def record_message(msg, timestamp = nil)
95
+ !@timestamps ? @messages += msg : @messages << {
96
+ :messages => msg,
97
+ :timestamp => timestamp
98
+ }
99
+ end
100
+
101
+ def get_parse_output(messages, options = nil)
53
102
  # return type
54
103
  # 0 messages: nil
55
104
  # 1 message: the message
56
105
  # >1 message: an array of messages
57
- # might make sense to make this an array no matter what...
58
- output = result[:messages].length < 2 ?
59
- (result[:messages].empty? ? nil : result[:messages][0]) : result[:messages]
60
- output = { :messages => output, :timestamp => options[:timestamp] } unless options.nil? || options[:timestamp].nil?
61
- output
106
+ # might make sense to make this an array no matter what...iii dunnoo
107
+ output = messages.length < 2 ? (messages.empty? ? nil : messages[0]) : messages
108
+ output = { :messages => output, :timestamp => options[:timestamp] } if @timestamps && !options.nil?
109
+ output
62
110
  end
63
111
 
64
112
  end
data/lib/nibbler.rb CHANGED
@@ -16,7 +16,7 @@ require 'nibbler/hex_char_array_filter'
16
16
  #
17
17
  module Nibbler
18
18
 
19
- VERSION = "0.0.4"
19
+ VERSION = "0.0.5"
20
20
 
21
21
  # shortcut to Parser.new
22
22
  def self.new(*a, &block)
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: midi-nibbler
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ari Russo
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-04 00:00:00 -04:00
13
+ date: 2011-05-16 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency