2017-01-06 02:10:15 -05:00
/*
2006-01-02 17:28:59 +00:00
* FreeSWITCH Modular Media Switching Software Library / Soft - Switch Application
2021-04-02 12:18:16 -07:00
* Copyright ( C ) 2005 - 2021 , Anthony Minessale II < anthm @ freeswitch . org >
2006-01-02 17:28:59 +00:00
*
* 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
2009-02-04 21:20:54 +00:00
* Anthony Minessale II < anthm @ freeswitch . org >
2006-01-02 17:28:59 +00:00
* Portions created by the Initial Developer are Copyright ( C )
* the Initial Developer . All Rights Reserved .
*
* Contributor ( s ) :
2017-01-06 02:10:15 -05:00
*
2009-02-04 21:20:54 +00:00
* Anthony Minessale II < anthm @ freeswitch . org >
2006-01-02 17:28:59 +00:00
*
*
2021-04-02 12:18:16 -07:00
* switch_resample . h
2006-01-02 17:28:59 +00:00
*
*/
/*! \file switch_resample.h
\ brief Audio Resample Code
2006-01-05 21:03:22 +00:00
This module implements a generic interface for doing audio resampling it currently uses libresample but can be ported to
any resample library with a little effort . I decided against making this interface pluggable because there are not many
2017-01-06 02:10:15 -05:00
options in terms of resample libraries so it seemed like a waste but I did opt to frontend the interface in case a better
2006-01-05 21:03:22 +00:00
way comes along some day . = D
2017-01-06 02:10:15 -05:00
2006-01-02 17:28:59 +00:00
*/
2009-03-05 04:39:35 +00:00
# define switch_normalize_volume(x) if (x > 4) x = 4; if (x < -4) x = -4;
2021-04-02 12:18:16 -07:00
# define SWITCH_GRANULAR_VOLUME_MAX 50
# define switch_normalize_volume_granular(x) if (x > SWITCH_GRANULAR_VOLUME_MAX) x = SWITCH_GRANULAR_VOLUME_MAX; if (x < -SWITCH_GRANULAR_VOLUME_MAX) x = -SWITCH_GRANULAR_VOLUME_MAX;
2006-01-02 17:28:59 +00:00
# ifndef SWITCH_RESAMPLE_H
# define SWITCH_RESAMPLE_H
2009-07-28 18:50:33 +00:00
# define SWITCH_RESAMPLE_QUALITY 2
2006-01-05 21:03:22 +00:00
# include <switch.h>
2006-09-07 14:23:31 +00:00
SWITCH_BEGIN_EXTERN_C
2006-04-12 16:34:34 +00:00
/*!
\ defgroup resamp Audio Resample Functions
\ ingroup core1
2017-01-06 02:10:15 -05:00
\ {
2006-04-12 16:34:34 +00:00
*/
2006-01-05 21:03:22 +00:00
/*! \brief An audio resampling handle */
2010-02-06 03:38:24 +00:00
typedef struct {
2006-01-05 21:03:22 +00:00
/*! a pointer to store the resampler object */
2006-01-02 17:28:59 +00:00
void * resampler ;
2006-01-05 21:03:22 +00:00
/*! the rate to resample from in hz */
2006-01-02 17:28:59 +00:00
int from_rate ;
2006-01-05 21:03:22 +00:00
/*! the rate to resample to in hz */
2006-01-02 17:28:59 +00:00
int to_rate ;
2006-01-05 21:03:22 +00:00
/*! the factor to resample by (from / to) */
2006-01-02 17:28:59 +00:00
double factor ;
2008-08-11 20:41:18 +00:00
double rfactor ;
2010-02-06 03:38:24 +00:00
int16_t * to ;
/*! the size of the to buffer used */
uint32_t to_len ;
/*! the total size of the to buffer */
uint32_t to_size ;
2014-06-12 22:06:33 +05:00
/*! the number of channels */
int channels ;
2009-02-13 23:35:17 +00:00
2006-04-29 01:00:52 +00:00
} switch_audio_resampler_t ;
2006-01-02 17:28:59 +00:00
2006-01-05 21:03:22 +00:00
/*!
\ brief Prepare a new resampler handle
\ param new_resampler NULL pointer to aim at the new handle
\ param from_rate the rate to transfer from in hz
\ param to_rate the rate to transfer to in hz
2009-02-13 23:35:17 +00:00
\ param quality the quality desired
2006-01-05 21:03:22 +00:00
\ return SWITCH_STATUS_SUCCESS if the handle was created
*/
2009-02-10 00:58:54 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_resample_perform_create ( switch_audio_resampler_t * * new_resampler ,
2009-02-13 23:35:17 +00:00
uint32_t from_rate , uint32_t to_rate , uint32_t to_size ,
2010-02-06 03:38:24 +00:00
int quality , uint32_t channels , const char * file , const char * func , int line ) ;
2009-02-10 00:58:54 +00:00
2009-02-13 23:35:17 +00:00
2009-07-23 15:55:13 +00:00
# define switch_resample_create(_n, _fr, _tr, _ts, _q, _c) switch_resample_perform_create(_n, _fr, _tr, _ts, _q, _c, __FILE__, __SWITCH_FUNC__, __LINE__)
2006-01-02 17:28:59 +00:00
2006-01-05 21:03:22 +00:00
/*!
\ brief Destroy an existing resampler handle
\ param resampler the resampler handle to destroy
*/
2007-12-12 21:30:55 +00:00
SWITCH_DECLARE ( void ) switch_resample_destroy ( switch_audio_resampler_t * * resampler ) ;
2006-01-05 21:03:22 +00:00
/*!
\ brief Resample one float buffer into another using specifications of a given handle
\ param resampler the resample handle
\ param src the source data
\ param srclen the length of the source data
\ return the used size of dst
*/
2009-02-13 23:35:17 +00:00
SWITCH_DECLARE ( uint32_t ) switch_resample_process ( switch_audio_resampler_t * resampler , int16_t * src , uint32_t srclen ) ;
2006-01-02 17:28:59 +00:00
2006-01-05 21:03:22 +00:00
/*!
\ brief Convert an array of floats to an array of shorts
\ param f the float buffer
\ param s the short buffer
\ param len the length of the buffers
\ return the size of the converted buffer
*/
2006-03-30 23:02:50 +00:00
SWITCH_DECLARE ( switch_size_t ) switch_float_to_short ( float * f , short * s , switch_size_t len ) ;
2006-01-05 21:03:22 +00:00
/*!
\ brief Convert an array of chars to an array of floats
\ param c the char buffer
\ param f the float buffer
\ param len the length of the buffers
\ return the size of the converted buffer
*/
2006-01-02 17:28:59 +00:00
SWITCH_DECLARE ( int ) switch_char_to_float ( char * c , float * f , int len ) ;
2006-01-05 21:03:22 +00:00
/*!
\ brief Convert an array of floats to an array of chars
\ param f an array of floats
\ param c an array of chars
\ param len the length of the buffers
\ return the size of the converted buffer
*/
2006-01-02 17:28:59 +00:00
SWITCH_DECLARE ( int ) switch_float_to_char ( float * f , char * c , int len ) ;
2006-01-05 21:03:22 +00:00
/*!
\ brief Convert an array of shorts to an array of floats
\ param s an array of shorts
\ param f an array of floats
\ param len the size of the buffers
\ return the size of the converted buffer
*/
2006-01-02 17:28:59 +00:00
SWITCH_DECLARE ( int ) switch_short_to_float ( short * s , float * f , int len ) ;
2006-01-05 21:03:22 +00:00
/*!
\ brief Perform a byteswap on a buffer of 16 bit samples
\ param buf an array of samples
\ param len the size of the array
*/
2006-01-02 17:28:59 +00:00
SWITCH_DECLARE ( void ) switch_swap_linear ( int16_t * buf , int len ) ;
2007-03-21 18:50:51 +00:00
/*!
\ brief Generate static noise
\ param data the audio data buffer
\ param samples the number of 2 byte samples
\ param divisor the volume factor
*/
2014-06-13 04:49:37 +05:00
SWITCH_DECLARE ( void ) switch_generate_sln_silence ( int16_t * data , uint32_t samples , uint32_t channels , uint32_t divisor ) ;
2007-03-21 18:50:51 +00:00
2006-10-25 01:00:26 +00:00
/*!
\ brief Change the volume of a signed linear audio frame
\ param data the audio data
\ param samples the number of 2 byte samples
\ param vol the volume factor - 4 - > 4
*/
SWITCH_DECLARE ( void ) switch_change_sln_volume ( int16_t * data , uint32_t samples , int32_t vol ) ;
2011-02-20 14:37:23 -06:00
/*!
\ brief Change the volume of a signed linear audio frame with more granularity
\ param data the audio data
\ param samples the number of 2 byte samples
\ param vol the volume factor - 12 - > 12
*/
SWITCH_DECLARE ( void ) switch_change_sln_volume_granular ( int16_t * data , uint32_t samples , int32_t vol ) ;
2006-04-12 16:34:34 +00:00
///\}
2015-06-12 23:55:24 -05:00
SWITCH_DECLARE ( uint32_t ) switch_merge_sln ( int16_t * data , uint32_t samples , int16_t * other_data , uint32_t other_samples , int channels ) ;
SWITCH_DECLARE ( uint32_t ) switch_unmerge_sln ( int16_t * data , uint32_t samples , int16_t * other_data , uint32_t other_samples , int channels ) ;
2014-06-12 22:06:33 +05:00
SWITCH_DECLARE ( void ) switch_mux_channels ( int16_t * data , switch_size_t samples , uint32_t orig_channels , uint32_t channels ) ;
2009-01-15 17:47:21 +00:00
2015-06-05 23:46:56 -05:00
# define switch_resample_calc_buffer_size(_to, _from, _srclen) ((uint32_t)(((float)_to / (float)_from) * (float)_srclen) * 2)
2015-06-04 13:13:25 -05:00
2017-01-05 18:34:01 -06:00
SWITCH_DECLARE ( void ) switch_agc_set ( switch_agc_t * agc , uint32_t energy_avg ,
uint32_t low_energy_point , uint32_t margin , uint32_t change_factor , uint32_t period_len ) ;
2017-01-18 15:12:08 -06:00
SWITCH_DECLARE ( switch_status_t ) switch_agc_create ( switch_agc_t * * agcP , uint32_t energy_avg ,
uint32_t low_energy_point , uint32_t margin , uint32_t change_factor , uint32_t period_len ) ;
2017-01-18 13:19:41 -06:00
SWITCH_DECLARE ( void ) switch_agc_destroy ( switch_agc_t * * agcP ) ;
SWITCH_DECLARE ( switch_status_t ) switch_agc_feed ( switch_agc_t * agc , int16_t * data , uint32_t samples , uint32_t channels ) ;
SWITCH_DECLARE ( void ) switch_agc_set_energy_avg ( switch_agc_t * agc , uint32_t energy_avg ) ;
2017-01-18 15:12:08 -06:00
SWITCH_DECLARE ( void ) switch_agc_set_energy_low ( switch_agc_t * agc , uint32_t low_energy_point ) ;
2017-04-27 15:28:59 -05:00
SWITCH_DECLARE ( void ) switch_agc_set_token ( switch_agc_t * agc , const char * token ) ;
2006-09-07 15:15:39 +00:00
SWITCH_END_EXTERN_C
2006-01-02 17:28:59 +00:00
# endif
2006-11-27 22:30:48 +00:00
/* For Emacs:
* Local Variables :
* mode : c
2008-02-03 22:14:57 +00:00
* indent - tabs - mode : t
2006-11-27 22:30:48 +00:00
* tab - width : 4
* c - basic - offset : 4
* End :
* For VIM :
2013-06-25 11:50:17 -05:00
* vim : set softtabstop = 4 shiftwidth = 4 tabstop = 4 noet :
2006-11-27 22:30:48 +00:00
*/