RTS API Documentation  1.10.11
switch_resample.h
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2021, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  *
28  *
29  * switch_resample.h
30  *
31  */
32 /*! \file switch_resample.h
33  \brief Audio Resample Code
34 
35  This module implements a generic interface for doing audio resampling it currently uses libresample but can be ported to
36  any resample library with a little effort. I decided against making this interface pluggable because there are not many
37  options in terms of resample libraries so it seemed like a waste but I did opt to frontend the interface in case a better
38  way comes along some day. =D
39 
40 */
41 #define switch_normalize_volume(x) if (x > 4) x = 4; if (x < -4) x = -4;
42 #define SWITCH_GRANULAR_VOLUME_MAX 50
43 #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;
44 
45 #ifndef SWITCH_RESAMPLE_H
46 #define SWITCH_RESAMPLE_H
47 #define SWITCH_RESAMPLE_QUALITY 2
48 #include <switch.h>
50 /*!
51  \defgroup resamp Audio Resample Functions
52  \ingroup core1
53  \{
54 */
55 /*! \brief An audio resampling handle */
56  typedef struct {
57  /*! a pointer to store the resampler object */
58  void *resampler;
59  /*! the rate to resample from in hz */
60  int from_rate;
61  /*! the rate to resample to in hz */
62  int to_rate;
63  /*! the factor to resample by (from / to) */
64  double factor;
65  double rfactor;
66  int16_t *to;
67  /*! the size of the to buffer used */
68  uint32_t to_len;
69  /*! the total size of the to buffer */
70  uint32_t to_size;
71  /*! the number of channels */
72  int channels;
73 
75 
76 /*!
77  \brief Prepare a new resampler handle
78  \param new_resampler NULL pointer to aim at the new handle
79  \param from_rate the rate to transfer from in hz
80  \param to_rate the rate to transfer to in hz
81  \param quality the quality desired
82  \return SWITCH_STATUS_SUCCESS if the handle was created
83  */
85  uint32_t from_rate, uint32_t to_rate, uint32_t to_size,
86  int quality, uint32_t channels, const char *file, const char *func, int line);
87 
88 
89 #define switch_resample_create(_n, _fr, _tr, _ts, _q, _c) switch_resample_perform_create(_n, _fr, _tr, _ts, _q, _c, __FILE__, __SWITCH_FUNC__, __LINE__)
90 
91 /*!
92  \brief Destroy an existing resampler handle
93  \param resampler the resampler handle to destroy
94  */
96 
97 /*!
98  \brief Resample one float buffer into another using specifications of a given handle
99  \param resampler the resample handle
100  \param src the source data
101  \param srclen the length of the source data
102  \return the used size of dst
103  */
104 SWITCH_DECLARE(uint32_t) switch_resample_process(switch_audio_resampler_t *resampler, int16_t *src, uint32_t srclen);
105 
106 
107 /*!
108  \brief Convert an array of floats to an array of shorts
109  \param f the float buffer
110  \param s the short buffer
111  \param len the length of the buffers
112  \return the size of the converted buffer
113  */
115 
116 /*!
117  \brief Convert an array of chars to an array of floats
118  \param c the char buffer
119  \param f the float buffer
120  \param len the length of the buffers
121  \return the size of the converted buffer
122  */
123 SWITCH_DECLARE(int) switch_char_to_float(char *c, float *f, int len);
124 
125 /*!
126  \brief Convert an array of floats to an array of chars
127  \param f an array of floats
128  \param c an array of chars
129  \param len the length of the buffers
130  \return the size of the converted buffer
131  */
132 SWITCH_DECLARE(int) switch_float_to_char(float *f, char *c, int len);
133 
134 /*!
135  \brief Convert an array of shorts to an array of floats
136  \param s an array of shorts
137  \param f an array of floats
138  \param len the size of the buffers
139  \return the size of the converted buffer
140  */
141 SWITCH_DECLARE(int) switch_short_to_float(short *s, float *f, int len);
142 
143 /*!
144  \brief Perform a byteswap on a buffer of 16 bit samples
145  \param buf an array of samples
146  \param len the size of the array
147  */
148 SWITCH_DECLARE(void) switch_swap_linear(int16_t *buf, int len);
149 
150 /*!
151  \brief Generate static noise
152  \param data the audio data buffer
153  \param samples the number of 2 byte samples
154  \param divisor the volume factor
155  */
156 SWITCH_DECLARE(void) switch_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t channels, uint32_t divisor);
157 
158 /*!
159  \brief Change the volume of a signed linear audio frame
160  \param data the audio data
161  \param samples the number of 2 byte samples
162  \param vol the volume factor -4 -> 4
163  */
164 SWITCH_DECLARE(void) switch_change_sln_volume(int16_t *data, uint32_t samples, int32_t vol);
165 
166 /*!
167  \brief Change the volume of a signed linear audio frame with more granularity
168  \param data the audio data
169  \param samples the number of 2 byte samples
170  \param vol the volume factor -12 -> 12
171  */
172 SWITCH_DECLARE(void) switch_change_sln_volume_granular(int16_t *data, uint32_t samples, int32_t vol);
173 ///\}
174 
175 SWITCH_DECLARE(uint32_t) switch_merge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples, int channels);
176 SWITCH_DECLARE(uint32_t) switch_unmerge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples, int channels);
177 SWITCH_DECLARE(void) switch_mux_channels(int16_t *data, switch_size_t samples, uint32_t orig_channels, uint32_t channels);
178 
179 #define switch_resample_calc_buffer_size(_to, _from, _srclen) ((uint32_t)(((float)_to / (float)_from) * (float)_srclen) * 2)
180 
181 SWITCH_DECLARE(void) switch_agc_set(switch_agc_t *agc, uint32_t energy_avg,
182  uint32_t low_energy_point, uint32_t margin, uint32_t change_factor, uint32_t period_len);
183 SWITCH_DECLARE(switch_status_t) switch_agc_create(switch_agc_t **agcP, uint32_t energy_avg,
184  uint32_t low_energy_point, uint32_t margin, uint32_t change_factor, uint32_t period_len);
186 SWITCH_DECLARE(switch_status_t) switch_agc_feed(switch_agc_t *agc, int16_t *data, uint32_t samples, uint32_t channels);
187 SWITCH_DECLARE(void) switch_agc_set_energy_avg(switch_agc_t *agc, uint32_t energy_avg);
188 SWITCH_DECLARE(void) switch_agc_set_energy_low(switch_agc_t *agc, uint32_t low_energy_point);
189 SWITCH_DECLARE(void) switch_agc_set_token(switch_agc_t *agc, const char *token);
191 #endif
192 /* For Emacs:
193  * Local Variables:
194  * mode:c
195  * indent-tabs-mode:t
196  * tab-width:4
197  * c-basic-offset:4
198  * End:
199  * For VIM:
200  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
201  */
int switch_char_to_float(char *c, float *f, int len)
Convert an array of chars to an array of floats.
#define SWITCH_END_EXTERN_C
Definition: switch.h:43
void switch_agc_destroy(switch_agc_t **agcP)
void switch_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t channels, uint32_t divisor)
Generate static noise.
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)
void switch_resample_destroy(switch_audio_resampler_t **resampler)
Destroy an existing resampler handle.
void switch_agc_set_energy_avg(switch_agc_t *agc, uint32_t energy_avg)
void switch_agc_set_energy_low(switch_agc_t *agc, uint32_t low_energy_point)
uint32_t switch_unmerge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples, int channels)
void switch_change_sln_volume(int16_t *data, uint32_t samples, int32_t vol)
Change the volume of a signed linear audio frame.
switch_status_t switch_agc_feed(switch_agc_t *agc, int16_t *data, uint32_t samples, uint32_t channels)
switch_byte_t switch_byte_t * buf
switch_size_t switch_float_to_short(float *f, short *s, switch_size_t len)
Convert an array of floats to an array of shorts.
switch_status_t switch_resample_perform_create(switch_audio_resampler_t **new_resampler, uint32_t from_rate, uint32_t to_rate, uint32_t to_size, int quality, uint32_t channels, const char *file, const char *func, int line)
Prepare a new resampler handle.
int switch_short_to_float(short *s, float *f, int len)
Convert an array of shorts to an array of floats.
uintptr_t switch_size_t
void switch_swap_linear(int16_t *buf, int len)
Perform a byteswap on a buffer of 16 bit samples.
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)
int switch_float_to_char(float *f, char *c, int len)
Convert an array of floats to an array of chars.
void switch_agc_set_token(switch_agc_t *agc, const char *token)
void switch_mux_channels(int16_t *data, switch_size_t samples, uint32_t orig_channels, uint32_t channels)
An audio resampling handle.
switch_status_t
Common return values.
void switch_change_sln_volume_granular(int16_t *data, uint32_t samples, int32_t vol)
Change the volume of a signed linear audio frame with more granularity.
Main Library Header.
uint32_t switch_merge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples, int channels)
#define SWITCH_DECLARE(type)
uint32_t switch_resample_process(switch_audio_resampler_t *resampler, int16_t *src, uint32_t srclen)
Resample one float buffer into another using specifications of a given handle.
#define SWITCH_BEGIN_EXTERN_C
Definition: switch.h:42