Emacs上でGoogle翻訳の結果を音声合成で読み上げる

はじめに

Emacsから任意のテキスト(日本語/英語)を音声合成することが可能になったので,その応用先のひとつとして提案するものである.

設定

Google翻訳Emacsから実行するために,google translate.elを使用する.
MELPAからもダウンロード可能である.自身の環境に合わせて適切に設定されたい.
また音声合成のために,拙作speech synth.elを使用する.こちらも記事を参考にして適切に設定されたい.

最後に,以下を設定ファイルに追記する.

(when (locate-library "google-translate")
  (eval-after-load "google-translate"
    '(progn
       (defun google-translate-translate (source-language target-language text)
         "Translate TEXT from SOURCE-LANGUAGE to TARGET-LANGUAGE.

In case of `google-translate-output-destination' is nil pops up a
buffer named *Google Translate* with available translations of
TEXT. In case of `google-translate-output-destination' is
`echo-area' outputs translation in the echo area. If
`google-translate-output-destination' is `popup' outputs
translation in the popup tooltip using `popup' package.

To deal with multi-line regions, sequences of white space
are replaced with a single space. If the region contains not text, a
message is printed."
         (let* ((json (google-translate-request source-language
                                                target-language
                                                text)))
           (if (null json)
               (message "Nothing to translate.")
             (let* ((detailed-translation
                     (google-translate-json-detailed-translation json))
                    (gtos
                     (make-gtos
                      :source-language source-language
                      :target-language target-language
                      :auto-detected-language (aref json 2)
                      :text text
                      :text-phonetic (google-translate-json-text-phonetic json)
                      :translation (google-translate-json-translation json)
                      :translation-phonetic (google-translate-json-translation-phonetic json)
                      :detailed-translation detailed-translation
                      :suggestion (when (null detailed-translation)
                                    (google-translate-json-suggestion json)))))
               (speech-synth-execute-synthesis (gtos-translation gtos))
               (cond
                ((null google-translate-output-destination)
                 (google-translate-buffer-output-translation gtos))
                ((equal google-translate-output-destination 'echo-area)
                 (google-translate-echo-area-output-translation gtos))
                ((equal google-translate-output-destination 'popup)
                 (google-translate-popup-output-translation gtos)))))))
       )))

上記は関数の再定義である.
speech-synth-execute-synthesisを挿入しているだけである.

使い方

翻訳したい文章をリージョンで囲って,M-x google-translate-at-point とすれば,翻訳された文章が*Google Translate*バッファに表示された後,合成音声が再生される.
筆者の環境では,とりあえず英語から日本語への翻訳で動作を確認している.

問題点

音声合成処理および再生処理自体は非同期なので問題はないが,合成したい文章が長いときには合成処理に時間がかかるので,音声再生までのラグが発生することは避けられない.
よって翻訳結果の文章が長い場合には,相応に待たされることになる.
この問題への対処は今後の課題である.

おわりに

今回,Google翻訳の結果を音声合成する方法について提案した.
音声合成の応用先について可能性を模索中である.

追記

この記事 http://tam5917.hatenablog.com/entry/2013/10/27/084403 で紹介している「ゆっくりボイス」を用い,上記のspeech-synthe-execute-synthesisをsoftalk-textに置き換えればより高速に(ほぼ即時)合成することは可能である.