Record speakers output with PyAudio

前端 未结 5 886
离开以前
离开以前 2020-11-28 08:41

I\'m trying to record the output from my computer speakers with PyAudio.
I tried to modify the code example given in the PyAudio documentation, but it doesn\'t work.

5条回答
  •  -上瘾入骨i
    2020-11-28 09:26

    The speaker is an output stream even if you open it as an input. The hostApi value of the speaker is probably 0. You can check the 'maxInputChannels' and 'maxOutputChannels' of every connected devices and the maxInputChannels for the speaker shall be 0. You can't write to an input stream and you can't read from an output stream.

    You can detect the available devices with the following code:

    import pyaudio 
    
    # detect devices:
    p = pyaudio.PyAudio()
    host_info = p.get_host_api_info_by_index(0)    
    device_count = host_info.get('deviceCount')
    devices = []
    
    # iterate between devices:
    for i in range(0, device_count):
        device = p.get_device_info_by_host_api_device_index(0, i)
        devices.append(device['name'])
    
    print devices
    

    After you get all the connected devices you can check the 'hostApi' of each devices. For instance if the speaker index is 5 than:

    p.get_device_info_by_host_api_device_index(0, 5)['hostApi']
    

提交回复
热议问题