how to run espeak from php in linux

2.1k views Asked by At

I am running the espeak from linux in terminal which is working completely fine. My problem is that i want to run the espeak from php(in xampp).

<?php
$a="espeak sometext";
system($a);
?>

but that is not working.and is not producing any sound

where as in windows this thing is working absolutely fine with espeak command line program when called in cmd by php script the code is

<?php
$a='espeak "your text"';
system($a);
?>

can you guys please help me in running this in linux as well. Thanks in advance.

4

There are 4 answers

0
prashant dhirendra On

use shell_exec() function instead of using system in LINUX

<?php
$a='espeak "your text"';
shell_exec($a);
?>
0
Shameerariff On

This espeak cannot be used with the help of the system command as the system command will be executed only in the server not in the client computer. In other words speaking if your server has the audio card it will play the sound in the server. The only option is you got to use the lame the guide is given in the egudiedog.net

0
AudioBubble On

You can try something like this you may change parameters (voice) according your needs and if supported by espeak. This was used for a test with Greek Language. You may also want to check this answer as well.

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  define('ESPEAK', 'C:\path\to\eSpeak\command_line\espeak');
  define('LAME', 'C:\path\to\eSpeak\command_line\lame');
} else {
  define('ESPEAK', '/usr/bin/espeak');
  define('LAME', '/usr/bin/lame');
}
  $text = 'This is a test';
  $voice = "mb-gr2";
  $speed_delta = 0;
  $speed = 145;
  $pitch = 90;
  $volume = 100;
  $filename = date("Y-m-d-H-i-s").'.mp3';     
  $text = escapeshellarg($text);
if (!file_exists($filename)) {
  $cmd = ESPEAK." -v $voice $text -s $speed -p $pitch -a $volume --stdout  | ".LAME." --preset voice -q 9 --vbr-new - $filename";
   exec($cmd);  
   echo $filename;
  } 
2
Axel Kemper On

I am calling eSpeak in a bash script:

#!/bin/bash
#
#  announce  -  script to convert text parameter into spoken sound
#
#  Prerequisites:
#  eSpeak
#  mbrola binary
#  mbrola voice
#  alsa sound
#  libportaudio
#
#  Axel Kemper  21-Feb-2015
#
#==================================================================
#

get_abs_dir() {
  echo "$(cd "$(dirname "$1")" && pwd)"
}

set_volume() {
  #  "amixer scontrols" lists all simple ALSA controls
  #  http://manpages.ubuntu.com/manpages/lucid/man1/amixer.1.html
  amixer set Headphone $1 > /dev/null
  amixer set Headphone $2 > /dev/null
  amixer set PCM $1 > /dev/null
  amixer set PCM $2 > /dev/null
}

text=$*
amplitude=100      #  0 .. 200   default: 100
pitch=50           #  0 .. 99    default: 50
wordsPerMinute=90  #  80 .. 450  default: 175
charFormat=2       #  8-bit character set
charFormat=1       #  UTF-8 character set
soundDevice=sysdefault  #  cf. aplay -L
esHome=$(get_abs_dir $0)
esHome=${esHome%/bin}

voice=mb-de2       #   "de+f2"

export LD_LIBRARY_PATH=$esHome/shared_library

set_volume 100% on
#  espeak-data has to reside in $esHome
espeak --path=$esHome -a $amplitude -p $pitch -b $charFormat -v $voice "$text" --stdout | aplay -D$soundDevice &> /dev/null
set_volume 0% off

The script allows you to adjust eSpeak parameters.

Rather than piping the output of eSpeak to aplay as local alsa sound player on the server, you can pipe it to a local .wav file which you then pass to your web client.