diff --git a/decoder.cpp b/decoder.cpp index 8ea3656..e4c54b5 100644 --- a/decoder.cpp +++ b/decoder.cpp @@ -49,15 +49,21 @@ Decoder::~Decoder() delete[] glue; } -void Decoder::addFragment(intptr_t bufferPtr, uint32_t length) +void Decoder::addFragment(const emscripten::val& array) { + uint32_t length = array["length"].as(); + if (length < GLUE_LENGTH / 2) { std::cout << "An attempt to add fragment smaller then half of the glue buffer, ignoring"; return; } - std::cout << "Adding new fragment " << length << " bytes long" << std::endl; - uint8_t* buffer = (uint8_t(*))bufferPtr; - RawBuffer rb = {buffer, length, 0, 0}; + uint8_t* buffer = new uint8_t[length]; + + for (int i = 0; i < length; ++i) { + buffer[i] = array[std::to_string(i)].as(); + } + + RawBuffer rb = {buffer, length}; pending.push_back(rb); switch (state) { @@ -128,7 +134,7 @@ emscripten::val Decoder::decode(uint32_t count) for (int j = 0; j < samplesPerFrame; ++j) { for (int k = 0; k < channels; ++k) { float value = mad_f_todouble(synth->pcm.samples[k][j]); - chans[k].set(std::to_string(success * samplesPerFrame + j), emscripten::val(value)); + chans[k].set(std::to_string(i * samplesPerFrame + j), emscripten::val(value)); } } } @@ -293,7 +299,7 @@ void Decoder::switchToGlue() switchBuffer(glue, GLUE_LENGTH); std::cout << "Freeing the drained fragment" << std::endl; - free(pending[0].ptr); + delete[] pending[0].ptr; pending.pop_front(); } diff --git a/decoder.h b/decoder.h index 5a8bc9b..3f030e6 100644 --- a/decoder.h +++ b/decoder.h @@ -16,7 +16,7 @@ public: Decoder(); ~Decoder(); - void addFragment(intptr_t bufferPtr, uint32_t length); + void addFragment(const emscripten::val& array); emscripten::val decode(uint32_t count = UINT32_MAX); bool hasMore() const; uint32_t framesLeft(uint32_t max = UINT32_MAX); @@ -34,8 +34,6 @@ private: struct RawBuffer { uint8_t* ptr; uint32_t length; - uint32_t offset; - uint32_t gueard; }; State state; diff --git a/index.html b/index.html index ca3eaba..f6fd8b9 100644 --- a/index.html +++ b/index.html @@ -12,14 +12,13 @@