The Pi Of Life
Posts: 9
Joined: Mon Jun 01, 2015 1:25 pm
Location: Belgium
Contact: Website

Raspberry Pi talking clock

Mon Jun 01, 2015 6:48 pm

Hi all,

I am a long time reader, newbie poster on these forums,

Thought I'd share my latest project with you guys, hoping to get some improvement tips and/or feedback.

I was looking into a way to make the Raspberry Pi speak the current time.
Using google's TTS-engine was not an option because it doesn't sound very natural IMO.

So I asked a female friend with a pleasureable voice to record some .wav files.
The files contain the numbers 0 to 60 (all separate files), the words 'Good morning', 'Good afternoon', 'Good evening', 'Good night', 'It is', 'Hours', 'Minutes', 'AM' and 'PM'

The next step was writing a script that converts the time to call the appropriate audio-files.
One thing I noticed along the way was that omxplayer took its time to play each file. So there was a one-second gap between each audio-file.

Combining all audio-files to one big file was a possible solution. Due to the WAV-header in each file it is not possible to just cat out all files to a 'bigfile.wav'

Searching the web for a while, I found a repository called sox, wich does the trick (and much more)

steps taken:

1) sudo apt-get update
2) sudo apt-get install sox
3) copy all wav-files to /home/pi/sounds
4) write a script:

Code: Select all

#!/usr/bin/env python

# Imports
import os
from time import strftime

# Audio
output = '/home/pi/voices/output.wav'
gmorning = '/home/pi/voices/gmorning.wav'
gafternoon = '/home/pi/voices/gafternoon.wav'
gevening = '/home/pi/voices/gevening.wav'
gnight = '/home/pi/voices/gnight.wav'
it_is = '/home/pi/voices/its.wav'
hours = '/home/pi/voices/hour.wav'
minutes = '/home/pi/voices/minutes.wav'
am = '/home/pi/voices/am.wav'
pm = '/home/pi/voices/pm.wav'

# Program starts here
if int(strftime("%H")) >= 5 and int(strftime("%H")) <= 11:
     greeting = gmorning
elif int(strftime("%H")) >= 12 and int(strftime("%H")) <= 17:
     greeting = gafternoon
elif int(strftime("%H")) >= 18 and int(strftime("%H")) <=23 :
     greeting = gevening
elif int(strftime("%H")) <=5 and int(strftime("%H")) >= 0 :
     greeting = gnight
hour = int(strftime("%H"))
if hour > 12:
   hour = hour - 12
   post = pm
elseif hour = 12:
   post = pm
else:
   post = am
minute = int(strftime("%M"))
u = '/home/pi/voices/'+str(hour)+'.wav'
m = '/home/pi/voices/'+str(minute)+'.wav'
combine = 'sox '+greeting+' '+it_is+' '+u+' '+hours+' '+' '+m+' '+minutes+' '+post+' '+output
os.system(combine)
os.system('omxplayer /home/pi/voices/output.wav > /dev/null')

User avatar
rpdom
Posts: 17273
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Raspberry Pi talking clock

Mon Jun 01, 2015 7:00 pm

sox is also capable of playing the files on its own, without omxplayer. If you invoke it with the "play" command, it will play the file without the delay that omxplayer has.

The Pi Of Life
Posts: 9
Joined: Mon Jun 01, 2015 1:25 pm
Location: Belgium
Contact: Website

Re: Raspberry Pi talking clock

Sun Jun 07, 2015 11:55 am

Thank you for your feedback ;)

I have tried playing all files separately with 'play' but there is still an annoying gap between all the words, that makes the output sound 'robotic' again.

So for now, I think the sox combining-trick is the best approach. The result here is a full sentence without any gaps :D

Return to “Other projects”