{"id":141,"date":"2021-02-19T20:23:00","date_gmt":"2021-02-19T20:23:00","guid":{"rendered":""},"modified":"2023-12-18T12:13:11","modified_gmt":"2023-12-18T12:13:11","slug":"a-new-program-for-google-voice-aiy","status":"publish","type":"post","link":"https:\/\/doctor-dark.co.uk\/blog\/a-new-program-for-google-voice-aiy\/","title":{"rendered":"A new program for Google Voice AIY"},"content":{"rendered":"<p style=\"text-align: justify;\"><span style=\"font-family: verdana; font-size: medium;\">My earlier post, about a function that could send things to the Google AIY Raspberry Pi Assistant, and supposedly prevent more than one message being spoken at a time, was errmmm, wrong. That&#8217;s a technical term, that we programmers use.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: verdana; font-size: medium;\">I wrote a test program for my Pi cluster, whereby each of the sixteen cores would announce its hostname and rank. It&#8217;s not all that often you get the opportunity to use the word cacophony, but&#8230;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: verdana; font-size: medium;\">Basically, several processes could all think the Assistant was not busy, and they all sent messages in the time it took for the first message to start being spoken.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: verdana; font-size: medium;\">I spent a while looking at how to program mutual exclusivity for a resource, and was impressed by how complex such an apparently simple thing can get.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: verdana; font-size: medium;\">I decided that what was needed was a simple program, running on the Assistant, that would watch a directory, notice when a file to be spoken arrived, and speak the text in that file. Python makes it easy to deal with more than one file in the directory. Here&#8217;s what I wrote&#8230;<\/span><\/p>\n<div style=\"text-align: left;\">\n<div style=\"text-align: justify;\"><span style=\"font-family: courier; font-size: small;\">#!\/usr\/bin\/python3<\/span><\/div>\n<p><span style=\"font-family: courier; font-size: x-small;\">#<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\"># Program to run on PiAssistant, to watch for newly arrived text files in<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\"># \/home\/pi\/Messages that it should speak, and delete them once it has.<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">import os<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">import time<\/span><span style=\"font-family: courier; font-size: x-small;\"><br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">path = &#8220;\/home\/pi\/Messages\/&#8221;<\/span><span style=\"font-family: courier; font-size: x-small;\"><br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">while True:<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">&nbsp; &nbsp; with os.scandir(path) as entries:<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; for entry in entries:<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f = open(entry, &#8220;r&#8221;)<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content = f.read()<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.close()<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command = &#8220;python ~\/AIY-projects-python\/src\/aiy\/voice\/tts.py &#8220;&#8221; + <br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content + &#8220;&#8221; &#8211;lang en-GB &#8211;volume 10 &#8211;pitch 60 &#8211;speed 90&#8243;<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os.system(command)<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(0.5)<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os.remove(path+entry.name)<br \/><\/span><span style=\"font-family: courier; font-size: x-small;\">&nbsp; &nbsp; time.sleep(0.5)<\/span><\/div>\n<p style=\"text-align: left;\">\n<p style=\"text-align: justify;\"><span style=\"font-family: verdana; font-size: medium;\">When it spots one of more files in the Messages directory, it reads the text, and sends it out to be spoken. It can supposedly only do one file at a time, but&#8230; Still the cacophony!<\/span><span style=\"font-family: courier;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial; font-size: x-small;\">#RaspberryPi #GoogleAIY #Python&nbsp; <\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>My earlier post, about a function that could send things to the Google AIY Raspberry Pi Assistant, and supposedly prevent more than one message being spoken at a time, was errmmm, wrong. That&#8217;s a technical term, that we programmers use. I wrote a test program for my Pi cluster, whereby each of the sixteen cores &hellip; <a href=\"https:\/\/doctor-dark.co.uk\/blog\/a-new-program-for-google-voice-aiy\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A new program for Google Voice AIY&#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-141","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\/141","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=141"}],"version-history":[{"count":1,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":350,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts\/141\/revisions\/350"}],"wp:attachment":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}