@ekx/auph
Version:
[](https://github.com/eliasku/auph/actions/workflows/build.yml) [](https://www.npmjs.com/package/auph) [ {
function BufferObj(h, s, b,
// HEAP pointer to C-callback
_f,
// HEAP pointer to C-userdata
_u) {
if (_f === void 0) { _f = 0; }
if (_u === void 0) { _u = 0; }
this.h = h;
this.s = s;
this.b = b;
this._f = _f;
this._u = _u;
}
return BufferObj;
}());
export { BufferObj };
export var buffers = [null];
var buffersMaxCount = 128;
export function getNextBufferObj() {
var next = len(buffers);
for (var i = 1; i < next; ++i) {
var buffer = buffers[i];
if (buffer.s === 0) {
return buffer.h;
}
}
if (next < buffersMaxCount) {
var b = new BufferObj(next | 536870912 /* Buffer */, 0, null);
add(buffers, b);
return b.h;
}
return 0;
}
export function _bufferDestroy(obj) {
// TODO:
// if ((obj.s & Flag.Stream) !== 0 && obj.data) {
// URL.revokeObjectURL(obj.b as string);
// }
obj.h = nextHandle(obj.h);
obj.s = 0;
obj.b = null;
}
export function _getBufferObj(buffer) {
var obj = buffers[buffer & iMask];
if (obj && obj.h === buffer) {
return obj;
}
return null;
}
function _decodeAudioData(ctx, obj, buffer) {
var success = function (audioBuffer) {
obj.s |= 2 /* Loaded */;
obj.b = audioBuffer;
};
var fail = function (err) {
error(18 /* BufferDecodeError */, err);
_bufferDestroy(obj);
};
// TODO: maybe callbacks will be deprecated?
ctx.decodeAudioData(buffer, success, fail);
}
export function _bufferMemory(obj, ctx, data, flags) {
obj.s |= 1 /* Active */;
var buffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
// TODO: streaming
// if (flags & Flag.Stream) {
// obj.s |= Flag.Stream;
_decodeAudioData(ctx, obj, buffer);
}
export function _buffer_set_callback(obj, ctx, f, u) {
obj.s |= 1 /* Active */ | 2 /* Loaded */ | 16 /* Callback */;
obj.b = new AudioBuffer({ length: 8192, sampleRate: 44100, numberOfChannels: 2 });
obj._f = f;
obj._u = u;
}
export function _bufferLoad(obj, ctx, filepath, flags) {
obj.s |= 1 /* Active */;
// TODO: streaming
//if (flags & Flag.Stream) {
//obj.s |= Flag.Stream;
fetch(new Request(filepath))
.then(function (response) { return response.arrayBuffer(); })
.then(function (buffer) { return _decodeAudioData(ctx, obj, buffer); })
.catch(function (reason) {
error(19 /* BufferLoadError */, reason);
_bufferDestroy(obj);
});
}
//# sourceMappingURL=Buffer.js.map