FreeSWITCH API Documentation  1.7.0
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-2014, 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_caller.h -- Caller Identification
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_normalize_volume_granular(x) if (x > 13) x = 13; if (x < -13) x = -13;
43 
44 #ifndef SWITCH_RESAMPLE_H
45 #define SWITCH_RESAMPLE_H
46 #define SWITCH_RESAMPLE_QUALITY 2
47 #include <switch.h>
49 /*!
50  \defgroup resamp Audio Resample Functions
51  \ingroup core1
52  \{
53 */
54 /*! \brief An audio resampling handle */
55  typedef struct {
56  /*! a pointer to store the resampler object */
57  void *resampler;
58  /*! the rate to resample from in hz */
59  int from_rate;
60  /*! the rate to resample to in hz */
61  int to_rate;
62  /*! the factor to resample by (from / to) */
63  double factor;
64  double rfactor;
65  int16_t *to;
66  /*! the size of the to buffer used */
67  uint32_t to_len;
68  /*! the total size of the to buffer */
69  uint32_t to_size;
70  /*! the number of channels */
71  int channels;
72 
74 
75 /*!
76  \brief Prepare a new resampler handle
77  \param new_resampler NULL pointer to aim at the new handle
78  \param from_rate the rate to transfer from in hz
79  \param to_rate the rate to transfer to in hz
80  \param quality the quality desired
81  \return SWITCH_STATUS_SUCCESS if the handle was created
82  */
84  uint32_t from_rate, uint32_t to_rate, uint32_t to_size,
85  int quality, uint32_t channels, const char *file, const char *func, int line);
86 
87 
88 #define switch_resample_create(_n, _fr, _tr, _ts, _q, _c) switch_resample_perform_create(_n, _fr, _tr, _ts, _q, _c, __FILE__, __SWITCH_FUNC__, __LINE__)
89 
90 /*!
91  \brief Destroy an existing resampler handle
92  \param resampler the resampler handle to destroy
93  */
95 
96 /*!
97  \brief Resample one float buffer into another using specifications of a given handle
98  \param resampler the resample handle
99  \param src the source data
100  \param srclen the length of the source data
101  \return the used size of dst
102  */
103 SWITCH_DECLARE(uint32_t) switch_resample_process(switch_audio_resampler_t *resampler, int16_t *src, uint32_t srclen);
104 
105 
106 /*!
107  \brief Convert an array of floats to an array of shorts
108  \param f the float buffer
109  \param s the short buffer
110  \param len the length of the buffers
111  \return the size of the converted buffer
112  */
114 
115 /*!
116  \brief Convert an array of chars to an array of floats
117  \param c the char buffer
118  \param f the float buffer
119  \param len the length of the buffers
120  \return the size of the converted buffer
121  */
122 SWITCH_DECLARE(int) switch_char_to_float(char *c, float *f, int len);
123 
124 /*!
125  \brief Convert an array of floats to an array of chars
126  \param f an array of floats
127  \param c an array of chars
128  \param len the length of the buffers
129  \return the size of the converted buffer
130  */
131 SWITCH_DECLARE(int) switch_float_to_char(float *f, char *c, int len);
132 
133 /*!
134  \brief Convert an array of shorts to an array of floats
135  \param s an array of shorts
136  \param f an array of floats
137  \param len the size of the buffers
138  \return the size of the converted buffer
139  */
140 SWITCH_DECLARE(int) switch_short_to_float(short *s, float *f, int len);
141 
142 /*!
143  \brief Perform a byteswap on a buffer of 16 bit samples
144  \param buf an array of samples
145  \param len the size of the array
146  */
147 SWITCH_DECLARE(void) switch_swap_linear(int16_t *buf, int len);
148 
149 /*!
150  \brief Generate static noise
151  \param data the audio data buffer
152  \param samples the number of 2 byte samples
153  \param divisor the volume factor
154  */
155 SWITCH_DECLARE(void) switch_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t channels, uint32_t divisor);
156 
157 /*!
158  \brief Change the volume of a signed linear audio frame
159  \param data the audio data
160  \param samples the number of 2 byte samples
161  \param vol the volume factor -4 -> 4
162  */
163 SWITCH_DECLARE(void) switch_change_sln_volume(int16_t *data, uint32_t samples, int32_t vol);
164 
165 /*!
166  \brief Change the volume of a signed linear audio frame with more granularity
167  \param data the audio data
168  \param samples the number of 2 byte samples
169  \param vol the volume factor -12 -> 12
170  */
171 SWITCH_DECLARE(void) switch_change_sln_volume_granular(int16_t *data, uint32_t samples, int32_t vol);
172 ///\}
173 
174 SWITCH_DECLARE(uint32_t) switch_merge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples, int channels);
175 SWITCH_DECLARE(uint32_t) switch_unmerge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples, int channels);
176 SWITCH_DECLARE(void) switch_mux_channels(int16_t *data, switch_size_t samples, uint32_t orig_channels, uint32_t channels);
177 
178 #define switch_resample_calc_buffer_size(_to, _from, _srclen) ((uint32_t)(((float)_to / (float)_from) * (float)_srclen) * 2)
179 
180 
182 #endif
183 /* For Emacs:
184  * Local Variables:
185  * mode:c
186  * indent-tabs-mode:t
187  * tab-width:4
188  * c-basic-offset:4
189  * End:
190  * For VIM:
191  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
192  */
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_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t channels, uint32_t divisor)
Generate static noise.
void switch_resample_destroy(switch_audio_resampler_t **resampler)
Destroy an existing resampler handle.
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_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.
int switch_float_to_char(float *f, char *c, int len)
Convert an array of floats to an array of chars.
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