Projects

Ticket #733: coreaudio.rb

File coreaudio.rb, 2.0 KB (added by nagachika00@…, 2 years ago)
Line 
1# -*- encoding: utf-8 -*-
2
3framework "CoreAudio"
4
5module CoreAudio
6  # constants shortcut
7  SystemObject = KAudioObjectSystemObject
8  PropertyScopeGlobal = KAudioObjectPropertyScopeGlobal
9  PropertyElementMaster = KAudioObjectPropertyElementMaster
10
11  # typdef
12  class AudioObjectID < Pointer
13    def initialize
14      super("I")
15    end
16  end
17  class AudioDeviceID < AudioObjectID
18  end
19  class AudioStreamID < AudioObjectID
20  end
21
22  module_function
23
24  def get_property(type, selector, scope=PropertyScopeGlobal, element=PropertyElementMaster)
25    addr = AudioObjectPropertyAddress.new(selector, scope, element)
26    size_p = Pointer.new_with_type("I")
27    size_p[0] = 4
28    if type.is_a?(Pointer)
29      data_p = type
30    elsif type.class < Pointer
31      data_p = type.new
32    else
33      data_p = Pointer.new(type)
34    end
35    osstatus = AudioObjectGetPropertyData(SystemObject, addr, 0, nil, size_p, data_p)
36    unless osstatus == 0
37      raise "AudioObjectGetPropertyData() fail '#{[osstatus].pack("N")}'"
38    end
39    data_p[0]
40  end
41
42  def get_default_output_device
43    devid_p = AudioDeviceID.new("I")
44    get_property(devid_p, KAudioHardwarePropertyDefaultOutputDevice)
45  end
46
47  def create_io_proc_id(devid, &blk)
48    proc_id_p = Pointer.new("^?")
49    osstatus = AudioDeviceCreateIOProcID(devid, blk, nil, proc_id_p)
50    unless osstatus == 0
51      raise "AudioDeviceCreateIOProcID() fail '#{[osstatus].pack("N")}'"
52    end
53    proc_id_p[0]
54  end
55
56  def destroy_io_proc_id(devid, procid)
57    osstatus = AudioDeviceDestroyIOProcID(devid, procid)
58    unless osstatus == 0
59      raise "AudioDeviceDestroyIOProcID() fail '#{[osstatus].pack("N")}'"
60    end
61    nil
62  end
63
64  def device_start(devid, procid)
65    osstatus = AudioDeviceStart(devid, procid)
66    unless osstatus == 0
67      raise "AudioDeciceStart() fail '#{[osstatus].pack("N")}'"
68    end
69  end
70
71  def device_stop(devid, procid)
72    osstatus = AudioDeviceStop(devid, procid)
73    unless osstatus == 0
74      raise "AudioDeciceStop() fail '#{[osstatus].pack("N")}'"
75    end
76  end
77end