]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
resample into core
authorAnthony Minessale <anthony.minessale@gmail.com>
Mon, 2 Jan 2006 17:28:59 +0000 (17:28 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Mon, 2 Jan 2006 17:28:59 +0000 (17:28 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@254 d0543943-73ff-0310-b7d9-9358b9ac24b2

12 files changed:
src/include/switch.h
src/include/switch_core.h
src/include/switch_frame.h
src/include/switch_module_interfaces.h
src/include/switch_resample.h [new file with mode: 0644]
src/include/switch_types.h
src/include/switch_utils.h
src/mod/mod_rawaudio/mod_rawaudio.c
src/switch_core.c
src/switch_resample.c [new file with mode: 0644]
src/switch_utils.c
w32/vsnet/FreeSwitchCore.vcproj

index 7808b57bea2945c90249a7784129d5335ef6cea8..37b25cfbc24b164b3fcb2da3f1248259a5a95921 100644 (file)
@@ -28,9 +28,9 @@
  * switch.h -- Main Library Header
  *
  */
-/*! \file switch.h\r
-    \brief Main Library Header\r
-*/\r
+/*! \file switch.h
+    \brief Main Library Header
+*/
 
 #ifndef SWITCH_H
 #define SWITCH_H
@@ -61,7 +61,7 @@ extern "C" {
 #include <switch_channel.h>
 #include <switch_buffer.h>
 #include <switch_event.h>
-
+#include <switch_resample.h>
 #ifdef __cplusplus
 }
 #endif
index a0c50d79111f081054f3fd6d4ad9800001d088ae..f3ee2c95e069f9cf353d5ce3673c5681bc3f6bac 100644 (file)
@@ -29,8 +29,8 @@
  * switch_core.h -- Core Library
  *
  */
-/*! \file switch_core.h\r
-    \brief Core Library\r
+/*! \file switch_core.h
+    \brief Core Library
 */
 
 #ifndef SWITCH_CORE_H
index 5eb3c793df22d5e4ae64cd41f1dd27bae1cb0083..217503b064e4bf32189ba34028e62302f2296fc4 100644 (file)
@@ -29,8 +29,8 @@
  * switch_frame.h -- Media Frame Structure
  *
  */
-/*! \file switch_frame.h\r
-    \brief Media Frame Structure\r
+/*! \file switch_frame.h
+    \brief Media Frame Structure
 */
 
 #ifndef SWITCH_FRAME_H
index 4ce264e3f3262895172c756976e08f0393c0c0bb..1d9fd1e8de51c27e87dc42e43cb5cff10f0a2140 100644 (file)
@@ -29,8 +29,8 @@
  * switch_module_interfaces.h -- Module Interface Definitions
  *
  */
-/*! \file switch_module_interfaces.h\r
-    \brief Module Interface Definitions\r
+/*! \file switch_module_interfaces.h
+    \brief Module Interface Definitions
 */
 #ifndef SWITCH_MODULE_INTERFACES_H
 #define SWITCH_MODULE_INTERFACES_H
diff --git a/src/include/switch_resample.h b/src/include/switch_resample.h
new file mode 100644 (file)
index 0000000..6fb09b3
--- /dev/null
@@ -0,0 +1,80 @@
+/* 
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ *
+ * The Initial Developer of the Original Code is
+ * Anthony Minessale II <anthmct@yahoo.com>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * 
+ * Anthony Minessale II <anthmct@yahoo.com>
+ *
+ *
+ * switch_caller.h -- Caller Identification
+ *
+ */
+/*! \file switch_resample.h
+    \brief Audio Resample Code
+*/
+
+#ifndef SWITCH_RESAMPLE_H
+#define SWITCH_RESAMPLE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct switch_audio_resampler {
+       void *resampler;
+       int from_rate;
+       int to_rate;
+       double factor;
+       float *from;
+       int from_len;
+       size_t from_size;
+       float *to;
+       int to_len;
+       size_t to_size;
+};
+
+#include <switch.h>
+SWITCH_DECLARE(switch_status) switch_resample_create(switch_audio_resampler **new_resampler,
+                                                                                                        int from_rate,
+                                                                                                        size_t from_size,
+                                                                                                        int to_rate,
+                                                                                                        size_t to_size,
+                                                                                                        switch_memory_pool *pool);
+
+SWITCH_DECLARE(int) switch_resample_process(switch_audio_resampler *resampler, float *src, int srclen, float *dst, int dstlen, int last);
+
+SWITCH_DECLARE(size_t) switch_float_to_short(float *f, short *s, size_t len);
+SWITCH_DECLARE(int) switch_char_to_float(char *c, float *f, int len);
+SWITCH_DECLARE(int) switch_float_to_char(float *f, char *c, int len);
+SWITCH_DECLARE(int) switch_short_to_float(short *s, float *f, int len);
+SWITCH_DECLARE(void) switch_swap_linear(int16_t *buf, int len);
+SWITCH_DECLARE(void) switch_resample_destroy(switch_audio_resampler *resampler);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif
+
+
index 74707e7a2cd8236e47cb48e65184505e6d36b2da..1620ae8bcc4f0c6da872633b2e4ddeed167537a2 100644 (file)
@@ -153,7 +153,7 @@ typedef enum {
 } switch_event_t;
 
 
-
+typedef struct switch_audio_resampler switch_audio_resampler;
 typedef struct switch_event_header switch_event_header;
 typedef struct switch_event switch_event;
 typedef struct switch_event_subclass switch_event_subclass;
index c521890141056a284301d30d8d482aba553e255c..13dff0403983c3500db6e7b8a3bd48f926e9196a 100644 (file)
@@ -71,12 +71,8 @@ typedef typeof(tv.tv_usec) switch_suseconds_t;
 SWITCH_DECLARE(unsigned int) switch_separate_string(char *buf, char delim, char **array, int arraylen);
 SWITCH_DECLARE(switch_status) switch_socket_create_pollfd(switch_pollfd_t *poll, switch_socket_t *sock, unsigned int flags, switch_memory_pool *pool);
 SWITCH_DECLARE(int) switch_socket_waitfor(switch_pollfd_t *poll, int ms);
-SWITCH_DECLARE(void) switch_swap_linear(int16_t *buf, int len);
 SWITCH_DECLARE(char *) switch_cut_path(char *in);
-SWITCH_DECLARE(size_t) switch_float_to_short(float *f, short *s, size_t len);
-SWITCH_DECLARE(int) switch_char_to_float(char *c, float *f, int len);
-SWITCH_DECLARE(int) switch_float_to_char(float *f, char *c, int len);
-SWITCH_DECLARE(int) switch_short_to_float(short *s, float *f, int len);
+
 
 #if !defined(switch_strdupa) && defined(__GNUC__)
 # define switch_strdupa(s)                                                                     \
index 72b1af4b126e64bd945b4ad7fc4b6e6c70747873..1310a6c504c87257114025936d526c6c6a8380c4 100644 (file)
 
 static const char modname[] = "mod_rawaudio";
 
-#ifndef MIN
-#define MIN(a,b) ((a) < (b) ? (a) : (b))
-#endif
-
-#ifndef MAX
-#define MAX(a,b) ((a) > (b) ? (a) : (b))
-#endif
-
-#define QUALITY 1
-
-struct raw_resampler {
-       void *resampler;
-       int from;
-       int to;
-       double factor;
-       float *buf;
-       int buf_len;
-       int buf_size;
-       float *new_buf;
-       int new_buf_len;
-       int new_buf_size;
-};
-
-struct raw_context {
-       struct raw_resampler *enc;
-       struct raw_resampler *dec;
-};
-
-
-static int resample(void *handle, double factor, float *src, int srclen, float *dst, int dstlen, int last)
-{
-    int o=0, srcused=0, srcpos=0, out=0;
-
-    for(;;) {
-        int srcBlock = MIN(srclen-srcpos, srclen);
-        int lastFlag = (last && (srcBlock == srclen-srcpos));
-        o = resample_process(handle, factor, &src[srcpos], srcBlock, lastFlag, &srcused, &dst[out], dstlen-out);
-        //printf("resampling %d/%d (%d) %d %f\n",  srcpos, srclen,  MIN(dstlen-out, dstlen), srcused, factor);
-
-               srcpos += srcused;
-               if (o >= 0) {
-            out += o;
-               }
-               if (o < 0 || (o == 0 && srcpos == srclen)) {
-            break;
-               }
-    }
-    return out;
-}
-
 
 static switch_status switch_raw_init(switch_codec *codec, switch_codec_flag flags, const struct switch_codec_settings *codec_settings)
 {
@@ -96,10 +46,6 @@ static switch_status switch_raw_init(switch_codec *codec, switch_codec_flag flag
        if (!(encoding || decoding)) {
                return SWITCH_STATUS_FALSE;
        } else {
-               if (!(context = switch_core_alloc(codec->memory_pool, sizeof(*context)))) {
-                       return SWITCH_STATUS_MEMERR;
-               }
-               codec->private = context;
                return SWITCH_STATUS_SUCCESS;
        }
 }
@@ -114,56 +60,13 @@ static switch_status switch_raw_encode(switch_codec *codec,
                                                                 int *encoded_rate,
                                                                 unsigned int *flag)
 {
-       struct raw_context *context = codec->private;
-
-       /* NOOP indicates that the audio in is already the same as the audio out, so no conversion was necessary.
-          TBD Support varying number of channels
-       */
-       //printf("encode %d->%d (%d)\n", other_codec->implementation->samples_per_second, codec->implementation->samples_per_second, decoded_rate);
-
-       if (other_codec && 
-               codec->implementation->samples_per_second != other_codec->implementation->samples_per_second && 
-               decoded_rate != other_codec->implementation->samples_per_second) {
-               const short *ddp = decoded_data;
-               short *edp = encoded_data;
-               size_t ddplen = decoded_data_len / 2;
-
-               if (!context->enc) {
-                       
-                       if (!(context->enc = switch_core_alloc(codec->memory_pool, sizeof(struct raw_resampler)))) {
-                               return SWITCH_STATUS_MEMERR;
-                       }
-
-                       context->enc->from = other_codec->implementation->samples_per_second;
-                       context->enc->to = codec->implementation->samples_per_second;
-                       context->enc->factor = ((double)context->enc->to / (double)context->enc->from);
-
-                       context->enc->resampler = resample_open(QUALITY, context->enc->factor, context->enc->factor);
-                       switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Encode Resample %d->%d %f\n", context->enc->from, context->enc->to, context->enc->factor);
-                       context->enc->buf_size = codec->implementation->bytes_per_frame * 10;
-                       context->enc->buf = (float *) switch_core_alloc(codec->memory_pool, context->enc->buf_size);
-                       context->enc->new_buf_size = codec->implementation->bytes_per_frame * 10;
-                       context->enc->new_buf = (float *) switch_core_alloc(codec->memory_pool, context->enc->new_buf_size);
-               }
-
-               if (context->enc) {
-                       context->enc->buf_len = switch_short_to_float(decoded_data, context->enc->buf, (int)ddplen);
-                       context->enc->new_buf_len = resample(context->enc->resampler, 
-                                                                                                       context->enc->factor, 
-                                                                                                       context->enc->buf, 
-                                                                                                       context->enc->buf_len, 
-                                                                                                       context->enc->new_buf, 
-                                                                                                       context->enc->new_buf_size, 
-                                                                                                       0);
-                       switch_float_to_short(context->enc->new_buf, edp, decoded_data_len * 2);
-                       *encoded_data_len = context->enc->new_buf_len * 2;
-                       *encoded_rate = context->enc->to;
-                       return SWITCH_STATUS_SUCCESS;
-               }
-
-               return SWITCH_STATUS_GENERR;
+
+       /* NOOP indicates that the audio in is already the same as the audio out, so no conversion was necessary.*/
+       if (decoded_rate != codec->implementation->samples_per_second) {
+               memcpy(encoded_data, decoded_data, decoded_data_len);
+               *encoded_data_len = decoded_data_len;
+               return SWITCH_STATUS_RESAMPLE;
        }
-       
        return SWITCH_STATUS_NOOP;
 }
 
@@ -177,72 +80,18 @@ static switch_status switch_raw_decode(switch_codec *codec,
                                                                 int *decoded_rate,
                                                                 unsigned int *flag) 
 {
-       struct raw_context *context = codec->private;
-
-       //printf("decode %d->%d (%d)\n", other_codec->implementation->samples_per_second, codec->implementation->samples_per_second, encoded_rate);
-
-
-       if (other_codec && 
-               codec->implementation->samples_per_second != other_codec->implementation->samples_per_second &&
-               encoded_rate != other_codec->implementation->samples_per_second) {
-               short *ddp = decoded_data;
-               const short *edp = encoded_data;
-               size_t edplen = encoded_data_len / 2;
-
-               if (!context->dec) {
-                       if (!(context->dec = switch_core_alloc(codec->memory_pool, sizeof(struct raw_resampler)))) {
-                               return SWITCH_STATUS_MEMERR;
-                       }
-
-
-                       context->dec->from = other_codec->implementation->samples_per_second;
-                       context->dec->to = codec->implementation->samples_per_second;
-                       context->dec->factor = ((double)context->dec->from / (double)context->dec->to);
-
-                       context->dec->resampler = resample_open(QUALITY, context->dec->factor, context->dec->factor);
-                       switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Decode Resample %d->%d %f\n", context->dec->from, context->dec->to, context->dec->factor);
-                       
-                       context->dec->buf_size = codec->implementation->bytes_per_frame * 10;
-                       context->dec->buf = (float *) switch_core_alloc(codec->memory_pool, context->dec->buf_size);
-                       context->dec->new_buf_size = codec->implementation->bytes_per_frame * 10;
-                       context->dec->new_buf = (float *) switch_core_alloc(codec->memory_pool, context->dec->new_buf_size);
-               }
-
-               if (context->dec) {
-                       context->dec->buf_len = switch_short_to_float(encoded_data, context->dec->buf, (int)edplen);
-                       context->dec->new_buf_len = resample(context->dec->resampler,
-                                                                                                       context->dec->factor, 
-                                                                                                       context->dec->buf,
-                                                                                                       context->dec->buf_len, 
-                                                                                                       context->dec->new_buf, 
-                                                                                                       context->dec->new_buf_size, 
-                                                                                                       0);
-                       switch_float_to_short(context->dec->new_buf, ddp, (int)edplen);
-                       *decoded_data_len = context->dec->new_buf_len * 2;
-                       *decoded_rate = context->dec->to;
-                       return SWITCH_STATUS_SUCCESS;
-               }
-
-               return SWITCH_STATUS_GENERR;
+       if (encoded_rate != other_codec->implementation->samples_per_second) {
+               memcpy(decoded_data, encoded_data, encoded_data_len);
+               *decoded_data_len = encoded_data_len;
+               return SWITCH_STATUS_RESAMPLE;
        }
-
-
        return SWITCH_STATUS_NOOP;
 }
 
 
 static switch_status switch_raw_destroy(switch_codec *codec)
 {
-       struct raw_context *context = codec->private;
-
-       if (context->enc && context->enc->resampler){
-               resample_close(context->enc->resampler);
-       }
-
-       if (context->dec && context->dec->resampler){
-               resample_close(context->dec->resampler);
-       }
-
+       
        return SWITCH_STATUS_SUCCESS;
 }
 
index 62ed2ebe436663b8a84f702bc8f14c827fd432e0..6c0013922a9325464bf6dc546de2d0bcfae40b8f 100644 (file)
@@ -71,6 +71,10 @@ struct switch_core_session {
        unsigned char *raw_read_buf[3200];
        unsigned char *enc_read_buf[3200];
 
+
+       switch_audio_resampler *read_resampler;
+       switch_audio_resampler *write_resampler;
+
        switch_mutex_t *mutex;
        switch_thread_cond_t *cond;
 
@@ -702,9 +706,7 @@ SWITCH_DECLARE(switch_status) switch_core_session_read_frame(switch_core_session
        if (status == SWITCH_STATUS_SUCCESS && need_codec) {
                switch_frame *enc_frame, *read_frame = *frame;
 
-               if (read_frame->codec->codec_interface == session->read_codec->codec_interface) {
-                       status = SWITCH_STATUS_SUCCESS;
-               } else if (read_frame->codec) {
+               if (read_frame->codec) {
                        unsigned int flag = 0;
                        session->raw_read_frame.datalen = session->raw_read_frame.buflen;
                        status = switch_core_codec_decode(read_frame->codec,
@@ -718,6 +720,15 @@ SWITCH_DECLARE(switch_status) switch_core_session_read_frame(switch_core_session
                                                                                   &flag);
 
                        switch (status) {
+                       case SWITCH_STATUS_RESAMPLE:
+                               if (!session->read_resampler) {
+                                       switch_resample_create(&session->read_resampler,
+                                               read_frame->codec->implementation->samples_per_second,
+                                               read_frame->codec->implementation->bytes_per_frame * 10,
+                                               session->read_codec->implementation->samples_per_second,
+                                               session->read_codec->implementation->bytes_per_frame * 10,
+                                               session->pool);
+                               }
                        case SWITCH_STATUS_SUCCESS:
                                read_frame = &session->raw_read_frame;
                                break;
@@ -730,6 +741,21 @@ SWITCH_DECLARE(switch_status) switch_core_session_read_frame(switch_core_session
                                break;
                        }
                }
+               if (session->read_resampler) {
+                       short *data = read_frame->data;
+
+                       session->read_resampler->from_len = switch_short_to_float(data, session->read_resampler->from, (int)read_frame->datalen / 2 );
+                       session->read_resampler->to_len = switch_resample_process(session->read_resampler,
+                               session->read_resampler->from,
+                               session->read_resampler->from_len, 
+                               session->read_resampler->to, 
+                               (int)session->read_resampler->to_size, 
+                               0);
+                       switch_float_to_short(session->read_resampler->to, data, read_frame->datalen);
+                       read_frame->samples = session->read_resampler->to_len;
+                       read_frame->datalen = session->read_resampler->to_len * 2;
+                       read_frame->rate = session->read_resampler->to_rate;
+               }
 
                if (session->read_codec) {
                        if ((*frame)->datalen == session->read_codec->implementation->bytes_per_frame) {
@@ -828,11 +854,7 @@ SWITCH_DECLARE(switch_status) switch_core_session_write_frame(switch_core_sessio
        }
 
        if (need_codec) {
-
-               if (frame->codec->codec_interface == session->write_codec->codec_interface) {
-                       write_frame = frame;
-                       status = SWITCH_STATUS_SUCCESS;
-               } else if (frame->codec) {
+               if (frame->codec) {
                        session->raw_write_frame.datalen = session->raw_write_frame.buflen;
                        status = switch_core_codec_decode(frame->codec,
                                                                                   session->write_codec,
@@ -845,7 +867,17 @@ SWITCH_DECLARE(switch_status) switch_core_session_write_frame(switch_core_sessio
                                                                                   &flag);
 
                        switch (status) {
-
+                       case SWITCH_STATUS_RESAMPLE:
+                               write_frame = &session->raw_write_frame;
+                               if (!session->write_resampler) {
+                                       status = switch_resample_create(&session->write_resampler,
+                                               frame->codec->implementation->samples_per_second,
+                                               frame->codec->implementation->bytes_per_frame * 10,
+                                               session->write_codec->implementation->samples_per_second,
+                                               session->write_codec->implementation->bytes_per_frame * 10,
+                                               session->pool);
+                               }
+                               break;
                        case SWITCH_STATUS_SUCCESS:
                                write_frame = &session->raw_write_frame;
                                break;
@@ -859,7 +891,21 @@ SWITCH_DECLARE(switch_status) switch_core_session_write_frame(switch_core_sessio
                                break;
                        }
                } 
-
+               if (session->write_resampler) {
+                       short *data = write_frame->data;
+
+                       session->write_resampler->from_len = switch_short_to_float(data, session->write_resampler->from, (int)write_frame->datalen / 2);
+                       session->write_resampler->to_len = switch_resample_process(session->write_resampler,
+                               session->write_resampler->from,
+                               session->write_resampler->from_len, 
+                               session->write_resampler->to, 
+                               (int)session->write_resampler->to_size, 
+                               0);
+                       switch_float_to_short(session->write_resampler->to, data, write_frame->datalen);
+                       write_frame->samples = session->write_resampler->to_len;
+                       write_frame->datalen = session->write_resampler->to_len * 2;
+                       write_frame->rate = session->write_resampler->to_rate;
+               }
                if (session->write_codec) {
                        if (write_frame->datalen == session->write_codec->implementation->bytes_per_frame) {
                                perfect = TRUE;
diff --git a/src/switch_resample.c b/src/switch_resample.c
new file mode 100644 (file)
index 0000000..412c16f
--- /dev/null
@@ -0,0 +1,191 @@
+/* 
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ *
+ * The Initial Developer of the Original Code is
+ * Anthony Minessale II <anthmct@yahoo.com>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * 
+ * Anthony Minessale II <anthmct@yahoo.com>
+ *
+ *
+ * switch_caller.c -- Caller Identification
+ *
+ */
+#include <switch_resample.h>
+#include <libresample.h>
+#define NORMFACT (float)0x8000
+#define MAXSAMPLE (float)0x7FFF
+#define MAXSAMPLEC (char)0x7F
+#define QUALITY 1
+
+#ifndef MIN
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+#endif
+
+#ifndef MAX
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#endif
+
+
+
+SWITCH_DECLARE(switch_status) switch_resample_create(switch_audio_resampler **new_resampler,
+                                                                                                        int from_rate,
+                                                                                                        size_t from_size,
+                                                                                                        int to_rate,
+                                                                                                        size_t to_size,
+                                                                                                        switch_memory_pool *pool)
+{
+       switch_audio_resampler *resampler;
+       
+       if (!(resampler = switch_core_alloc(pool, sizeof(*resampler)))) {
+               return SWITCH_STATUS_MEMERR;
+       }
+
+       resampler->from_rate = from_rate;
+       resampler->to_rate = to_rate;
+       resampler->factor = ((double)resampler->to_rate / (double)resampler->from_rate);
+
+       resampler->resampler = resample_open(QUALITY, resampler->factor, resampler->factor);
+       switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Resampler %d->%d %f\n", resampler->from_rate, resampler->to_rate, resampler->factor);
+       resampler->from_size = from_size;
+       resampler->from = (float *) switch_core_alloc(pool, resampler->from_size);
+       resampler->to_size = to_size;
+       resampler->to = (float *) switch_core_alloc(pool, resampler->to_size);
+
+       *new_resampler = resampler;
+       return SWITCH_STATUS_SUCCESS;
+}
+
+
+SWITCH_DECLARE(int) switch_resample_process(switch_audio_resampler *resampler, float *src, int srclen, float *dst, int dstlen, int last)
+{
+    int o=0, srcused=0, srcpos=0, out=0;
+
+    for(;;) {
+        int srcBlock = MIN(srclen-srcpos, srclen);
+        int lastFlag = (last && (srcBlock == srclen-srcpos));
+        o = resample_process(resampler->resampler, resampler->factor, &src[srcpos], srcBlock, lastFlag, &srcused, &dst[out], dstlen-out);
+        //printf("resampling %d/%d (%d) %d %f\n",  srcpos, srclen,  MIN(dstlen-out, dstlen), srcused, factor);
+
+               srcpos += srcused;
+               if (o >= 0) {
+            out += o;
+               }
+               if (o < 0 || (o == 0 && srcpos == srclen)) {
+            break;
+               }
+    }
+    return out;
+}
+
+SWITCH_DECLARE(void) switch_resample_destroy(switch_audio_resampler *resampler)
+{
+       resample_close(resampler->resampler);
+}
+
+
+SWITCH_DECLARE(size_t) switch_float_to_short(float *f, short *s, size_t len)
+{
+       size_t i;
+       float ft;
+       for(i=0;i<len;i++) {
+               ft = f[i] * NORMFACT;
+               if(ft >= 0) {
+                       s[i] = (short)(ft+0.5);
+               } else {
+                       s[i] = (short)(ft-0.5);
+               }
+               if (s[i] > (short)MAXSAMPLE) s[i] = (short)MAXSAMPLE;
+               if (s[i] < (short)-MAXSAMPLE) s[i] = (short)-MAXSAMPLE;
+       }
+       return len;
+}
+
+SWITCH_DECLARE(int) switch_char_to_float(char *c, float *f, int len)
+{
+       int i;
+
+       if (len % 2) {
+               return(-1);
+       }
+
+       for(i=1;i<len;i+=2) {
+               f[(int)(i/2)] = (float)(((c[i])*0x100) + c[i-1]);
+               f[(int)(i/2)] /= NORMFACT;
+               if (f[(int)(i/2)] > MAXSAMPLE) f[(int)(i/2)] = MAXSAMPLE;
+               if (f[(int)(i/2)] < -MAXSAMPLE) f[(int)(i/2)] = -MAXSAMPLE;
+       }
+       return len/2;
+}
+
+SWITCH_DECLARE(int) switch_float_to_char(float *f, char *c, int len)
+{
+       int i;
+       float ft;
+       long l;
+       for(i=0;i<len;i++) {
+               ft = f[i] * NORMFACT;
+               if (ft >= 0) {
+                       l = (long)(ft+0.5);
+               } else {
+                       l = (long)(ft-0.5);
+               }
+               c[i*2] = (unsigned char)((l)&0xff);
+               c[i*2+1] = (unsigned char)(((l)>>8)&0xff);
+       }
+       return len*2;
+}
+
+SWITCH_DECLARE(int) switch_short_to_float(short *s, float *f, int len)
+{
+       int i;
+
+       for(i=0;i<len;i++) {
+               f[i] = (float)(s[i]) / NORMFACT;
+               //f[i] = (float) s[i];
+       }
+       return len;
+}
+
+
+SWITCH_DECLARE(char *) switch_cut_path(char *in)
+{
+       char *p, *ret = in;
+       char delims[] = "/\\";
+       char *i;
+
+       for(i = delims; *i; i++) {
+               p = in;
+               while((p = strchr(p, *i))) {
+                       ret = ++p;
+               }
+       }
+       return ret;
+}
+
+
+SWITCH_DECLARE(void) switch_swap_linear(int16_t *buf, int len)
+{
+       int i;
+       for (i = 0; i < len; i++) {
+               buf[i] = ((buf[i] >> 8) & 0x00ff) | ((buf[i] << 8) & 0xff00); 
+       }
+}
\ No newline at end of file
index 68d55c00f2cff37aa6263a4bcd320282544658cc..5e485b84276dec518d8eeac86778f6f333382f16 100644 (file)
  *
  */
 #include <switch_utils.h>
-#define NORMFACT (float)0x8000
-#define MAXSAMPLE (float)0x7FFF
-#define MAXSAMPLEC (char)0x7F
-
-
-SWITCH_DECLARE(size_t) switch_float_to_short(float *f, short *s, size_t len)
-{
-       size_t i;
-       float ft;
-       for(i=0;i<len;i++) {
-               ft = f[i] * NORMFACT;
-               if(ft >= 0) {
-                       s[i] = (short)(ft+0.5);
-               } else {
-                       s[i] = (short)(ft-0.5);
-               }
-               if (s[i] > (short)MAXSAMPLE) s[i] = (short)MAXSAMPLE;
-               if (s[i] < (short)-MAXSAMPLE) s[i] = (short)-MAXSAMPLE;
-       }
-       return len;
-}
-
-SWITCH_DECLARE(int) switch_char_to_float(char *c, float *f, int len)
-{
-       int i;
-
-       if (len % 2) {
-               return(-1);
-       }
-
-       for(i=1;i<len;i+=2) {
-               f[(int)(i/2)] = (float)(((c[i])*0x100) + c[i-1]);
-               f[(int)(i/2)] /= NORMFACT;
-               if (f[(int)(i/2)] > MAXSAMPLE) f[(int)(i/2)] = MAXSAMPLE;
-               if (f[(int)(i/2)] < -MAXSAMPLE) f[(int)(i/2)] = -MAXSAMPLE;
-       }
-       return len/2;
-}
-
-SWITCH_DECLARE(int) switch_float_to_char(float *f, char *c, int len)
-{
-       int i;
-       float ft;
-       long l;
-       for(i=0;i<len;i++) {
-               ft = f[i] * NORMFACT;
-               if (ft >= 0) {
-                       l = (long)(ft+0.5);
-               } else {
-                       l = (long)(ft-0.5);
-               }
-               c[i*2] = (unsigned char)((l)&0xff);
-               c[i*2+1] = (unsigned char)(((l)>>8)&0xff);
-       }
-       return len*2;
-}
-
-SWITCH_DECLARE(int) switch_short_to_float(short *s, float *f, int len)
-{
-       int i;
-
-       for(i=0;i<len;i++) {
-               f[i] = (float)(s[i]) / NORMFACT;
-               //f[i] = (float) s[i];
-       }
-       return len;
-}
-
-
-SWITCH_DECLARE(char *) switch_cut_path(char *in)
-{
-       char *p, *ret = in;
-       char delims[] = "/\\";
-       char *i;
-
-       for(i = delims; *i; i++) {
-               p = in;
-               while((p = strchr(p, *i))) {
-                       ret = ++p;
-               }
-       }
-       return ret;
-}
-
-
-SWITCH_DECLARE(void) switch_swap_linear(int16_t *buf, int len)
-{
-       int i;
-       for (i = 0; i < len; i++) {
-               buf[i] = ((buf[i] >> 8) & 0x00ff) | ((buf[i] << 8) & 0xff00); 
-       }
-}
-
 
 SWITCH_DECLARE(unsigned int) switch_separate_string(char *buf, char delim, char **array, int arraylen)
 {
index dddb82de5049fc21a4116e28dbef57c5268fdf49..1f360abce5bcdf91adc6a3a40d6eafafc13b65ce 100644 (file)
@@ -43,7 +43,7 @@
                                Name="VCCLCompilerTool"\r
                                Optimization="0"\r
                                AdditionalIncludeDirectories="..\..\src\include;..\..\libs\include"\r
-                               PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;APR_DECLARE_EXPORT;APU_DECLARE_EXPORT;API_DECLARE_EXPORT"\r
+                               PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;APR_DECLARE_EXPORT;APU_DECLARE_EXPORT;API_DECLARE_EXPORT;_CRT_SECURE_NO_DEPRECATE"\r
                                MinimalRebuild="true"\r
                                BasicRuntimeChecks="3"\r
                                RuntimeLibrary="3"\r
@@ -63,6 +63,7 @@
                        />\r
                        <Tool\r
                                Name="VCLinkerTool"\r
+                               AdditionalOptions="/NODEFAULTLIB:LIBCMTD"\r
                                AdditionalDependencies="Ws2_32.lib Mswsock.lib apr-1.lib sqlite.lib apriconv-1.lib aprutil-1.lib xml.lib Rpcrt4.lib libresampled.lib"\r
                                OutputFile="$(OutDir)/FreeSwitch.dll"\r
                                LinkIncremental="2"\r
                        <Tool\r
                                Name="VCCLCompilerTool"\r
                                AdditionalIncludeDirectories="..\..\src\include;..\..\libs\include"\r
-                               PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;APR_DECLARE_EXPORT;APU_DECLARE_EXPORT;API_DECLARE_EXPORT"\r
+                               PreprocessorDefinitions=" WIN32;NDEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;APR_DECLARE_EXPORT;APU_DECLARE_EXPORT;API_DECLARE_EXPORT;_CRT_SECURE_NO_DEPRECATE"\r
                                RuntimeLibrary="2"\r
                                UsePrecompiledHeader="0"\r
                                WarningLevel="3"\r
                        />\r
                        <Tool\r
                                Name="VCLinkerTool"\r
+                               AdditionalOptions="/NODEFAULTLIB:LIBCMTD"\r
                                AdditionalDependencies="Ws2_32.lib Mswsock.lib apr-1.lib sqlite.lib apriconv-1.lib aprutil-1.lib xml.lib Rpcrt4.lib libresample.lib"\r
                                OutputFile="$(OutDir)/FreeSwitch.dll"\r
                                LinkIncremental="1"\r
                                RelativePath="..\..\src\switch_mutex.c"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\..\src\switch_resample.c"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath="..\..\src\switch_utils.c"\r
                                >\r
                                RelativePath="..\..\src\include\switch_platform.h"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\..\src\include\switch_resample.h"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath="..\..\src\include\switch_sqlite.h"\r
                                >\r