mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-23 11:27:38 +02:00
clean up
This commit is contained in:
parent
1d22986619
commit
aba04a0675
10 changed files with 202 additions and 221 deletions
|
@ -22,65 +22,87 @@
|
|||
|
||||
using namespace std;
|
||||
|
||||
#define READSIZE 16384
|
||||
|
||||
static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
|
||||
size_t encodedSamples = 0;
|
||||
static msg::PcmChunk* encodedChunk = NULL;
|
||||
|
||||
|
||||
FlacEncoder::FlacEncoder(const msg::SampleFormat& format) : Encoder(format), encoder(NULL)
|
||||
FlacEncoder::FlacEncoder(const msg::SampleFormat& format) : Encoder(format), encoder_(NULL), pcmBufferSize_(0), encodedSamples_(0)
|
||||
{
|
||||
encodedChunk = new msg::PcmChunk();
|
||||
headerChunk = new msg::Header("flac");
|
||||
encodedChunk_ = new msg::PcmChunk();
|
||||
headerChunk_ = new msg::Header("flac");
|
||||
pcmBuffer_ = (FLAC__int32*)malloc(pcmBufferSize_ * sizeof(FLAC__int32));
|
||||
initEncoder();
|
||||
}
|
||||
|
||||
|
||||
FlacEncoder::~FlacEncoder()
|
||||
{
|
||||
if (encoder != NULL)
|
||||
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);
|
||||
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()
|
||||
{
|
||||
return headerChunk;
|
||||
delete encodedChunk_;
|
||||
free(pcmBuffer_);
|
||||
}
|
||||
|
||||
|
||||
double FlacEncoder::encode(msg::PcmChunk* chunk)
|
||||
{
|
||||
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);
|
||||
int samples = chunk->getSampleCount();
|
||||
int frames = chunk->getFrameCount();
|
||||
logO << "payload: " << chunk->payloadSize << "\tframes: " << frames << "\tsamples: " << samples << "\tduration: " << chunk->duration<chronos::msec>().count() << "\n";
|
||||
|
||||
double res = encodedSamples / ((double)sampleFormat.rate / 1000.);
|
||||
if (encodedSamples > 0)
|
||||
if (pcmBufferSize_ < samples)
|
||||
{
|
||||
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);
|
||||
pcmBufferSize_ = samples;
|
||||
pcmBuffer_ = (FLAC__int32*)realloc(pcmBuffer_, pcmBufferSize_ * sizeof(FLAC__int32));
|
||||
}
|
||||
|
||||
for(int i=0; i<samples; i++)
|
||||
{
|
||||
pcmBuffer_[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_, pcmBuffer_, frames);
|
||||
|
||||
double resMs = encodedSamples_ / ((double)sampleFormat_.rate / 1000.);
|
||||
if (encodedSamples_ > 0)
|
||||
{
|
||||
logD << "encoded: " << chunk->payloadSize << "\tframes: " << encodedSamples_ << "\tres: " << resMs << "\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();
|
||||
return resMs;//chunk->duration<chronos::msec>().count();
|
||||
}
|
||||
|
||||
|
||||
FLAC__StreamEncoderWriteStatus FlacEncoder::write_callback(const FLAC__StreamEncoder *encoder,
|
||||
const FLAC__byte buffer[],
|
||||
size_t bytes,
|
||||
unsigned samples,
|
||||
unsigned current_frame)
|
||||
{
|
||||
logD << "write_callback: " << bytes << ", " << samples << ", " << current_frame << "\n";
|
||||
if ((current_frame == 0) && (bytes > 0) && (samples == 0))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -91,29 +113,11 @@ FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder
|
|||
unsigned current_frame,
|
||||
void *client_data)
|
||||
{
|
||||
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;
|
||||
return flacEncoder->write_callback(encoder, buffer, bytes, samples, current_frame);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void FlacEncoder::initEncoder()
|
||||
{
|
||||
FLAC__bool ok = true;
|
||||
|
@ -121,93 +125,53 @@ void FlacEncoder::initEncoder()
|
|||
FLAC__StreamMetadata_VorbisComment_Entry entry;
|
||||
|
||||
// allocate the encoder
|
||||
if((encoder = FLAC__stream_encoder_new()) == NULL) {
|
||||
if ((encoder_ = FLAC__stream_encoder_new()) == NULL)
|
||||
{
|
||||
fprintf(stderr, "ERROR: allocating encoder\n");
|
||||
return;
|
||||
}
|
||||
|
||||
ok &= FLAC__stream_encoder_set_verify(encoder, true);
|
||||
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);
|
||||
ok &= FLAC__stream_encoder_set_verify(encoder_, true);
|
||||
// compression levels (0-8):
|
||||
// https://xiph.org/flac/api/group__flac__stream__encoder.html#gae49cf32f5256cb47eecd33779493ac85
|
||||
// latency:
|
||||
// 0-2: 1152 frames, ~26.1224ms
|
||||
// 3-8: 4096 frames, ~92.8798ms
|
||||
ok &= FLAC__stream_encoder_set_compression_level(encoder_, 2);
|
||||
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);
|
||||
|
||||
// now add some metadata; we'll add some tags and a padding block
|
||||
if(ok) {
|
||||
if(
|
||||
(metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
|
||||
(metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
|
||||
// there are many tag (vorbiscomment) functions but these are convenient for this particular use:
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", "Some Artist") ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, false) ||
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "YEAR", "1984") ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, false)
|
||||
) {
|
||||
if (ok)
|
||||
{
|
||||
if (
|
||||
(metadata_[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
|
||||
(metadata_[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
|
||||
// there are many tag (vorbiscomment) functions but these are convenient for this particular use:
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TITLE", "SnapStream") ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata_[0], entry, false) ||
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "VERSION", VERSION) ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata_[0], entry, false)
|
||||
)
|
||||
{
|
||||
fprintf(stderr, "ERROR: out of memory or tag error\n");
|
||||
ok = false;
|
||||
}
|
||||
|
||||
metadata[1]->length = 1234; // set the padding length
|
||||
|
||||
ok = FLAC__stream_encoder_set_metadata(encoder, metadata, 2);
|
||||
metadata_[1]->length = 1234; // set the padding length
|
||||
ok = FLAC__stream_encoder_set_metadata(encoder_, metadata_, 2);
|
||||
}
|
||||
|
||||
// initialize encoder
|
||||
if(ok) {
|
||||
init_status = FLAC__stream_encoder_init_stream(encoder, write_callback, NULL, NULL, NULL, this);
|
||||
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
|
||||
if (ok)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
/*
|
||||
// read blocks of samples from WAVE file and feed to encoder
|
||||
if(ok) {
|
||||
size_t left = (size_t)total_samples;
|
||||
while(ok && left) {
|
||||
size_t need = (left>READSIZE? (size_t)READSIZE : (size_t)left);
|
||||
if(fread(buffer, channels*(bps/8), need, fin) != need) {
|
||||
fprintf(stderr, "ERROR: reading from WAVE file\n");
|
||||
ok = false;
|
||||
}
|
||||
else {
|
||||
// convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC
|
||||
size_t i;
|
||||
for(i = 0; i < need*channels; i++) {
|
||||
// inefficient but simple and works on big- or little-endian machines
|
||||
pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buffer[2*i+1] << 8) | (FLAC__int16)buffer[2*i]);
|
||||
}
|
||||
// feed samples to encoder
|
||||
ok = FLAC__stream_encoder_process_interleaved(encoder, pcm, need);
|
||||
}
|
||||
left -= need;
|
||||
}
|
||||
}
|
||||
|
||||
ok &= FLAC__stream_encoder_finish(encoder);
|
||||
|
||||
fprintf(stderr, "encoding: %s\n", ok? "succeeded" : "FAILED");
|
||||
fprintf(stderr, " state: %s\n", FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder)]);
|
||||
|
||||
// now that encoding is finished, the metadata can be freed
|
||||
FLAC__metadata_object_delete(metadata[0]);
|
||||
FLAC__metadata_object_delete(metadata[1]);
|
||||
|
||||
FLAC__stream_encoder_delete(encoder);
|
||||
fclose(fin);
|
||||
|
||||
return 0;
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
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);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue