简体   繁体   English

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

[英]How to invoke python sound device package on Built-In Output

I am trying to record computer's audio output (not input to the microphone, but input to the speakers - before it leaves the device) on Mac OS X 10.14.1 with python3 and sounddevice. 我试图在Mac OS X 10.14.1上使用python3和sounddevice记录计算机的音频输出(不是输入到麦克风,而是输入到扬声器 - 在它离开设备之前)。

As you can see from the following traceback, I have tried every sensible number of input channels and default number of input channels, with no luck. 从下面的回溯中可以看出,我已经尝试了每个合理数量的输入通道和默认输入通道数,没有运气。 I have also looked at the source code, but haven't been able to determine my problem. 我也查看了源代码,但无法确定我的问题。

Is there a way to record the audio output stream on my system? 有没有办法在我的系统上录制音频输出流?

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

It looks like you are telling it to record: 看起来你要告诉它记录:

sd.rec(...

and the device you are recording has no inputs, which is true. 并且您正在录制的设备没有输入,这是真的。

A good example of playing sounds is shown here: 这里显示了播放声音的一个很好的例子:

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

or use the very concise example provided by Mark below. 或使用下面Mark提供的非常简洁的示例。

As far as getting the output back into an input that takes some advanced work like down at the driver level. 至于将输出恢复为输入,这需要在驱动程序级别执行一些高级工作。 I note that there is an OSX virtual device called SoundFlower which does this. 我注意到有一个名为SoundFlower的OSX虚拟设备可以做到这一点。 See: 看到:

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

The source can be found here: 来源可以在这里找到:

https://github.com/mattingalls/Soundflower https://github.com/mattingalls/Soundflower

Take your choice on leveraging this existing tool or a much steeper path of trying to understand and extract the subset of behavior that you desire (the guts are in SoundflowerEngine::createAudioStreams). 您可以选择利用这个现有工具或更陡峭的路径来尝试理解和提取您想要的行为子集(胆量在SoundflowerEngine :: createAudioStreams中)。

I have wrote the following script which runs in both Python 2.x and 3.x. 我编写了以下脚本,它在Python 2.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. 以下代码在Windows中运行完美,但尚未在MAC OS X中进行测试。希望这可能对您有用。

And most importantly don't forget to install sounddevice, soundfile, numpy 最重要的是不要忘记安装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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM