Arkwood has a long-standing hatred of the BBC, and in particular BBC News. ‘It’s all a pack of lies,’ he said on their coverage of current affairs. But he still wants to know about the weather, and anything to do with Scotland. So I wrote some Python code on our tiny Raspberry Pi computer that listens to the BBC News channel, and whenever anything is mentioned on these subjects I alert him by way of playing a sound on his headphones. Sounds far-fetched, but read on…
Here’s the code
import pygame from speech import Speech from storage import Storage from time import sleep speech = Speech() storage = Storage() # constants NEWS_FILE = "news.txt" KEYWORDS_WEATHER = ["weather", "sun", "rain", "cloud"] KEYWORDS_SCOTLAND = ["scotland", "scottish"] # set up alerts pygame.mixer.init() alert_weather = pygame.mixer.Sound("161545__dasdeer__rain.wav") alert_scotland = pygame.mixer.Sound("90093__alienxxx__piper-and-drums.wav") # loop until armageddon while True: # get news chat news_chat = Speech().speech_to_text('/home/pi/PiAUISuite/VoiceCommand/speech-recog.sh').lower() # store chat storage.write_timestamp(NEWS_FILE, news_chat) # play alert if keyword found for word in news_chat.split(): if (word in KEYWORDS_WEATHER): alert_weather.play() storage.write_timestamp(NEWS_FILE, "[play rain]") break elif (word in KEYWORDS_SCOTLAND): alert_scotland.play() storage.write_timestamp(NEWS_FILE, "[play bagpipes]") break # pause before next sample sleep(5)
Okay. So first we set up some constants, including keywords that Arkwood wants us to listen out for. Then we set up some sound files to play when the keywords are uttered by a BBC anchorman.
The code then loops, using Google’s Speech To Text service to sample the BBC News channel with a microphone pressed up against the TV speaker. We store each sample in a file for reference – here’s an example of some of the news we have captured:
2014-04-24 18:45:16: committed to his world destruction you can have peace 2014-04-24 18:45:26: under what circumstances would you speak again 2014-04-24 18:45:37: do when i'm worse than other 2014-04-24 18:45:48: normal blood sugar message 2014-04-24 18:45:59: zebras the very people are committed to r&r 2014-04-24 18:46:11: your mom to clarity as long as im 2014-04-24 18:46:21: and i midland 2014-04-24 18:46:31: actually mean never 2014-04-24 18:46:43: pension of tools 2014-04-24 18:46:53: could i think 2014-04-24 18:47:04: pullman from the us 60 state john kerry 2014-04-24 18:47:15: if they continue to get anywhere 2014-04-24 18:47:26: plenty of people here 2014-04-24 18:47:36: free cod 2014-04-24 18:47:46: 2014-04-24 18:47:56: the characterization of the 2014-04-24 18:48:08: big risk in the sense that as you know better than anybody 2014-04-24 18:48:19: mr me some old animosity 2014-04-24 18:48:29: ione both sides 2014-04-24 18:48:41: when they weren't ceremoniously boots 2014-04-24 18:48:52: most infamous and brotherhood and in egypt 2014-04-24 18:49:03: 6842 years ago to their own app 2014-04-24 18:49:13: i could sing materials into gaza 2014-04-24 18:49:25: bison his people have to deal with monistat to skepticism 2014-04-24 18:49:35: give your 2014-04-24 18:49:47: ukrainian government says its fully synthetic in action 2014-04-24 18:49:58: based on the border with ukraine 2014-04-24 18:50:09: exponent natalia until labor is internet 2014-04-24 18:50:19: 12 2014-04-24 18:50:30: ration earlier in the morning today 2014-04-24 18:50:41: the checkpoint they want some 2014-04-24 18:50:53: we had president putin 2014-04-24 18:51:04: should i buy the cranium hoopla gang
Now we can check each sample to see if it contains our keywords. If we find anything to do with the weather then we use Pygame to play our rain sound file (the weather is invariably rain in the UK, even when the forecast says ‘sun’ or ‘cloud’). If we find that Scotland has been mentioned then we play the bagpipes file instead. Arkwood’s headphones are attached to the audio jack of our Raspberry Pi.
2014-04-24 19:25:43: sacramento weather today 2014-04-24 19:25:43: [play rain]
‘My God!’ my debauched Belgian friend said, ‘It actually works. I can ignore all the BBC’s fibs until I hear the sound of my favourite subjects’.
And to get the sound output through the headphones I made use of the following article:
Disable HDMI Audio & Force 1/8″ Jack Audio?
Google’s Speech To Text service is invoked from our Speech class:
from subprocess import Popen, PIPE class Speech(object): # converts speech to text def speech_to_text(self, filepath): try: # utilise PiAUISuite to turn speech into text text = Popen(['sudo', filepath], stdout=PIPE).communicate() # tidy up text text = text.replace('"', '').strip() # debug print(text) return text except: print ("Error translating speech")
And we store BBC News chat to file using our Storage class:
import datetime class Storage(object): # write to timestamp file def write_timestamp(self, filename, value): timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") try: with open(filename, "a") as timestamp_file: timestamp_file.write(timestamp + ': ' + value.strip() + '\n') except: print ("Error writing timestamp")
I had one question for Arkwood though. ‘Hey. How come you’re interested in Scotland so much, what with you being Belgian and all?’
My buddy gave me an angry glare then replied, ‘I like lochs. What’s it to you, piss face?’