Reputation: 316
I want to record audio with PyAudio in Python. I'm using this code:
import pyaudio
import time
WIDTH = 2
CHANNELS = 2
RATE = 44100
p = pyaudio.PyAudio()
def callback(in_data, frame_count, time_info, status):
return (in_data, pyaudio.paContinue)
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.1)
stream.stop_stream()
stream.close()
p.terminate()
It doesn't record and play audio, but finishes with error. If I run it from console, it prints:
jack server is not running or cannot be started
Full output is here: https://pastebin.com/JR5ADT3p
What I should do?
Upvotes: 0
Views: 93
Reputation: 827
Try this:
def record(duration=3, fs=8000):
nsamples = duration*fs
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=fs, input=True,
frames_per_buffer=nsamples)
buffer = stream.read(nsamples)
array = np.frombuffer(buffer, dtype='int16')
stream.stop_stream()
stream.close()
p.terminate()
return array
my_recordvoice = record() # say something
Upvotes: 1