From 7810b8bdf0352f5886c3af06c4e2cd7b01eeae99 Mon Sep 17 00:00:00 2001 From: rikky Date: Sat, 24 Oct 2009 19:12:02 +0000 Subject: [PATCH] Add:speech/espeak:Added new speech plugin for tts output by espeak. This first version only supports win32. git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@2684 ffa7fe5e-494d-0410-b361-a75ebd5db220 --- navit/configure.in | 13 +- navit/navit/attr_def.h | 1 + navit/navit/navigation.c | 4 +- navit/navit/speech/Makefile.am | 3 + navit/navit/speech/espeak/Makefile.am | 6 + navit/navit/speech/espeak/speak.c | 475 ++++++++++++++++++++++++++++++++ navit/navit/support/Makefile.am | 3 + navit/navit/support/espeak/Makefile.am | 15 +- navit/navit/support/espeak/speak_init.c | 6 + 9 files changed, 518 insertions(+), 8 deletions(-) create mode 100644 navit/navit/speech/espeak/Makefile.am create mode 100644 navit/navit/speech/espeak/speak.c create mode 100644 navit/navit/support/espeak/speak_init.c diff --git a/navit/configure.in b/navit/configure.in index 36d33eb..e198828 100644 --- a/navit/configure.in +++ b/navit/configure.in @@ -50,6 +50,7 @@ graphics_sdl=yes; graphics_sdl_reason=default graphics_win32=no; graphics_win32_reason=default speech_android=no; speech_android_reason=default speech_cmdline=yes; speech_cmdline_reason=default +speech_espeak=no; speech_espeak_reason=default speech_speech_dispatcher=yes; speech_speech_dispatcher_reason=default vehicle_demo=yes; vehicle_demo_reason=default vehicle_file=yes; vehicle_file_reason=default @@ -65,7 +66,7 @@ AC_CANONICAL_HOST win32=no win32ce=no case $host_os in -wince) +wince|mingw32ce|cegcc) win32=yes win32ce=yes AC_DEFINE(HAVE_API_WIN32_BASE, 1, [Have Windows Base API]) @@ -73,6 +74,7 @@ wince) gui_win32=yes; gui_win32_reason="host_os is wince" graphics_win32=yes; graphics_win32_reason="host_os is wince" vehicle_wince=yes; vehcile_wince_reason="host_os is wince" + speech_espeak=yes; speech_espeak_reason="host_os is wince" support_libpng=yes ;; mingw32) @@ -81,7 +83,9 @@ mingw32) AC_DEFINE(HAVE_API_WIN32, 1, [Have Windows API]) gui_win32=yes; gui_win32_reason="host_os is mingw32" graphics_win32=yes; graphics_win32_reason="host_os is mingw32" + speech_espeak=yes; speech_espeak_reason="host_os is mingw32" support_libpng=yes + LIBS="$LIBS -lwinmm" ;; linux*_android) android=yes @@ -723,7 +727,10 @@ AM_CONDITIONAL(SPEECH_ANDROID, test "x${speech_android}" = "xyes") # cmdline AC_ARG_ENABLE(speech-cmdline, [ --disable-speech-cmdline disable speech type cmdline], speech_cmdline=$enableval;speech_cmdline_reason="configure parameter") AM_CONDITIONAL(SPEECH_CMDLINE, test "x${speech_cmdline}" = "xyes") -# cmdline +# espeak +AC_ARG_ENABLE(speech-espeak, [ --disable-speech-espeak disable speech type espeak], speech_espeak=$enableval;speech_espeak_reason="configure parameter") +AM_CONDITIONAL(SPEECH_ESPEAK, test "x${speech_espeak}" = "xyes") +# speech-dispatcher AC_ARG_ENABLE(speech-speech-dispatcher, [ --disable-speech-speech-dispatcher disable speech type speech-dispatcher], speech_speech_dispatcher=$enableval;speech_speech_dispatcher_reason="configure parameter") if test "x$speech_speech_dispatcher" = "xyes"; then AC_CHECK_HEADER(libspeechd.h, AC_DEFINE([HAVE_LIBSPEECHD],[],Define to 1 if you have the header file.) SPEECHD_LIBS="-lspeechd", speech_speech_dispatcher=no; speech_speech_dispatcher_reason="libspeechd.h missing") @@ -820,6 +827,7 @@ navit/osd/core/Makefile navit/speech/Makefile navit/speech/android/Makefile navit/speech/cmdline/Makefile +navit/speech/espeak/Makefile navit/speech/speech_dispatcher/Makefile navit/support/Makefile navit/support/espeak/Makefile @@ -925,6 +933,7 @@ echo " python: $binding_python ($binding_python_reason)" echo "Speech types:" echo " android: $speech_android ($speech_android_reason)" echo " cmdline: $speech_cmdline ($speech_cmdline_reason)" +echo " espeak: $speech_espeak ($speech_espeak_reason)" echo " speech_dispatcher: $speech_speech_dispatcher ($speech_speech_dispatcher_reason)" echo "Vehicle types:" diff --git a/navit/navit/attr_def.h b/navit/navit/attr_def.h index 9f0aea3..2c3bcb7 100644 --- a/navit/navit/attr_def.h +++ b/navit/navit/attr_def.h @@ -256,6 +256,7 @@ ATTR(house_number_right_last) ATTR(town_name_match) ATTR(district_name_match) ATTR(street_name_match) +ATTR(language) ATTR2(0x0003ffff,type_string_end) ATTR2(0x00040000,type_special_begin) ATTR(order) diff --git a/navit/navit/navigation.c b/navit/navit/navigation.c index ce01152..398f300 100644 --- a/navit/navit/navigation.c +++ b/navit/navit/navigation.c @@ -1520,10 +1520,10 @@ show_maneuver(struct navigation *nav, struct navigation_itm *itm, struct navigat if (level != -2) { /* TRANSLATORS: The first argument is strength, the second direction, the third distance and the fourth destination Example: 'Turn 'slightly' 'left' in '100 m' 'onto baker street' */ - ret=g_strdup_printf(_("Turn %1$s%2$s %3$s%4$s"), strength, dir, d, destination ? destination:""); + ret=g_strdup_printf(_("Turn %s%s %s%s"), strength, dir, d, destination ? destination:""); } else { /* TRANSLATORS: First argument is strength, second direction, third how many roads to skip, fourth destination */ - ret=g_strdup_printf(_("then turn %1$s%2$s %3$s%4$s"), strength, dir, d, destination ? destination:""); + ret=g_strdup_printf(_("then turn %s%s %s%s"), strength, dir, d, destination ? destination:""); } g_free(destination); } else { diff --git a/navit/navit/speech/Makefile.am b/navit/navit/speech/Makefile.am index 1d40c8e..ddab19c 100644 --- a/navit/navit/speech/Makefile.am +++ b/navit/navit/speech/Makefile.am @@ -5,6 +5,9 @@ endif if SPEECH_CMDLINE SUBDIRS += cmdline endif +if SPEECH_ESPEAK + SUBDIRS += espeak +endif if SPEECH_SPEECH_DISPATCHER SUBDIRS += speech_dispatcher endif diff --git a/navit/navit/speech/espeak/Makefile.am b/navit/navit/speech/espeak/Makefile.am new file mode 100644 index 0000000..aef0ec8 --- /dev/null +++ b/navit/navit/speech/espeak/Makefile.am @@ -0,0 +1,6 @@ +include $(top_srcdir)/Makefile.inc +AM_CPPFLAGS = @NAVIT_CFLAGS@ -I$(top_srcdir)/navit -DMODULE=speech_espeak +modulespeech_LTLIBRARIES = libspeech_espeak.la +libspeech_espeak_la_SOURCES = speak.c +libspeech_espeak_la_LDFLAGS = -module -avoid-version @MODULE_LDFLAGS@ + diff --git a/navit/navit/speech/espeak/speak.c b/navit/navit/speech/espeak/speak.c new file mode 100644 index 0000000..6a8d10c --- /dev/null +++ b/navit/navit/speech/espeak/speak.c @@ -0,0 +1,475 @@ +/** + * Navit, a modular navigation system. + * Copyright (C) 2005-2008 Navit Team + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#define _WIN32_WINNT 0x0500 + +#include "config.h" + +#ifdef HAVE_API_WIN32_BASE +#include +#include +#include +#else +#include +#endif + +#include +#include +#include "item.h" +#include "plugin.h" +#include "speech.h" +#include "debug.h" + +#include "support/espeak/speech.h" +#include "support/espeak/speak_lib.h" +#include "support/espeak/phoneme.h" +#include "support/espeak/synthesize.h" +#include "support/espeak/voice.h" +#include "support/espeak/translate.h" + + +#define SAMPLES_PER_BUFFER 1024 +#define BUFFERS 8 + + +// ----- some stuff needed by espeak ---------------------------------- +char path_home[N_PATH_HOME]; // this is the espeak-data directory +int (* uri_callback)(int, const char *, const char *) = NULL; +int (* phoneme_callback)(const char *) = NULL; +FILE *f_wave = NULL; + +int GetFileLength(const char *filename) +{ + struct stat statbuf; + + if(stat(filename,&statbuf) != 0) + return(0); + + if((statbuf.st_mode & S_IFMT) == S_IFDIR) + return(-2); // a directory + + return(statbuf.st_size); +} + +void MarkerEvent(int type, unsigned int char_position, int value, unsigned char *out_ptr) +{ +} + +char *Alloc(int size) +{ + return g_malloc(size); +} + +void Free(void *ptr) +{ + g_free(ptr); +} + +// -------------------------------------------------------------------- + + +enum speech_messages +{ + msg_say = WM_USER, + msg_exit +}; + +enum speech_state +{ + state_available, + state_speaking_phase_1, + state_speaking_phase_2, + state_speaking_phase_3 + +}; + +struct speech_priv { + GList *free_buffers; + HWAVEOUT h_wave_out; + enum speech_state state; + GList *phrases; + HWND h_queue; + HANDLE h_message_thread; +}; + + +static void waveout_close(struct speech_priv* sp_priv) +{ + waveOutClose(sp_priv->h_wave_out); +} + +static BOOL waveout_open(struct speech_priv* sp_priv) +{ + MMRESULT result = 0; + + HWAVEOUT hwo; + static WAVEFORMATEX wmTemp; + wmTemp.wFormatTag = WAVE_FORMAT_PCM; + wmTemp.nChannels = 1; + wmTemp.nSamplesPerSec = 22050; + wmTemp.wBitsPerSample = 16; + wmTemp.nBlockAlign = wmTemp.nChannels * wmTemp.wBitsPerSample / 8; + wmTemp.nAvgBytesPerSec = wmTemp.nSamplesPerSec * wmTemp.nBlockAlign; + wmTemp.cbSize = 0; + result = waveOutOpen(&hwo, (UINT) WAVE_MAPPER, &wmTemp, (DWORD)sp_priv->h_queue, (DWORD)sp_priv, CALLBACK_WINDOW); + sp_priv->h_wave_out = hwo; + + return (result==MMSYSERR_NOERROR); +} + +static int wave_out(struct speech_priv* sp_priv) +{ + unsigned char wav_outbuf[SAMPLES_PER_BUFFER * 2]; + int isDone; + + WAVEHDR *WaveHeader = g_list_first(sp_priv->free_buffers)->data; + sp_priv->free_buffers = g_list_remove(sp_priv->free_buffers, WaveHeader); + + out_ptr = out_start = wav_outbuf; + out_end = wav_outbuf + sizeof(wav_outbuf); + + isDone = WavegenFill(0); + + if ( out_ptr < out_end ) + { + memset ( out_ptr, 0, out_end - out_ptr ); + } + memcpy(WaveHeader->lpData, wav_outbuf, WaveHeader->dwBufferLength); + waveOutWrite(sp_priv->h_wave_out, WaveHeader, sizeof(WAVEHDR)); + + return isDone; +} + +static BOOL initialise(void) +{ + int param; + int result; + + WavegenInit(22050,0); // 22050 + if((result = LoadPhData()) != 1) + { + if(result == -1) + { + dbg(0, "Failed to load espeak-data\n"); + return FALSE; + } + else + dbg(0, "Wrong version of espeak-data 0x%x (expects 0x%x) at %s\n",result,version_phdata,path_home); + } + LoadConfig(); + SetVoiceStack(NULL); + SynthesizeInit(); + + for(param=0; paramfree_buffers && this->state != state_speaking_phase_3 ) + { + if(Generate(phoneme_list,&n_phoneme_list,1)==0) + { + if (!SpeakNextClause(NULL,NULL,1)) + { + this->state = state_speaking_phase_2; + } + } + + if ( wave_out(this)!= 0 && this->state == state_speaking_phase_2) + { + this->state = state_speaking_phase_3; + } + } +} + +static void start_speaking(struct speech_priv* sp_priv) +{ + char *phrase = g_list_first(sp_priv->phrases)->data; + + sp_priv->state = state_speaking_phase_1; + + SpeakNextClause(NULL, phrase,0); + wave_out(sp_priv); + fill_buffer(sp_priv); +} + +static LRESULT CALLBACK speech_message_handler( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) +{ + dbg(1, "message_handler called\n"); + + switch (uMsg) + { + case msg_say: + { + struct speech_priv* sp_priv = (struct speech_priv*)wParam; + sp_priv->phrases = g_list_append(sp_priv->phrases, (char*)lParam); + + if ( sp_priv->state == state_available ) + { + start_speaking(sp_priv); + } + + } + break; + case MM_WOM_DONE: + { + dbg(2, "Wave buffer done\n"); + WAVEHDR *WaveHeader = (WAVEHDR *)lParam; + + struct speech_priv* sp_priv = (struct speech_priv*)WaveHeader->dwUser; + sp_priv->free_buffers = g_list_append(sp_priv->free_buffers, WaveHeader); + + if ( sp_priv->state != state_speaking_phase_3) + { + fill_buffer(sp_priv); + } + else if ( g_list_length(sp_priv->free_buffers) == BUFFERS && sp_priv->state == state_speaking_phase_3 ) + { + // remove the spoken phrase from the list + char *phrase = g_list_first(sp_priv->phrases)->data; + g_free( phrase ); + sp_priv->phrases = g_list_remove(sp_priv->phrases, phrase); + + if ( sp_priv->phrases ) + { + start_speaking(sp_priv); + } + else + { + sp_priv->state = state_available; + } + } + } + break; + case msg_exit: + ExitThread(0); + break; + + default: + break; + + } + + return TRUE; +} + +static void speech_message_dispatcher( struct speech_priv * sp_priv) +{ + BOOL bRet; + MSG msg; + + while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0) + { + if (bRet == -1) + { + dbg(0, "Error getting message from queue\n"); + break; + } + else + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } +} + +static void create_buffers(struct speech_priv *sp_priv) +{ + int buffer_counter; + for (buffer_counter = 0; buffer_counter < BUFFERS; buffer_counter++) + { + WAVEHDR *WaveHeader = g_new0(WAVEHDR, 1); + + WaveHeader->dwBufferLength = SAMPLES_PER_BUFFER * 2; + WaveHeader->lpData = (char *)VirtualAlloc(0, WaveHeader->dwBufferLength, MEM_COMMIT, PAGE_READWRITE); + WaveHeader->dwUser = (DWORD)sp_priv; + waveOutPrepareHeader(sp_priv->h_wave_out, WaveHeader, sizeof(WAVEHDR)); + + sp_priv->free_buffers = g_list_append( sp_priv->free_buffers, WaveHeader ); + } +} + +static DWORD startThread( LPVOID sp_priv) +{ + struct speech_priv *this = (struct speech_priv *) sp_priv; + // Create message queue + TCHAR *g_szClassName = TEXT("SpeechQueue"); + WNDCLASS wc; + HWND hwnd; + + + memset(&wc, 0 , sizeof(WNDCLASS)); + wc.lpfnWndProc = speech_message_handler; + wc.hInstance = GetModuleHandle(NULL); + wc.lpszClassName = g_szClassName; + + if (!RegisterClass(&wc)) + { + dbg(0, "Window registration for message queue failed\n"); + return 1; + } + + HWND hWndParent = NULL; +#ifndef HAVE_API_WIN32_CE + hWndParent = HWND_MESSAGE; +#endif + + // create a message only window + hwnd = CreateWindow( + g_szClassName, + TEXT("Navit"), + 0, + 0, + 0, + 0, + 0, + hWndParent, + NULL, + GetModuleHandle(NULL), + NULL); + + if (hwnd == NULL) + { + dbg(0, "Window creation failed: %d\n", GetLastError()); + return 1; + } + + this->h_queue = hwnd; + this->phrases = NULL; + this->state = state_available; + + if(!waveout_open(this)) + { + dbg(0, "Can't open wave output\n"); + return 1; + } + + this->free_buffers = NULL; + create_buffers(this); + + speech_message_dispatcher(this); + + return 0; +} + +static int +espeak_say(struct speech_priv *this, const char *text) +{ + dbg(1, "Speak: '%s'\n", text); + char *phrase = g_strdup(text); + + if (!PostMessage(this->h_queue, msg_say, (WPARAM)this, (LPARAM)phrase)) + { + dbg(0, "PostThreadMessage 'say' failed\n"); + } + + return 0; +} + +static void free_list(gpointer pointer, gpointer this ) +{ + if ( this ) + { + struct speech_priv *sp_priv = (struct speech_priv *)this; + WAVEHDR *WaveHeader = (WAVEHDR *)pointer; + + waveOutUnprepareHeader(sp_priv->h_wave_out, WaveHeader, sizeof(WAVEHDR)); + VirtualFree(WaveHeader->lpData, WaveHeader->dwBufferLength, MEM_DECOMMIT); + } + g_free(pointer); +} + +static void +espeak_destroy(struct speech_priv *this) +{ + g_list_foreach( this->free_buffers, free_list, (gpointer)this ); + g_list_free( this->free_buffers ); + + g_list_foreach( this->phrases, free_list, 0 ); + g_list_free(this->phrases); + + waveout_close(this); + g_free(this); +} + +static struct speech_methods espeak_meth = { + espeak_destroy, + espeak_say, +}; + +static struct speech_priv * +espeak_new(struct speech_methods *meth, struct attr **attrs) { + struct speech_priv *this = NULL; + struct attr *path; + struct attr *language; + char *lang_str = "default"; + + path=attr_search(attrs, NULL, attr_path); + if (! path) + { + dbg(0, "Missing path to espeak data\n"); + return NULL; + } + strcpy(path_home,path->u.str); + + if ( !initialise() ) + { + return NULL; + } + + language=attr_search(attrs, NULL, attr_language); + if ( language ) + { + lang_str = language->u.str; + } + + if(SetVoiceByName(lang_str) != EE_OK) + { + dbg(0, "Error setting language to: '%s'\n", lang_str); + } + + SetParameter(espeakRATE,170,0); + SetParameter(espeakVOLUME,100,0); + SetParameter(espeakCAPITALS,option_capitals,0); + SetParameter(espeakPUNCTUATION,option_punctuation,0); + SetParameter(espeakWORDGAP,0,0); + +// if(pitch_adjustment != 50) +// { +// SetParameter(espeakPITCH,pitch_adjustment,0); +// } + DoVoiceChange(voice); + + this=g_new(struct speech_priv,1); + this->h_message_thread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)startThread, (PVOID)this, 0, NULL); + + *meth=espeak_meth; + + + return this; +} + +void +plugin_init(void) +{ + plugin_register_speech_type("espeak", espeak_new); +} diff --git a/navit/navit/support/Makefile.am b/navit/navit/support/Makefile.am index 455cb6c..412f520 100644 --- a/navit/navit/support/Makefile.am +++ b/navit/navit/support/Makefile.am @@ -23,4 +23,7 @@ endif if SUPPORT_LIBPNG SUBDIRS+=libpng endif +if SPEECH_ESPEAK + SUBDIRS+=espeak +endif DIST_SUBDIRS=espeak ezxml glib wordexp win32 zlib libc libpng diff --git a/navit/navit/support/espeak/Makefile.am b/navit/navit/support/espeak/Makefile.am index b3ab3e8..1b25bd8 100755 --- a/navit/navit/support/espeak/Makefile.am +++ b/navit/navit/support/espeak/Makefile.am @@ -1,6 +1,13 @@ -bin_PROGRAMS = speak - -speak_SOURCES = speak.c compiledict.c dictionary.c intonation.c \ +include $(top_srcdir)/Makefile.inc +AM_CPPFLAGS = @NAVIT_CFLAGS@ -DMODULE=support_espeak +noinst_LTLIBRARIES = libsupport_espeak.la +libsupport_espeak_la_SOURCES = compiledict.c dictionary.c intonation.c \ readclause.c setlengths.c numbers.c synth_mbrola.c \ synthdata.c synthesize.c translate.c \ - tr_languages.c voices.c wavegen.c phonemelist.c klatt.c + tr_languages.c voices.c wavegen.c phonemelist.c klatt.c speak_init.c +libsupport_espeak_la_LDFLAGS = -module -avoid-version @MODULE_LDFLAGS@ + +#bin_PROGRAMS = speak + +#speak_SOURCES = speak.c $(libsupport_espeak_la_SOURCES) + diff --git a/navit/navit/support/espeak/speak_init.c b/navit/navit/support/espeak/speak_init.c new file mode 100644 index 0000000..06a3423 --- /dev/null +++ b/navit/navit/support/espeak/speak_init.c @@ -0,0 +1,6 @@ +#include "../../plugin.h" + +void +plugin_init(void) +{ +} -- 2.7.4