still no good, but a bit cleaned up
This commit is contained in:
parent
1f40231b9b
commit
db0b829fbd
113
decoder.cpp
113
decoder.cpp
@ -8,7 +8,9 @@ channels(0),
|
|||||||
cachedLength(0),
|
cachedLength(0),
|
||||||
samplesPerFrame(0),
|
samplesPerFrame(0),
|
||||||
glue(new uint8_t[GLUE_LENGTH]),
|
glue(new uint8_t[GLUE_LENGTH]),
|
||||||
lastSync(-1),
|
cachedNext(NULL),
|
||||||
|
cachedThis(NULL),
|
||||||
|
cachedError(MAD_ERROR_NONE),
|
||||||
cached(false),
|
cached(false),
|
||||||
synth(),
|
synth(),
|
||||||
stream(),
|
stream(),
|
||||||
@ -107,7 +109,6 @@ emscripten::val Decoder::decode(uint32_t count)
|
|||||||
int res = mad_frame_decode(&frame, &stream);
|
int res = mad_frame_decode(&frame, &stream);
|
||||||
|
|
||||||
if (res == 0) {
|
if (res == 0) {
|
||||||
lastSync = stream.bufend - stream.this_frame;
|
|
||||||
++success;
|
++success;
|
||||||
} else {
|
} else {
|
||||||
if (MAD_RECOVERABLE(stream.error)) {
|
if (MAD_RECOVERABLE(stream.error)) {
|
||||||
@ -153,7 +154,6 @@ bool Decoder::hasMore() const
|
|||||||
uint32_t Decoder::framesLeft(uint32_t max)
|
uint32_t Decoder::framesLeft(uint32_t max)
|
||||||
{
|
{
|
||||||
if (state == empty || state == onGlueHalf) {
|
if (state == empty || state == onGlueHalf) {
|
||||||
cached = true;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,13 +178,16 @@ uint32_t Decoder::framesLeft(uint32_t max)
|
|||||||
++cachedLength;
|
++cachedLength;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
std::cout << "framesLeft::" << mad_stream_errorstr(&probe) << std::endl;
|
std::cout << "framesLeft error: " << mad_stream_errorstr(&probe) << std::endl;
|
||||||
if (!MAD_RECOVERABLE(probe.error)) {
|
if (!MAD_RECOVERABLE(probe.error)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cachedNext = probe.next_frame;
|
||||||
|
cachedThis = probe.this_frame;
|
||||||
|
cachedError = probe.error;
|
||||||
mad_header_finish(&ph);
|
mad_header_finish(&ph);
|
||||||
mad_stream_finish(&probe);
|
mad_stream_finish(&probe);
|
||||||
std::cout << cachedLength << " frames are available for decoding" << std::endl;
|
std::cout << cachedLength << " frames are available for decoding" << std::endl;
|
||||||
@ -196,13 +199,12 @@ uint32_t Decoder::framesLeft(uint32_t max)
|
|||||||
|
|
||||||
void Decoder::pullBuffer()
|
void Decoder::pullBuffer()
|
||||||
{
|
{
|
||||||
mad_header dummy;
|
if (cached == false) {
|
||||||
mad_header_init(&dummy);
|
std::cout << "Error in pullBuffer method!" << std::endl;
|
||||||
while(stream.error == 0 || MAD_RECOVERABLE(stream.error)) {
|
|
||||||
mad_header_decode(&dummy, &stream);
|
|
||||||
std::cout << "pullBuffer:: " << mad_stream_errorstr(&stream) << std::endl;
|
|
||||||
}
|
}
|
||||||
mad_header_finish(&dummy);
|
stream.this_frame = cachedThis;
|
||||||
|
stream.next_frame = cachedNext;
|
||||||
|
stream.error = cachedError;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Decoder::changeBuffer()
|
void Decoder::changeBuffer()
|
||||||
@ -224,39 +226,8 @@ void Decoder::changeBuffer()
|
|||||||
break;
|
break;
|
||||||
case onGlueFull:
|
case onGlueFull:
|
||||||
std::cout << "Having another fragment " << pending[0].length << " bytes long" << std::endl;
|
std::cout << "Having another fragment " << pending[0].length << " bytes long" << std::endl;
|
||||||
left = (uint64_t)stream.bufend - (uint64_t)stream.next_frame;
|
|
||||||
// std::cout << "MD_LENGTH_BEFORE: " << stream.anc_bitlen << std::endl;
|
|
||||||
mad_stream_buffer(&stream, pending[0].ptr, pending[0].length);
|
|
||||||
|
|
||||||
|
switchBuffer(pending[0].ptr, pending[0].length);
|
||||||
//mad_stream_skip(&stream, left);
|
|
||||||
stream.error = MAD_ERROR_NONE;
|
|
||||||
if (lastSync != -1) {
|
|
||||||
if (lastSync > GLUE_LENGTH / 2) {
|
|
||||||
std::cout << "Error: bytes to read in the buffer are more then glue buffer can fit LASTSYNC (" << lastSync << ")" << std::endl;
|
|
||||||
throw 1;
|
|
||||||
}
|
|
||||||
uint64_t target = (uint64_t)stream.buffer + GLUE_LENGTH / 2 - lastSync;
|
|
||||||
while ((uint64_t)(stream.this_frame) < target) {
|
|
||||||
// std::cout << "pulling" << std::endl;
|
|
||||||
mad_frame_decode(&frame, &stream);
|
|
||||||
}
|
|
||||||
stream.error = MAD_ERROR_NONE;
|
|
||||||
|
|
||||||
// stream.this_frame = stream.buffer + GLUE_LENGTH / 2 - lastSync;
|
|
||||||
// stream.next_frame = stream.buffer + GLUE_LENGTH / 2 - left;
|
|
||||||
// stream.sync = 1;
|
|
||||||
|
|
||||||
lastSync = -1;
|
|
||||||
|
|
||||||
// mad_bitptr next_frame;
|
|
||||||
// mad_bit_init(&next_frame, stream.next_frame);
|
|
||||||
// stream.anc_ptr = stream.ptr;
|
|
||||||
// stream.anc_bitlen = mad_bit_length(&stream.ptr, &next_frame);
|
|
||||||
// mad_bit_finish(&next_frame);
|
|
||||||
//
|
|
||||||
// std::cout << ", MD_LENGTH_AFTER: " << stream.anc_bitlen << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < GLUE_LENGTH/2; ++i) {
|
for (int i = 0; i < GLUE_LENGTH/2; ++i) {
|
||||||
glue[i] = pending[0].ptr[pending[0].length - GLUE_LENGTH/2 + i];
|
glue[i] = pending[0].ptr[pending[0].length - GLUE_LENGTH/2 + i];
|
||||||
@ -317,46 +288,32 @@ void Decoder::initializeProbe(mad_stream& probe)
|
|||||||
void Decoder::switchToGlue()
|
void Decoder::switchToGlue()
|
||||||
{
|
{
|
||||||
std::cout << "Switching to glue" << std::endl;
|
std::cout << "Switching to glue" << std::endl;
|
||||||
uint32_t left = (uint64_t)stream.bufend - (uint64_t)stream.next_frame;
|
|
||||||
if (left > GLUE_LENGTH / 2) {
|
|
||||||
std::cout << "Error: bytes to read in the buffer are more then glue buffer can fit (" << left << ")" << std::endl;
|
|
||||||
throw 1;
|
|
||||||
}
|
|
||||||
// std::cout << "MD_LENGTH_BEFORE: " << stream.anc_bitlen << std::endl;
|
|
||||||
mad_stream_buffer(&stream, glue, GLUE_LENGTH);
|
|
||||||
|
|
||||||
|
switchBuffer(glue, GLUE_LENGTH);
|
||||||
if (lastSync != -1) {
|
|
||||||
if (lastSync > GLUE_LENGTH / 2) {
|
|
||||||
std::cout << "Error: bytes to read in the buffer are more then glue buffer can fit LASTSYNC (" << lastSync << ")" << std::endl;
|
|
||||||
throw 1;
|
|
||||||
}
|
|
||||||
uint64_t target = (uint64_t)stream.bufend - GLUE_LENGTH / 2 - lastSync;
|
|
||||||
while ((uint64_t)(stream.this_frame) < target) {
|
|
||||||
// std::cout << "pulling" << std::endl;
|
|
||||||
mad_frame_decode(&frame, &stream);
|
|
||||||
}
|
|
||||||
stream.error = MAD_ERROR_NONE;
|
stream.error = MAD_ERROR_NONE;
|
||||||
// stream.this_frame = stream.bufend - GLUE_LENGTH / 2 - lastSync;
|
|
||||||
// stream.next_frame = stream.bufend - GLUE_LENGTH / 2 - left;
|
|
||||||
// stream.sync = 1;
|
|
||||||
|
|
||||||
// mad_bitptr next_frame;
|
|
||||||
// mad_bit_init(&next_frame, stream.next_frame);
|
|
||||||
// stream.anc_ptr = stream.ptr;
|
|
||||||
// stream.anc_bitlen = mad_bit_length(&stream.ptr, &next_frame);
|
|
||||||
// mad_bit_finish(&next_frame);
|
|
||||||
//
|
|
||||||
// std::cout << ", MD_LENGTH_AFTER: " << stream.anc_bitlen << std::endl;
|
|
||||||
|
|
||||||
lastSync = -1;
|
|
||||||
}
|
|
||||||
//mad_stream_skip(&stream, left);
|
|
||||||
stream.error = MAD_ERROR_NONE;
|
|
||||||
|
|
||||||
//std::cout << "SKIPLEN (" << stream.skiplen << ")" << std::endl;
|
|
||||||
|
|
||||||
std::cout << "Freeing the drained fragment" << std::endl;
|
std::cout << "Freeing the drained fragment" << std::endl;
|
||||||
free(pending[0].ptr);
|
free(pending[0].ptr);
|
||||||
pending.pop_front();
|
pending.pop_front();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Decoder::switchBuffer(uint8_t* bufferPtr, uint32_t length)
|
||||||
|
{
|
||||||
|
uint32_t left;
|
||||||
|
if (stream.error != MAD_ERROR_BUFLEN) {
|
||||||
|
std::cout << "WARNING: Switching buffers while the previous one is not drained, error: " << mad_stream_errorstr(&stream) << std::endl;
|
||||||
|
}
|
||||||
|
if (stream.next_frame != NULL) {
|
||||||
|
left = stream.bufend - stream.next_frame;
|
||||||
|
} else {
|
||||||
|
std::cout << "WARNING: not supposed to happen" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (left > GLUE_LENGTH / 2) {
|
||||||
|
std::cout << "Error: bytes to read in the buffer are more then glue buffer can fit (" << left << ")" << std::endl;
|
||||||
|
throw 1;
|
||||||
|
}
|
||||||
|
mad_stream_buffer(&stream, bufferPtr + GLUE_LENGTH / 2 - left, length - (GLUE_LENGTH / 2 - left));
|
||||||
|
|
||||||
|
stream.error = MAD_ERROR_NONE;
|
||||||
|
}
|
||||||
|
@ -45,7 +45,9 @@ private:
|
|||||||
uint32_t cachedLength;
|
uint32_t cachedLength;
|
||||||
uint16_t samplesPerFrame;
|
uint16_t samplesPerFrame;
|
||||||
uint8_t* glue;
|
uint8_t* glue;
|
||||||
int32_t lastSync;
|
uint8_t const* cachedNext;
|
||||||
|
uint8_t const* cachedThis;
|
||||||
|
mad_error cachedError;
|
||||||
bool cached;
|
bool cached;
|
||||||
|
|
||||||
mad_synth synth;
|
mad_synth synth;
|
||||||
@ -61,6 +63,7 @@ private:
|
|||||||
void prepareNextBuffer();
|
void prepareNextBuffer();
|
||||||
void initializeProbe(mad_stream& probe);
|
void initializeProbe(mad_stream& probe);
|
||||||
void switchToGlue();
|
void switchToGlue();
|
||||||
|
void switchBuffer(uint8_t* bufferPtr, uint32_t length);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DECODER_H
|
#endif // DECODER_H
|
||||||
|
@ -30,7 +30,8 @@
|
|||||||
createFileFragments(reader.result);
|
createFileFragments(reader.result);
|
||||||
|
|
||||||
scheduleData();
|
scheduleData();
|
||||||
ctx.resume();
|
|
||||||
|
setTimeout(.bind(ctx), 1000);
|
||||||
}
|
}
|
||||||
reader.readAsArrayBuffer(file);
|
reader.readAsArrayBuffer(file);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user