1 /********************************************************************************
2  * Copyright (C) 2007 by Prakash Punnoor                                        *
3  * prakash@punnoor.de                                                           *
4  *                                                                              *
5  * This library is free software; you can redistribute it and/or                *
6  * modify it under the terms of the GNU Lesser General Public                   *
7  * License as published by the Free Software Foundation; either                 *
8  * version 2 of the License                                                     *
9  *                                                                              *
10  * This library is distributed in the hope that it will be useful,              *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of               *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU            *
13  * Lesser General Public License for more details.                              *
14  *                                                                              *
15  * You should have received a copy of the GNU Lesser General Public             *
16  * License along with this library; if not, write to the Free Software          *
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 #include "aftenxx.h"
20 
21 namespace Aften
22 {
23 #include "aften.h"
24 
25 /// Constructor: Initializes the encoder with specified context
FrameEncoder(AftenContext & context)26 FrameEncoder::FrameEncoder(AftenContext &context)
27 {
28     m_context = context;
29     int nReturnValue = aften_encode_init(&m_context);
30     if (nReturnValue > 0)
31         throw nReturnValue;
32 }
33 
34 /// Destructor: Deinitalizes the encoder
~FrameEncoder()35 FrameEncoder::~FrameEncoder()
36 {
37     aften_encode_close(&m_context);
38 }
39 
40 /// Encodes PCM samples to an A/52 frame
Encode(unsigned char * frameBuffer,const void * samples)41 int FrameEncoder::Encode(unsigned char *frameBuffer, const void *samples)
42 {
43     return aften_encode_frame(&m_context, frameBuffer, samples);
44 }
45 
46 /// Gets a context with default values
GetDefaultsContext()47 AftenContext FrameEncoder::GetDefaultsContext()
48 {
49     AftenContext context;
50     aften_set_defaults(&context);
51     return context;
52 }
53 
54 /// Determines the proper A/52 acmod and lfe parameters based on the
55 /// number of channels and the WAVE_FORMAT_EXTENSIBLE channel mask.  If the
56 /// channelMask value has the high bit set to 1 (e.g. 0xFFFFFFFF), then the default
57 /// plain WAVE channel selection is assumed.
58 /// On error, an execption is thrown. On success, the acmod and lfe params are set
59 /// to appropriate values.
WaveChannelsToAcmod(int channels,unsigned int channelMask,int & acmod,bool & hasLfe)60 void Utility::WaveChannelsToAcmod(int channels, unsigned int channelMask, int &acmod, bool &hasLfe)
61 {
62     int nLfe;
63     if (aften_wav_channels_to_acmod(channels, channelMask, &acmod, &nLfe) != 0)
64         throw -1;
65     hasLfe = nLfe != 0;
66 }
67 
68 /// overload for convenience
WaveChannelsToAcmod(int channels,unsigned int channelMask,int & acmod,int & hasLfe)69 void Utility::WaveChannelsToAcmod(int channels, unsigned int channelMask, int &acmod, int &hasLfe)
70 {
71     if (aften_wav_channels_to_acmod(channels, channelMask, &acmod, &hasLfe) != 0)
72         throw -1;
73 }
74 
75 /// Takes a channel-interleaved array of audio samples, where the channel order
76 /// is the default Wave order. The samples are rearranged to the proper A/52
77 /// channel order based on the acmod and lfe parameters.
RemapWaveToA52(void * samples,int samplesCount,int channels,A52SampleFormat format,int acmod)78 void Utility::RemapWaveToA52(void *samples, int samplesCount, int channels,
79                              A52SampleFormat format, int acmod)
80 {
81     aften_remap_wav_to_a52(samples, samplesCount, channels, format, acmod);
82 }
83 
84 /// Takes a channel-interleaved array of audio samples, where the channels are
85 /// in MPEG order. The samples are rearranged to the proper A/52 channel order
86 /// based on the acmod parameter.
RemapMpegToA52(void * samples,int samplesCount,int channels,A52SampleFormat format,int acmod)87 void Utility::RemapMpegToA52(void *samples, int samplesCount, int channels,
88                              A52SampleFormat format, int acmod)
89 {
90     aften_remap_mpeg_to_a52(samples, samplesCount, channels, format, acmod);
91 }
92 
93 /// Tells whether libaften was configured to use floats or doubles
GetFloatType()94 FloatType Utility::GetFloatType()
95 {
96     return aften_get_float_type();
97 }
98 }
99