Voice with MicroPython for the EV3

The new MicroPython environment for the EV3 has sound commands… but not ‘speak’ like the full ev3dev python (ev3dev-lang-pyhton).

But it has ‘os.system’ so we can make system calls…

So

#!/usr/bin/env pybricks-micropython
import os
os.system('espeak "hello" --stdout | aplay')

works but as soon as we instantiate an ev3brick it stops working:

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
import os
os.system('espeak "hello" --stdout | aplay')

The error is ‘aplay: main:788: audio open error: Device or resource busy’

So it seems pybricks allocates the ALSA sound card in some exclusive way. Strange because ‘aplay -L’ shows there is a software mixer:

null
Discard all samples (playback) or generate zero samples (capture)
default:CARD=legoev3
LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker
Default Audio Device
sysdefault:CARD=legoev3
LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker
Default Audio Device
dmix:CARD=legoev3,DEV=0
LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker
Direct sample mixing device
dsnoop:CARD=legoev3,DEV=0
LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker
Direct sample snooping device
hw:CARD=legoev3,DEV=0
LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker
Direct hardware device without any conversions
plughw:CARD=legoev3,DEV=0
LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker
Hardware device with all software conversions

So after several try&fail iteractions, I got a workaround by creating a ‘/home/robot/.asoundrc’ file:

pcm.hwmix {
type dmix
ipc_key 1939 # must be unique
slave { pcm "hw:CARD=legoev3" }
}
pcm.!default hwmix

This works… unless we also use ‘brick.sound.beep()’… then it locks in some other way. Oh well, nothing is perfect.

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *