This commit is contained in:
badaix 2015-01-06 19:56:54 +01:00
parent bc1c572d78
commit 82f7876da2
3 changed files with 41 additions and 44 deletions

View file

@ -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 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 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::Header* flacHeader = NULL;
static msg::PcmChunk* flacChunk = NULL; static msg::PcmChunk* flacChunk = NULL;
static msg::PcmChunk* pcmChunk = NULL; static msg::PcmChunk* pcmChunk = NULL;
static FLAC__StreamDecoder *decoder = 0; static FLAC__StreamDecoder *decoder = NULL;
FlacDecoder::FlacDecoder() : Decoder() FlacDecoder::FlacDecoder() : Decoder()
{ {
flacChunk = new msg::PcmChunk();
} }
FlacDecoder::~FlacDecoder() FlacDecoder::~FlacDecoder()
{ {
delete flacChunk;
delete decoder;
} }
@ -54,7 +54,6 @@ bool FlacDecoder::decode(msg::PcmChunk* chunk)
bool FlacDecoder::setHeader(msg::Header* chunk) bool FlacDecoder::setHeader(msg::Header* chunk)
{ {
flacHeader = chunk; flacHeader = chunk;
flacChunk = new msg::PcmChunk();
FLAC__bool ok = true; FLAC__bool ok = true;
FLAC__StreamDecoderInitStatus init_status; FLAC__StreamDecoderInitStatus init_status;
@ -116,7 +115,7 @@ FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder
//logO << "Write start\n"; //logO << "Write start\n";
(void)decoder; (void)decoder;
if(channels != 2 || bps != 16) { /* if(channels != 2 || bps != 16) {
fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n"); fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; 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); fprintf(stderr, "ERROR: This frame contains %d channels (should be 2)\n", frame->header.channels);
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
} }
if(buffer [0] == NULL) { */ if(buffer [0] == NULL) {
fprintf(stderr, "ERROR: buffer [0] is NULL\n"); fprintf(stderr, "ERROR: buffer [0] is NULL\n");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; 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"; //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; 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; (void)decoder, (void)client_data;
/* print some stats */ /* print some stats */
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) { if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
/* save for later */ {
total_samples = metadata->data.stream_info.total_samples; logO << "sample rate : " << metadata->data.stream_info.sample_rate << "Hz\n";
sample_rate = metadata->data.stream_info.sample_rate; logO << "channels : " << metadata->data.stream_info.channels << "\n";
channels = metadata->data.stream_info.channels; logO << "bits per sample: " << metadata->data.stream_info.bits_per_sample << "\n";
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";
} }
} }

View file

@ -4,18 +4,34 @@
using namespace std; using namespace std;
FlacEncoder::FlacEncoder(const msg::SampleFormat& format) : Encoder(format), encoder(0)
{
initEncoder();
}
#define READSIZE 16384 #define READSIZE 16384
static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/]; static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
size_t encodedSamples = 0; 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() msg::Header* FlacEncoder::getHeaderChunk()
@ -37,7 +53,7 @@ logD << "payload: " << chunk->payloadSize << "\tsamples: " << chunk->payloadSize
double res = encodedSamples / ((double)sampleFormat.rate / 1000.); double res = encodedSamples / ((double)sampleFormat.rate / 1000.);
if (encodedSamples > 0) if (encodedSamples > 0)
{ {
logO << "encoded: " << chunk->payloadSize << "\tsamples: " << encodedSamples << "\tres: " << res << "\n"; logD << "encoded: " << chunk->payloadSize << "\tsamples: " << encodedSamples << "\tres: " << res << "\n";
encodedSamples = 0; encodedSamples = 0;
chunk->payloadSize = encodedChunk->payloadSize; chunk->payloadSize = encodedChunk->payloadSize;
chunk->payload = (char*)realloc(chunk->payload, 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, unsigned current_frame,
void *client_data) void *client_data)
{ {
logO << "write_callback: " << bytes << ", " << samples << ", " << current_frame << "\n"; logD << "write_callback: " << bytes << ", " << samples << ", " << current_frame << "\n";
FlacEncoder* flacEncoder = (FlacEncoder*)client_data; FlacEncoder* flacEncoder = (FlacEncoder*)client_data;
if ((current_frame == 0) && (bytes > 0) && (samples == 0)) if ((current_frame == 0) && (bytes > 0) && (samples == 0))
{ {
@ -82,11 +98,8 @@ logO << "write_callback: " << bytes << ", " << samples << ", " << current_frame
void FlacEncoder::initEncoder() void FlacEncoder::initEncoder()
{ {
encodedChunk = new msg::PcmChunk();
headerChunk = new msg::Header("flac");
FLAC__bool ok = true; FLAC__bool ok = true;
FLAC__StreamEncoderInitStatus init_status; FLAC__StreamEncoderInitStatus init_status;
FLAC__StreamMetadata *metadata[2];
FLAC__StreamMetadata_VorbisComment_Entry entry; FLAC__StreamMetadata_VorbisComment_Entry entry;
// allocate the encoder // allocate the encoder
@ -96,7 +109,7 @@ void FlacEncoder::initEncoder()
} }
ok &= FLAC__stream_encoder_set_verify(encoder, true); 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_channels(encoder, sampleFormat.channels);
ok &= FLAC__stream_encoder_set_bits_per_sample(encoder, sampleFormat.bits); ok &= FLAC__stream_encoder_set_bits_per_sample(encoder, sampleFormat.bits);
ok &= FLAC__stream_encoder_set_sample_rate(encoder, sampleFormat.rate); ok &= FLAC__stream_encoder_set_sample_rate(encoder, sampleFormat.rate);

View file

@ -13,12 +13,14 @@ class FlacEncoder : public Encoder
{ {
public: public:
FlacEncoder(const msg::SampleFormat& format); FlacEncoder(const msg::SampleFormat& format);
~FlacEncoder();
virtual double encode(msg::PcmChunk* chunk); virtual double encode(msg::PcmChunk* chunk);
msg::Header* getHeaderChunk(); msg::Header* getHeaderChunk();
protected: protected:
void initEncoder(); void initEncoder();
FLAC__StreamEncoder *encoder; 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); // virtual void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate);
}; };