Class: CoreAudio::AudioDevice

Inherits:
AudioObject show all
Defined in:
lib/macos/core_audio/audio_device.rb,
lib/macos/core_audio/audio_device.rb

Overview

Split the constants so that AudioStream can see ‘PropertyLatency’

AudioHardwareBase.h: AudioDevice Properties collapse

PropertyConfigurationApplication =
'capp'
PropertyDeviceUID =
'uid '
PropertyModelUID =
'muid'
PropertyTransportType =
'tran'
PropertyRelatedDevices =
'akin'
PropertyClockDomain =
'clkd'
PropertyDeviceIsAlive =
'livn'
PropertyDeviceIsRunning =
'goin'
PropertyDeviceCanBeDefaultDevice =
'dflt'
PropertyDeviceCanBeDefaultSystemDevice =
'sflt'
PropertyLatency =
'ltnc'
PropertyStreams =
'stm#'
PropertyControlList =
'ctrl'
PropertySafetyOffset =
'saft'
PropertyNominalSampleRate =
'nsrt'
PropertyAvailableNominalSampleRates =
'nsr#'
PropertyIcon =
'icon'
PropertyIsHidden =
'hidn'
PropertyPreferredChannelsForStereo =
'dch2'
PropertyPreferredChannelLayout =
'srnd'

AudioHardware.h: AudioDevice Properties collapse

PropertyPlugIn =
'plug'
PropertyDeviceHasChanged =
'diff'
PropertyDeviceIsRunningSomewhere =
'gone'
ProcessorOverload =
'over'
PropertyIOStoppedAbnormally =
'stpd'
PropertyHogMode =
'oink'
PropertyBufferFrameSize =
'fsiz'
PropertyBufferFrameSizeRange =
'fsz#'
PropertyUsesVariableBufferFrameSizes =
'vfsz'
PropertyIOCycleUsage =
'ncyc'
PropertyStreamConfiguration =
'slay'
PropertyIOProcStreamUsage =
'suse'
PropertyActualSampleRate =
'asrt'

Constants inherited from AudioObject

CoreAudio::AudioObject::ObjectID, CoreAudio::AudioObject::PropertyBaseClass, CoreAudio::AudioObject::PropertyClass, CoreAudio::AudioObject::PropertyElement, CoreAudio::AudioObject::PropertyElementCategoryName, CoreAudio::AudioObject::PropertyElementMaster, CoreAudio::AudioObject::PropertyElementName, CoreAudio::AudioObject::PropertyElementNumberName, CoreAudio::AudioObject::PropertyFirmwareVersion, CoreAudio::AudioObject::PropertyIdentify, CoreAudio::AudioObject::PropertyManufacturer, CoreAudio::AudioObject::PropertyModelName, CoreAudio::AudioObject::PropertyName, CoreAudio::AudioObject::PropertyOwnedObjects, CoreAudio::AudioObject::PropertyOwner, CoreAudio::AudioObject::PropertyScope, CoreAudio::AudioObject::PropertyScopeGlobal, CoreAudio::AudioObject::PropertyScopeInput, CoreAudio::AudioObject::PropertyScopeOutput, CoreAudio::AudioObject::PropertyScopePlayThrough, CoreAudio::AudioObject::PropertySelector, CoreAudio::AudioObject::PropertySerialNumber, CoreAudio::AudioObject::SystemObject

Instance Attribute Summary

Attributes inherited from AudioObject

#id

Properties collapse

Sample Rate collapse

Instance Method Summary collapse

Methods inherited from AudioObject

#device_name, #device_uid, #external?, #get_property, #initialize, #internal?, #model_uid, #set_property, system, #transport_type

Constructor Details

This class inherits a constructor from CoreAudio::AudioObject

Instance Method Details

#actual_sample_rateFloat

Returns the measured sample rate in Hertz.

Returns:

  • (Float)

    the measured sample rate in Hertz



111
112
113
114
# File 'lib/macos/core_audio/audio_device.rb', line 111

def actual_sample_rate
    address = PropertyAddress.global_master(PropertyActualSampleRate)
    get_property(address).get_float64(0)
end

#available_sample_ratesArray<Number,Range>

Returns the available sampling rates, or sample-rate-ranges.

Returns:

  • (Array<Number,Range>)

    the available sampling rates, or sample-rate-ranges



117
118
119
120
121
122
123
124
# File 'lib/macos/core_audio/audio_device.rb', line 117

def available_sample_rates
    address = PropertyAddress.global_master(PropertyAvailableNominalSampleRates)
    buffer = get_property(address)
    buffer = buffer.get_array_of_float64(0, buffer.size / FFI::Type::DOUBLE.size)

    # Convert the range pairs into actual Ranges, unless the Range is empty
    buffer.each_slice(2).map {|a,b| (a==b) ? a : (a..b)}
end

#buffer_frame_sizeObject



85
86
87
88
# File 'lib/macos/core_audio/audio_device.rb', line 85

def buffer_frame_size
    address = PropertyAddress.global_master(PropertyBufferFrameSize)
    get_property(address).get_uint32(0)
end

#running?Bool

Returns true if the device is running.

Returns:

  • (Bool)

    true if the device is running



91
92
93
94
# File 'lib/macos/core_audio/audio_device.rb', line 91

def running?
    address = PropertyAddress.global_master(PropertyDeviceIsRunning)
    0 != get_property(address).get_uint32(0)
end

#running_somewhere?Boolean

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/macos/core_audio/audio_device.rb', line 96

def running_somewhere?
    address = PropertyAddress.global_master(PropertyDeviceIsRunningSomewhere)
    0 != get_property(address).get_uint32(0)
end

#sample_rateFloat

Returns the device’s nominal sample rate.

Returns:

  • (Float)

    the device’s nominal sample rate



127
128
129
130
# File 'lib/macos/core_audio/audio_device.rb', line 127

def sample_rate
    address = PropertyAddress.global_master(PropertyNominalSampleRate)
    get_property(address).get_float64(0)
end

#sample_rate=(rate) ⇒ Object

Parameters:

  • rate (Float)

    the new sample rate in Hertz



133
134
135
136
137
138
139
# File 'lib/macos/core_audio/audio_device.rb', line 133

def sample_rate=(rate)
    address = PropertyAddress.global_master(PropertyNominalSampleRate)
    ffi_rate = FFI::MemoryPointer.new(:double)
    ffi_rate.put_float64(0, rate)
    status = set_property(address, ffi_rate)
    raise "status #{status} => '#{[status].pack('L').reverse}'" unless 0 == status
end

#start(&block) ⇒ Object

Start the AudioDevice

If a block is provided, register it as a callback before starting the device
@note The device will continue to run until `stop` is called


146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/macos/core_audio/audio_device.rb', line 146

def start(&block)
    if block_given?
	io_proc_id = FFI::MemoryPointer.new(:pointer)
	status = CoreAudio.AudioDeviceCreateIOProcID(id, block, nil, io_proc_id)

	raise "Couldn't create an IO Proc #{status} => '#{[status].pack('L').reverse}'" unless status.zero? # && !@proc_id.nil?

	@proc_id = io_proc_id.get_pointer(0)
    end

    Thread.start do
	CoreAudio.AudioDeviceStart(id, @proc_id)
    end
end

#stopObject

Stop the AudioDevice and delete any registered callbacks



162
163
164
165
# File 'lib/macos/core_audio/audio_device.rb', line 162

def stop
    CoreAudio.AudioDeviceStop(id, @proc_id)
    CoreAudio.AudioDeviceDestroyIOProcID(id, @proc_id)
end

#streamsArray<AudioStream>

Returns an array of CoreAudio::AudioStreams, one for each stream provided by the device.

Returns:



102
103
104
105
106
# File 'lib/macos/core_audio/audio_device.rb', line 102

def streams
    address = PropertyAddress.global_master(PropertyStreams)
    buffer = get_property(address)
    buffer.get_array_of_uint32(0, buffer.size/FFI::Type::UINT32.size).map {|stream_id| AudioStream.new(stream_id)}
end