如何直接从麦克风播放输入数据

时间:2017-11-23 07:17:57

标签: python audio playback microphone speaker

我想在没有缓冲的情况下从麦克风播放许多输入数据。我试过,但有缓冲。这是我的代码。

import pyaudio
import wave
import urllib.request
import struct
import numpy as np
import sounddevice as sd
import matplotlib.pyplot as plt

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

# Parameters ----------------------------------------------
Window_Size = 22050 # Point
FORMAT_D = pyaudio.paFloat32; FORMAT_W = pyaudio.paInt32
CHANNELS = 1 # Mono
Sample_Rate = 22050 # Hz
dT = 1/Sample_Rate
RECORD_SECONDS = 20 # s
NOFFRAMES = int(Sample_Rate/Window_Size * RECORD_SECONDS)
WAVE_OUTPUT_FILENAME = "output.wav"
#-----------------------------------------------------------

p = pyaudio.PyAudio()

stream_D = p.open(format=FORMAT_D,
                channels=CHANNELS,
                rate=Sample_Rate,
                input=True,
                frames_per_buffer=Window_Size)

stream_W = p.open(format=FORMAT_W,
            channels=CHANNELS,
            rate=Sample_Rate,
            input=True,
            frames_per_buffer=Window_Size)

print("* recording")

frames = []

# "I think the problem appears from here"------------------------------
for i in range(0, int(Sample_Rate/Window_Size * RECORD_SECONDS)):
    data_D = stream_D.read(Window_Size)
#    data_W = stream_W.read(Window_Size)
    decoded = np.fromstring(data_D, 'Float32')
#    np.savetxt(str(i)+'ttt.txt',transform)
    sd.play(decoded,22050)
#    frames.append(data_W)
#-------------------------------------------------------

print("* done recording")

stream_D.stop_stream()
stream_D.close()
p.terminate()

#plt.plot(transform)
#plt.show()

# Save as a wave file---------------------------
#wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
#wf.setnchannels(CHANNELS)
#wf.setsampwidth(p.get_sample_size(FORMAT_W))
#wf.setframerate(Sample_Rate)
#wf.writeframes(b''.join(frames))
#wf.close()
#-------------------------------------------

此代码执行以1s间隔保存来自麦克风的输入数据,将字节数据转换为nparray数据(np.transform()),并使用扬声器(sd.play())播放数据。此代码有效,但是当循环重新启动时会有缓冲。我想顺利播放麦克风的声音。当我第一次询问时,有人建议使用回调函数,所以我添加了它,但是,我不知道如何使用它。我如何摆脱缓冲?有一些例子吗?我应该使用线程还是多处理?

1 个答案:

答案 0 :(得分:0)

延迟是由于缓冲区大小...根据

使用1k缓冲区可以获得可忽略的延迟
# Window_Size = 22050 # Point
Window_Size = 1024 # Point