1
/* $Id: stereo.h 2394 2008-12-23 17:27:53Z bennylp $ */
3
* Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
* Additional permission under GNU GPL version 3 section 7:
22
* If you modify this program, or any covered work, by linking or
23
* combining it with the OpenSSL project's OpenSSL library (or a
24
* modified version of that library), containing parts covered by the
25
* terms of the OpenSSL or SSLeay licenses, Teluu Inc. (http://www.teluu.com)
26
* grants you additional permission to convey the resulting work.
27
* Corresponding Source for a non-source form of such a combination
28
* shall include the source code for the parts of OpenSSL used as well
29
* as that of the covered work.
31
#ifndef __PJMEDIA_STEREO_H__
32
#define __PJMEDIA_STEREO_H__
36
* @brief Monochannel and multichannel converter.
39
#include <pjmedia/errno.h>
40
#include <pjmedia/port.h>
41
#include <pjmedia/types.h>
42
#include <pj/assert.h>
46
* @defgroup PJMEDIA_STEREO Monochannel and multichannel audio frame converter
47
* @ingroup PJMEDIA_FRAME_OP
48
* @brief Mono - multi-channels audio conversion
57
* Multichannel to monochannel conversion mixes samples from all channels
58
* into the monochannel.
60
#define PJMEDIA_STEREO_MIX PJ_TRUE
65
* Multichannel to monochannel conversion, it has two operation mode specified
66
* by param options, @see pjmedia_stereo_options. This function can work safely
67
* using the same buffer (in place conversion).
69
* @param mono Output buffer to store the mono frame extracted
70
* from the multichannels frame.
71
* @param multi Input frame containing multichannels audio.
72
* @param channel_count Number of channels in the input frame.
73
* @param samples_per_frame Number of samples in the input frame.
74
* @param mix If the value is PJ_TRUE then the input channels
75
* will be mixed to produce output frame, otherwise
76
* only frame from channel_src will be copied to the
78
* @param channel_src When mixing is disabled, the mono output frame
79
* will be copied from this channel number.
81
* @return PJ_SUCCESS on success;
83
PJ_INLINE(pj_status_t) pjmedia_convert_channel_nto1(pj_int16_t mono[],
84
const pj_int16_t multi[],
85
unsigned channel_count,
86
unsigned samples_per_frame,
92
PJ_ASSERT_RETURN(mono && multi && channel_count && samples_per_frame &&
93
channel_src < channel_count, PJ_EINVAL);
96
for (i = channel_src; i < samples_per_frame; i += channel_count) {
102
for (i = 0; i < samples_per_frame; i += channel_count) {
104
for(j = 0; j < channel_count; ++j)
107
if (tmp > 32767) tmp = 32767;
108
else if (tmp < -32768) tmp = -32768;
109
*mono = (pj_int16_t) tmp;
119
* Monochannel to multichannel conversion, it will just duplicate the samples
120
* from monochannel frame to all channels in the multichannel frame.
121
* This function can work safely using the same buffer (in place conversion)
122
* as long as the buffer is big enough for the multichannel samples.
124
* @param multi Output buffer to store the multichannels frame
125
* mixed from the mono frame.
126
* @param mono The input monochannel audio frame.
127
* @param channel_count Desired number of channels in the output frame.
128
* @param samples_per_frame Number of samples in the input frame.
129
* @param options Options for conversion, currently must be zero.
131
* @return PJ_SUCCESS on success;
133
PJ_INLINE(pj_status_t) pjmedia_convert_channel_1ton(pj_int16_t multi[],
134
const pj_int16_t mono[],
135
unsigned channel_count,
136
unsigned samples_per_frame,
139
const pj_int16_t *src;
141
PJ_ASSERT_RETURN(mono && multi && channel_count && samples_per_frame,
143
PJ_ASSERT_RETURN(options == 0, PJ_EINVAL);
145
PJ_UNUSED_ARG(options);
147
src = mono + samples_per_frame - 1;
148
samples_per_frame *= channel_count;
149
while (samples_per_frame) {
151
for (i=1; i<=channel_count; ++i)
152
multi[samples_per_frame-i] = *src;
153
samples_per_frame -= channel_count;
162
* Options for channel converter port. The @pjmedia_stereo_options is also
163
* valid for this port options.
165
enum pjmedia_stereo_port_options
168
* Specifies whether this port should not destroy downstream port when
169
* this port is destroyed.
171
PJMEDIA_STEREO_DONT_DESTROY_DN = 4
176
* Create a mono-multi channel converter port. This creates a converter session,
177
* which will adjust the samples of audio frame to a different channel count
178
* when the port's get_frame() and put_frame() is called.
180
* When the port's get_frame() is called, this port will get a frame from
181
* the downstream port and convert the frame to the target channel count before
182
* returning it to the caller.
184
* When the port's put_frame() is called, this port will convert the frame
185
* to the downstream port's channel count before giving the frame to the
188
* @param pool Pool to allocate the structure and buffers.
189
* @param dn_port The downstream port, which channel count is to
190
* be converted to the target channel count.
191
* @param channel_count This port channel count.
192
* @param options Bitmask flags from #pjmedia_stereo_port_options
193
* and also application may add PJMEDIA_STEREO_MIX
195
* When this flag is zero, the default behavior
196
* is to use simple N-to-1 channel converter and
197
* to destroy downstream port when this port is
199
* @param p_port Pointer to receive the stereo port instance.
201
* @return PJ_SUCCESS on success.
203
PJ_DECL(pj_status_t) pjmedia_stereo_port_create( pj_pool_t *pool,
204
pjmedia_port *dn_port,
205
unsigned channel_count,
207
pjmedia_port **p_port );
216
#endif /* __PJMEDIA_STEREO_H__ */