2019-01-14 02:19:37 +00:00
|
|
|
#include "decoder.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
Decoder::Decoder():
|
|
|
|
sampleRate(0),
|
|
|
|
channels(0),
|
|
|
|
cachedLength(0),
|
|
|
|
samplesPerFrame(0),
|
2019-01-22 21:36:50 +00:00
|
|
|
buffer(new uint8_t[BUFFER_LENGTH]),
|
2019-01-14 02:19:37 +00:00
|
|
|
cached(false),
|
2019-01-18 01:50:48 +00:00
|
|
|
synth(new mad_synth()),
|
|
|
|
stream(new mad_stream()),
|
|
|
|
frame(new mad_frame()),
|
2019-01-22 21:36:50 +00:00
|
|
|
context(0)
|
2019-01-14 02:19:37 +00:00
|
|
|
{
|
2019-01-22 21:36:50 +00:00
|
|
|
for (int i = 0; i < BUFFER_LENGTH; ++i) {
|
|
|
|
buffer[i] = 0;
|
2019-01-14 02:19:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 01:50:48 +00:00
|
|
|
mad_frame_init(frame);
|
|
|
|
mad_stream_init(stream);
|
|
|
|
mad_synth_init(synth);
|
2019-01-14 02:19:37 +00:00
|
|
|
|
2019-01-22 21:36:50 +00:00
|
|
|
mad_stream_buffer(stream, buffer, 0);
|
|
|
|
|
2019-01-14 02:19:37 +00:00
|
|
|
emscripten::val AudioContext = emscripten::val::global("AudioContext");
|
|
|
|
if (!AudioContext.as<bool>()) {
|
|
|
|
AudioContext = emscripten::val::global("webkitAudioContext");
|
|
|
|
}
|
|
|
|
|
|
|
|
context = AudioContext.new_();
|
|
|
|
}
|
|
|
|
|
|
|
|
Decoder::~Decoder()
|
|
|
|
{
|
|
|
|
context.call<void>("close");
|
|
|
|
|
2019-01-18 01:50:48 +00:00
|
|
|
mad_synth_finish(synth);
|
|
|
|
mad_stream_finish(stream);
|
|
|
|
mad_frame_finish(frame);
|
|
|
|
|
|
|
|
delete synth;
|
|
|
|
delete stream;
|
|
|
|
delete frame;
|
2019-01-14 02:19:37 +00:00
|
|
|
|
2019-01-22 21:36:50 +00:00
|
|
|
delete[] buffer;
|
2019-01-14 02:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Decoder::addFragment(intptr_t bufferPtr, uint32_t length)
|
|
|
|
{
|
|
|
|
std::cout << "Adding new fragment " << length << " bytes long" << std::endl;
|
2019-01-22 21:36:50 +00:00
|
|
|
uint8_t* bf = (uint8_t(*))bufferPtr;
|
|
|
|
uint64_t ol = stream->bufend - stream->buffer;
|
2019-01-14 02:19:37 +00:00
|
|
|
|
2019-01-22 21:36:50 +00:00
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
buffer[ol + i] = bf[i];
|
2019-01-14 02:19:37 +00:00
|
|
|
}
|
2019-01-22 21:36:50 +00:00
|
|
|
//free(bf);
|
|
|
|
|
|
|
|
stream->bufend = stream->bufend + length;
|
|
|
|
cached = false;
|
|
|
|
framesLeft();
|
2019-01-14 02:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emscripten::val Decoder::decode(uint32_t count)
|
|
|
|
{
|
|
|
|
emscripten::val ret = emscripten::val::undefined();
|
|
|
|
|
|
|
|
int available = framesLeft(count);
|
|
|
|
int success = 0;
|
|
|
|
if (available > 0) {
|
|
|
|
ret = context.call<emscripten::val>("createBuffer", channels, available * samplesPerFrame, sampleRate);
|
|
|
|
|
|
|
|
std::vector<emscripten::val> chans(channels, emscripten::val::undefined());
|
|
|
|
for (int i = 0; i < channels; ++i) {
|
|
|
|
chans[i] = ret.call<emscripten::val>("getChannelData", i);
|
|
|
|
}
|
|
|
|
|
2019-01-22 21:36:50 +00:00
|
|
|
for (int i = 0; i < available; ++i) {
|
2019-01-18 01:50:48 +00:00
|
|
|
int res = mad_frame_decode(frame, stream);
|
2019-01-14 02:19:37 +00:00
|
|
|
|
|
|
|
if (res == 0) {
|
|
|
|
++success;
|
|
|
|
} else {
|
2019-01-18 01:50:48 +00:00
|
|
|
if (MAD_RECOVERABLE(stream->error)) {
|
2019-01-14 02:19:37 +00:00
|
|
|
|
2019-01-18 01:50:48 +00:00
|
|
|
std::cout << "Unexpected error during the decoding process: " << mad_stream_errorstr(stream) << std::endl;
|
2019-01-14 02:19:37 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 01:50:48 +00:00
|
|
|
mad_synth_frame(synth, frame);
|
2019-01-14 02:19:37 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < samplesPerFrame; ++j) {
|
|
|
|
for (int k = 0; k < channels; ++k) {
|
2019-01-18 01:50:48 +00:00
|
|
|
float value = mad_f_todouble(synth->pcm.samples[k][j]);
|
2019-01-22 21:59:27 +00:00
|
|
|
chans[k].set(std::to_string(i * samplesPerFrame + j), emscripten::val(value));
|
2019-01-14 02:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedLength -= available;
|
2019-01-18 01:50:48 +00:00
|
|
|
std::cout << "Processed " << available << " frames, " << success << " successfully, last error " << mad_stream_errorstr(stream) << std::endl;
|
2019-01-14 02:19:37 +00:00
|
|
|
|
|
|
|
if (cachedLength == 0) {
|
2019-01-22 21:36:50 +00:00
|
|
|
framesLeft();
|
2019-01-14 02:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Decoder::hasMore() const
|
|
|
|
{
|
2019-01-22 21:36:50 +00:00
|
|
|
return stream->bufend != stream->buffer && stream->error != MAD_ERROR_BUFLEN;
|
2019-01-14 02:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Decoder::framesLeft(uint32_t max)
|
|
|
|
{
|
2019-01-22 21:36:50 +00:00
|
|
|
if (!hasMore()) {
|
2019-01-14 02:19:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cached == false) {
|
2019-01-22 21:36:50 +00:00
|
|
|
bool found = false;
|
2019-01-14 02:19:37 +00:00
|
|
|
mad_stream probe;
|
|
|
|
mad_header ph;
|
|
|
|
initializeProbe(probe);
|
|
|
|
mad_header_init(&ph);
|
|
|
|
|
|
|
|
while (cachedLength < max) {
|
|
|
|
if (mad_header_decode(&ph, &probe) == 0) {
|
2019-01-22 21:36:50 +00:00
|
|
|
found = true;
|
2019-01-14 02:19:37 +00:00
|
|
|
if (sampleRate == 0) {
|
|
|
|
sampleRate = ph.samplerate;
|
|
|
|
channels = MAD_NCHANNELS(&ph);
|
|
|
|
samplesPerFrame = MAD_NSBSAMPLES(&ph) * 32; //not sure why 32, it's in libmad source
|
|
|
|
} else {
|
|
|
|
if (sampleRate != ph.samplerate || channels != MAD_NCHANNELS(&ph) || samplesPerFrame != MAD_NSBSAMPLES(&ph) * 32) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (probe.next_frame > probe.this_frame) {
|
|
|
|
++cachedLength;
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-22 21:36:50 +00:00
|
|
|
if (!found) {
|
|
|
|
stream->next_frame = probe.next_frame;
|
|
|
|
}
|
2019-01-14 14:50:41 +00:00
|
|
|
std::cout << "framesLeft error: " << mad_stream_errorstr(&probe) << std::endl;
|
2019-01-14 02:19:37 +00:00
|
|
|
if (!MAD_RECOVERABLE(probe.error)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mad_header_finish(&ph);
|
|
|
|
mad_stream_finish(&probe);
|
|
|
|
std::cout << cachedLength << " frames are available for decoding" << std::endl;
|
|
|
|
cached = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::min(cachedLength, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Decoder::initializeProbe(mad_stream& probe)
|
|
|
|
{
|
|
|
|
mad_stream_init(&probe);
|
|
|
|
|
2019-01-18 01:50:48 +00:00
|
|
|
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;
|
2019-01-14 02:19:37 +00:00
|
|
|
//probe.main_data = stream.main_data;
|
|
|
|
//probe.md_len = stream.md_len;
|
2019-01-18 01:50:48 +00:00
|
|
|
//probe.options = stream->options;
|
|
|
|
//probe.error = stream->error;
|
2019-01-14 02:19:37 +00:00
|
|
|
}
|