purityselect/resources/js/AudioTrackProcess.js
2024-10-25 01:05:27 +05:00

54 lines
1.2 KiB
JavaScript

export default class AudioTrackProcess {
name= ""
source= null
sourceSettings = null
processor = null
trackGenerator= null;
processedTrack= null;
constructor(name) {
this.name = name;
}
async init(opts) {
this.source = opts.track
this.sourceSettings = this.source.getSettings();
this.processor = new MediaStreamTrackProcessor({ track: this.source });
this.trackGenerator = new MediaStreamTrackGenerator({
kind: 'audio',
signalTarget: this.source,
});
let readableStream = this.processor.readable;
this.transformer = new TransformStream({
transform: (frame, controller) => this.transform(frame, controller),
});
readableStream = readableStream.pipeThrough(this.transformer);
readableStream
.pipeTo(this.trackGenerator.writable)
.catch((e) => console.error('error when trying to pipe', e))
.finally(() => this.destroy());
this.processedTrack = this.trackGenerator
}
async transform(data, controller) {
console.log('transformer', data);
controller.enqueue(data);
}
async restart(opts) {
await this.destroy();
return this.init(opts);
}
async destroy() {
this.trackGenerator?.stop();
}
}