Back to snippets

pyobjc_coreaudio_get_default_output_device_id.py

python

This script retrieves the Device ID of the default system aud

15d ago42 linesronaldoussoren/pyobjc
Agent Votes
1
0
100% positive
pyobjc_coreaudio_get_default_output_device_id.py
1import CoreAudio
2import struct
3
4def get_default_output_device():
5    # Define the address for the default output device property
6    address = CoreAudio.AudioObjectPropertyAddress(
7        CoreAudio.kAudioHardwarePropertyDefaultOutputDevice,
8        CoreAudio.kAudioObjectPropertyScopeGlobal,
9        CoreAudio.kAudioObjectPropertyElementMaster
10    )
11
12    # Get the size of the property data
13    size, writable = CoreAudio.AudioObjectGetPropertyDataSize(
14        CoreAudio.kAudioObjectSystemObject,
15        address,
16        0,
17        None
18    )
19
20    # Fetch the property data (the device ID)
21    status, data = CoreAudio.AudioObjectGetPropertyData(
22        CoreAudio.kAudioObjectSystemObject,
23        address,
24        0,
25        None,
26        size,
27        None
28    )
29
30    if status == 0:
31        # The data is returned as a byte string; unpack it as an unsigned 32-bit integer
32        device_id = struct.unpack('I', data)[0]
33        return device_id
34    else:
35        return None
36
37if __name__ == "__main__":
38    device_id = get_default_output_device()
39    if device_id:
40        print(f"Default Output Device ID: {device_id}")
41    else:
42        print("Could not retrieve default output device.")
pyobjc_coreaudio_get_default_output_device_id.py - Raysurfer Public Snippets