Make your Raspberry Pi speak!

I found an article online once that taught me the “say” command on OSX – which i think was pretty cool! The command line function allows you to activate the speech to text feature of OSX and can be used for a variety of fun project. You can try it by firing up terminal (Applications > Utilities > Terminal) in OSX and typing

Say "hello world"

I embarked on an audacious project to teach my son programming over the holiday and thought this feature would be a really cool addition to one of the lessons. The idea is to teach him programming in python on the raspberry pi so he learn basic programming structures and also the hardware that it runs on. Natively the RPI does not have this (say) function built in but lucky I found a way to get it done. Here follows the instructions to get the RPI to speak.

The Raspberry Pi needs mplayer and internet access to make this work. By default it does not have mplayer installed by default.

sudo apt-get update
sudo apt-get install mplayer

Now you can create a command out of it by creating a /usr/bin/say file

#!/bin/bash
mplayer "http://translate.google.com/translate_tts?tl=en&q=$1";

Depending what speaker setup you have, you may need to adjust some settings. In particular, you can try telling the Pi what audio interface to use with the command

amixer cset numid=2


numbid can be any of
0=auto
1=analog
2=HDMI

On a side note, if you get this error from amixer:

amixer: Mixer attach default error: No such file or directory

The fix for this is to run this command

sudo modprobe snd-bcm2835

That command will create the necessary device directories in /dev/snd/

To actually run the file as a command or program it has to be executable, and in linux that means changing the permission attribute on the file:

chmod +x /usr/bin/say

And that’s it, you can now get the RPI to speak by typing the say “hello world” like on OSX.

3 thoughts on “Make your Raspberry Pi speak!

  • April 30, 2015 at 8:34 pm
    Permalink

    Very cool! Now you need to feed it a news site to read out loud over breakfast!

    Reply
    • May 1, 2015 at 12:07 pm
      Permalink

      That’s a damn good idea!

      Reply
  • April 14, 2016 at 1:26 am
    Permalink

    I’m getting that same error on my RPi2 (Arch Linux) running kernel 4.4.7:
    amixer: Mixer attach default error: No such file or directory

    If I downgrade to 4.1.21 the error is gone:
    Simple mixer control ‘PCM’,0
    Capabilities: pvolume pvolume-joined pswitch pswitch-joined
    Playback channels: Mono
    Limits: Playback -10239 – 400
    Mono: Playback 0 [96%] [0.00dB] [on]

    I see that loading the snd-bcm2835 module does some setup in /dev/snd as you mentioned and that might be to blame:

    Under 4.1.21:
    ls -l /dev/snd
    total 0
    crw-rw—- 1 root audio 116, 0 Feb 22 09:40 controlC0
    crw-rw—- 1 root audio 116, 16 Feb 22 09:40 pcmC0D0p
    crw-rw—- 1 root audio 116, 17 Feb 22 09:40 pcmC0D1p
    crw-rw—- 1 root audio 116, 1 Feb 22 09:40 seq
    crw-rw—- 1 root audio 116, 33 Feb 22 09:40 timer

    But under 4.4.7:
    ls -l /dev/snd
    total 0
    crw-rw—- 1 root audio 116, 1 Feb 22 09:40 seq
    crw-rw—- 1 root audio 116, 33 Feb 22 09:40 timer

    Any thoughts?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This blog is kept spam free by WP-SpamFree.