]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
api tweaks
authorAnthony Minessale <anthony.minessale@gmail.com>
Wed, 29 Nov 2006 17:10:40 +0000 (17:10 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Wed, 29 Nov 2006 17:10:40 +0000 (17:10 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@3485 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_module_interfaces.h
src/mod/languages/mod_spidermonkey/mod_spidermonkey.c
src/switch_event.c
src/switch_ivr.c

index 9d137e63a6be8b3e11ac04d688b3b47370ab8598..4e9d2902ea0fa22da683fac3f7e64e7470189559 100644 (file)
@@ -313,6 +313,8 @@ struct switch_file_handle {
        void *private_info;
        int64_t pos;
        switch_buffer_t *audio_buffer;
+    uint32_t thresh;
+    uint32_t silence_hits;
 };
 
 /*! \brief Abstract interface to an asr module */
index 657a644b618909494ef78d45e766e3fb9cd7e7dd..ea6f189a49fdffa91ef127b25f3efb6c82f8eb04 100644 (file)
@@ -828,7 +828,7 @@ static JSBool session_recordfile(JSContext *cx, JSObject *obj, uintN argc, jsval
        int len = 0;
        switch_input_callback_function_t dtmf_func = NULL;
        struct input_callback_state cb_state = {0};
-       switch_file_handle_t fh;
+       switch_file_handle_t fh = {0};
        JSFunction *function;
     int32 limit = 0;
 
@@ -860,9 +860,21 @@ static JSBool session_recordfile(JSContext *cx, JSObject *obj, uintN argc, jsval
         if (argc > 3) {
             JS_ValueToInt32(cx, argv[3], &limit);
         }
+
+        if (argc > 4) {
+            int32_t thresh;
+            JS_ValueToInt32(cx, argv[4], &thresh);
+            fh.thresh = thresh;
+        }
+
+        if (argc > 5) {
+            int32_t silence_hits;
+            JS_ValueToInt32(cx, argv[5], &silence_hits);
+            fh.silence_hits = silence_hits;
+        }
        }
 
-       memset(&fh, 0, sizeof(fh));
+    
        cb_state.extra = &fh;
        cb_state.ret = BOOLEAN_TO_JSVAL( JS_FALSE );
        switch_ivr_record_file(jss->session, &fh, file_name, dtmf_func, bp, len, limit);
@@ -925,7 +937,7 @@ static JSBool session_streamfile(JSContext *cx, JSObject *obj, uintN argc, jsval
        int len = 0;
        switch_input_callback_function_t dtmf_func = NULL;
        struct input_callback_state cb_state = {0};
-       switch_file_handle_t fh;
+       switch_file_handle_t fh = {0};
        JSFunction *function;
 
        channel = switch_core_session_get_channel(jss->session);
@@ -963,12 +975,10 @@ static JSBool session_streamfile(JSContext *cx, JSObject *obj, uintN argc, jsval
 
     if (argc > 4) {
         int32 samps;
-        uint32_t pos = 0;
         JS_ValueToInt32(cx, argv[4], &samps);
-        switch_core_file_seek(&fh, &pos, samps, SEEK_CUR);
+        fh.samples = samps;
     }
 
-       memset(&fh, 0, sizeof(fh));
        cb_state.extra = &fh;
        cb_state.ret = BOOLEAN_TO_JSVAL( JS_FALSE );
        switch_ivr_play_file(jss->session, &fh, file_name, timer_name, dtmf_func, bp, len);
index bfce2561240a545876b122f8dc9403376a50e394..e6e53aebe1f667e48a9aec654b5b50971dcd5f12 100644 (file)
@@ -606,7 +606,9 @@ SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, ch
        
        *str = NULL;
 
-       if (!(buf = malloc(blocksize))) {
+       dlen = blocksize * 2;
+
+       if (!(buf = malloc(dlen))) {
                return SWITCH_STATUS_MEMERR;
        }
 
@@ -615,9 +617,7 @@ SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, ch
         return SWITCH_STATUS_MEMERR;
     }
 
-       dlen = blocksize;
-
-    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "hit serialze!.\n");
+    //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "hit serialze!.\n");
        for (hp = event->headers; hp; hp = hp->next) {
         /*
          * grab enough memory to store 3x the string (url encode takes one char and turns it into %XX)
@@ -628,7 +628,7 @@ SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, ch
          */
         if(encode_len < ((strlen(hp->value) * 3) + 1)) {
             char* tmp;
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Allocing %d was %d.\n", ((strlen(hp->value) * 3) + 1), encode_len);
+               //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Allocing %d was %d.\n", ((strlen(hp->value) * 3) + 1), encode_len);
             /* we can use realloc for initial alloc as well, if encode_buf is zero it treats it as a malloc */
             if(!(tmp = realloc(encode_buf, ((strlen(hp->value) * 3) + 1)))) {
                 /* oh boy, ram's gone, give back what little we grabbed and bail */
index 08065956d9d7a73eec8605fe4182c82410f88fde..986f37ae8036bb8c3dd5d40e302a07ded493bbdb 100644 (file)
@@ -428,6 +428,12 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se
         start = time(NULL);
     }
 
+    if (fh->thresh) {
+        if (!fh->silence_hits) {
+            fh->silence_hits = 20;
+        }
+    }
+
        while(switch_channel_ready(channel)) {
                switch_size_t len;
                switch_event_t *event;
@@ -477,6 +483,26 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se
                if (!SWITCH_READ_ACCEPTABLE(status)) {
                        break;
                }
+
+        if (fh->thresh) {
+            int16_t *fdata = (int16_t *) read_frame->data;
+            uint32_t samples = read_frame->datalen / sizeof(*fdata);
+            uint32_t score, count = 0, j = 0;
+            double energy = 0;
+
+            for (count = 0; count < samples; count++) {
+                energy += abs(fdata[j]);
+                j += read_codec->implementation->number_of_channels;
+            }
+               
+            score = energy / samples;
+            if (score < fh->thresh) {
+                if (!--fh->silence_hits) {
+                    break;
+                }
+            }
+        }
+
                if (!switch_test_flag(fh, SWITCH_FILE_PAUSE)) {
                        len = (switch_size_t) read_frame->datalen / 2;
                        switch_core_file_write(fh, read_frame->data, &len);
@@ -983,6 +1009,10 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
        write_frame.data = abuf;
        write_frame.buflen = sizeof(abuf);
 
+    if (fh->samples) {
+        uint32_t pos = 0;
+        switch_core_file_seek(fh, &pos, fh->samples, SEEK_CUR);
+    }
        
        if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_TITLE, &p) == SWITCH_STATUS_SUCCESS) {
                title = (char *) switch_core_session_strdup(session, (char *)p);