]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
refactor say api to try and develop a good working example template
authorAnthony Minessale <anthony.minessale@gmail.com>
Fri, 22 Dec 2006 02:13:56 +0000 (02:13 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Fri, 22 Dec 2006 02:13:56 +0000 (02:13 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@3791 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_module_interfaces.h
src/include/switch_types.h
src/mod/formats/mod_sndfile/mod_sndfile.c
src/mod/say/mod_say_en/mod_say_en.c

index b48bcff80036228d2d73d0226c140df7f02177ea..e8abf5efcf2dd63e5f35706f9ee7f0082fca4c12 100644 (file)
@@ -417,13 +417,7 @@ struct switch_say_interface {
        /*! the name of the interface */
        const char *interface_name;
        /*! function to pass down to the module */
-    switch_status_t (*say_function)(switch_core_session_t *session,
-                                    char *tosay,
-                                    switch_say_type_t type,
-                                    switch_say_method_t method,
-                                    switch_input_callback_function_t dtmf_callback,
-                                    void *buf,
-                                    uint32_t buflen);
+    switch_say_callback_t say_function;
        const struct switch_say_interface *next;
 };
 
index ed09ebd7316d1f11f50768b57c1f1d216081c57c..ac528629e5450b8597a988c46129c8e7eb1d99ef 100644 (file)
@@ -891,7 +891,6 @@ typedef struct switch_speech_interface switch_speech_interface_t;
 typedef struct switch_asr_interface switch_asr_interface_t;
 typedef struct switch_directory_interface switch_directory_interface_t;
 typedef struct switch_chat_interface switch_chat_interface_t;
-typedef struct switch_say_interface switch_say_interface_t;
 typedef struct switch_core_port_allocator switch_core_port_allocator_t;
 typedef struct switch_media_bug switch_media_bug_t;
 typedef void (*switch_media_bug_callback_t)(switch_media_bug_t *, void *, switch_abc_type_t);
@@ -917,6 +916,14 @@ typedef switch_status_t (*switch_input_callback_function_t)(switch_core_session_
                                                                                                                        switch_input_type_t input_type,
                                                                                                                        void *buf,
                                                                                                                        unsigned int buflen);
+typedef struct switch_say_interface switch_say_interface_t;
+typedef switch_status_t (*switch_say_callback_t)(switch_core_session_t *session,
+                                                 char *tosay,
+                                                 switch_say_type_t type,
+                                                 switch_say_method_t method,
+                                                 switch_input_callback_function_t input_callback,
+                                                 void *buf,
+                                                 uint32_t buflen);
 typedef int (*switch_core_db_callback_func_t)(void *pArg, int argc, char **argv, char **columnNames);
 typedef switch_status_t (*switch_module_load_t) (switch_loadable_module_interface_t **, char *);
 typedef switch_status_t (*switch_module_reload_t) (void);
index 89e0c1ca90d66afdbd936db58039d0df64d8272b..ef79b62a70e6cf34664a76f21709aa1736b898cd 100644 (file)
@@ -135,7 +135,7 @@ static switch_status_t sndfile_file_open(switch_file_handle_t *handle, char *pat
                return SWITCH_STATUS_GENERR;
        }
 
-       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Opening File [%s] %dhz\n", path, context->sfinfo.samplerate);
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opening File [%s] %dhz\n", path, context->sfinfo.samplerate);
        handle->samples = (unsigned int) context->sfinfo.frames;
        handle->samplerate = context->sfinfo.samplerate;
        handle->channels = (uint8_t)context->sfinfo.channels;
index 79e15837f68cb9e5276b36dd23521306a50194a8..409733e7cc6a810a52229cd933805459313d94e2 100644 (file)
 
 static const char modname[] = "mod_say_en";
 
+typedef struct {
+       switch_core_session_t *session;
+       switch_input_callback_function_t input_callback;
+       void *buf;
+       uint32_t buflen;
+} common_args_t;
+
+static void play_group(int a, int b, int c, char *what, common_args_t *args)
+{
+       char tmp[80] = "";
+
+       if (a) {
+               snprintf(tmp, sizeof(tmp), "digits/%d.wav", a);
+               switch_ivr_play_file(args->session, NULL, tmp, NULL, args->input_callback, args->buf, args->buflen);
+               switch_ivr_play_file(args->session, NULL, "digits/hundred.wav", NULL, args->input_callback, args->buf, args->buflen);
+       }
+
+       if (b) {
+               if (b > 1) {
+                       snprintf(tmp, sizeof(tmp), "digits/%d0.wav", b);
+                       switch_ivr_play_file(args->session, NULL, tmp, NULL, args->input_callback, args->buf, args->buflen);
+               } else {
+                       snprintf(tmp, sizeof(tmp), "digits/%d%d.wav", b, c);
+                       switch_ivr_play_file(args->session, NULL, tmp, NULL, args->input_callback, args->buf, args->buflen);
+                       c = 0;
+               }
+       }
+
+       if (c) {
+               snprintf(tmp, sizeof(tmp), "digits/%d.wav", c);
+               switch_ivr_play_file(args->session, NULL, tmp, NULL, args->input_callback, args->buf, args->buflen);
+       }
+
+       if (what && (a || b || c)) {
+               switch_ivr_play_file(args->session, NULL, what, NULL, args->input_callback, args->buf, args->buflen);
+       }
+
+}
+                                          
+static char *strip_commas(char *in, char *out, switch_size_t len)
+{
+       char *p = in, *q = out;
+       char *ret = out;
+       int x = 0;
+       
+       for(;p && *p; p++) {
+               if ((*p > 47 && *p < 58)) {
+                       *q++ = *p;
+               } else if (*p != ',') {
+                       ret = NULL;
+                       break;
+               }
+
+               if (++x > len) {
+                       ret = NULL;
+                       break;
+               }
+       }
+
+       return ret;
+}
+
+static switch_status_t en_say_general_count(switch_core_session_t *session,
+                                                                                       char *tosay,
+                                                                                       switch_say_type_t type,
+                                                                                       switch_say_method_t method,
+                                                                                       switch_input_callback_function_t input_callback,
+                                                                                       void *buf,
+                                                                                       uint32_t buflen)
+{
+       switch_channel_t *channel;
+       int in;
+       int x = 0, places[9] = {0};
+       char tmp[80] = "";
+       char sbuf[13] = "";
+       common_args_t args = {0};
+
+       assert(session != NULL);
+       channel = switch_core_session_get_channel(session);
+       assert(channel != NULL);
+                       
+       args.session = session;
+       args.input_callback = input_callback;
+       args.buf = buf;
+       args.buflen = buflen;
+       
+       if (!(tosay = strip_commas(tosay, sbuf, sizeof(sbuf))) || strlen(tosay) > 9) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Parse Error!\n");
+               return SWITCH_STATUS_GENERR;
+       }
+
+       in = atoi(tosay);
+       
+       for(x = 8; x >= 0; x--) {
+               int num = (int)pow(10, x);
+               if ((places[x] = in / num)) {
+                       in -= places[x] * num;
+               }
+       }
+
+       switch (method) {
+       case SSM_PRONOUNCED:
+               play_group(places[8], places[7], places[6], "digits/million.wav", &args);
+               play_group(places[5], places[4], places[3], "digits/thousand.wav", &args);
+               play_group(places[2], places[1], places[0], NULL, &args);
+               break;
+       case SSM_ITERATED:
+               for(x = 8; x >= 0; x--) {
+                       if (places[x]) {
+                               snprintf(tmp, sizeof(tmp), "digits/%d.wav", places[x]);
+                               switch_ivr_play_file(session, NULL, tmp, NULL, input_callback, buf, buflen);
+                       }
+               }
+               break;
+       default:
+               break;
+       }
+
+}
+
 static switch_status_t en_say(switch_core_session_t *session,
                                                          char *tosay,
                                                          switch_say_type_t type,
@@ -41,107 +161,24 @@ static switch_status_t en_say(switch_core_session_t *session,
                                                          void *buf,
                                                          uint32_t buflen)
 {
-       switch_channel_t *channel;
-
-       assert(session != NULL);
-       channel = switch_core_session_get_channel(session);
-       assert(channel != NULL);
        
+       switch_say_callback_t say_cb = NULL;
+
        switch(type) {
        case SST_NUMBER:
        case SST_ITEMS:
        case SST_PERSONS:
        case SST_MESSAGES:
-               {
-                       int in;
-                       int x, places[7] = {0};
-                       char tmp[25];
-
-                       in = atoi(tosay);
-
-                       for(x = 6; x >= 0; x--) {
-                               int num = (int)pow(10, x);
-                               if ((places[x] = in / num)) {
-                                       in -= places[x] * num;
-                               }
-                       }
-
-                       switch (method) {
-                       case SSM_PRONOUNCED:
-                               if (places[6]) {
-                                       snprintf(tmp, sizeof(tmp), "digits/%d.wav", places[6]);
-                                       switch_ivr_play_file(session, NULL, tmp, NULL, input_callback, buf, buflen);
-                                       switch_ivr_play_file(session, NULL, "digits/million.wav", NULL, input_callback, buf, buflen);
-                               }
-
-                               if (places[5]) {
-                                       snprintf(tmp, sizeof(tmp), "digits/%d.wav", places[5]);
-                                       switch_ivr_play_file(session, NULL, tmp, NULL, input_callback, buf, buflen);
-                                       switch_ivr_play_file(session, NULL, "digits/hundred.wav", NULL, input_callback, buf, buflen);
-                               }
-                       
-                               if (places[4]) {
-                                       if (places[4] > 1) {
-                                               snprintf(tmp, sizeof(tmp), "digits/%d0.wav", places[4]);
-                                               switch_ivr_play_file(session, NULL, tmp, NULL, input_callback, buf, buflen);
-                                       } else {
-                                               snprintf(tmp, sizeof(tmp), "digits/%d%d.wav", places[4], places[3]);
-                                               switch_ivr_play_file(session, NULL, tmp, NULL, input_callback, buf, buflen);
-                                               places[3] = 0;
-                                       }
-                               }
-                       
-                               if (places[3]) {
-                                       snprintf(tmp, sizeof(tmp), "digits/%d.wav", places[3]);
-                                       switch_ivr_play_file(session, NULL, tmp, NULL, input_callback, buf, buflen);
-                               }
-
-                               if (places[5] || places[4] || places[3]) {
-                                       switch_ivr_play_file(session, NULL, "digits/thousand.wav", NULL, input_callback, buf, buflen);
-                               }
-
-                               if (places[2]) {
-                                       snprintf(tmp, sizeof(tmp), "digits/%d.wav", places[2]);
-                                       switch_ivr_play_file(session, NULL, tmp, NULL, input_callback, buf, buflen);
-                                       switch_ivr_play_file(session, NULL, "digits/hundred.wav", NULL, input_callback, buf, buflen);
-                               }
-
-                               if (places[1]) {
-                                       if (places[1] > 1) {
-                                               snprintf(tmp, sizeof(tmp), "digits/%d0.wav", places[1]);
-                                               switch_ivr_play_file(session, NULL, tmp, NULL, input_callback, buf, buflen);
-                                       } else {
-                                               snprintf(tmp, sizeof(tmp), "digits/%d%d.wav", places[1], places[0]);
-                                               switch_ivr_play_file(session, NULL, tmp, NULL, input_callback, buf, buflen);
-                                               places[0] = 0;
-                                       }
-                               }
-
-                               if (places[0]) {
-                                       snprintf(tmp, sizeof(tmp), "digits/%d.wav", places[0]);
-                                       switch_ivr_play_file(session, NULL, tmp, NULL, input_callback, buf, buflen);
-                               }
-
-                               break;
-                       case SSM_ITERATED:
-                               for(x = 7; x >= 0; x--) {
-                                       if (places[x]) {
-                                               snprintf(tmp, sizeof(tmp), "digits/%d.wav", places[x]);
-                                               switch_ivr_play_file(session, NULL, tmp, NULL, input_callback, buf, buflen);
-                                       }
-                               }
-                               break;
-                       default:
-                               break;
-                       }
-               }
+               say_cb = en_say_general_count;
                break;
        default:
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Finish ME!\n");
                break;
        }
-
-
+       
+       if (say_cb) {
+               say_cb(session, tosay, type, method, input_callback, buf, buflen);
+       } 
 
        return SWITCH_STATUS_SUCCESS;
 }