代码之家  ›  专栏  ›  技术社区  ›  Free Url

如何在内置输出上调用python声音设备包

  •  1
  • Free Url  · 技术社区  · 6 年前

    我正试图用python3和sounddevice在Mac OS X 10.14.1上录制计算机的音频输出(不是麦克风的输入,而是扬声器的输入-在离开设备之前)。

    Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sounddevice as sd
    >>> sd.query_devices()
    > 0 Built-in Microphone, Core Audio (2 in, 0 out)
    < 1 Built-in Output, Core Audio (0 in, 2 out)
    >>> sd.default.device = 1
    >>> print("Channels should be 0 (number of input channels) or 2 (number of output channels)")
    Channels should be 0 (number of input channels) or 2 (number of output channels)
    >>> duration = 10
    >>> fs = 44100
    >>> x = sd.rec(int(duration * fs), samplerate=fs, channels=2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 224, in rec
        ctx.input_dtype, callback, blocking, **kwargs)
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 2417, in start_stream
        **kwargs)
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 1301, in __init__
        **_remove_self(locals()))
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 780, in __init__
        'Error opening {0}'.format(self.__class__.__name__))
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 2572, in _check
        raise PortAudioError(errormsg, err)
    sounddevice.PortAudioError: Error opening InputStream: Invalid number of channels [PaErrorCode -9998]
    >>> x = sd.rec(int(duration * fs), samplerate=fs, channels=0)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 224, in rec
        ctx.input_dtype, callback, blocking, **kwargs)
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 2417, in start_stream
        **kwargs)
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 1301, in __init__
        **_remove_self(locals()))
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 780, in __init__
        'Error opening {0}'.format(self.__class__.__name__))
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 2572, in _check
        raise PortAudioError(errormsg, err)
    sounddevice.PortAudioError: Error opening InputStream: Invalid number of channels [PaErrorCode -9998]
    >>> x = sd.rec(int(duration * fs), samplerate=fs)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 215, in rec
        ctx.frames = ctx.check_out(out, frames, channels, dtype, mapping)
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sounddevice.py", line 2351, in check_out
        'Unable to determine number of input channels')
    TypeError: Unable to determine number of input channels
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   John M I Davis    6 年前

    sd.rec(...
    

    而且你正在录制的设备没有输入,这是真的。

    播放声音的一个好例子如下所示:

    https://python-sounddevice.readthedocs.io/en/0.3.12/examples.html#play-a-sound-file

    或者使用下面Mark提供的非常简洁的示例。

    至于将输出返回到需要一些高级工作的输入,比如在驱动程序级别。我注意到有一个名为SoundFlower的OSX虚拟设备可以做到这一点。见:

    https://apple.stackexchange.com/questions/221980/os-x-route-audio-output-to-audio-input

    可在此处找到来源:

    https://github.com/mattingalls/Soundflower

    您可以选择利用这个现有工具,或者选择一条更陡峭的道路,尝试理解和提取您想要的行为子集(勇气在SoundflowerEngine::createAudioStreams中)。

        2
  •  0
  •   Ghantey    6 年前

    我编写了以下脚本,该脚本在Python2.x和3.x中运行。

    以下代码在windows中运行良好,但尚未在MAC OS X中测试。希望这对您有用。

    最重要的是不要忘记安装sounddevice、soundfile和numpy

    import sounddevice
    import soundfile
    
    rec_rate = 40000  # Hertz
    rec_duration = 10  # seconds
    rec_name = 'names.wav'  # Name for file
    rec_data = sounddevice.rec(int(rec_rate * rec_duration), samplerate=rec_rate, channels=1, blocking=True)  # Recording ...
    soundfile.write(rec_name, rec_data, rec_rate)  # Writing recorded sound in a file