Currently i can record audio and save it as a NumPy array. What i need is after audio has been recorded i want to be able to record again but play this NumPy array at the same time
import pyaudio
import numpy
CHUNK = 1024
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK)
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(numpy.fromstring(data, dtype=numpy.int16))
numpydata = numpy.hstack(frames)
stream.stop_stream()
stream.close()
p.terminate()
You can use threading. Go to the official documentation for more information here I don't know recording and playing audio very well, so I have just created a template that should work for you.
Here is my example:
from threading import Thread
def record():
#Put your recording function here
def play():
#Put your playing function here
Thread(target = record).start()
Thread(target = play).start()
#These two start the two functions at the same time. If you want to only run the play
#function after it runs the record function once, you could do something like this:
Here is the better one:
from threading import Thread
def record():
#Put your recording function here
def play():
#Put your playing function here
while recorded!=True
Thread(target = record)
recorded=True
Thread(target = record).start()
Thread(target = play).start()
To repeat the last two lines in the second example, you can just add a while
or for
loop. Please feel free to ask questions in the comments.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.