File: AudioEncoder.h

package info (click to toggle)
darkice 1.3-0.1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,024 kB
  • sloc: cpp: 32,044; sh: 11,508; makefile: 129
file content (493 lines) | stat: -rw-r--r-- 15,409 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*------------------------------------------------------------------------------

   Copyright (c) 2000-2007 Tyrell Corporation. All rights reserved.

   Tyrell DarkIce

   File     : AudioEncoder.h
   Version  : $Revision$
   Author   : $Author$
   Location : $HeadURL$
   
   Copyright notice:

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License  
    as published by the Free Software Foundation; either version 3
    of the License, or (at your option) any later version.
   
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    GNU General Public License for more details.
   
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

------------------------------------------------------------------------------*/
#ifndef AUDIO_ENCODER_H
#define AUDIO_ENCODER_H

#ifndef __cplusplus
#error This is a C++ include file
#endif


/* ============================================================ include files */

#include "Referable.h"
#include "Sink.h"
#include "AudioSource.h"


/* ================================================================ constants */


/* =================================================================== macros */


/* =============================================================== data types */

/**
 *  An audio encoder
 *
 *  @author  $Author$
 *  @version $Revision$
 */
class AudioEncoder : public Sink, public virtual Referable
{
    public:
        /**
         *  Type to specify bitrate mode. Possible values:
         *  - cbr - constant bitrate mode
         *          described by bitrate
         *  - abr - average bitrate mode
         *          described by an average bitrate and quality
         *  - vbr - variable bitrate mode
         *          described by quality
         */
        enum BitrateMode { cbr, abr, vbr };

    private:

        /**
         *  The Sink to dump the encoded data to
         */
        Ref<Sink>           sink;

        /**
         *  Sample rate of the input.
         */
        unsigned int        inSampleRate;

        /**
         *  Number of bits per sample of the input.
         */
        unsigned int        inBitsPerSample;

        /**
         *  Number of channels of the input.
         */
        unsigned int        inChannel;

        /**
         *  Is the input big endian or little endian?
         */
        bool                inBigEndian;

        /**
         *  The bitrate mode of the encoder
         */
        BitrateMode         outBitrateMode;

        /**
         *  Bit rate of the output in kbits/sec, for fixed bitrate encodings.
         */
        unsigned int        outBitrate;

        /**
         *  Quality of the output, for variable bitrate encodings.
         */
        double              outQuality;

        /**
         *  Sample rate of the output.
         */
        unsigned int        outSampleRate;

        /**
         *  Number of channels of the output.
         */
        unsigned int        outChannel;

        /**
         *  Initialize the object.
         *
         *  @param sink the sink to send encoded output to
         *  @param inSampleRate sample rate of the input.
         *  @param inBitsPerSample number of bits per sample of the input.
         *  @param inChannel number of channels  of the input.
         *  @param inBigEndian shows if the input is big or little endian.
         *  @param outBitrateMode the bit rate mode of the output.
         *  @param outBitrate bit rate of the output.
         *  @param outSampleRate sample rate of the output.
         *  @param outChannel number of channels of the output.
         *  @exception Exception
         */
        inline void
        init (      Sink          * sink,
                    unsigned int    inSampleRate,
                    unsigned int    inBitsPerSample,
                    unsigned int    inChannel,
                    bool            inBigEndian,
                    BitrateMode     outBitrateMode,
                    unsigned int    outBitrate,
                    double          outQuality,
                    unsigned int    outSampleRate,
                    unsigned int    outChannel )        throw ( Exception )
        {
            this->sink             = sink;
            this->inSampleRate     = inSampleRate;
            this->inBitsPerSample  = inBitsPerSample;
            this->inChannel        = inChannel;
            this->inBigEndian      = inBigEndian;
            this->outBitrateMode   = outBitrateMode;
            this->outBitrate       = outBitrate;
            this->outQuality       = outQuality;
            this->outSampleRate    = outSampleRate;
            this->outChannel       = outChannel;

            if ( outQuality < -0.1 || 1.0 < outQuality ) {
                throw Exception( __FILE__, __LINE__, "invalid encoder quality");
            }
        }

        /**
         *  De-iitialize the object.
         *
         *  @exception Exception
         */
        inline void
        strip ( void )                                  throw ( Exception )
        {
        }


    protected:

        /**
         *  Default constructor. Always throws an Exception.
         *
         *  @exception Exception
         */
        inline
        AudioEncoder ( void )                           throw ( Exception )
        {
            throw Exception( __FILE__, __LINE__);
        }

        /**
         *  Constructor.
         *
         *  @param sink the sink to send encoded output to
         *  @param inSampleRate sample rate of the input.
         *  @param inBitsPerSample number of bits per sample of the input.
         *  @param inChannel number of channels  of the input.
         *  @param inBigEndian shows if the input is big or little endian
         *  @param outBitrateMode the bit rate mode of the output.
         *  @param outBitrate bit rate of the output (kbits/sec).
         *  @param outQuality the quality of the stream.
         *  @param outSampleRate sample rate of the output.
         *                       If 0, inSampleRate is used.
         *  @param outChannel number of channels of the output.
         *                    If 0, inChannel is used.
         *  @exception Exception
         */
        inline
        AudioEncoder (  Sink          * sink,
                        unsigned int    inSampleRate,
                        unsigned int    inBitsPerSample,
                        unsigned int    inChannel, 
                        bool            inBigEndian,
                        BitrateMode     outBitrateMode,
                        unsigned int    outBitrate,
                        double          outQuality,
                        unsigned int    outSampleRate = 0,
                        unsigned int    outChannel    = 0 )
                                                        throw ( Exception )
        {
            init ( sink,
                   inSampleRate,
                   inBitsPerSample,
                   inChannel,
                   inBigEndian,
                   outBitrateMode,
                   outBitrate,
                   outQuality,
                   outSampleRate ? outSampleRate : inSampleRate,
                   outChannel    ? outChannel    : inChannel );
        }

        /**
         *  Constructor.
         *
         *  @param sink the sink to send encoded output to
         *  @param as get input sample rate, bits per sample and channels
         *            from this AudioSource.
         *  @param outBitrateMode the bit rate mode of the output.
         *  @param outBitrate bit rate of the output (kbits/sec).
         *  @param outQuality the quality of the stream.
         *  @param outSampleRate sample rate of the output.
         *                       If 0, input sample rate is used.
         *  @param outChannel number of channels of the output.
         *                    If 0, input channel is used.
         *  @exception Exception
         */
        inline
        AudioEncoder (  Sink                  * sink,
                        const AudioSource     * as,
                        BitrateMode             outBitrateMode,
                        unsigned int            outBitrate,
                        double                  outQuality,
                        unsigned int            outSampleRate = 0,
                        unsigned int            outChannel    = 0 )
                                                        throw ( Exception)
        {
            init( sink,
                  as->getSampleRate(),
                  as->getBitsPerSample(),
                  as->getChannel(),
                  as->isBigEndian(),
                  outBitrateMode,
                  outBitrate,
                  outQuality,
                  outSampleRate ? outSampleRate : as->getSampleRate(),
                  outChannel    ? outChannel    : as->getChannel() );
        }

        /**
         *  Copy constructor.
         *
         *  @param encoder the AudioEncoder to copy.
         */
        inline
        AudioEncoder (  const AudioEncoder &    encoder )   throw ( Exception )
        {
            init ( encoder.sink.get(),
                   encoder.inSampleRate,
                   encoder.inBitsPerSample,
                   encoder.inChannel,
                   encoder.inBigEndian,
                   encoder.outBitrateMode,
                   encoder.outBitrate,
                   encoder.outQuality,
                   encoder.outSampleRate,
                   encoder.outChannel );
        }

        /**
         *  Assignment operator.
         *
         *  @param encoder the AudioEncoder to assign this to.
         *  @return a reference to this AudioEncoder.
         *  @exception Exception
         */
        inline virtual AudioEncoder &
        operator= ( const AudioEncoder &        encoder )   throw ( Exception )
        {
            if ( this != &encoder ) {
                strip();

                init ( encoder.sink.get(),
                       encoder.inSampleRate,
                       encoder.inBitsPerSample,
                       encoder.inChannel,
                       encoder.inBigEndian,
                       encoder.outBitrateMode,
                       encoder.outBitrate,
                       encoder.outQuality,
                       encoder.outSampleRate,
                       encoder.outChannel );
            }

            return *this;
        }


    public:

        /**
         *  Destructor.
         *
         *  @exception Exception
         */
        inline virtual
        ~AudioEncoder ( void )          throw ( Exception )
        {
            strip();
        }

        /**
         *  Get the underlying sink, that the encoded content is sent to.
         *
         *  @return the underlying sink
         */
        inline virtual Ref<Sink>
        getSink(void)                   throw ()
        {
            return sink;
        }

        /**
         *  Get the number of channels of the input.
         *
         *  @return the number of channels  of the input.
         */
        inline int
        getInChannel ( void ) const         throw ()
        {
            return inChannel;
        }

        /**
         *  Tell if the input is big or little endian.
         *
         *  @return true if the input is big endian, false if little endian.
         */
        inline bool
        isInBigEndian ( void ) const         throw ()
        {
            return inBigEndian;
        }

        /**
         *  Get the sample rate of the input.
         *
         *  @return the sample rate of the input.
         */
        inline int
        getInSampleRate ( void ) const      throw ()
        {
            return inSampleRate;
        }

        /**
         *  Get the number of bits per sample of the input.
         *
         *  @return the number of bits per sample of the input.
         */
        inline int
        getInBitsPerSample ( void ) const   throw ()
        {
            return inBitsPerSample;
        }

        /**
         *  Get the number of channels of the output.
         *
         *  @return the number of channels of the output.
         */
        inline int
        getOutChannel ( void ) const        throw ()
        {
            return outChannel;
        }

        /**
         *  Get the sample rate of the output.
         *
         *  @return the sample rate of the output.
         */
        inline int
        getOutSampleRate ( void ) const     throw ()
        {
            return outSampleRate;
        }

        /**
         *  Get the bit rate mode of the output.
         *
         *  @return the bit rate mode of the output.
         */
        inline BitrateMode
        getOutBitrateMode ( void ) const        throw ()
        {
            return outBitrateMode;
        }

        /**
         *  Get the bit rate of the output in kbits/sec, for fixed bitrate
         *  encodings.
         *
         *  @return the bit rate of the output.
         */
        inline unsigned int
        getOutBitrate ( void ) const        throw ()
        {
            return outBitrate;
        }

        /**
         *  Get the encoding quality of the output, for variable bitrate
         *  encodings.
         *
         *  @return the encoding quality of the output.
         */
        inline double
        getOutQuality ( void ) const        throw ()
        {
            return outQuality;
        }

        /**
         *  Check whether encoding is in progress.
         *
         *  @return true if encoding is in progress, false otherwise.
         */
        virtual bool
        isRunning ( void ) const           throw ()                 = 0;

        /**
         *  Start encoding. This function returns as soon as possible,
         *  with encoding started in the background.
         *
         *  @return true if encoding has started, false otherwise.
         *  @exception Exception
         */
        virtual bool
        start ( void )                      throw ( Exception )     = 0;

        /**
         *  Stop encoding. Stops the encoding running in the background.
         *
         *  @exception Exception
         */
        virtual void
        stop ( void )                       throw ( Exception )     = 0;

        /**
         *  Cut what the sink has been doing so far, and start anew.
         *  This usually means separating the data sent to the sink up
         *  until now, and start saving a new chunk of data.
         *
         *  Typically this action is delegated to the underlying sink.
         */
        inline virtual void
        cut ( void )                                    throw ()
        {
            sink->cut();
        }

};


/* ================================================= external data structures */


/* ====================================================== function prototypes */



#endif  /* AUDIO_ENCODER_H */