File: FlacEngine.h

package info (click to toggle)
alsaplayer 0.99.82-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,892 kB
  • sloc: ansic: 40,741; cpp: 14,400; makefile: 983; sh: 796; lex: 751; asm: 45; python: 29; sed: 16
file content (245 lines) | stat: -rw-r--r-- 7,486 bytes parent folder | download | duplicates (2)
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
//---------------------------------------------------------------------------
//  This class handles the interface between flac audio streams and
//  AlsaPlayer buffers.
//
//  Copyright (c) 2002 by Drew Hess <dhess@bothan.net>
//
//  Based on wav_engine.c (C) 1999 by Andy Lo A Foe
//  and vorbis_engine.c (C) 2002 by Andy Lo A Foe
//  and mad_engine.c (C) 2002 by Andy Lo A Foe
//  and the flac xmms plugin (C) 2001 by Josh Coalson
//
/*  This file is part of AlsaPlayer.
 *
 *  AlsaPlayer 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.
 *
 *  AlsaPlayer 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, see <http://www.gnu.org/licenses/>.
 */
//--------------------------------------------------------------------------


#ifndef _FLAC_ENGINE_H_
#define _FLAC_ENGINE_H_

extern "C"
{
#include <FLAC/stream_decoder.h>
}

namespace Flac
{

class FlacStream;

class FlacEngine
{
 public:

    //--------------------------------------------------------
    // Does the engine support these min and max block sizes?
    //--------------------------------------------------------

    static bool supportsBlockSizes (unsigned int min, unsigned int max);


    //-----------------------------------------------------------------
    // Does the engine support a flac stream with the indicated number
    // of channels?
    //-----------------------------------------------------------------

    static bool supportsChannels (unsigned int nchannels);


    //--------------------------------------------------
    // Supports the indicated number of bits per sample?
    //--------------------------------------------------

    static bool supportsBps (unsigned int bps);


 public:

    FlacEngine (FlacStream * f);
    ~FlacEngine ();


    //---------------------------------------------------------
    // Get the number of channels in an AlsaPlayer audio block.
    //---------------------------------------------------------

    int   apChannels () const;


    //------------------------------------------------------------------
    // Get the number of bytes per sample in an AlsaPlayer audio stream.
    //------------------------------------------------------------------

    int   apBytesPerSample () const;


    //------------------------------------------------------
    // Get the number of bytes in an AlsaPlayer audio block.
    //------------------------------------------------------

    int   apBlockSize () const;


    //-----------------------------------------------------------------
    // Get the number of AlsaPlayer audio blocks contained in the flac
    // audio stream to which the engine is attached.
    //-----------------------------------------------------------------

    int   apBlocks () const;


    //-------------------------------------------------------------------
    // Given an AlsaPlayer block number, return the time in seconds at
    // which the block is played relative to the beginning of the stream.
    //-------------------------------------------------------------------

    float blockTime (int block) const;


    //----------------------------------------------------------------
    // Seek to an AlsaPlayer block in the stream.  The real seek is
    // deferred until the next time the block method is invoked.
    //
    // Returns false if the seek is illegal, in which case the current
    // block position is not changed.
    //----------------------------------------------------------------

    bool  seekToBlock (int block);


    //----------------------------------------------------------------------
    // Decode the current AlsaPlayer block, placing the uncompressed audio
    // samples info a pre-allocated buffer buf.  The buffer must be at
    // least as large as an AlsaPlayer block (use apBlockSize to get this
    // information from the engine).
    //
    // Returns false if the decode process fails, or if the stream is
    // out of samples.
    //----------------------------------------------------------------------

    bool  decodeBlock (short * buf);


 private:

    FlacEngine ();


    //--------------------------------------------------------------------
    // Initialize the engine.  Called by a flac stream object after it
    // opens the stream.
    //
    // Returns true if successfully initialized, false otherwise.
    //------------------------------------------------------------------

    bool  init ();


    //-------------------------------------------------------------------
    // Write the uncompressed audio data into the buffer set with setBuf.
    //-------------------------------------------------------------------

    bool  writeBuf (const FLAC__Frame * block, const FLAC__int32 * const buffer[],
		    unsigned int flacSamples, unsigned int bps);


    //-------------------------------------------------------------------
    // Fill an AlsaPlayer buffer (16-bit 2-channel PCM audio, left/right
    // interleave) with decoded flac audio data.  Pad the AlsaPlayer
    // buffer with silence if necessary.
    //-------------------------------------------------------------------

    void writeAlsaPlayerBuf (unsigned int apSamps, const FLAC__int32 * ch0,
			     const FLAC__int32 * ch1, unsigned int flacSamps,
			     int shift);


    friend class FlacStream;
    friend class FlacSeekableStream;
    friend class OggFlacStream;

 private:

    FlacStream *       _f;
    short *           _buf;
    int              _apBlocksPerFlacBlock;


    //-------------------
    // Stream accounting.
    //-------------------

    FLAC__uint64     _currSamp;          // current sample position
    int              _currApBlock;       // current AlsaPlayer block
    int              _lastDecodedBlock;  // last flac block that was
                                         // successfully decoded


    //-----------------------------------------------------------
    // AlsaPlayer always wants 2 channels.
    //-----------------------------------------------------------

    static const unsigned int AP_CHANNELS = 2;

}; // class FlacEngine


//----------------
// Inline methods.
//----------------


// static
inline bool
FlacEngine::supportsBlockSizes (unsigned int min, unsigned int max)
{
    // The flac format doesn't require a uniform block size for the stream,
    // but the reference encoder is implemented this way, which makes our
    // life much simpler.  Let's do a sanity check just to make sure.

    return min == max && min >= FLAC__MIN_BLOCK_SIZE &&
	   max <= FLAC__MAX_BLOCK_SIZE;
}

// static
inline bool
FlacEngine::supportsChannels (unsigned int nchannels)
{
    return nchannels > 0 && nchannels <= FLAC__MAX_CHANNELS;
}

// static
inline bool
FlacEngine::supportsBps (unsigned int bps)
{
    return bps == 8 || bps == 16;
}

inline int
FlacEngine::apChannels () const
{
    return AP_CHANNELS;
}

inline int
FlacEngine::apBytesPerSample () const
{
    return sizeof (_buf [0]);
}

} // namespace Flac

#endif /* _FLAC_ENGINE_H_ */