Micropython audio library problems using Nicla Vision

Hi!

I am trying to play a .wav file after detecting a voice command with Nicla Vision.

What is working:
Playing.wav files without voice command activation.
Detecting voice commands without playing .wav file.

What is not working:
Nicla vision shuts down after playing the .wav.

I used this code at first:

import audio
import tf
import micro_speech
import os
import pyb
import time
import struct

# MCP4728 I2C address
MCP4728_ADDR = 0x60

# Initialize I2C bus
i2c = pyb.I2C(1, pyb.I2C.MASTER, baudrate=400000)

def read_wav_file(file_path):
    try:
        with open(file_path, 'rb') as f:
            wav_data = f.read()
        return wav_data
    except Exception as e:
        print("Error reading WAV file:", e)
        return None

def send_to_dac(data):
    for byte in data:
        # Convert 8-bit to 16-bit by shifting and send
        chunk = struct.pack('<H', byte << 8)
        i2c.send(chunk, MCP4728_ADDR)

def play_wav(file_path):
    wav_data = read_wav_file(file_path)
    if wav_data:
        audio_data = wav_data[44:]  # Assuming the WAV data starts at byte 44 (after the header)
        send_to_dac(audio_data)

labels = ["Silence", "Unknown", "Yes", "No"]

led_red = pyb.LED(1)
led_green = pyb.LED(2)

model = tf.load("/speech.tflite")
speech = micro_speech.MicroSpeech()
audio.init(channels=1, frequency=16000, gain_db=24, highpass=0.9883)

# Start audio streaming
audio.start_streaming(speech.audio_callback)

while True:
    # Run micro-speech without a timeout and filter detections by label index.
    idx = speech.listen(model, timeout=0, threshold=0.70, filter=[2, 3])
    led = led_green if idx == 2 else led_red
    if labels[idx] == 'No':
        play_wav("hpy.wav")
    print(labels[idx])
    for i in range(0, 4):
        led.on()
        time.sleep_ms(25)
        led.off()
        time.sleep_ms(25)

I’ve narrowed the problem to audio library and tried to deinitialize it before playing the .wav file and reinitialize it after. But the audio.deint() (audio — Audio Module — MicroPython 1.22 documentation) raises an exception: AttributeError: ‘module’ Object has no attribute 'deinť.

import audio
import tf
import micro_speech
import os
import pyb
import time
import struct

# MCP4728 I2C address
MCP4728_ADDR = 0x60

# Initialize I2C bus
i2c = pyb.I2C(1, pyb.I2C.MASTER, baudrate=400000)

def read_wav_file(file_path):
    try:
        with open(file_path, 'rb') as f:
            wav_data = f.read()
        return wav_data
    except Exception as e:
        print("Error reading WAV file:", e)
        return None

def send_to_dac(data):
    for byte in data:
        # Convert 8-bit to 16-bit by shifting and send
        chunk = struct.pack('<H', byte << 8)
        print(chunk)
        i2c.send(chunk, MCP4728_ADDR)

def play_wav(file_path):
    wav_data = read_wav_file(file_path)
    if wav_data:
        audio_data = wav_data[44:]  # Assuming the WAV data starts at byte 44 (after the header)
        send_to_dac(audio_data)

labels = ["Silence", "Unknown", "Yes", "No"]

led_red = pyb.LED(1)
led_green = pyb.LED(2)

model = tf.load("/speech.tflite")
speech = micro_speech.MicroSpeech()
audio.init(channels=1, frequency=16000, gain_db=24, highpass=0.9883)

# Start audio streaming
audio.start_streaming(speech.audio_callback)

while True:
    # Run micro-speech without a timeout and filter detections by label index.
    idx = speech.listen(model, timeout=0, threshold=0.70, filter=[2, 3])
    led = led_green if idx == 2 else led_red
    if labels[idx] == 'No':
        audio.stop_streaming()
        audio.deint() #<--- AttributeError: 'module' Object has no attribute 'deinť
        play_wav("hpy.wav")
        audio.init(channels=1, frequency=16000, gain_db=24, highpass=0.9883)
        audio.start_streaming(speech.audio_callback)
    print(labels[idx])
    for i in range(0, 4):
        led.on()
        time.sleep_ms(25)
        led.off()
        time.sleep_ms(25)

I’ve tried not using audio.deint() but i get this exception. (OSError. Failed to init DFSDM) At least here the board doesn’t reset.

import audio
import tf
import micro_speech
import os
import pyb
import time
import struct

# MCP4728 I2C address
MCP4728_ADDR = 0x60

# Initialize I2C bus
i2c = pyb.I2C(1, pyb.I2C.MASTER, baudrate=400000)

def read_wav_file(file_path):
    try:
        with open(file_path, 'rb') as f:
            wav_data = f.read()
        return wav_data
    except Exception as e:
        print("Error reading WAV file:", e)
        return None

def send_to_dac(data):
    for byte in data:
        # Convert 8-bit to 16-bit by shifting and send
        chunk = struct.pack('<H', byte << 8)
        i2c.send(chunk, MCP4728_ADDR)

def play_wav(file_path):
    wav_data = read_wav_file(file_path)
    if wav_data:
        audio_data = wav_data[44:]  # Assuming the WAV data starts at byte 44 (after the header)
        send_to_dac(audio_data)

labels = ["Silence", "Unknown", "Yes", "No"]

led_red = pyb.LED(1)
led_green = pyb.LED(2)

model = tf.load("/speech.tflite")
speech = micro_speech.MicroSpeech()
audio.init(channels=1, frequency=16000, gain_db=24, highpass=0.9883)

# Start audio streaming
audio.start_streaming(speech.audio_callback)

while True:
    # Run micro-speech without a timeout and filter detections by label index.
    idx = speech.listen(model, timeout=0, threshold=0.70, filter=[2, 3])
    led = led_green if idx == 2 else led_red
    if labels[idx] == 'No':
        audio.stop_streaming()
        play_wav("hpy.wav")
        audio.init(channels=1, frequency=16000, gain_db=24, highpass=0.9883)
        audio.start_streaming(speech.audio_callback)
    print(labels[idx])
    for i in range(0, 4):
        led.on()
        time.sleep_ms(25)
        led.off()
        time.sleep_ms(25)



Any ideas how to approach this?

Hi, let me check the code in the audio module.

Any updates on this by any chance?

Yes, I’ve found the issue and will fix it soon.

1 Like

@iabdalkader - Thank you!

Just an update on this, I found some issues with the audio module and I’ve already fixed them, but there’s another issue with our tensorflow library that @kwagyeman is looking into right now.

Thanks for the update!

Hi, we are working on a fix for you. Ibrahim is activity debugging this right now.

1 Like

Hi! I finally have an update for you. Micro Speech should be working again now and it has been improved by a lot! Please install the dev firmware from the IDE, and use this example:

Thanks, will check it out soon!