gpt4 book ai didi

python-3.x - 无限实时连续流式传输音频信号,Python

转载 作者:行者123 更新时间:2023-12-02 22:18:54 25 4
gpt4 key购买 nike

我有一个简单的问题,在使用 pyaudio 库从 Python 的音频插孔流式传输音频信号时,我如何才能继续流式传输音频信号,直到我选择“停止”程序。

例子:我们的方式capture our web camera在无限 while 循环下无限帧。

例如:在这段代码(取自 link )中,仅记录流 5 秒,将进行哪些修改以达到我的目的

import pyaudio
import wave
import numpy as np
CHUNK = 44100
FORMAT = pyaudio.paInt32
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)

print("* recording")
frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
audio_data = np.fromstring(data, dtype=np.int32)
print(data)
print(audio_data)
frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

此链接 (Handling audio data using callback mode) 上给出的代码也记录了 4-5 秒。

如果有人能帮助我,我将不胜感激!!

最佳答案

好吧,同时我找到了解决方案

import pyaudio
import numpy as np
import pylab
import time
import sys
import matplotlib.pyplot as plt


RATE = 44100
CHUNK = int(RATE/20) # RATE / number of updates per second

def soundplot(stream):

t1=time.time()
#use np.frombuffer if you face error at this line
data = np.fromstring(stream.read(CHUNK),dtype=np.int16)
print(data)

if __name__=="__main__":
p=pyaudio.PyAudio()
stream=p.open(format=pyaudio.paInt16,channels=1,rate=RATE,input=True,
frames_per_buffer=CHUNK)
for i in range(sys.maxsize**10):
soundplot(stream)
stream.stop_stream()
stream.close()
p.terminate()

还有这篇文章here将以简单而具体的方式帮助您

关于python-3.x - 无限实时连续流式传输音频信号,Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48653745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com