无限连续不断地实时流式传输音频信号,Python [英] Continuesly streaming audio signal real time infinitely, Python
问题描述
我有一个简单的问题,在使用python音频音频流从Python的音频插孔传输音频信号时,如何使用pyaudio库继续音频流信号传输,直到选择停止"程序.
I have a simple question, while streaming audio signal from audio jack in Python, using pyaudio library how can I keep streaming the audio signal until I choose to "stop" the program.
示例:我们捕获网络摄像头的方式在无限while循环中无限帧.
Example: The way we capture our web camera frames infinitely under a infinite while loop.
例如:在此代码(摘自链接)中,流仅持续5秒钟,将进行哪些修改以达到我的目的
For example: In this code(take from link)that records the stream just for 5 seconds what will be the modification that will serve my purpose
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()
此链接上给出的代码(处理音频数据使用回调模式)将其记录4-5秒.
Also the code given on this link (Handling audio data using callback mode) records it for 4-5 seconds.
如果有人可以帮助我,我将非常感激!
I will be really grateful if someone could help me with this!!
推荐答案
嗯,与此同时,我想出了解决方案
Well , Meanwhile I figured out solution
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()
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()
这篇文章此处将以简单而具体的方式为您提供帮助
And this post here will help you in simple and concrete way
这篇关于无限连续不断地实时流式传输音频信号,Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!