mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-04-13 15:50:59 +00:00
resample into core
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@254 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
parent
7dc6dd3f5a
commit
111a0f6deb
@ -28,9 +28,9 @@
|
||||
* switch.h -- Main Library Header
|
||||
*
|
||||
*/
|
||||
/*! \file switch.h
|
||||
\brief Main Library Header
|
||||
*/
|
||||
/*! \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
|
||||
|
@ -29,8 +29,8 @@
|
||||
* switch_core.h -- Core Library
|
||||
*
|
||||
*/
|
||||
/*! \file switch_core.h
|
||||
\brief Core Library
|
||||
/*! \file switch_core.h
|
||||
\brief Core Library
|
||||
*/
|
||||
|
||||
#ifndef SWITCH_CORE_H
|
||||
|
@ -29,8 +29,8 @@
|
||||
* switch_frame.h -- Media Frame Structure
|
||||
*
|
||||
*/
|
||||
/*! \file switch_frame.h
|
||||
\brief Media Frame Structure
|
||||
/*! \file switch_frame.h
|
||||
\brief Media Frame Structure
|
||||
*/
|
||||
|
||||
#ifndef SWITCH_FRAME_H
|
||||
|
@ -29,8 +29,8 @@
|
||||
* switch_module_interfaces.h -- Module Interface Definitions
|
||||
*
|
||||
*/
|
||||
/*! \file switch_module_interfaces.h
|
||||
\brief Module Interface Definitions
|
||||
/*! \file switch_module_interfaces.h
|
||||
\brief Module Interface Definitions
|
||||
*/
|
||||
#ifndef SWITCH_MODULE_INTERFACES_H
|
||||
#define SWITCH_MODULE_INTERFACES_H
|
||||
|
80
src/include/switch_resample.h
Normal file
80
src/include/switch_resample.h
Normal file
@ -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
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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) \
|
||||
|
@ -34,56 +34,6 @@
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
191
src/switch_resample.c
Normal file
191
src/switch_resample.c
Normal file
@ -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);
|
||||
}
|
||||
}
|
@ -30,99 +30,6 @@
|
||||
*
|
||||
*/
|
||||
#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)
|
||||
{
|
||||
|
@ -43,7 +43,7 @@
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
AdditionalIncludeDirectories="..\..\src\include;..\..\libs\include"
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;APR_DECLARE_EXPORT;APU_DECLARE_EXPORT;API_DECLARE_EXPORT"
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;APR_DECLARE_EXPORT;APU_DECLARE_EXPORT;API_DECLARE_EXPORT;_CRT_SECURE_NO_DEPRECATE"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="3"
|
||||
@ -63,6 +63,7 @@
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
AdditionalOptions="/NODEFAULTLIB:LIBCMTD"
|
||||
AdditionalDependencies="Ws2_32.lib Mswsock.lib apr-1.lib sqlite.lib apriconv-1.lib aprutil-1.lib xml.lib Rpcrt4.lib libresampled.lib"
|
||||
OutputFile="$(OutDir)/FreeSwitch.dll"
|
||||
LinkIncremental="2"
|
||||
@ -129,7 +130,7 @@
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
AdditionalIncludeDirectories="..\..\src\include;..\..\libs\include"
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;APR_DECLARE_EXPORT;APU_DECLARE_EXPORT;API_DECLARE_EXPORT"
|
||||
PreprocessorDefinitions=" WIN32;NDEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;APR_DECLARE_EXPORT;APU_DECLARE_EXPORT;API_DECLARE_EXPORT;_CRT_SECURE_NO_DEPRECATE"
|
||||
RuntimeLibrary="2"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="3"
|
||||
@ -147,6 +148,7 @@
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
AdditionalOptions="/NODEFAULTLIB:LIBCMTD"
|
||||
AdditionalDependencies="Ws2_32.lib Mswsock.lib apr-1.lib sqlite.lib apriconv-1.lib aprutil-1.lib xml.lib Rpcrt4.lib libresample.lib"
|
||||
OutputFile="$(OutDir)/FreeSwitch.dll"
|
||||
LinkIncremental="1"
|
||||
@ -229,6 +231,10 @@
|
||||
RelativePath="..\..\src\switch_mutex.c"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\src\switch_resample.c"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\src\switch_utils.c"
|
||||
>
|
||||
@ -295,6 +301,10 @@
|
||||
RelativePath="..\..\src\include\switch_platform.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\src\include\switch_resample.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\src\include\switch_sqlite.h"
|
||||
>
|
||||
|
Loading…
x
Reference in New Issue
Block a user