python - 同时使用 pyaudio 播放和录制声音

标签 python numpy audio talkback

我正在尝试创建一个程序来立即回复。我似乎无法让它发挥作用。有些网站说使用 numpy 数组,但我不知道如何使用。

import pyaudio
import wave
import time
import multiprocessing as mp
import pyaudio
import numpy as np
import sounddevice as sd

fs = 44100
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
audio = pyaudio.PyAudio()
RECORD_SECONDS = 5
stream = audio.open(format=FORMAT, channels=CHANNELS,
                rate=RATE, input=True,
                frames_per_buffer=CHUNK)
myarray = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    myarray.append(data)

    myrecording = sd.play(myarray, fs, channels=2)

Traceback (most recent call last): File "SoundTest.py", line 24, in myrecording = sd.play(myarray, fs, channels=2) line 2170, in check_data dtype = _check_dtype(data.dtype) File "/home/lordvile/.local/lib/python2.7/site-packages/sounddevice.py", line 2316, in _check_dtype raise TypeError('Unsupported data type: ' + repr(dtype)) TypeError: Unsupported data type: 'string32768'

最佳答案

下面是同时录制和播放的“PyAudio”和“Sounddevice”的示例。对我来说最重要的是明确地将输入和输出设备设置为有效值。因此,我在这两个示例中都包含了声音设备的打印。

声音设备:

与 PyAudio 相比,它的工作方式就像一个魅力。至少对我来说,sounddevice 提供了一个稳定的接口(interface),可以同时进行音频录制和播放。尽管有一些控制台输出提示输入/输出上溢/下溢,但记录/播放会重新生成,最终我会得到流畅工作的“回声”。

import sounddevice as sd

fs = 44100

def callback(indata, outdata, frames, time, status):
    if status:
        print(status)
    outdata[:] = indata

print(sd.query_devices())

try:
    with sd.Stream(device=(0,1), samplerate=fs, dtype='float32', latency=None, channels=2, callback=callback):
        input()
except KeyboardInterrupt:
    pass

PyAudio:

这段代码应该(?)工作。对我来说,它并不能始终如一地工作。它已经工作了大约一分钟一次,但通常我会收到错误:“OSError:[Errno -9981]输入溢出”。我不知道为什么它总是抛出这个错误,但是你可以自己尝试一下。

import pyaudio
import json

FORMAT = pyaudio.paInt16
CHANNELS = 1
CHUNK = 1024
RATE = 44100

audio = pyaudio.PyAudio()

for i in range(audio.get_device_count()):
    print(json.dumps(audio.get_device_info_by_index(i), indent=2))

stream = audio.open(format              = FORMAT,
                    channels            = CHANNELS,
                    rate                = RATE,
                    input               = True,
                    output              = True,
                    input_device_index  = 0,
                    output_device_index = 1,
                    frames_per_buffer   = CHUNK)

try:
    frames = []
    print("* echoing")
    print("Press CTRL+C to stop")
    while True:
        data = stream.read(CHUNK)
        frames.append(data)
        if len(frames)>0:
            stream.write(frames.pop(0),CHUNK)
    print("* done echoing")
except KeyboardInterrupt:
    stream.stop_stream()
    stream.close()
    audio.terminate()

关于python - 同时使用 pyaudio 播放和录制声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38878908/

相关文章:

python - `numpy.mean` 与元组一起用作 `axis` 参数 : not working with a masked array

audio - 嵌入音频以节省带宽?要嵌入还是流式播放?

python - 字符串替换条件

python - 当一个系列被传递给 Numpy exp() 函数时,为什么会返回一个系列?

python - 从arduino收集 'double'类型数据

python - 为什么 Python 的 Numpy zeros 和空函数之间的速度差异对于更大的数组大小消失了?

audio - 在Google Speech to Text API中需要WAV文件的正确编解码器

android - 播放Sound OnClick Android

python - 使用 PIL 缩放图像以保持透明度和颜色?

使用包文件中的 dict 进行 Python 日志记录