[英]Compress Audio Buffer size in Javascript
My main goal is to compress an audio file to reduce its size with JavaScript.我的主要目标是使用 JavaScript 压缩音频文件以减小其大小。 But the tricky part are all the restrictions and requirements I should complain to for my use case:
但棘手的部分是我应该为我的用例抱怨的所有限制和要求:
In my opinion a library or an algorithm for data size reduction based on the Web Audio API AudioBuffer would be the best solution because even if it is not compatible with the NodeJS library for audio, hopefully I will be able to patch it so it works in my use case.在我看来,基于 Web 音频 API AudioBuffer 的库或用于减少数据大小的算法将是最好的解决方案,因为即使它与用于音频的 NodeJS 库不兼容,希望我能够修补它以便它在我的用例。
I've found an audio encoder javascript library which works in both Node and Browser called lamejs
.我找到了一个音频编码器 javascript 库,它可以在节点和浏览器中运行,名为
lamejs
。
So what I needed to achieve could be done as follows:所以我需要实现的可以如下完成:
const mp3encoder = new lamejs.Mp3Encoder(1, SAMPLE_RATE, KBPS);
let samples = audioBuffer.getChannelData(0).map(x => x * 32767.5);
const encoded = mp3encoder.encodeBuffer(samples);
mp3Data.push(encoded);
mp3Data.push(mp3encoder.flush());
Note that you need to normalize the audio volume, because in a typical AudioBuffer data is stored as a float between 0 and 1, that's why when reading the channel data you should multiply it by 32767.5 to be compatible with lamejs
;请注意,您需要标准化音频音量,因为在典型的 AudioBuffer 中,数据存储为 0 和 1 之间的浮点数,这就是为什么在读取通道数据时应将其乘以 32767.5 以与
lamejs
兼容;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.