{"id":1540,"date":"2019-04-14T20:01:02","date_gmt":"2019-04-14T19:01:02","guid":{"rendered":"https:\/\/ofalcao.pt\/blog\/?p=1540"},"modified":"2019-04-14T20:11:11","modified_gmt":"2019-04-14T19:11:11","slug":"voice-with-micropython-for-the-ev3","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2019\/voice-with-micropython-for-the-ev3","title":{"rendered":"Voice with MicroPython for the EV3"},"content":{"rendered":"\n<p>The new MicroPython environment for the EV3 has sound commands&#8230; but not &#8216;speak&#8217; like the full ev3dev python (ev3dev-lang-pyhton).<\/p>\n\n\n\n<p>But it has &#8216;os.system&#8217; so we can make system calls&#8230;<\/p>\n\n\n\n<p>So <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/env pybricks-micropython<br>import os<br>os.system('espeak \"hello\" --stdout | aplay')<\/pre>\n\n\n\n<p>works but as soon as we instantiate an ev3brick it stops working:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/env pybricks-micropython<br>from pybricks import ev3brick as brick<br>import os<br>os.system('espeak \"hello\" --stdout | aplay')<\/pre>\n\n\n\n<p>The error is &#8216;aplay: main:788: audio open error: Device or resource busy&#8217;<\/p>\n\n\n\n<p>So it seems pybricks allocates the ALSA sound card in some exclusive way. Strange because &#8216;aplay -L&#8217; shows there is a software mixer:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">null<br>     Discard all samples (playback) or generate zero samples (capture)<br> default:CARD=legoev3<br>     LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker<br>     Default Audio Device<br> sysdefault:CARD=legoev3<br>     LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker<br>     Default Audio Device<br> dmix:CARD=legoev3,DEV=0<br>     LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker<br>     Direct sample mixing device<br> dsnoop:CARD=legoev3,DEV=0<br>     LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker<br>     Direct sample snooping device<br> hw:CARD=legoev3,DEV=0<br>     LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker<br>     Direct hardware device without any conversions<br> plughw:CARD=legoev3,DEV=0<br>     LEGO MINDSTORMS EV3 Speaker, LEGO MINDSTORMS EV3 Speaker<br>     Hardware device with all software conversions<\/pre>\n\n\n\n<p>So after several try&amp;fail iteractions, I got a workaround by creating a &#8216;\/home\/robot\/.asoundrc&#8217; file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pcm.hwmix {<br>     type dmix<br>     ipc_key 1939 # must be unique<br>     slave { pcm \"hw:CARD=legoev3\" }<br> }<br>pcm.!default hwmix<\/pre>\n\n\n\n<p>This works&#8230; unless we also use &#8216;brick.sound.beep()&#8217;&#8230; then it locks in some other way. Oh well, nothing is perfect.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The new MicroPython environment for the EV3 has sound commands&#8230; but not &#8216;speak&#8217; like the full ev3dev python (ev3dev-lang-pyhton). But it has &#8216;os.system&#8217; so we can make system calls&#8230; So #!\/usr\/bin\/env pybricks-micropythonimport osos.system(&#8216;espeak &#8220;hello&#8221; &#8211;stdout | aplay&#8217;) works but as soon as we instantiate an ev3brick it stops working: #!\/usr\/bin\/env pybricks-micropythonfrom pybricks import ev3brick as &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2019\/voice-with-micropython-for-the-ev3\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;Voice with MicroPython for the EV3&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[],"series":[],"class_list":["post-1540","post","type-post","status-publish","format-standard","hentry","category-sem-categoria"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-oQ","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=1540"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1540\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=1540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=1540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=1540"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=1540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}