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

2024-03-29 00:07:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python3和sounddevice在macosx10.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

Tags: ofinpynumberlibpackageslinelibrary
2条回答

看起来你要它录制:

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中)。在

我已经编写了以下脚本,它可以在python2.x和3.x中运行

The following code runs perfectly in windows but have not yet tested in MAC OS X. Hope this might work for you.

最重要的是不要忘了安装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

相关问题 更多 >