{"id":143,"date":"2021-02-02T14:08:00","date_gmt":"2021-02-02T14:08:00","guid":{"rendered":"http:\/\/doctor-dark.co.uk\/blog\/2021\/02\/02\/using-google-aiy-voice\/"},"modified":"2023-12-18T12:13:25","modified_gmt":"2023-12-18T12:13:25","slug":"using-google-aiy-voice","status":"publish","type":"post","link":"https:\/\/doctor-dark.co.uk\/blog\/using-google-aiy-voice\/","title":{"rendered":"Using Google AIY voice."},"content":{"rendered":"<h2 style=\"text-align: left;\"><span style=\"font-family: verdana;\">Using Google AIY voice, part 94.<\/span><\/h2>\n<div style=\"text-align: justify;\"><span style=\"font-family: verdana;\">I wrote a useful function, so that any of my Raspberry Pi machines could send a string to the Google Assistant as a message to be spoken aloud, and it worked very well. Here it is&#8230;<\/span><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div>\n<div>\n<div><span style=\"font-family: arial; font-size: xx-small;\"># A function to send a string to PiAssistant for output as speech<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">import paramiko<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\"><br \/><\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">ssh = paramiko.SSHClient()<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">ssh.load_host_keys(filename=&#8217;\/home\/pi\/.ssh\/known_hosts&#8217;)<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\"><br \/><\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">server, username, password = (&#8216;PiAssistant&#8217;, &#8216;pi&#8217;, &#8216;your_password-goes_here&#8217;)<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">def say_this_with_assistant(say):<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">&nbsp; &nbsp; ssh.connect(server, username=username, password=password)<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">&nbsp; &nbsp; command = &#8220;python ~\/AIY-projects-python\/src\/aiy\/voice\/tts.py &#8220;&#8221; + say + &#8220;&#8221; &#8211;lang en-GB &#8211;volume 10 &#8211;pitch 60&#8243;<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">&nbsp; &nbsp; ssh.exec_command(command)<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">&nbsp; &nbsp; ssh.close()<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">&nbsp; &nbsp; return&nbsp;<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\"><br \/><\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">print(&#8220;Starting&#8230;n&#8221;)<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">say_this_with_assistant(&#8220;Normal service will be resumed as soon as possible. &#8220;)<\/span><\/div>\n<div><span style=\"font-family: arial; font-size: xx-small;\">print(&#8220;Finished.n&#8221;)<\/span><\/div>\n<div><span style=\"font-family: arial;\"><br \/><\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"font-size: medium;\"><span style=\"font-family: arial;\">The snag occurs when two, or more, machines want to say something at the same time. The PiAssistant calmly multitasks, and says the strings at the same time, which sounds interesting, but tends to be incomprehensible. I hoped that <b>voice\/tts.py<\/b> would have some way of letting other programs know whether it was busy, but it doesn&#8217;t. However, the low level Linux <b>ps<\/b> command can tell when&nbsp;<\/span><b style=\"font-family: arial;\">voice\/tts.py<\/b><span style=\"font-family: arial;\">&nbsp;is running, so I added a check for that as a Paramiko command, like this&#8230;<\/span><\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"font-family: arial; font-size: medium;\"><br \/><\/span><\/div>\n<div style=\"text-align: justify;\">\n<div style=\"font-family: arial; font-size: x-small;\"># A function to send a string to PiAssistant for output as speech when it&#8217;s free<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">import paramiko<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">import time<\/div>\n<div style=\"font-family: arial; font-size: x-small;\"><\/div>\n<div style=\"font-family: arial; font-size: x-small;\">ssh = paramiko.SSHClient()<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">ssh.load_host_keys(filename=&#8217;\/home\/pi\/.ssh\/known_hosts&#8217;)<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())<\/div>\n<div style=\"font-family: arial; font-size: x-small;\"><\/div>\n<div style=\"font-family: arial; font-size: x-small;\">def say_when_free(say):<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; server, username, password = (&#8216;PiAssistant&#8217;, &#8216;pi&#8217;, &#8216;<span style=\"text-align: left;\">your_password-goes_here<\/span>&#8216;)<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; ssh.connect(server, username=username, password=password)&nbsp; &nbsp;&nbsp;<\/div>\n<div style=\"font-family: arial; font-size: x-small;\"><\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; command = &#8220;ps -ef | grep -v grep | grep &#8220;voice.tts&#8221; | wc -l&#8221;<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; while True:<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; answer = str(ssh_stdout.read())<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; if answer[2] == &#8220;0&#8221;:<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; command = &#8220;python ~\/AIY-projects-python\/src\/aiy\/voice\/tts.py &#8220;&#8221; + say + &#8220;&#8221; &#8211;lang en-GB &#8211;volume 10 &#8211;pitch 60&#8243;<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; ssh.exec_command(command)<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; ssh.close()<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">&nbsp; &nbsp; return&nbsp;<\/div>\n<div style=\"font-family: arial; font-size: x-small;\"><\/div>\n<div style=\"font-family: arial; font-size: x-small;\">print(&#8220;Starting&#8230;n&#8221;)<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">say_when_free(&#8220;I waited until I was allowed to say this. &#8220;)<\/div>\n<div style=\"font-family: arial; font-size: x-small;\">print(&#8220;Finished.n&#8221;)<\/div>\n<div style=\"font-family: arial; font-size: x-small;\"><\/div>\n<div><span style=\"font-family: verdana; font-size: medium;\">I&#8217;m not going to claim&nbsp; any of this as properly written Python, the best way to do what is required, or even particularly original, but it does currently seem to be the <b><i>only <\/i><\/b>description of how to do this that&#8217;s on the internet.<\/span><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Using Google AIY voice, part 94. I wrote a useful function, so that any of my Raspberry Pi machines could send a string to the Google Assistant as a message to be spoken aloud, and it worked very well. Here it is&#8230; # A function to send a string to PiAssistant for output as speech &hellip; <a href=\"https:\/\/doctor-dark.co.uk\/blog\/using-google-aiy-voice\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using Google AIY voice.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-143","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=143"}],"version-history":[{"count":1,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":417,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts\/143\/revisions\/417"}],"wp:attachment":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}