]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add resampler to speech handles
authorAnthony Minessale <anthony.minessale@gmail.com>
Wed, 18 Feb 2009 18:53:28 +0000 (18:53 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Wed, 18 Feb 2009 18:53:28 +0000 (18:53 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12141 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_core.h
src/include/switch_module_interfaces.h
src/include/switch_types.h
src/mod/applications/mod_conference/mod_conference.c
src/mod/asr_tts/mod_cepstral/mod_cepstral.c
src/switch_core_speech.c
src/switch_ivr_play_say.c

index 1f501dd77a461ce11456939ec74264135528996b..49d80088e46788620bf5042f49e37033ee21ffde 100644 (file)
@@ -1432,7 +1432,7 @@ SWITCH_DECLARE(void) switch_core_speech_float_param_tts(switch_speech_handle_t *
   \return SWITCH_STATUS_SUCCESS with len adjusted to the bytes written if successful
 */
 SWITCH_DECLARE(switch_status_t) switch_core_speech_read_tts(switch_speech_handle_t *sh,
-                                                                                                                       void *data, switch_size_t *datalen, uint32_t *rate, switch_speech_flag_t *flags);
+                                                                                                                       void *data, switch_size_t *datalen, switch_speech_flag_t *flags);
 /*! 
   \brief Close an open speech handle
   \param sh the speech handle to close
index 67d1f31839316a7c65c5d72457d906965f0c89af..e1e2f510f192714d6764f251c704088f6b9756b5 100644 (file)
@@ -389,7 +389,7 @@ struct switch_speech_interface {
        /*! function to feed audio to the ASR */
        switch_status_t (*speech_feed_tts) (switch_speech_handle_t *sh, char *text, switch_speech_flag_t *flags);
        /*! function to read audio from the TTS */
-       switch_status_t (*speech_read_tts) (switch_speech_handle_t *sh, void *data, switch_size_t *datalen, uint32_t *rate, switch_speech_flag_t *flags);
+       switch_status_t (*speech_read_tts) (switch_speech_handle_t *sh, void *data, switch_size_t *datalen, switch_speech_flag_t *flags);
        void (*speech_flush_tts) (switch_speech_handle_t *sh);
        void (*speech_text_param_tts) (switch_speech_handle_t *sh, char *param, const char *val);
        void (*speech_numeric_param_tts) (switch_speech_handle_t *sh, char *param, int val);
@@ -420,6 +420,16 @@ struct switch_speech_handle {
        char *param;
        /*! the handle's memory pool */
        switch_memory_pool_t *memory_pool;
+       switch_audio_resampler_t *resampler;
+       switch_buffer_t *buffer;
+       switch_byte_t *dbuf;
+       switch_size_t dbuflen;  
+       /*! the current samplerate */
+       uint32_t samplerate;
+       /*! the current native samplerate */
+       uint32_t native_rate;
+       /*! the number of channels */
+
        /*! private data for the format module to store handle specific info */
        void *private_info;
 };
index dce378e27768a4f31a567e83bfe55896760c601c..c91b6be004f66434fbd7482a30c8ddb660e492fa 100644 (file)
@@ -944,7 +944,9 @@ typedef enum {
        SWITCH_SPEECH_FLAG_PEEK = (1 << 1),
        SWITCH_SPEECH_FLAG_FREE_POOL = (1 << 2),
        SWITCH_SPEECH_FLAG_BLOCKING = (1 << 3),
-       SWITCH_SPEECH_FLAG_PAUSE = (1 << 4)
+       SWITCH_SPEECH_FLAG_PAUSE = (1 << 4),
+       SWITCH_SPEECH_FLAG_OPEN = (1 << 5),
+       SWITCH_SPEECH_FLAG_DONE = (1 << 6)
 } switch_speech_flag_enum_t;
 typedef uint32_t switch_speech_flag_t;
 
index 7dd67566ce490e8ded4e386ec7044e6f86960f53..b707304521eb6744b4090deb81ed1b0b43472369 100644 (file)
@@ -983,9 +983,8 @@ static void *SWITCH_THREAD_FUNC conference_thread_run(switch_thread_t *thread, v
                                file_sample_len = samples;
                                if (conference->fnode->type == NODE_TYPE_SPEECH) {
                                        switch_speech_flag_t flags = SWITCH_SPEECH_FLAG_BLOCKING;
-                                       uint32_t rate = conference->rate;
 
-                                       if (switch_core_speech_read_tts(conference->fnode->sh, file_frame, &file_data_len, &rate, &flags) == SWITCH_STATUS_SUCCESS) {
+                                       if (switch_core_speech_read_tts(conference->fnode->sh, file_frame, &file_data_len, &flags) == SWITCH_STATUS_SUCCESS) {
                                                file_sample_len = file_data_len / 2;
                                        } else {
                                                file_sample_len = file_data_len = 0;
@@ -1997,9 +1996,8 @@ static void conference_loop_output(conference_member_t *member)
                                } else {                /* send the node frame instead of the conference frame to the call leg */
                                        if (member->fnode->type == NODE_TYPE_SPEECH) {
                                                switch_speech_flag_t flags = SWITCH_SPEECH_FLAG_BLOCKING;
-                                               uint32_t rate = member->conference->rate;
-
-                                               if (switch_core_speech_read_tts(member->fnode->sh, file_frame, &file_data_len, &rate, &flags) == SWITCH_STATUS_SUCCESS) {
+                                               
+                                               if (switch_core_speech_read_tts(member->fnode->sh, file_frame, &file_data_len, &flags) == SWITCH_STATUS_SUCCESS) {
                                                        file_sample_len = file_data_len / 2;
                                                } else {
                                                        file_sample_len = file_data_len = 0;
index 6df1617595b5281a2a66856f6cfe0ce7767312e9..196a0aa029cc91ba3472a1f208468399147ec40f 100644 (file)
@@ -256,7 +256,7 @@ static void cepstral_speech_flush_tts(switch_speech_handle_t *sh)
        swift_port_stop(cepstral->port, SWIFT_ASYNC_ANY, SWIFT_EVENT_NOW);
 }
 
-static switch_status_t cepstral_speech_read_tts(switch_speech_handle_t *sh, void *data, size_t *datalen, uint32_t *rate, switch_speech_flag_t *flags)
+static switch_status_t cepstral_speech_read_tts(switch_speech_handle_t *sh, void *data, size_t *datalen, switch_speech_flag_t *flags)
 {
        cepstral_t *cepstral;
        size_t desired = *datalen;
index 0c10e1af496521d12fa0b4ebec47d48674a8929e..2497123e7538e90161e6a6eb4b936342604b2906 100644 (file)
@@ -80,8 +80,14 @@ SWITCH_DECLARE(switch_status_t) switch_core_speech_open(switch_speech_handle_t *
        sh->rate = rate;
        sh->name = switch_core_strdup(pool, module_name);
        sh->samples = switch_samples_per_packet(rate, interval);
+       sh->samplerate = rate;
+       sh->native_rate = rate;
 
-       return sh->speech_interface->speech_open(sh, voice_name, rate, flags);
+       if ((status = sh->speech_interface->speech_open(sh, voice_name, rate, flags)) == SWITCH_STATUS_SUCCESS) {
+               switch_set_flag(sh, SWITCH_SPEECH_FLAG_OPEN);
+       }
+
+       return status;
 }
 
 SWITCH_DECLARE(switch_status_t) switch_core_speech_feed_tts(switch_speech_handle_t *sh, char *text, switch_speech_flag_t *flags)
@@ -128,17 +134,93 @@ SWITCH_DECLARE(void) switch_core_speech_float_param_tts(switch_speech_handle_t *
 }
 
 SWITCH_DECLARE(switch_status_t) switch_core_speech_read_tts(switch_speech_handle_t *sh,
-                                                                                                                       void *data, switch_size_t *datalen, uint32_t *rate, switch_speech_flag_t *flags)
+                                                                                                                       void *data, switch_size_t *datalen, switch_speech_flag_t *flags)
 {
+       switch_status_t status;
+       switch_size_t want, orig_len = *datalen;
+       
        switch_assert(sh != NULL);
+       
+       want = *datalen;
+
+ top:
+
+       if (sh->buffer && (switch_buffer_inuse(sh->buffer) >= orig_len || switch_test_flag(sh, SWITCH_SPEECH_FLAG_DONE))) {
+               if ((*datalen = switch_buffer_read(sh->buffer, data, orig_len))) {
+                       return SWITCH_STATUS_SUCCESS;
+               }
+       }
+
+       if (switch_test_flag(sh, SWITCH_SPEECH_FLAG_DONE)) {
+               switch_clear_flag(sh, SWITCH_SPEECH_FLAG_DONE);
+               *datalen = 0;
+               return SWITCH_STATUS_FALSE;
+       }
+
+ more:
+
+       if ((status = sh->speech_interface->speech_read_tts(sh, data, datalen, flags)) != SWITCH_STATUS_SUCCESS) {
+               switch_set_flag(sh, SWITCH_SPEECH_FLAG_DONE);
+               goto top;
+       }
+
+       
+       if (sh->native_rate && sh->samplerate && sh->native_rate != sh->samplerate) {
+               if (!sh->resampler) {
+                       if (switch_resample_create(&sh->resampler,
+                                                                          sh->native_rate, sh->samplerate, (uint32_t) orig_len, SWITCH_RESAMPLE_QUALITY) != SWITCH_STATUS_SUCCESS) {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Unable to create resampler!\n");
+                               return SWITCH_STATUS_GENERR;
+                       }
+               }
+
+               switch_resample_process(sh->resampler, data, *datalen / 2);
+               if (sh->resampler->to_len < want / 2 || sh->resampler->to_len > orig_len / 2) {
+                       if (!sh->buffer) {
+                               int factor = sh->resampler->to_len * sh->samplerate / 1000;
+                               switch_buffer_create_dynamic(&sh->buffer, factor, factor, 0);
+                               switch_assert(sh->buffer);
+                       }                       
+                       if (!sh->dbuf || sh->dbuflen < sh->resampler->to_len * 2) {
+                               sh->dbuflen = sh->resampler->to_len * 2;
+                               sh->dbuf = switch_core_alloc(sh->memory_pool, sh->dbuflen);
+                       }
+                       switch_assert(sh->resampler->to_len <= sh->dbuflen);
+               
+                       memcpy((int16_t *) sh->dbuf, sh->resampler->to, sh->resampler->to_len * 2);
+                       switch_buffer_write(sh->buffer, sh->dbuf, sh->resampler->to_len * 2);
+                       
+                       if (switch_buffer_inuse(sh->buffer) < want) {
+                               *datalen = want;
+                               goto more;
+                       }
+                       *datalen = switch_buffer_read(sh->buffer, data, orig_len);
+                       status = SWITCH_STATUS_SUCCESS;
+               } else {
+                       memcpy(data, sh->resampler->to, sh->resampler->to_len * 2);
+                       *datalen = sh->resampler->to_len * 2;
+                       status = SWITCH_STATUS_SUCCESS;
+               }
+       }
+
+       return status;
 
-       return sh->speech_interface->speech_read_tts(sh, data, datalen, rate, flags);
 }
 
 
 SWITCH_DECLARE(switch_status_t) switch_core_speech_close(switch_speech_handle_t *sh, switch_speech_flag_t *flags)
 {
        switch_status_t status = sh->speech_interface->speech_close(sh, flags);
+       
+       if (!switch_test_flag(sh, SWITCH_SPEECH_FLAG_OPEN)) {
+               return SWITCH_STATUS_FALSE;
+       }
+
+       if (sh->buffer) {
+               switch_buffer_destroy(&sh->buffer);
+       }
+
+       switch_resample_destroy(&sh->resampler);
 
        UNPROTECT_INTERFACE(sh->speech_interface);
 
@@ -146,6 +228,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_speech_close(switch_speech_handle_t
                switch_core_destroy_memory_pool(&sh->memory_pool);
        }
 
+       switch_clear_flag(sh, SWITCH_SPEECH_FLAG_OPEN);
+       
        return status;
 }
 
index e554522c280a386f4c4784d2bdc46183a25829d4..a406dc89c709770072211c75daa801dfef41e940 100644 (file)
@@ -1582,7 +1582,6 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_speak_text_handle(switch_core_session
        int lead_in_out = 10;
        switch_status_t status = SWITCH_STATUS_SUCCESS;
        switch_speech_flag_t flags = SWITCH_SPEECH_FLAG_NONE;
-       uint32_t rate = 0;
        switch_size_t extra = 0;
        char *p, *tmp = NULL;
        const char *star, *pound;
@@ -1753,7 +1752,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_speak_text_handle(switch_core_session
                }
 
                flags = SWITCH_SPEECH_FLAG_BLOCKING;
-               status = switch_core_speech_read_tts(sh, abuf, &ilen, &rate, &flags);
+               status = switch_core_speech_read_tts(sh, abuf, &ilen, &flags);
 
                if (status != SWITCH_STATUS_SUCCESS) {
                        for (x = 0; !done && x < lead_in_out; x++) {