tav-media
Version:
Cross platform media editing framework
141 lines (140 loc) • 5.85 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { tav } from '../tav';
export class WebAudioReader {
constructor(filePath, bytesOffset, length) {
this.bitDepth = 16;
this.contentTime = 0;
this.contentSample = 0;
this.released = false;
this.type = 0;
if (filePath === '') {
const module = tav;
const arrayBuffer = new Uint8Array(module.HEAPU8.buffer, bytesOffset, length);
const newArrayBuffer = arrayBuffer.slice(0, length);
this.byteBuffer = newArrayBuffer.buffer;
}
else {
WebAudioReader.preload(filePath);
this.byteBuffer = WebAudioReader.audios[filePath];
}
this.outputSampleCount = 2048;
this.audioPromise = new Promise((resolve) => {
if (filePath !== '' && WebAudioReader.audioBuffers[filePath]) {
const audioBuffer = WebAudioReader.audioBuffers[filePath];
this.audioBuffer = audioBuffer;
this.sampleRate = audioBuffer.sampleRate;
this.channelCount = audioBuffer.numberOfChannels;
this.audioLength = audioBuffer.length;
resolve(this);
}
else {
const audioCtx = new AudioContext();
audioCtx.decodeAudioData(this.byteBuffer).then((audioBuffer) => {
this.audioBuffer = audioBuffer;
this.sampleRate = audioBuffer.sampleRate;
this.channelCount = audioBuffer.numberOfChannels;
this.audioLength = audioBuffer.length;
if (filePath !== '') {
WebAudioReader.audioBuffers[filePath] = audioBuffer;
}
resolve(this);
});
}
});
}
static preload(path) {
return __awaiter(this, void 0, void 0, function* () {
if (this.audios[path]) {
return Promise.resolve(this.audios[path]);
}
let url = path;
if (!(path.startsWith('http') || path.startsWith('blob:'))) {
url = WebAudioReader.baseUrl + path;
}
const response = yield fetch(url);
const buffer = yield response.arrayBuffer();
this.audios[path] = buffer;
const audioCtx = new AudioContext();
const audioBuffer = yield audioCtx.decodeAudioData(buffer);
WebAudioReader.audioBuffers[path] = audioBuffer;
});
}
static MakeFromPath(path) {
return new WebAudioReader(path, 0, 0);
}
static MakeFromBytes(bytesOffset, length) {
return new WebAudioReader('', bytesOffset, length);
}
decodeAudio() {
return this.audioPromise;
}
seekTo(contentTime) {
const targetTime = contentTime / 1000000;
this.contentTime = targetTime;
this.contentSample = Math.ceil(this.contentTime * this.sampleRate);
if (this.contentSample > this.audioLength) {
this.contentTime = 0;
this.contentSample = 0;
}
}
readNextSample() {
const module = tav;
const outData = [];
const factor = 2 ** (this.bitDepth - 1);
let sampleCount = 0;
if (this.audioBuffer) {
sampleCount = this.outputSampleCount;
for (let channel = 0; channel < this.channelCount; channel++) {
const channelData = new Float32Array(this.audioBuffer.getChannelData(channel));
sampleCount = Math.min(channelData.length - this.contentSample, this.outputSampleCount);
for (let dataIndex = 0; dataIndex < sampleCount; dataIndex++) {
const value = Math.floor(channelData[this.contentSample + dataIndex] * factor + 0.5);
outData[this.channelCount * dataIndex + channel] = value;
}
}
this.contentSample += sampleCount;
this.contentTime += 1.0 * sampleCount / this.sampleRate;
}
const audioBuffer = new Int16Array(outData);
const audioUnit8Buffer = new Uint8Array(audioBuffer.buffer);
const numBytes = audioUnit8Buffer.byteLength * Uint8Array.BYTES_PER_ELEMENT;
const dataPtr = module._malloc(numBytes);
if (audioBuffer.length > 0) {
this.lastBufferData = dataPtr;
}
const dataOnHeap = new Uint8Array(module.HEAPU8.buffer, dataPtr, numBytes);
dataOnHeap.set(audioUnit8Buffer);
return {
bytes: dataOnHeap.byteOffset,
length: dataOnHeap.length,
sampleRate: this.sampleRate,
channels: this.channelCount,
outputSamplesCount: sampleCount,
};
}
setOptions() {
}
freeBuffer() {
if (!this.lastBufferData) {
return;
}
const module = tav;
module._free(this.lastBufferData);
this.lastBufferData = null;
}
release() {
this.freeBuffer();
this.released = true;
}
}
WebAudioReader.baseUrl = '/sample/';
WebAudioReader.audios = {};
WebAudioReader.audioBuffers = {};