继续无限地实时传输音频信号,Python

时间:2018-02-06 23:43:27

标签: python-3.x audio real-time pyaudio

我有一个简单的问题,在使用Python音频插孔传输音频信号时,使用pyaudio库我怎样才能保持音频信号流,直到我选择"停止"该计划。

示例:我们capture our web camera在无限循环下无限制地构建的方式。

例如:在此代码中(从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秒。

如果有人可以帮助我,我将非常感激!!

2 个答案:

答案 0 :(得分:4)

嗯,同时我想出了解决方案

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()

这篇文章here将以简单而具体的方式为您提供帮助

答案 1 :(得分:0)

您好,这是我的代码,用于分别录制音频和视频并暂停音频和视频,希望对您有帮助

import cv2
import numpy as np
from datetime import datetime
import gtk
import keyboard

import pyaudio
import wave
import sys

flagrecord=True
#chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
ropen=True
chunk = int(RATE/20) 


def show_webcam(flagrecord):

    cam = cv2.VideoCapture(0)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    frame_width = int(cam.get(3))
    frame_height = int(cam.get(4))
    FONT = cv2.FONT_HERSHEY_PLAIN
    filename = datetime.now().strftime("%Y-%m-%d_%H.%M.%S") + ".avi"
    filenamea = datetime.now().strftime("%Y-%m-%d_%H.%M.%S") 


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

    out = cv2.VideoWriter(filename,fourcc, 20, (frame_width,frame_height))

    all = []
    aux = []
    stream.start_stream()

    flagaudio=False

    while True:

        ret_val, img = cam.read()
        title = datetime.now().strftime("%Y-%m-%d*%H:%M:%S")

        if flagrecord: 

            img = cv2.flip(img,1)
            cv2.putText(img, "REC", (40,40), FONT, 3 , (0,0,255), 3)
            cv2.circle(img, (20,20), 10 , (0,0,255), -1)
            cv2.rectangle(img, (30,430),(600,480),(0,0,0), -1)
            cv2.putText(img, title, (40,470), FONT, 3 , (255,255,255), 2)
            cv2.imshow('Grabacion de Audiencias', img)
            data = stream.read(chunk)
            aux.append(data)
            out.write(img)

        else:

            img = cv2.flip(img,1)
            cv2.putText(img, "PAUSE", (40,40), FONT, 3 , (255,0,0), 3)
            cv2.circle(img, (20,20), 10 , (255,0,0), -1)
            cv2.rectangle(img, (50,430),(570,480),(0,0,0), -1)
            cv2.putText(img, "Audiencias En Pausa", (60,470), FONT, 3 , (255,0,0), 2)
            cv2.imshow('Grabacion de Audiencias', img)

            if flagaudio:
               all+=aux
               del  aux[:]
               data= 0
               stream.stop_stream()
            else:
               pass


        q=cv2.waitKey(1)
        if q == 27:
            break  
        if q == ord('p'):
            flagrecord=False
            flagaudio = True
        if q == ord('c'):
            flagrecord=True
            flagaudio=False
            stream.start_stream()
        if q == ord('q'):
            break  

    cam.release()        
    out.release()        
    cv2.destroyAllWindows()

    stream.close()
    p.terminate()
    all+=aux
    data = ''.join(all)
    wf = wave.open(filenamea, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(data)
    wf.close()




def main():
    show_webcam(mirror=True)


if __name__ == '__main__':
    main()