experimental FLAC support

This commit is contained in:
badaix 2015-01-04 12:32:49 +01:00
parent 176ed8abc0
commit d99c3cb9a0
10 changed files with 289 additions and 35 deletions

View file

@ -5,28 +5,75 @@
using namespace std;
FlacEncoder::FlacEncoder(const msg::SampleFormat& format) : Encoder(format)
FlacEncoder::FlacEncoder(const msg::SampleFormat& format) : Encoder(format), encoder(0)
{
headerChunk = new HeaderMessage("flac");
initEncoder();
}
#define READSIZE 16384
static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
size_t encodedSamples = 0;
static msg::PcmChunk* encodedChunk;
msg::Header* FlacEncoder::getHeaderChunk()
{
return headerChunk;
}
double FlacEncoder::encode(msg::PcmChunk* chunk)
{
return chunk->duration<chronos::msec>().count();
logD << "payload: " << chunk->payloadSize << "\tsamples: " << chunk->payloadSize/4 << "\n";
int samples = chunk->payloadSize / 4;
for(int i=0; i<samples*2/*channels*/; i++)
{
pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)chunk->payload[2*i+1] << 8) | (FLAC__int16)(0x00ff&chunk->payload[2*i]));
}
FLAC__stream_encoder_process_interleaved(encoder, pcm, samples);
double res = encodedSamples / ((double)sampleFormat.rate / 1000.);
if (encodedSamples > 0)
{
logD << "encoded: " << chunk->payloadSize << "\tsamples: " << encodedSamples << "\tres: " << res << "\n";
encodedSamples = 0;
chunk->payloadSize = encodedChunk->payloadSize;
chunk->payload = (char*)realloc(chunk->payload, encodedChunk->payloadSize);
memcpy(chunk->payload, encodedChunk->payload, encodedChunk->payloadSize);
encodedChunk->payloadSize = 0;
encodedChunk->payload = (char*)realloc(encodedChunk->payload, 0);
}
return res;//chunk->duration<chronos::msec>().count();
}
#define READSIZE 1024
static unsigned total_samples = 0; /* can use a 32-bit number due to WAVE size limitations */
static FLAC__byte buffer[READSIZE/*samples*/ * 2/*bytes_per_sample*/ * 2/*channels*/]; /* we read the WAVE data into here */
static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
static void write_callback(const FLAC__StreamEncoder *encoder, const unsigned char*, long unsigned int bytes, unsigned int samples, unsigned int current_frame, void *client_data)
FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder,
const FLAC__byte buffer[],
size_t bytes,
unsigned samples,
unsigned current_frame,
void *client_data)
{
cout << "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))
{
msg::Header* headerChunk = flacEncoder->getHeaderChunk();
headerChunk->payload = (char*)realloc(headerChunk->payload, headerChunk->payloadSize + bytes);
memcpy(headerChunk->payload + headerChunk->payloadSize, buffer, bytes);
headerChunk->payloadSize += bytes;
}
else
{
encodedChunk->payload = (char*)realloc(encodedChunk->payload, encodedChunk->payloadSize + bytes);
memcpy(encodedChunk->payload + encodedChunk->payloadSize, buffer, bytes);
encodedChunk->payloadSize += bytes;
encodedSamples += samples;
}
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
}
@ -35,8 +82,9 @@ static void write_callback(const FLAC__StreamEncoder *encoder, const unsigned ch
void FlacEncoder::initEncoder()
{
encodedChunk = new msg::PcmChunk();
headerChunk = new msg::Header("flac");
FLAC__bool ok = true;
FLAC__StreamEncoder *encoder = 0;
FLAC__StreamEncoderInitStatus init_status;
FLAC__StreamMetadata *metadata[2];
FLAC__StreamMetadata_VorbisComment_Entry entry;
@ -48,11 +96,10 @@ void FlacEncoder::initEncoder()
}
ok &= FLAC__stream_encoder_set_verify(encoder, true);
ok &= FLAC__stream_encoder_set_compression_level(encoder, 5);
ok &= FLAC__stream_encoder_set_compression_level(encoder, 8);
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);
ok &= FLAC__stream_encoder_set_total_samples_estimate(encoder, 0);
// now add some metadata; we'll add some tags and a padding block
if(ok) {
@ -76,7 +123,7 @@ void FlacEncoder::initEncoder()
// initialize encoder
if(ok) {
init_status = FLAC__stream_encoder_init_stream(encoder, write_callback, NULL, NULL, NULL, NULL);
init_status = FLAC__stream_encoder_init_stream(encoder, write_callback, NULL, NULL, NULL, this);
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
fprintf(stderr, "ERROR: initializing encoder: %s\n", FLAC__StreamEncoderInitStatusString[init_status]);
ok = false;
@ -122,12 +169,13 @@ void FlacEncoder::initEncoder()
*/
}
/*
void progress_callback(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
{
(void)encoder, (void)client_data;
fprintf(stderr, "wrote %d bytes, %d, %u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
}
*/