2

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.

Example: The way we capture our web camera frames infinitely under a infinite while loop.

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

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!!

Laveena
  • 453
  • 1
  • 7
  • 24
  • 2
    You can have a look at my example [rec_unlimited.py](https://github.com/spatialaudio/python-sounddevice/blob/master/examples/rec_unlimited.py). It uses the [sounddevice](http://python-sounddevice.readthedocs.io/) module, but you can do the same thing with PyAudio. – Matthias Feb 07 '18 at 09:25

2 Answers2

6

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

And this post here will help you in simple and concrete way

NSVR
  • 192
  • 1
  • 2
  • 12
Laveena
  • 453
  • 1
  • 7
  • 24
0

Hello this is my code with which audio and video is recorded separately and pause the audio and video I hope it helps you

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()
mikey
  • 46
  • 5