mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-23 11:27:38 +02:00
flac
This commit is contained in:
parent
bc1c572d78
commit
82f7876da2
3 changed files with 41 additions and 44 deletions
|
@ -13,23 +13,23 @@ static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *
|
|||
static void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
|
||||
static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
|
||||
|
||||
static FLAC__uint64 total_samples = 0;
|
||||
static unsigned sample_rate = 0;
|
||||
static unsigned channels = 0;
|
||||
static unsigned bps = 0;
|
||||
|
||||
static msg::Header* flacHeader = NULL;
|
||||
static msg::PcmChunk* flacChunk = NULL;
|
||||
static msg::PcmChunk* pcmChunk = NULL;
|
||||
static FLAC__StreamDecoder *decoder = 0;
|
||||
static FLAC__StreamDecoder *decoder = NULL;
|
||||
|
||||
|
||||
FlacDecoder::FlacDecoder() : Decoder()
|
||||
{
|
||||
flacChunk = new msg::PcmChunk();
|
||||
}
|
||||
|
||||
|
||||
FlacDecoder::~FlacDecoder()
|
||||
{
|
||||
delete flacChunk;
|
||||
delete decoder;
|
||||
}
|
||||
|
||||
|
||||
|
@ -54,7 +54,6 @@ bool FlacDecoder::decode(msg::PcmChunk* chunk)
|
|||
bool FlacDecoder::setHeader(msg::Header* chunk)
|
||||
{
|
||||
flacHeader = chunk;
|
||||
flacChunk = new msg::PcmChunk();
|
||||
FLAC__bool ok = true;
|
||||
FLAC__StreamDecoderInitStatus init_status;
|
||||
|
||||
|
@ -116,7 +115,7 @@ FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder
|
|||
//logO << "Write start\n";
|
||||
(void)decoder;
|
||||
|
||||
if(channels != 2 || bps != 16) {
|
||||
/* if(channels != 2 || bps != 16) {
|
||||
fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
|
@ -124,7 +123,7 @@ FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder
|
|||
fprintf(stderr, "ERROR: This frame contains %d channels (should be 2)\n", frame->header.channels);
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
if(buffer [0] == NULL) {
|
||||
*/ if(buffer [0] == NULL) {
|
||||
fprintf(stderr, "ERROR: buffer [0] is NULL\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
|
@ -150,17 +149,6 @@ FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder
|
|||
//logO << "Write end: " << flacChunk->payloadSize << "\n";
|
||||
}
|
||||
|
||||
/* write decoded PCM samples */
|
||||
/* for(i = 0; i < frame->header.blocksize; i++) {
|
||||
if(
|
||||
!write_little_endian_int16(f, (FLAC__int16)buffer[0][i]) || // left channel
|
||||
!write_little_endian_int16(f, (FLAC__int16)buffer[1][i]) // right channel
|
||||
) {
|
||||
fprintf(stderr, "ERROR: write error\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
}
|
||||
*/
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
||||
}
|
||||
|
||||
|
@ -170,17 +158,11 @@ void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMet
|
|||
(void)decoder, (void)client_data;
|
||||
|
||||
/* print some stats */
|
||||
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
|
||||
/* save for later */
|
||||
total_samples = metadata->data.stream_info.total_samples;
|
||||
sample_rate = metadata->data.stream_info.sample_rate;
|
||||
channels = metadata->data.stream_info.channels;
|
||||
bps = metadata->data.stream_info.bits_per_sample;
|
||||
|
||||
logO << "sample rate : " << sample_rate << "Hz\n";
|
||||
logO << "channels : " << channels << "\n";
|
||||
logO << "bits per sample: " << bps << "\n";
|
||||
logO << "total samples : " << total_samples << "\n";
|
||||
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
|
||||
{
|
||||
logO << "sample rate : " << metadata->data.stream_info.sample_rate << "Hz\n";
|
||||
logO << "channels : " << metadata->data.stream_info.channels << "\n";
|
||||
logO << "bits per sample: " << metadata->data.stream_info.bits_per_sample << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,18 +4,34 @@
|
|||
|
||||
using namespace std;
|
||||
|
||||
|
||||
FlacEncoder::FlacEncoder(const msg::SampleFormat& format) : Encoder(format), encoder(0)
|
||||
{
|
||||
initEncoder();
|
||||
}
|
||||
|
||||
|
||||
#define READSIZE 16384
|
||||
|
||||
static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
|
||||
size_t encodedSamples = 0;
|
||||
static msg::PcmChunk* encodedChunk;
|
||||
static msg::PcmChunk* encodedChunk = NULL;
|
||||
|
||||
|
||||
FlacEncoder::FlacEncoder(const msg::SampleFormat& format) : Encoder(format), encoder(NULL)
|
||||
{
|
||||
encodedChunk = new msg::PcmChunk();
|
||||
headerChunk = new msg::Header("flac");
|
||||
initEncoder();
|
||||
}
|
||||
|
||||
|
||||
FlacEncoder::~FlacEncoder()
|
||||
{
|
||||
if (encoder != NULL)
|
||||
{
|
||||
FLAC__stream_encoder_finish(encoder);
|
||||
FLAC__metadata_object_delete(metadata[0]);
|
||||
FLAC__metadata_object_delete(metadata[1]);
|
||||
FLAC__stream_encoder_delete(encoder);
|
||||
}
|
||||
|
||||
delete encodedChunk;
|
||||
}
|
||||
|
||||
|
||||
|
||||
msg::Header* FlacEncoder::getHeaderChunk()
|
||||
|
@ -37,7 +53,7 @@ logD << "payload: " << chunk->payloadSize << "\tsamples: " << chunk->payloadSize
|
|||
double res = encodedSamples / ((double)sampleFormat.rate / 1000.);
|
||||
if (encodedSamples > 0)
|
||||
{
|
||||
logO << "encoded: " << chunk->payloadSize << "\tsamples: " << encodedSamples << "\tres: " << res << "\n";
|
||||
logD << "encoded: " << chunk->payloadSize << "\tsamples: " << encodedSamples << "\tres: " << res << "\n";
|
||||
encodedSamples = 0;
|
||||
chunk->payloadSize = encodedChunk->payloadSize;
|
||||
chunk->payload = (char*)realloc(chunk->payload, encodedChunk->payloadSize);
|
||||
|
@ -57,7 +73,7 @@ FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder
|
|||
unsigned current_frame,
|
||||
void *client_data)
|
||||
{
|
||||
logO << "write_callback: " << bytes << ", " << samples << ", " << current_frame << "\n";
|
||||
logD << "write_callback: " << bytes << ", " << samples << ", " << current_frame << "\n";
|
||||
FlacEncoder* flacEncoder = (FlacEncoder*)client_data;
|
||||
if ((current_frame == 0) && (bytes > 0) && (samples == 0))
|
||||
{
|
||||
|
@ -82,11 +98,8 @@ logO << "write_callback: " << bytes << ", " << samples << ", " << current_frame
|
|||
|
||||
void FlacEncoder::initEncoder()
|
||||
{
|
||||
encodedChunk = new msg::PcmChunk();
|
||||
headerChunk = new msg::Header("flac");
|
||||
FLAC__bool ok = true;
|
||||
FLAC__StreamEncoderInitStatus init_status;
|
||||
FLAC__StreamMetadata *metadata[2];
|
||||
FLAC__StreamMetadata_VorbisComment_Entry entry;
|
||||
|
||||
// allocate the encoder
|
||||
|
@ -96,7 +109,7 @@ void FlacEncoder::initEncoder()
|
|||
}
|
||||
|
||||
ok &= FLAC__stream_encoder_set_verify(encoder, true);
|
||||
ok &= FLAC__stream_encoder_set_compression_level(encoder, 1);
|
||||
ok &= FLAC__stream_encoder_set_compression_level(encoder, 5);
|
||||
ok &= FLAC__stream_encoder_set_channels(encoder, sampleFormat.channels);
|
||||
ok &= FLAC__stream_encoder_set_bits_per_sample(encoder, sampleFormat.bits);
|
||||
ok &= FLAC__stream_encoder_set_sample_rate(encoder, sampleFormat.rate);
|
||||
|
|
|
@ -13,12 +13,14 @@ class FlacEncoder : public Encoder
|
|||
{
|
||||
public:
|
||||
FlacEncoder(const msg::SampleFormat& format);
|
||||
~FlacEncoder();
|
||||
virtual double encode(msg::PcmChunk* chunk);
|
||||
msg::Header* getHeaderChunk();
|
||||
|
||||
protected:
|
||||
void initEncoder();
|
||||
FLAC__StreamEncoder *encoder;
|
||||
FLAC__StreamMetadata *metadata[2];
|
||||
// virtual void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue