python-sounddevice icon indicating copy to clipboard operation
python-sounddevice copied to clipboard

Can't listen to speaks on MacOS

Open RobinFrcd opened this issue 5 months ago • 5 comments

Hi, I'm already using sounddevice to analyse the output of an Application on Linux, it works well, thanks ! I've tried to do the same on MacOS but the app is not listed on the devices (only have mic and speakers), so I chose to analyze the speaker instead:

import sounddevice as sd

devices = sd.query_devices()
print(devices)

speakers = devices[1]
samplerate = 48000 
channels = speakers['max_output_channels']
blocksize = 1024  

s = sd.InputStream(device=speakers, channels=channels, samplerate=samplerate, blocksize=blocksize)
> 0 MacBook Air Microphone, Core Audio (1 in, 0 out)
< 1 MacBook Air Speakers, Core Audio (0 in, 2 out)

Traceback (most recent call last):
  File "/Users/xxx/.pyenv/versions/dev3914/lib/python3.9/site-packages/sounddevice.py", line 2746, in _split
    invalue, outvalue = value
ValueError: too many values to unpack (expected 2)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/xxx/.pyenv/versions/dev3914/lib/python3.9/site-packages/sounddevice.py", line 1429, in __init__
    _StreamBase.__init__(self, kind='input', wrap_callback='array',
  File "/Users/xxx/.pyenv/versions/dev3914/lib/python3.9/site-packages/sounddevice.py", line 825, in __init__
    _get_stream_parameters(kind, device, channels, dtype, latency,
  File "/Users/xxx/.pyenv/versions/dev3914/lib/python3.9/site-packages/sounddevice.py", line 2686, in _get_stream_parameters
    device = _get_device_id(device, kind, raise_on_error=True)
  File "/Users/xxx/.pyenv/versions/dev3914/lib/python3.9/site-packages/sounddevice.py", line 2784, in _get_device_id
    idev, odev = _split(id_or_query_string)
  File "/Users/xxx/.pyenv/versions/dev3914/lib/python3.9/site-packages/sounddevice.py", line 2750, in _split
    raise ValueError('Only single values and pairs are allowed') from e
ValueError: Only single values and pairs are allowed

Did I do something wrong ?

On Linux, I'm doing this and it works like a charm !

def process():
    devices = sd.query_devices()
    for device in devices:
        if device['name'] == 'Firefox':
            loopback_device = device['index']

    duration = 10
    samplerate = 48000  
    channels = 2 
    blocksize = 1024  

    with sd.InputStream(device=loopback_device, channels=channels, samplerate=samplerate, blocksize=blocksize) as stream:
        frames_to_capture = int(duration * samplerate / blocksize)

        for _ in range(frames_to_capture):
            data, overflowed = stream.read(blocksize)
            if overflowed:
                print("Warning: Overflow detected!")
                dataprocess(data)

Thanks !

RobinFrcd avatar Aug 27 '24 11:08 RobinFrcd