FreePBX に届いたメールを音声読み上げさせる その1 ~テキストを WAV ファイルに変換~

0)初めに
OS は CentOS6 で、FreePBX のものです。
以下のサイトを参考にさせていただきました。ありがとうございます。
http://server-setting.info/centos/open-jtalk-install.html

LinuxでFFmpegをほぼ全自動でビルドする(CentOS、Ubuntu等に対応)


http://d.hatena.ne.jp/leetmikeal/20130403/1364959490
http://www.xucker.jpn.org/pc/ffmpeg_wav.html
http://www.ksknet.net/cat28/mimeparser.html
http://d.hatena.ne.jp/ksmemo/20090111/p1
https://hgotoh.jp/wiki/doku.php/documents/mail/mail-005
http://blog.glidenote.com/blog/2012/08/02/incron/

1)テキストファイルを音声ファイルに変換できるようにする
1-1)コンパイル用のライブラリを整える
ライブラリで不足分をインストールすることと、FFmpeg コンパイル用に cmake のバージョンを2.8.8以上にする必要があります。

# yum update
# yum install zlib zlib-devel curl curl-devel bzip2 bzip2- devel libarchive libarchive-devel expat expat-devel qt qt-devel

# wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar. gz
# tar xzvf cmake-2.8.10.2.tar.gz
# cd cmake-2.8.10.2
# ./bootstrap --prefix=/usr/local/cmake-2.8.10. 2 --system-libs
# make && make install
# cd /usr/local
# ln -sfn cmake-2.8.10.2 cmake
# vi /etc/bashrc

export PATH=
で、始まる行を
export PATH=/usr/local/cmake/bin:$PATH
に、
export MANPATH=
で、始まる行を
export MANPATH=/usr/local/cmake/man:$MANPATH
に、書き換えて保存します。
~/.bash_profile のパスも同様に書き換えておきます。
その後、コンパイルするために一度実行してパスを書き換えておきます。

# export PATH=/usr/local/cmake/bin:$PATH
# export MANPATH=/usr/local/cmake/man:$MANPATH

1-2)libiconv.so.2 を認識させる

# vi /etc/ld.so.conf

/usr/local/lib の1行を書き加える

# ldconfig

 1-3)Open JTalk をインストール
各リンクは正しいとは限りません。サイトでリンクの確認を行ってください。

hts-engine : https://sourceforge.net/projects/hts-engine/files/hts_engine%20API/
Open JTalk : https://sourceforge.net/projects/open-jtalk/files/Open%20JTalk/

# cd /usr/local/src
# wget https://sourceforge.net/projects/hts-engine/files/l atest/download?source=files
# mv download?source=files hts_engine_API-1.10.tar.gz
# tar xzvf hts_engine_API-1.10.tar.gz
# cd hts_engine_API-1.10
# ./configure
# make
# make install
# cd ..
# wget https://sourceforge.net/projects/open-jtalk/files/latest/download?source=files
# mv download open_jtalk-1.09.tar.gz
# tar xzvf open_jtalk-1.09.tar.gz
# cd open_jtalk-1.09
# ./configure --with-hts-engine-header-path=/ussr/local/include --with-hts-engine-library-path=/usr/local/lib --with-charset=UTFF-8
# make
# make install
# cd ..
# wget https://sourceforge.net/projects/open-jtalk/files/HTS%20voice/hts_voice_nitech_jp_atr503_m001-1.05/hts_voice_nitech_jp_atr503_m001-1.05.tar.gz/download
# mv download hts_voice_nitech_jp_atr503_m001-1.05.tar.gz
# wget https://sourceforge.net/projects/mmdagent/files/MMDAgent_Example/MMDAgent_Example-1.6/MMDAgent_Example-1.6.zip/download
# mv download MMDAgent_Example-1.6.zip
# unzip -q MMDAgent_Example-1.6.zip
# tar xzvf hts_voice_nitech_jp_atr503_m001-1.05.tar.gz
# mkdir /var/lib/open_jtalk
# chmod a+r /var/lib/open_jtalk
# cp hts_voice_nitech_jp_atr503_m001-1.05/nitech_jp_atr503_m001.htsvoice /var/lib/open_jtalk/
# cp MMDAgent_Example-1.6/Voice/mei/*.htsvoice /var/lib/open_jtalk/
# chmod a+r /var/lib/open_jtalk -Rf

1-4) ffmpeg のインストール

# yum -y install autoconf automake bzip2 cmake f reetype-devel gcc gcc-c++ git wget libtool make mercurial nasm pkgconfig zlib-devel
# vi ffmpeg_build.sh

ファイルの中身は以下の通りです。(http://www.jifu-labo.net/2015/09/ffmpeg_build/ からの転載)

#!/bin/sh
 
cpu_core=2
enable_h264=1
enable_h265=1
enable_vp8=1
enable_aac=1
enable_mp3=1
enable_ogg=1
enable_opus=1
enable_ass=1
 
src_dir="$HOME/ffmpeg_sources"
prefix_dir="/usr/local/ffmpeg_build"
 
export PATH=$prefix_dir/bin:$PATH
export PKG_CONFIG_PATH="$prefix_dir/lib/pkgconfig"
enable_option=""
 
repo_yasm="git://github.com/yasm/yasm.git"
repo_x264="https://git.videolan.org/git/x264.git"
repo_x265="https://github.com/videolan/x265.git"
repo_aac="git://git.code.sf.net/p/opencore-amr/fdk-aac"
repo_opus="git://github.com/xiph/opus.git"
repo_libvpx="https://chromium.googlesource.com/webm/libvpx.git"
repo_libass="https://github.com/libass/libass.git"
repo_ffmpeg="git://source.ffmpeg.org/ffmpeg"
 
url_autoconf="http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz"
url_lame="http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz"
url_ogg="http://downloads.xiph.org/releases/ogg/libogg-1.3.2.tar.gz"
url_theora="http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.bz2"
url_vorbis="http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.4.tar.gz"
 
print_error()
{
    echo "error: $1"
}
 
run_git()
{
    repo="$1"
    opt="$2"
 
    dir=${repo##*/}
    dir=${dir%.git}
 
    if [ -d $dir ]; then
        cd $dir && git pull
        if [ $? -ne 0 ]; then
            print_error "git pull $dir" && exit 1
        fi
    else
        git clone $opt $repo
        if [ $? -ne 0 ]; then
            print_error "git clone $dir" && exit 1
        fi
        cd $dir
    fi
}
 
run_wget()
{
    url="$1"
    file=${url##*/}
    dir=${file%.tar.*}
 
    if [ ! -e $file ]; then
        wget $url
        if [ $? -ne 0 ]; then
            print_error "wget $file" && exit 1
        fi
    fi
 
    case $file in
        *.gz)  tar xvzf $file ;;
        *.bz2) tar xvjf $file ;;
    esac
 
    cd $dir
}
 
uid=<code>id | sed 's/uid=([0-9]+)(.+/1/'</code>
  
if [ $uid -ne 0 ];then
    print_error &quot;not root user&quot;
    exit 1
fi
 
mkdir -p $src_dir
mkdir -p $prefix_dir
 
aconf_ver=<code>LANG=C autoconf -V | head -n 1 | sed -e &quot;s/autoconf (GNU Autoconf) ([0-9]*).([0-9]*)/12/&quot;</code>
if [ $aconf_ver -lt 269 ]; then
    echo &quot;---------- build autoconf ----------&quot;
    run_wget $url_autoconf
    ./configure --prefix=&quot;$prefix_dir&quot; --bindir=&quot;$prefix_dir/bin&quot;
    make
    make install
    make distclean
fi
 
 
echo &quot;---------- build Yasm ----------&quot;
cd $src_dir
run_git $repo_yasm &quot;--depth 1&quot;
autoreconf -fiv
./configure --prefix=&quot;$prefix_dir&quot; --bindir=&quot;$prefix_dir/bin&quot;
make -j${cpu_core}
if [ $? -ne 0 ]; then
    print_error &quot;make yasm&quot; && exit 1
fi
make install
make distclean
 
 
if [ $enable_h264 -eq 1 ]; then
    echo &quot;---------- build libx264  ----------&quot;
    cd $src_dir
    run_git $repo_x264 &quot;&quot;
    ./configure --prefix=&quot;$prefix_dir&quot; --bindir=&quot;$prefix_dir/bin&quot; --enable-static
    make -j${cpu_core}
    if [ $? -ne 0 ]; then
        print_error &quot;make libx264&quot; && exit 1
    fi
    make install
    make distclean
    enable_option=&quot;${enable_option} --enable-libx264&quot;
fi
 
 
if [ $enable_h265 -eq 1 ]; then
    echo &quot;---------- build libx265  ----------&quot;
    cd $src_dir
    run_git $repo_x265 &quot;&quot;
    cd build/linux
    cmake -G &quot;Unix Makefiles&quot; -DCMAKE_INSTALL_PREFIX=&quot;$prefix_dir&quot; -DENABLE_SHARED:bool=off ../../source
    make -j${cpu_core}
    if [ $? -ne 0 ]; then
        print_error &quot;make libx265&quot; && exit 1
    fi
    make install
    make clean
    enable_option=&quot;${enable_option} --enable-libx265&quot;
fi
 
 
if [ $enable_aac -eq 1 ]; then
    echo &quot;---------- build libfdk_aac ----------&quot;
    cd $src_dir
    run_git $repo_aac &quot;--depth 1&quot;
    autoreconf -fiv
    ./configure --prefix=&quot;$prefix_dir&quot; --disable-shared
    make -j${cpu_core}
    if [ $? -ne 0 ]; then
        print_error &quot;make libfdk_aac&quot; && exit 1
    fi
    make install
    make distclean
    enable_option=&quot;${enable_option} --enable-libfdk-aac&quot;
fi
 
 
if [ $enable_mp3 -eq 1 ]; then
    echo &quot;---------- build libmp3lame ----------&quot;
    cd $src_dir
    run_wget $url_lame
    ./configure --prefix=&quot;$prefix_dir&quot; --bindir=&quot;$prefix_dir/bin&quot; --disable-shared --enable-nasm
    make -j${cpu_core}
    if [ $? -ne 0 ]; then
        print_error &quot;make libmp3lame&quot; && exit 1
    fi
    make install
    make distclean
    enable_option=&quot;${enable_option} --enable-libmp3lame&quot;
fi
 
 
if [ $enable_opus -eq 1 ]; then
    echo &quot;---------- build libopus ----------&quot;
    cd $src_dir
    run_git $repo_opus &quot;&quot;
    ./autogen.sh
    ./configure --prefix=&quot;$prefix_dir&quot; --disable-shared
    make -j${cpu_core}
    if [ $? -ne 0 ]; then
        print_error &quot;make libopus&quot; && exit 1
    fi
    make install
    make distclean
    enable_option=&quot;${enable_option} --enable-libopus&quot;
fi
 
 
if [ $enable_ogg -eq 1 ]; then
    echo &quot;---------- build libogg  ----------&quot;
    cd $src_dir
    run_wget $url_ogg
    ./configure --prefix=&quot;$prefix_dir&quot; --disable-shared
    make -j${cpu_core}
    if [ $? -ne 0 ]; then
        print_error &quot;make libogg&quot; && exit 1
    fi
    make install
    make distclean
 
    echo &quot;---------- build libvorbis ----------&quot;
    cd $src_dir
    run_wget $url_vorbis
    LDFLAGS=&quot;-L$prefix_dir/lib&quot; CPPFLAGS=&quot;-I$prefix_dir/include&quot; ./configure --prefix=&quot;$prefix_dir&quot; --with-ogg=&quot;$prefix_dir&quot; --disable-shared
    make -j${cpu_core}
    if [ $? -ne 0 ]; then
        print_error &quot;make libvorbis&quot; && exit 1
    fi
    make install
    make distclean
    enable_option=&quot;${enable_option} --enable-libvorbis&quot;
  
    echo &quot;---------- build libtheora ----------&quot;
    cd $src_dir
    run_wget $url_theora
    ./configure --prefix=&quot;$prefix_dir&quot; --disable-shared
    make -j${cpu_core}
    if [ $? -ne 0 ]; then
    print_error &quot;make libtheora&quot; && exit 1
    fi
    make install
    make clean
    enable_option=&quot;${enable_option} --enable-libtheora&quot;
fi
 
 
if [ $enable_vp8 -eq 1 ]; then
    echo &quot;---------- build libvpx ----------&quot;
    cd $src_dir
    run_git $repo_libvpx &quot;--depth 1&quot;
    ./configure --prefix=&quot;$prefix_dir&quot; --disable-examples
    make -j${cpu_core}
    if [ $? -ne 0 ]; then
        print_error &quot;make libvpx&quot; && exit 1
    fi
    make install
    make clean
    enable_option=&quot;${enable_option} --enable-libvpx&quot;
fi
 
 
if [ $enable_ass -eq 1 ]; then
    echo &quot;---------- build libass ----------&quot;
    cd $src_dir
    run_git $repo_libass &quot;--depth 1&quot;
    autoreconf -fiv
    ./configure --prefix=&quot;$prefix_dir&quot; --disable-shared
    make -j${cpu_core}
    if [ $? -ne 0 ]; then
        print_error &quot;make libass&quot; && exit 1
    fi
    make install
    make clean
    enable_option=&quot;${enable_option} --enable-libass&quot;
fi
 
 
echo &quot;---------- build FFmpeg ----------&quot;
cd $src_dir
run_git $repo_ffmpeg &quot;--depth 1&quot;
  
./configure 
  --prefix=&quot;$prefix_dir&quot; --extra-cflags=&quot;-I$prefix_dir/include&quot; 
  --extra-ldflags=&quot;-L$prefix_dir/lib&quot; 
  --bindir=&quot;$prefix_dir/bin&quot; 
  --pkg-config-flags=&quot;--static&quot; 
  --enable-gpl 
  --enable-nonfree 
  --enable-libfreetype 
  $enable_option
make -j${cpu_core}
if [ $? -ne 0 ]; then
    print_error &quot;make ffmpeg&quot; && exit 1
fi
make install
make distclean
hash -r

実行権限を与えて実行

# chmod u+x ffmpeg_build.sh
# sh ./ffmpeg_build.sh

これで、

ffmpeg -i 入力ファイル名 -ac 1 -ar 8000 出力ファイル名

で、Open_JTalk が出力した 48000Hz のファイルを 8000Hz のモノラルに変更できます。
Open_JTalk で直接 8000Hz の周波数で音声ファイルを出力すると、間延びして聞き取れないものが出来上がるので、FFmpegで変換するわけです。

1-5)テキスト wav 変換スクリプトの作成

# vi /bin/TextSpeach

ファイルの中身は以下の通りです。

#!/bin/bash

cat $1 | tr -d 'r' | tr -d 'n' > $1.tmp
open_jtalk -m /var/lib/open_jtalk/nitech_jp_atr503_m001.htsvoice 
             -x /usr/local/dic 
             -ow $2.tmp 
             $1.tmp
# cp $2 $2.tmp
ffmpeg -i $2.tmp -ac 1 -ar 8000 -y $2
rm -f $2.tmp

実行権の設定

# chmod a+x /bin/TextSpeach

コメントを残す

メールアドレスが公開されることはありません。