From df77690724bd7db2bd5909e58bc3820ba1447bed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B9=20=D0=93=D1=83=D0=B1=D0=B8=D1=87?= Date: Fri, 18 Jan 2019 04:50:48 +0300 Subject: [PATCH] clean up --- decoder.cpp | 95 +++++++++++++++++++++++++++-------------------------- decoder.h | 8 ++--- index.html | 2 +- 3 files changed, 54 insertions(+), 51 deletions(-) diff --git a/decoder.cpp b/decoder.cpp index 1a18e70..8ea3656 100644 --- a/decoder.cpp +++ b/decoder.cpp @@ -12,9 +12,9 @@ cachedNext(NULL), cachedThis(NULL), cachedError(MAD_ERROR_NONE), cached(false), -synth(), -stream(), -frame(), +synth(new mad_synth()), +stream(new mad_stream()), +frame(new mad_frame()), context(0), pending() { @@ -22,9 +22,9 @@ pending() glue[i] = 0; } - mad_frame_init(&frame); - mad_stream_init(&stream); - mad_synth_init(&synth); + mad_frame_init(frame); + mad_stream_init(stream); + mad_synth_init(synth); emscripten::val AudioContext = emscripten::val::global("AudioContext"); if (!AudioContext.as()) { @@ -38,9 +38,13 @@ Decoder::~Decoder() { context.call("close"); - mad_synth_finish(&synth); - mad_stream_finish(&stream); - mad_frame_finish(&frame); + mad_synth_finish(synth); + mad_stream_finish(stream); + mad_frame_finish(frame); + + delete synth; + delete stream; + delete frame; delete[] glue; } @@ -56,10 +60,9 @@ void Decoder::addFragment(intptr_t bufferPtr, uint32_t length) RawBuffer rb = {buffer, length, 0, 0}; pending.push_back(rb); - std::cout << "The state now is " << state << std::endl; switch (state) { case empty: - mad_stream_buffer(&stream, buffer, length); + mad_stream_buffer(stream, buffer, length); for (int i = 0; i < GLUE_LENGTH/2; ++i) { glue[i] = buffer[length - GLUE_LENGTH/2 + i]; @@ -106,32 +109,32 @@ emscripten::val Decoder::decode(uint32_t count) } for (int i = 0; success < available; ++i) { - int res = mad_frame_decode(&frame, &stream); + int res = mad_frame_decode(frame, stream); if (res == 0) { ++success; } else { - if (MAD_RECOVERABLE(stream.error)) { + if (MAD_RECOVERABLE(stream->error)) { - std::cout << "Unexpected error during the decoding process: " << mad_stream_errorstr(&stream) << std::endl; + std::cout << "Unexpected error during the decoding process: " << mad_stream_errorstr(stream) << std::endl; continue; } else { break; } } - mad_synth_frame(&synth, &frame); + mad_synth_frame(synth, frame); for (int j = 0; j < samplesPerFrame; ++j) { for (int k = 0; k < channels; ++k) { - float value = mad_f_todouble(synth.pcm.samples[k][j]); + float value = mad_f_todouble(synth->pcm.samples[k][j]); chans[k].set(std::to_string(success * samplesPerFrame + j), emscripten::val(value)); } } } cachedLength -= available; - std::cout << "Processed " << available << " frames, " << success << " successfully, last error " << mad_stream_errorstr(&stream) << std::endl; + std::cout << "Processed " << available << " frames, " << success << " successfully, last error " << mad_stream_errorstr(stream) << std::endl; if (cachedLength == 0) { cached = false; @@ -145,7 +148,7 @@ emscripten::val Decoder::decode(uint32_t count) bool Decoder::hasMore() const { if (pending.size() == 1) { - return stream.error != MAD_ERROR_BUFLEN; + return stream->error != MAD_ERROR_BUFLEN; } else { return true; } @@ -202,14 +205,13 @@ void Decoder::pullBuffer() if (cached == false) { std::cout << "Error in pullBuffer method!" << std::endl; } - stream.this_frame = cachedThis; - stream.next_frame = cachedNext; - stream.error = cachedError; + stream->this_frame = cachedThis; + stream->next_frame = cachedNext; + stream->error = cachedError; } void Decoder::changeBuffer() { - uint32_t left; switch (state) { case empty: std::cout << "Wrong state on switchBuffer method - empty, aborting" << std::endl; @@ -265,32 +267,30 @@ void Decoder::initializeProbe(mad_stream& probe) { mad_stream_init(&probe); - probe.buffer = stream.buffer; - probe.bufend = stream.bufend; - probe.skiplen = stream.skiplen; - probe.sync = stream.sync; - probe.freerate = stream.freerate; - probe.this_frame = stream.this_frame; - probe.next_frame = stream.next_frame; - probe.ptr.byte = stream.ptr.byte; - probe.ptr.cache = stream.ptr.cache; - probe.ptr.cache = stream.ptr.cache; - probe.anc_ptr.byte = stream.anc_ptr.byte; - probe.anc_ptr.cache = stream.anc_ptr.cache; - probe.anc_ptr.cache = stream.anc_ptr.cache; - probe.anc_bitlen = stream.anc_bitlen; + probe.buffer = stream->buffer; + probe.bufend = stream->bufend; + //probe.skiplen = stream->skiplen; + //probe.sync = stream->sync; + //probe.freerate = stream->freerate; + //probe.this_frame = stream->this_frame; + probe.next_frame = stream->next_frame; + //probe.ptr.byte = stream->ptr.byte; + //probe.ptr.cache = stream->ptr.cache; + //probe.ptr.cache = stream->ptr.cache; + //probe.anc_ptr.byte = stream->anc_ptr.byte; + //probe.anc_ptr.cache = stream->anc_ptr.cache; + //probe.anc_ptr.cache = stream->anc_ptr.cache; + //probe.anc_bitlen = stream->anc_bitlen; //probe.main_data = stream.main_data; //probe.md_len = stream.md_len; - probe.options = stream.options; - probe.error = stream.error; + //probe.options = stream->options; + //probe.error = stream->error; } void Decoder::switchToGlue() { std::cout << "Switching to glue" << std::endl; - switchBuffer(glue, GLUE_LENGTH); - stream.error = MAD_ERROR_NONE; std::cout << "Freeing the drained fragment" << std::endl; free(pending[0].ptr); @@ -300,11 +300,12 @@ void Decoder::switchToGlue() 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->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; + if (stream->next_frame != NULL) { + left = stream->bufend - stream->next_frame; } else { std::cout << "WARNING: not supposed to happen" << std::endl; } @@ -313,7 +314,9 @@ void Decoder::switchBuffer(uint8_t* bufferPtr, uint32_t length) 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; + mad_stream_buffer(stream, bufferPtr + GLUE_LENGTH / 2 - left, length - (GLUE_LENGTH / 2 - left)); + stream->error = MAD_ERROR_NONE; + + while (mad_header_decode(&frame->header, stream) != 0 && stream->error != MAD_ERROR_BUFLEN) {} } diff --git a/decoder.h b/decoder.h index f6e88e7..5a8bc9b 100644 --- a/decoder.h +++ b/decoder.h @@ -9,7 +9,7 @@ #include #include "mad.h" -#define GLUE_LENGTH 10240 +#define GLUE_LENGTH 6000 class Decoder { public: @@ -50,9 +50,9 @@ private: mad_error cachedError; bool cached; - mad_synth synth; - mad_stream stream; - mad_frame frame; + mad_synth* synth; + mad_stream* stream; + mad_frame* frame; emscripten::val context; std::deque pending; diff --git a/index.html b/index.html index a3a3ce4..ca3eaba 100644 --- a/index.html +++ b/index.html @@ -31,7 +31,7 @@ scheduleData(); - setTimeout(.bind(ctx), 1000); + setTimeout(ctx.resume.bind(ctx), 1000); } reader.readAsArrayBuffer(file);