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?