Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Published © CC BY-NC-SA

The Interactive Storytelling Radio

In this project we convert a neat looking radio into a voice-enabled, interactive storyteller. Future, here we come!

IntermediateFull instructions provided15 hours629
The Interactive Storytelling Radio

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Google AIY Voice Kit v1
×1
Radio
×1
Speaker
×1
Switch
×1
Wiring
×1

Software apps and online services

Google Cloud Platform
Google Dialogflow

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Multitool, Screwdriver
Multitool, Screwdriver

Story

Read more

Schematics

Dataflow

Story Decision Tree

Code

TelefunkenPublic.py

Python
import time
import os
import RPi.GPIO as GPIO  
from aiy.cloudspeech import CloudSpeechClient
from google.cloud import texttospeech
import dialogflow_v2 as dialogflow
import json

# Set credential file
# its a command to run in the shell -> export GOOGLE_APPLICATION_CREDENTIALS="/home/pi/telefunken/cloud_speech.json"

# Dialoglow Setup
session_client = dialogflow.SessionsClient()
session = session_client.session_path('YOUR DIALOGFLOW PROJECT ID', 12345)

# Instantiates a Google Cloud client
speechClient = texttospeech.TextToSpeechClient()
cloudClient = CloudSpeechClient()

# Button Setup, ground to CLK
GPIO.setmode(GPIO.BCM)  
switchPin = 11
GPIO.setup(switchPin, GPIO.OUT) 
GPIO.output(switchPin, GPIO.HIGH) 


def getDialogResponse(text):

    text_input = dialogflow.types.TextInput(text=text, language_code='EN')

    query_input = dialogflow.types.QueryInput(text=text_input)

    response = session_client.detect_intent(session=session, query_input=query_input)

    text = response.query_result.fulfillment_text

    return response

def say(text):

    synthesis_input = texttospeech.types.SynthesisInput(text=text)
    voice = texttospeech.types.VoiceSelectionParams(language_code='en-GB', ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE)
    audio_config = texttospeech.types.AudioConfig(audio_encoding=texttospeech.enums.AudioEncoding.LINEAR16)
    response = speechClient.synthesize_speech(synthesis_input,voice,audio_config)

    with open('/home/pi/telefunken/output.wav', 'wb') as out:
        out.write(response.audio_content)
        out.close()

    os.system('aplay ' + "/home/pi/telefunken/output.wav")


while True:

    turnedOn = GPIO.input(switchPin)

    if turnedOn:

        print('listening')
        spokenText = str(cloudClient.recognize())
        print(spokenText)

        if spokenText is not None:
            response = getDialogResponse(spokenText)
            textToSay = response.query_result.fulfillment_text
            say(textToSay)

    else:
        time.sleep(0.2)

Credits

Comments