Skip to content

Commit

Permalink
Minor code cleanups and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
zaf committed Aug 19, 2012
1 parent bf0c07f commit b8de132
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 60 deletions.
32 changes: 18 additions & 14 deletions ChangeLog
@@ -1,36 +1,40 @@
2011-11-11 Lefteris Zafiris <zaf.000@gmail.com> -2.1 2012-08-19 Lefteris Zafiris <zaf.000@gmail.com> - 2.2-rc1
Fixed compatibility with asterisk 11
Minor code cleanups and fixes.

2011-11-11 Lefteris Zafiris <zaf.000@gmail.com> - 2.1
Released stable version 2.1 Released stable version 2.1


2011-21-10 Lefteris Zafiris <zaf.000@gmail.com> - 2.1rc4 2011-10-21 Lefteris Zafiris <zaf.000@gmail.com> - 2.1-rc4
Changed config file handling. Config values are Changed config file handling. Config values are
read once during module loading. Added option for read once during module loading. Added option for
module reload that re-reads the config values. module reload that re-reads the config values.
Makefile updates. Makefile updates.


2011-03-09 Lefteris Zafiris <zaf.000@gmail.com> - 2.1rc3 2011-09-03 Lefteris Zafiris <zaf.000@gmail.com> - 2.1-rc3
Changed output file format to raw signed lineral. This Changed output file format to raw signed lineral. This
adds support for 16kHz sound playback in asterisk 1.6. adds support for 16kHz sound playback in asterisk 1.6.
Added checks for text input length. Added checks for text input length.

2011-03-09 Lefteris Zafiris <zaf.000@gmail.com> - 2.1rc2 2011-09-03 Lefteris Zafiris <zaf.000@gmail.com> - 2.1-rc2
Better error handling. Better error handling.
Using mkstemps() for safer temp file name generation, Using mkstemps() for safer temp file name generation,
instead of ast_random(). instead of ast_random().
Using ast_malloc() instead of malloc(). Using ast_malloc() instead of malloc().
Increased internal espeak synth buffer to 2sec. Increased internal espeak synth buffer to 2sec.


2011-27-08 Lefteris Zafiris <zaf.000@gmail.com> - 2.1rc1 2011-08-27 Lefteris Zafiris <zaf.000@gmail.com> - 2.1-rc1
Switched to libsamplerate for resampling. Resampling Switched to libsamplerate for resampling. Resampling
now is much faster. now is much faster.
Major code cleanup. Major code cleanup.


2011-19-08 Lefteris Zafiris <zaf.000@gmail.com> - 2.0 2011-08-19 Lefteris Zafiris <zaf.000@gmail.com> - 2.0
Added option for generating 16000Hz sound files. Added option for generating 16000Hz sound files.


2009-06-11 Lefteris Zafiris <zaf.000@gmail.com> - 1.6.0.1 2009-11-06 Lefteris Zafiris <zaf.000@gmail.com> - 1.6.0.1
Released as stable. Released as stable.
Fixed a bug in argument parsing that could Fixed a bug in argument parsing that could
cause asterisk to crash. cause asterisk to crash.

2009-02-09 Lefteris Zafiris <zaf.000@gmail.com> - 1.6-0.1-beta 2009-09-02 Lefteris Zafiris <zaf.000@gmail.com> - 1.6-0.1-beta
Initial release 1.6-0.1-beta Initial release 1.6-0.1-beta
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -22,7 +22,7 @@ OPTIMIZE=-O2
DEBUG=-g DEBUG=-g


LIBS+=-lespeak -lsndfile -lsamplerate LIBS+=-lespeak -lsndfile -lsamplerate
CFLAGS+=-pipe -fPIC -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -D_REENTRANT -D_GNU_SOURCE CFLAGS+=-pipe -fPIC -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -D_REENTRANT -D_GNU_SOURCE


all: _all all: _all
@echo " +-------- app_espeak Build Complete --------+" @echo " +-------- app_espeak Build Complete --------+"
Expand Down
79 changes: 34 additions & 45 deletions app_espeak.c
Expand Up @@ -35,6 +35,7 @@


ASTERISK_FILE_VERSION(__FILE__, "$Revision: 00 $") ASTERISK_FILE_VERSION(__FILE__, "$Revision: 00 $")
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <espeak/speak_lib.h> #include <espeak/speak_lib.h>
#include <sndfile.h> #include <sndfile.h>
#include <samplerate.h> #include <samplerate.h>
Expand Down Expand Up @@ -83,7 +84,7 @@ static int pitch;
static int capind; static int capind;
static const char *def_voice; static const char *def_voice;


static int read_config(void) static int read_config(const char *espeak_conf)
{ {
const char *temp; const char *temp;
/* Setting defaut config values */ /* Setting defaut config values */
Expand All @@ -97,37 +98,28 @@ static int read_config(void)
capind = DEF_CAPIND; capind = DEF_CAPIND;
def_voice = DEF_VOICE; def_voice = DEF_VOICE;


cfg = ast_config_load(ESPEAK_CONFIG, config_flags); cfg = ast_config_load(espeak_conf, config_flags);


if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
ast_log(LOG_WARNING, ast_log(LOG_WARNING,
"eSpeak: Unable to read confing file %s. Using default settings\n", "eSpeak: Unable to read confing file %s. Using default settings\n", espeak_conf);
ESPEAK_CONFIG);
} else { } else {
if ((temp = ast_variable_retrieve(cfg, "general", "usecache"))) if ((temp = ast_variable_retrieve(cfg, "general", "usecache")))
usecache = ast_true(temp); usecache = ast_true(temp);

if ((temp = ast_variable_retrieve(cfg, "general", "cachedir"))) if ((temp = ast_variable_retrieve(cfg, "general", "cachedir")))
cachedir = temp; cachedir = temp;

if ((temp = ast_variable_retrieve(cfg, "general", "samplerate"))) if ((temp = ast_variable_retrieve(cfg, "general", "samplerate")))
target_sample_rate = atoi(temp); target_sample_rate = (int) strtol(temp, NULL, 10);

if ((temp = ast_variable_retrieve(cfg, "voice", "speed"))) if ((temp = ast_variable_retrieve(cfg, "voice", "speed")))
speed = atoi(temp); speed = (int) strtol(temp, NULL, 10);

if ((temp = ast_variable_retrieve(cfg, "voice", "wordgap"))) if ((temp = ast_variable_retrieve(cfg, "voice", "wordgap")))
wordgap = atoi(temp); wordgap = (int) strtol(temp, NULL, 10);

if ((temp = ast_variable_retrieve(cfg, "voice", "volume"))) if ((temp = ast_variable_retrieve(cfg, "voice", "volume")))
volume = atoi(temp); volume = (int) strtol(temp, NULL, 10);

if ((temp = ast_variable_retrieve(cfg, "voice", "pitch"))) if ((temp = ast_variable_retrieve(cfg, "voice", "pitch")))
pitch = atoi(temp); pitch = (int) strtol(temp, NULL, 10);

if ((temp = ast_variable_retrieve(cfg, "voice", "capind"))) if ((temp = ast_variable_retrieve(cfg, "voice", "capind")))
capind = atoi(temp); capind = (int) strtol(temp, NULL, 10);

if ((temp = ast_variable_retrieve(cfg, "voice", "voice"))) if ((temp = ast_variable_retrieve(cfg, "voice", "voice")))
def_voice = temp; def_voice = temp;
} }
Expand Down Expand Up @@ -165,8 +157,8 @@ static int espeak_exec(struct ast_channel *chan, const char *data)
float *src_buff, *dst_buff; float *src_buff, *dst_buff;
char *mydata; char *mydata;
int writecache = 0; int writecache = 0;
char MD5_name[33] = ""; char MD5_name[33];
char cachefile[MAXLEN] = ""; char cachefile[MAXLEN];
char raw_name[17] = "/tmp/espk_XXXXXX"; char raw_name[17] = "/tmp/espk_XXXXXX";
char slin_name[23]; char slin_name[23];
int sample_rate; int sample_rate;
Expand All @@ -181,7 +173,6 @@ static int espeak_exec(struct ast_channel *chan, const char *data)
ast_log(LOG_ERROR, "eSpeak requires arguments (text and options)\n"); ast_log(LOG_ERROR, "eSpeak requires arguments (text and options)\n");
return -1; return -1;
} }

mydata = ast_strdupa(data); mydata = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, mydata); AST_STANDARD_APP_ARGS(args, mydata);


Expand All @@ -195,7 +186,6 @@ static int espeak_exec(struct ast_channel *chan, const char *data)
} }


args.text = ast_strip_quoted(args.text, "\"", "\""); args.text = ast_strip_quoted(args.text, "\"", "\"");

if (ast_strlen_zero(args.text)) { if (ast_strlen_zero(args.text)) {
ast_log(LOG_WARNING, "eSpeak: No text passed for synthesis.\n"); ast_log(LOG_WARNING, "eSpeak: No text passed for synthesis.\n");
return res; return res;
Expand Down Expand Up @@ -232,9 +222,7 @@ static int espeak_exec(struct ast_channel *chan, const char *data)
} }


/* Invoke eSpeak */ /* Invoke eSpeak */
sample_rate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, ESPK_BUFFER, NULL, 0); if ((sample_rate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, ESPK_BUFFER, NULL, 0)) == -1) {

if (sample_rate == -1) {
ast_log(LOG_ERROR, "eSpeak: Internal espeak error, aborting.\n"); ast_log(LOG_ERROR, "eSpeak: Internal espeak error, aborting.\n");
return -1; return -1;
} }
Expand All @@ -246,22 +234,17 @@ static int espeak_exec(struct ast_channel *chan, const char *data)
espeak_SetParameter(espeakPITCH, pitch, 0); espeak_SetParameter(espeakPITCH, pitch, 0);
espeak_SetParameter(espeakCAPITALS, capind, 0); espeak_SetParameter(espeakCAPITALS, capind, 0);


raw_fd = mkstemp(raw_name); if ((raw_fd = mkstemp(raw_name)) == -1) {

if (raw_fd == -1) {
ast_log(LOG_ERROR, "eSpeak: Failed to create audio file.\n"); ast_log(LOG_ERROR, "eSpeak: Failed to create audio file.\n");
return -1; return -1;
} }

if ((fl = fdopen(raw_fd, "w+")) == NULL) {
fl = fdopen(raw_fd, "w+");
if (fl == NULL) {
ast_log(LOG_ERROR, "eSpeak: Failed to open audio file '%s'\n", raw_name); ast_log(LOG_ERROR, "eSpeak: Failed to open audio file '%s'\n", raw_name);
return -1; return -1;
} }


espk_error = espk_error = espeak_Synth(args.text, strlen(args.text), 0, POS_CHARACTER,
espeak_Synth(args.text, strlen(args.text), 0, POS_CHARACTER, (int) strlen(args.text), espeakCHARS_AUTO, NULL, fl);
(int) strlen(args.text), espeakCHARS_AUTO, NULL, fl);
espeak_Terminate(); espeak_Terminate();
fclose(fl); fclose(fl);
if (espk_error != EE_OK) { if (espk_error != EE_OK) {
Expand All @@ -277,18 +260,24 @@ static int espeak_exec(struct ast_channel *chan, const char *data)
src_info.samplerate = (int) sample_rate; src_info.samplerate = (int) sample_rate;
src_info.channels = 1; src_info.channels = 1;
src_info.format = RAW_PCM_S16LE; src_info.format = RAW_PCM_S16LE;
src_file = sf_open(raw_name, SFM_RDWR, &src_info); if ((src_file = sf_open(raw_name, SFM_RDWR, &src_info)) == NULL) {
if (src_file == NULL) {
ast_log(LOG_ERROR, "eSpeak: Failed to read raw audio data '%s'\n", raw_name); ast_log(LOG_ERROR, "eSpeak: Failed to read raw audio data '%s'\n", raw_name);
ast_filedelete(raw_name, NULL); ast_filedelete(raw_name, NULL);
return -1; return -1;
} }
src_buff = (float *) ast_malloc(src_info.frames * sizeof(float)); if ((src_buff = (float *) ast_malloc(src_info.frames * sizeof(float))) == NULL) {
ast_log(LOG_ERROR, "eSpeak: Failed to allocate memory for resampling.\n");
sf_close(src_file);
return -1;
}
sf_readf_float(src_file, src_buff, src_info.frames); sf_readf_float(src_file, src_buff, src_info.frames);
dst_frames = dst_frames = src_info.frames * (sf_count_t) target_sample_rate / (sf_count_t) sample_rate;
src_info.frames * (sf_count_t) target_sample_rate / (sf_count_t) sample_rate; if ((dst_buff = (float *) ast_malloc(dst_frames * sizeof(float))) == NULL) {
dst_buff = (float *) ast_malloc(dst_frames * sizeof(float)); ast_log(LOG_ERROR, "eSpeak: Failed to allocate memory for resampling.\n");

sf_close(src_file);
ast_free(src_buff);
return -1;
}
rate_change.data_in = src_buff; rate_change.data_in = src_buff;
rate_change.data_out = dst_buff; rate_change.data_out = dst_buff;
rate_change.input_frames = src_info.frames; rate_change.input_frames = src_info.frames;
Expand Down Expand Up @@ -342,10 +331,10 @@ static int espeak_exec(struct ast_channel *chan, const char *data)
return res; return res;
} }


static int reload(void) static int reload_module(void)
{ {
ast_config_destroy(cfg); ast_config_destroy(cfg);
read_config(); read_config(ESPEAK_CONFIG);
return 0; return 0;
} }


Expand All @@ -357,13 +346,13 @@ static int unload_module(void)


static int load_module(void) static int load_module(void)
{ {
read_config(); read_config(ESPEAK_CONFIG);
return ast_register_application(app, espeak_exec, synopsis, descrip) ? return ast_register_application(app, espeak_exec, synopsis, descrip) ?
AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS; AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
} }


AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "eSpeak TTS Interface", AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "eSpeak TTS Interface",
.load = load_module, .load = load_module,
.unload = unload_module, .unload = unload_module,
.reload = reload, .reload = reload_module,
); );

0 comments on commit b8de132

Please sign in to comment.