Class: MIDIWinMM::Output

Inherits:
Object
  • Object
show all
Includes:
Device
Defined in:
lib/midi-winmm/output.rb

Overview

Output device class for the WinMM driver interface

Constant Summary collapse

BufferSize =
2048

Constants included from Device

Device::WinmmCallbackFlag

Instance Attribute Summary collapse

Attributes included from Device

#enabled, #id, #info, #name, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Device

all_by_type, #initialize

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



13
14
15
# File 'lib/midi-winmm/output.rb', line 13

def buffer
  @buffer
end

Class Method Details

.allObject



109
110
111
# File 'lib/midi-winmm/output.rb', line 109

def self.all
  Device.all_by_type[:output]
end

.firstObject



101
102
103
# File 'lib/midi-winmm/output.rb', line 101

def self.first
  Device::first(:output)
end

.lastObject



105
106
107
# File 'lib/midi-winmm/output.rb', line 105

def self.last
  Device::last(:output)
end

Instance Method Details

#closeObject

close this device



35
36
37
38
# File 'lib/midi-winmm/output.rb', line 35

def close
  Map.winmm_func(:midiOutClose, @handle)
  @enabled = false
end

#enable(options = {}, &block) ⇒ Object Also known as: start, open

initialize this device



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/midi-winmm/output.rb', line 16

def enable(options = {}, &block)
  init_output_buffer
  Map.winmm_func(:midiOutOpen, Output::HandlePointer, @id, Output::EventCallback, 0, Device::WinmmCallbackFlag)
  @handle = HandlePointer.read_int
  @enabled = true
  unless block.nil?
    begin
      yield(self)
    ensure
      close
    end
  else
    self
  end
end

#puts(*a) ⇒ Object

send a message of an indeterminate type



64
65
66
67
68
69
70
# File 'lib/midi-winmm/output.rb', line 64

def puts(*a)
  case a.first
    when Array    then puts_bytes(*a.first)
    when Numeric  then puts_bytes(*a) 
    when String   then puts_s(*a)
  end
end

#puts_bytes(*message_bytes) ⇒ Object

send a message consisting of Numeric bytes



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/midi-winmm/output.rb', line 73

def puts_bytes(*message_bytes)
  format = "C" * message_bytes.size
  
  packed = message_bytes.pack(format)
  data_pointer = FFI::MemoryPointer.new(message_bytes.size).put_bytes(0, packed)
  
  @header[:dwBufferLength] = message_bytes.size
  @header[:dwBytesRecorded] = message_bytes.size
  @header[:lpData] = data_pointer
  
  Map.winmm_func(:midiOutPrepareHeader, @handle, @header.pointer, @header.size)
  
  Map.winmm_func(:midiOutLongMsg, @handle, @header.pointer, @header.size)
  
end

#puts_s(data) ⇒ Object Also known as: puts_bytestr, puts_hex

send a message consisisting of a String of hex digits



90
91
92
93
94
95
96
97
# File 'lib/midi-winmm/output.rb', line 90

def puts_s(data)
  data = data.dup
	  output = []
  until (str = data.slice!(0,2)).eql?("")
  	output << str.hex
  end
  puts_bytes(*output)
end

#resetObject



59
60
61
# File 'lib/midi-winmm/output.rb', line 59

def reset
  Map.winmm_func(:midiOutReset, @handle)
end

#volumeObject

returns a hash of fixnum values { :left => n, :right => n2 }



41
42
43
44
45
46
47
48
49
50
# File 'lib/midi-winmm/output.rb', line 41

def volume
  volume = FFI::MemoryPointer.new(FFI.type_size(:ulong))
  
  Map.winmm_func(:midiOutGetVolume, @handle, volume)
  
  str = dwmsg_to_array_of_bytes(volume.read_ulong)
  left = str.slice!(0,4)
  
  { :left => left.hex, :right => str.hex }
end

#volume=(val) ⇒ Object

accepts either a hash of fixnums { :left => n, :right => n2 } or a single fixnum that will be applied to both channels



54
55
56
57
# File 'lib/midi-winmm/output.rb', line 54

def volume=(val)
  vol = val.kind_of?(Hash) ? (val[:left] + (val[:right] << 16)) : (val + (val << 16))
  Map.winmm_func(:midiOutSetVolume, @handle, vol)        
end