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
|
//---------------------------------------------------------------------------
// 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/>.
*/
//--------------------------------------------------------------------------
#include "FlacEngine.h"
#include <iostream>
#include <cmath>
#include <cstring>
#include "FlacStream.h"
#include "CorePlayer.h"
#include "alsaplayer_error.h"
namespace Flac
{
FlacEngine::FlacEngine (FlacStream * f)
: _f (f),
_buf (0),
_apBlocksPerFlacBlock (1),
_currSamp (0),
_currApBlock (0),
_lastDecodedBlock (-1)
{
} // FlacEngine::FlacEngine
FlacEngine::FlacEngine ()
: _f (0),
_buf (0),
_apBlocksPerFlacBlock (1),
_currSamp (0),
_currApBlock (0),
_lastDecodedBlock (-1)
{
} // FlacEngine::FlacEngine
FlacEngine::~FlacEngine ()
{
delete [] _buf;
_buf = 0;
// don't delete _f
} // FlacEngine::~FlacEngine
bool
FlacEngine::init ()
{
// Calculate the number of AlsaPlayer blocks in a flac block.
// This number must be chosen such that apBlockSize is no greater
// than BUF_SIZE (see alsaplayer/CorePlayer.h). It should
// also be a power of 2 so that it nicely divides the stream's
// samplesPerBlock without fractions, in order to prevent rounding
// errors when converting AlsaPlayer block numbers to flac sample
// numbers and vice versa.
//
// I believe this algorithm meets those 2 criteria for all valid
// block sizes in flac 1.02.
int tryme;
for (tryme = 1; tryme <= 32; tryme *= 2)
{
if ((_f->samplesPerBlock () * AP_CHANNELS / tryme) <= BUF_SIZE)
break;
}
if (tryme <= 32)
{
_apBlocksPerFlacBlock = tryme;
return true;
} else {
// ugh, give up, too big
alsaplayer_error("FlacEngine::init(): block size too big");
return false;
}
} // FlacEngine::init
int
FlacEngine::apBlockSize () const
{
if (!_f)
return 0;
// AlsaPlayer wants us to return a fixed-size audio block,
// regardless of what's actually in the audio stream.
//
// The flac block size is in units of samples. Each flac block
// we decode contains a block of samples for each channel in the
// audio stream.
//
// The AlsaPlayer block size is in units of bytes. If the
// audio is mono, we'll have to copy the data from the single
// flac channel into the 2nd AlsaPlayer channel.
//
// So the AlsaPlayer block size is the flack block size (samples
// per channel) times AP_CHANNELS divided
// by the number of AlsaPlayer blocks per flac block.
return _f->samplesPerBlock () * AP_CHANNELS / _apBlocksPerFlacBlock;
} // FlacEngine::apBlockSize
int
FlacEngine::apBlocks () const
{
if (!_f)
return 0;
return (int) ceilf (_apBlocksPerFlacBlock *
((float) _f->totalSamples () /
(float) _f->samplesPerBlock ()));
} // FlacEngine::apBlocks
float
FlacEngine::blockTime (int block) const
{
if (!_f)
return 0;
float pos = ((float) block / (float) _apBlocksPerFlacBlock) *
_f->samplesPerBlock ();
return pos / (float) _f->sampleRate ();
} // FlacEngine::blockTime
bool
FlacEngine::seekToBlock (int block)
{
if (!_f || block < 0 || block > apBlocks ())
return false;
// Some flac decoder interfaces allow us to seek to an exact sample,
// so translate the block number to the corresponding sample
// number. Don't actually seek to this sample yet, we do that later
// in flac_play_block.
_currSamp = (FLAC__uint64) (_f->samplesPerBlock () *
((float) block /
(float) _apBlocksPerFlacBlock));
_currApBlock = block;
return true;
} // FlacEngine::seekToBlock
bool
FlacEngine::decodeBlock (short * buf)
{
if (!_f || !buf)
return false;
// For some reason I haven't figured out yet, the flac library
// won't return an error when we try to process a block beyond
// the total number of samples, nor will it immediately set
// the decoder state to EOF when there are no more samples
// to read. So we check the current sample number here and
// if it's greater than the total number of samples, return false.
if (_currSamp >= _f->totalSamples ())
return false;
// Optimize the case where there's a 1:1 ratio of AlsaPlayer blocks
// to flac blocks.
if (_apBlocksPerFlacBlock == 1)
_buf = buf;
else if (!_buf)
_buf = new short [apBlockSize () * _apBlocksPerFlacBlock];
// Figure out where the next batch of AlsaPlayer samples comes from
bool status;
int currBlock = _currSamp / _f->samplesPerBlock ();
if (currBlock == _lastDecodedBlock)
{
// Next AlsaPlayer block is contained in the last decoded flac block
status = true;
}
else if (currBlock == _lastDecodedBlock + 1)
{
// Next AlsaPlayer block is the contained in the next flac block
status = _f->processOneBlock ();
if (status)
++_lastDecodedBlock;
}
else
{
// Seek to find the next AlsaPlayer block. Note that by always
// seeking to a flac block boundary, we can use
// _currApBlock % _apBlocksPerFlacBlock to determine
// the AlsaPlayer block offset within _buf.
status = _f->seekAbsolute (currBlock * _f->samplesPerBlock ());
if (status)
_lastDecodedBlock = currBlock;
}
if (status)
{
if (_buf != buf)
{
memcpy (buf,
_buf + (apBlockSize () *
(_currApBlock % _apBlocksPerFlacBlock)),
apBlockSize ());
}
else
{
// reset the internal buffer pointer
_buf = 0;
}
// If we chose _apBlocksPerFlacBlock well, this conversion
// will always be exact
_currSamp += _f->samplesPerBlock () / _apBlocksPerFlacBlock;
_currApBlock++;
}
else
{
if (_buf == buf)
_buf = 0;
}
return status;
} // FlacEngine::decodeBlock
static const FLAC__int16 PCM_SILENCE = 0;
void
FlacEngine::writeAlsaPlayerBuf (unsigned int apSamps,
const FLAC__int32 * ch0,
const FLAC__int32 * ch1,
unsigned int flacSamps,
int shift)
{
FLAC__int16 * pcm = (FLAC__int16 *) _buf;
unsigned int asample, fsample;
for (asample = 0, fsample = 0; fsample < flacSamps; fsample++)
{
pcm[asample++] = (FLAC__int16) (ch0[fsample] << shift);
pcm[asample++] = (FLAC__int16) (ch1[fsample] << shift);
}
for (; asample < apSamps; )
{
pcm[asample++] = PCM_SILENCE;
pcm[asample++] = PCM_SILENCE;
}
} // FlacEngine::writeAlsaPlayerBuf
bool
FlacEngine::writeBuf (const FLAC__Frame * block,
const FLAC__int32 * const buffer[],
unsigned int flacChannels,
unsigned int bps)
{
if (!_buf || !_f)
return false;
// In flac stereo streams, channel 0 is always left and channel 1
// is always right. This matches AlsaPlayer.
//
// flac 1.02 doesn't define channel order for > 2 channel sound,
// so grab the first two channels and hope for the best.
const FLAC__int32 * left = buffer[0];
const FLAC__int32 * right = flacChannels == 1 ?
buffer[0] : buffer[1];
// aww... flac gives us signed PCM data, regardless of the signed-ness
// of the original audio stream. How thoughtful.
//
// Decode a whole flac block's worth of AlsaPlayer blocks at once.
if (supportsBps (bps))
writeAlsaPlayerBuf (apBlockSize () * _apBlocksPerFlacBlock,
left, right, block->header.blocksize,
bps == 8 ? 8 : 0);
else
return false;
return true;
} // FlacEngine::writeBuf
} // namespace Flac
|