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;
}
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) {
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<uint8_t>();
}
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();
}

View File

@ -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;

View File

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