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

EventCallback =
Proc.new do |hmo, wMsg, dwInstance, dwParam1, dwParam2|
  msg_type = Map::CallbackMessageTypes[wMsg] || ''
  if msg_type.eql?(:output_data)
    header = dwParam1
    handle = HandlePointer.read_int
    Map.winmm_func(:midiOutUnprepareHeader, handle, header, Map::MIDIHdr.size)
  end
end
HandlePointer =
FFI::MemoryPointer.new(FFI.type_size(:int))

Constants included from Device

Device::BufferSize

Instance Attribute Summary

Attributes included from Device

#id, #info, #name, #type

Instance Method Summary (collapse)

Methods included from Device

condensed_list, count, find_all_by_name, find_by_name, #initialize, list, refresh_list

Instance Method Details

- (Object) close

close this device



25
26
27
28
# File 'lib/midi-winmm/output.rb', line 25

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

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

initialize this device



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/midi-winmm/output.rb', line 8

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

- (Object) puts(*a)

send a message of an indeterminate type



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

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

- (Object) puts_bytes(*message_bytes)

send a message consisting of Numeric bytes



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/midi-winmm/output.rb', line 63

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

- (Object) puts_bytestr(data)

send a message consisisting of a String of hex digits



80
81
82
83
84
85
86
87
# File 'lib/midi-winmm/output.rb', line 80

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

- (Object) reset



49
50
51
# File 'lib/midi-winmm/output.rb', line 49

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

- (Object) volume

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



31
32
33
34
35
36
37
38
39
40
# File 'lib/midi-winmm/output.rb', line 31

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

- (Object) volume=(val)

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



44
45
46
47
# File 'lib/midi-winmm/output.rb', line 44

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