<jupyter_start><jupyter_text>`sudo apt-get install python-pyaudio`<jupyter_code>import pyaudio
import wave
p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
        if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
            print(f"Input Device id {i} - " + p.get_device_info_by_host_api_device_index(0, i).get('name'))
RESPEAKER_RATE = 16000
RESPEAKER_CHANNELS = 2 # 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 = 0  # refer to input device id
CHUNK = 512
WAVE_OUTPUT_FILENAME = "output.wav"
RECORD_SECONDS = 5
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)):
    try:
        data = stream.read(CHUNK)
        frames.append(data)
    except IOError as ex:
        if ex[1] != pyaudio.paInputOverflowed:
            raise
        data = '\x00' * CHUNK  # or however you choose to handle it, e.g. return None
    

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()
import speech_recognition as sr
r = sr.Recognizer()
audio = b''.join(frames)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11079219288174512, 'fraction_numerical': 0.014351320321469576, 'mean_word_length': 4.052173913043478, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '119149', 'license': 'MIT', 'n_tokens_mistral': 679, 'n_tokens_neox': 638, 'n_words': 127, 'path': 'microphone/TestMicrophoneInput.ipynb', 'repo_name': 'sizingservers/Reachy.Audio', 'size': '1742'}