changed for decoder to work with Uint8Array in addFragment method

This commit is contained in:
Blue 2019-01-24 19:00:15 +03:00
parent df77690724
commit 7de90304e4
3 changed files with 19 additions and 27 deletions

View File

@ -49,15 +49,21 @@ Decoder::~Decoder()
delete[] glue; delete[] glue;
} }
void Decoder::addFragment(intptr_t bufferPtr, uint32_t length) void Decoder::addFragment(const emscripten::val& array)
{ {
uint32_t length = array["length"].as<uint32_t>();
if (length < GLUE_LENGTH / 2) { if (length < GLUE_LENGTH / 2) {
std::cout << "An attempt to add fragment smaller then half of the glue buffer, ignoring"; std::cout << "An attempt to add fragment smaller then half of the glue buffer, ignoring";
return; return;
} }
std::cout << "Adding new fragment " << length << " bytes long" << std::endl; uint8_t* buffer = new uint8_t[length];
uint8_t* buffer = (uint8_t(*))bufferPtr;
RawBuffer rb = {buffer, length, 0, 0}; for (int i = 0; i < length; ++i) {
buffer[i] = array[std::to_string(i)].as<uint8_t>();
}
RawBuffer rb = {buffer, length};
pending.push_back(rb); pending.push_back(rb);
switch (state) { switch (state) {
@ -128,7 +134,7 @@ emscripten::val Decoder::decode(uint32_t count)
for (int j = 0; j < samplesPerFrame; ++j) { for (int j = 0; j < samplesPerFrame; ++j) {
for (int k = 0; k < channels; ++k) { 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)); chans[k].set(std::to_string(i * samplesPerFrame + j), emscripten::val(value));
} }
} }
} }
@ -293,7 +299,7 @@ void Decoder::switchToGlue()
switchBuffer(glue, GLUE_LENGTH); switchBuffer(glue, GLUE_LENGTH);
std::cout << "Freeing the drained fragment" << std::endl; std::cout << "Freeing the drained fragment" << std::endl;
free(pending[0].ptr); delete[] pending[0].ptr;
pending.pop_front(); pending.pop_front();
} }

View File

@ -16,7 +16,7 @@ public:
Decoder(); Decoder();
~Decoder(); ~Decoder();
void addFragment(intptr_t bufferPtr, uint32_t length); void addFragment(const emscripten::val& array);
emscripten::val decode(uint32_t count = UINT32_MAX); emscripten::val decode(uint32_t count = UINT32_MAX);
bool hasMore() const; bool hasMore() const;
uint32_t framesLeft(uint32_t max = UINT32_MAX); uint32_t framesLeft(uint32_t max = UINT32_MAX);
@ -34,8 +34,6 @@ private:
struct RawBuffer { struct RawBuffer {
uint8_t* ptr; uint8_t* ptr;
uint32_t length; uint32_t length;
uint32_t offset;
uint32_t gueard;
}; };
State state; State state;

View File

@ -12,14 +12,13 @@
<script src="wrapper.js"></script> <script src="wrapper.js"></script>
<input type="file" id="file" name="file" /> <input type="file" id="file" name="file" />
<script> <script>
var minPortion = 20480; var minPortion = 4096;
var maxPortion = 40960; var maxPortion = 40960;
var time = 0; var time = 0;
var ctx = new AudioContext(); var ctx = new AudioContext();
ctx.suspend(); ctx.suspend();
var decoder; var decoder;
var fragments = []; var fragments = [];
var noize = [];
var decoding = false; var decoding = false;
function onChange(e) { function onChange(e) {
var file = e.target.files[0]; var file = e.target.files[0];
@ -51,21 +50,20 @@
setTimeout(schedule, Math.floor(audio.duration * 200)); setTimeout(schedule, Math.floor(audio.duration * 200));
decoding = true; decoding = true;
} else { } else {
//decoder.delete(); decoder.delete();
} }
} else { } else {
//decoder.delete(); decoder.delete();
} }
} }
function scheduleData() { function scheduleData() {
if (fragments.length) { if (fragments.length) {
var frag = fragments.shift(); var frag = fragments.shift();
decoder.addFragment(frag);
decoder.addFragment(frag.ptr, frag.length);
if (fragments.length) { if (fragments.length) {
setTimeout(scheduleData, 50); setTimeout(scheduleData, 100);
} }
if (!decoding) { if (!decoding) {
@ -80,20 +78,10 @@
while (shoved !== src.length) { while (shoved !== src.length) {
var ps = Math.min(Math.floor(Math.random() * (maxPortion - minPortion) + minPortion), src.length - shoved); var ps = Math.min(Math.floor(Math.random() * (maxPortion - minPortion) + minPortion), src.length - shoved);
var ptr = Module._malloc(ps);
var arr = new Uint8Array(Module.HEAPU8.buffer, ptr, ps);
var portion = new Uint8Array(buffer, shoved, ps) var portion = new Uint8Array(buffer, shoved, ps)
arr.set(portion, 0);
shoved += ps; shoved += ps;
fragments.push(portion);
fragments.push({
ptr: ptr,
length: ps
});
noize.push(Module._malloc(256));
} }
} }