Jan-22-2024, 04:34 AM
I am trying to use ReSpeaker 4 mic array v2.0. It appears to work perfectly using:
arecord -D plughw:1,0 -f S16_LE -c2 -r16000 test.wav
aplay test.wav
I am now trying to record speech using python 3.8.10 and pyaudio 0.2.11
I used the code supplied in the ReSpeaker documentation, but that blocked and could not be exited with ctrl-c or ctrl-d.
I modified the code to print the number of each chunk of data read. That made the code work....mostly. It normally runs to completion and save the audio file, but sometimes it halts on a random chunk, requiring the process to be killed.
I would appreciate a solution to this problem that enables speech to be recorded reliably, without a print statement (hopefully without changing the versions I use - because that will introduce a lot of knock-on bugs).
The code (including the 'print' statement that I added is:
arecord -D plughw:1,0 -f S16_LE -c2 -r16000 test.wav
aplay test.wav
I am now trying to record speech using python 3.8.10 and pyaudio 0.2.11
I used the code supplied in the ReSpeaker documentation, but that blocked and could not be exited with ctrl-c or ctrl-d.
I modified the code to print the number of each chunk of data read. That made the code work....mostly. It normally runs to completion and save the audio file, but sometimes it halts on a random chunk, requiring the process to be killed.
I would appreciate a solution to this problem that enables speech to be recorded reliably, without a print statement (hopefully without changing the versions I use - because that will introduce a lot of knock-on bugs).
The code (including the 'print' statement that I added is:
import pyaudio
import wave
RESPEAKER_RATE = 16000
RESPEAKER_CHANNELS = 1 # change base on firmwares, 1_channel_firmware.bin as 1 or 6_channels_firmware.bin as 6
RESPEAKER_WIDTH = 2
# run getDeviceInfo.py to get index
RESPEAKER_INDEX = 16 # refer to input device id
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(
rate=RESPEAKER_RATE,
format=p.get_format_from_width(RESPEAKER_WIDTH),
channels=RESPEAKER_CHANNELS,
input=True,
input_device_index=RESPEAKER_INDEX,)
print("* recording")
frames = []
for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)):
print("Reading {}\n".format(i)) #I added this line!
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(RESPEAKER_CHANNELS)
wf.setsampwidth(p.get_sample_size(p.get_format_from_width(RESPEAKER_WIDTH)))
wf.setframerate(RESPEAKER_RATE)
wf.writeframes(b''.join(frames))
wf.close()
