OH2 - Local Voice Rescue Mission

Hello,
I recently migrated to OH2 - a step I do not regret at all. OH2 rocks!
But there was this one thing: I could not use my local TTS any more :frowning:
And my…user base was not happy about that! (Quote: “Booo…not cool!”)
So for everyone who is using the original or my slightly extended version
of a local tts

Here is the way to keep using your local TTS with OpenHAB 2:
First, you need to create a copy of the php script “tts.php” (e.g. “tts-create.php”).
In the copy, change the mp3 file path to /etc/openhab2/sounds/tts.mp3 and remove all the bits that deliver the mp3 to the browser. For your convenience, I’ll list it further down.
The trick is to use a sendHttpGetRequest to create the mp3, then play it through the Squeezebox “playsound” function. (I suppose this will also work with sonos etc).
Here a code snippet with everything needed:

var String sinkToUse = "squeezebox:squeezeboxplayer:xxxx:yyyy"
var String TheURL="http://YOUR-IP/tts-create.php?msg="
rule "speak"
when
	Item your_trigger changed to 1
then
      var TheMessage=""
      TheMessage=TheURL+"Speak%20friend%20and%20enter."
      sendHttpGetRequest(TheMessage)
      playSound( sinkToUse, "tts.mp3")
end

And that’s it. Works.
If there is allready a stream playing, it is paused and resumed afterwards. If you have set a notificationvolume, this will be used for the playsound.
One thing you have to watch out for is that there may be no spaces in TheMessage, else the HttpRequest will fail.
So here is the PHP script:

<?php
$lng     = 'de-DE';
$msg     = $_GET['msg'];
$filewav = "tts.wav";
$filemp3 = "/etc/openhab2/sounds/tts.mp3";
require 'vendor/autoload.php';
use Lame\Lame;
use Lame\Settings;
//delete mp3 if present
if(file_exists($filemp3)) {
unlink($filemp3);
unlink($filewav);
}
     try {
	// espeak
	exec('/usr/bin/espeak -v mb-de4 -s 100 -p 20 -w ' . $filewav . ' "' . $msg . '"');
	//pico2wave
         //exec('/usr/bin/pico2wave -l=' . $lng . ' -w=' . $filewav . ' "' . $msg . '"');
         } catch(\RuntimeException $e) {
          var_dump($e->getMessage());
         }
// encoding type
$encoding = new Settings\Encoding\Preset();
$encoding->setType(Settings\Encoding\Preset::TYPE_STANDARD);
// lame settings
$settings = new Settings\Settings($encoding);
// lame wrapper
$lame = new Lame('/usr/bin/lame', $settings);
// convert wav - mp3 using lame
try {
    $lame->encode($filewav, $filemp3);
} catch(\RuntimeException $e) {
    var_dump($e->getMessage());
}
?>

I hope some will find this useful,
Best Regards,
-OLI

2 Likes