Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在内置输出上调用python声音设备包_Python_Python 3.x_Audio - Fatal编程技术网

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

如何在内置输出上调用python声音设备包,python,python-3.x,audio,Python,Python 3.x,Audio,我正试图用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

我正试图用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
Python 3.6.2(v3.6.2:5FD33B59262017年7月16日20:11:06)
[GCC 4.2.1(苹果公司建造5666)(dot 3)]关于达尔文
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>将sounddevice作为sd导入
>>>sd.query_devices()
>0内置麦克风,核心音频(2进0出)
<1内置输出,核心音频(0进2出)
>>>sd.default.device=1
>>>打印(“通道应为0(输入通道数)或2(输出通道数)”)
通道应为0(输入通道数)或2(输出通道数)
>>>持续时间=10
>>>fs=44100
>>>x=sd.rec(int(持续时间*fs),samplerate=fs,channels=2)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
rec中的文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第224行
ctx.input\u数据类型、回调、阻塞、**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第2417行,在start_流中
**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第1301行,在__
**_删除_self(locals()))
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第780行,在__
“打开{0}时出错。格式(self.\uuuuuu class.\uuuuuu name.\uuuuuu))
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第2572行,检查
raise PortAudioError(错误消息,错误)
sounddevice.PortAudioError:打开InputStream时出错:通道数无效[PaErrorCode-9998]
>>>x=sd.rec(int(持续时间*fs),samplerate=fs,channels=0)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
rec中的文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第224行
ctx.input\u数据类型、回调、阻塞、**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第2417行,在start_流中
**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第1301行,在__
**_删除_self(locals()))
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第780行,在__
“打开{0}时出错。格式(self.\uuuuuu class.\uuuuuu name.\uuuuuu))
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第2572行,检查
raise PortAudioError(错误消息,错误)
sounddevice.PortAudioError:打开InputStream时出错:通道数无效[PaErrorCode-9998]
>>>x=sd.rec(int(持续时间*fs),samplerate=fs)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
rec中的文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第215行
ctx.frames=ctx.check_out(out、帧、通道、数据类型、映射)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/sounddevice.py”,第2351行,在签出中
'无法确定输入通道的数量')
TypeError:无法确定输入通道的数量

我编写了以下脚本,该脚本同时在Python 2.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

您似乎在告诉它录制:

sd.rec(...
而且你正在录制的设备没有输入,这是真的

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

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

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

可在此处找到来源:


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

这更可能是一揽子计划中的一个问题吗?也许我们误解了你的问题。是否尝试将当前发送到扬声器的流的副本通过管道传输到文件?是的,我想将发送到扬声器的音频通过管道传输回文件(或内存)。看起来Soundfo电源是实现这一点的一种可能方式。