gpt4 book ai didi

javascript - 用 arrayBuffer 录制 mp3

转载 作者:行者123 更新时间:2023-12-03 12:18:18 26 4
gpt4 key购买 nike

我用 Stackoverflow 尝试了很多东西,但我做不到。所以我尝试用 nodejs (1) 从树莓派录制音频。在这个流通过一个 websocket 服务器之后(我没有放这段代码,因为它只是一个重定向)。最后一个在vuejs中的websocket监听流。在我想以 mp3 (2) 格式录制此流之后。但我有噪音或没有。1 - 树莓派:

ai = new audio.AudioIO({
inOptions: {
channelCount: 1,
sampleFormat: audio.SampleFormat16Bit,
sampleRate: 44100,
deviceId: 6, // Use -1 or omit the deviceId to select the default device
closeOnError: true // Close the stream if an audio error is detected, if set false then just log the error
}
});

ai.on('data', buf => {

clientAudioWebsocket.send(buf)
}
);


ai.start();

2-部分vuejs

  • 套接字:

     this.dataBuffer = []

    var self = this

    var connectionToLocalServer = new WebSocket("ws://"+ip +":4444")
    connectionToLocalServer.binaryType = "arraybuffer"

    connectionToLocalServer.onmessage = function(event) {
    self.dataBuffer.push(event.data);

    }
    connectionToLocalServer.onopen = function(event) {


    }
  • 部分arraybuffer到mp3

      concatArrayBuffers (bufs) {
    var offset = 0;
    var bytes = 0;
    var bufs2=bufs.map(function(buf,total){
    bytes += buf.byteLength;
    return buf;
    });
    var buffer = new ArrayBuffer(bytes);
    var store = new Uint8Array(buffer);
    bufs2.forEach(function(buf){
    store.set(new Uint8Array(buf.buffer||buf,buf.byteOffset),offset);
    offset += buf.byteLength;
    });
    return buffer }



    this.tmpResult = this.concatArrayBuffers(this.dataBuffer);

    var mp3Data = [];

    var mp3encoder = new lamejs.Mp3Encoder(1, 44100, 128);

    var mp3Tmp = mp3encoder.encodeBuffer(this.tmpResult, 0, Math.floor(this.tmpResult.byteLength / 2));
    //Push encode buffer to mp3Data variable
    mp3Data.push(mp3Tmp);

    // Get end part of mp3
    mp3Tmp = mp3encoder.flush();

    // Write last data to the output data, too
    // mp3Data contains now the complete mp3Data
    mp3Data.push(mp3Tmp);

而且我不明白为什么我有 16384 大小缓冲区的 arraybuffer。我真的对任何解决方案持开放态度,但我不想在服务器端进行。谢谢

最佳答案

所以我找到了解决方案,这是因为我没有创建 Int16Array :-((有时去外面 1 小时后你会找到解决方案会更好)

解决方案是:

let tmpResult = this.concatArrayBuffers(this.dataBuffer)

var samples = new Int16Array(tmpResult);
var buffer = [];
var mp3enc = new lamejs.Mp3Encoder(1, 44100, 128);
var remaining = samples.length;
var maxSamples = 1152;
for (var i = 0; remaining >= maxSamples; i += maxSamples) {
var mono = samples.subarray(i, i + maxSamples);
var mp3buf = mp3enc.encodeBuffer(mono);
if (mp3buf.length > 0) {
buffer.push(new Int8Array(mp3buf));
}
remaining -= maxSamples;
}
var d = mp3enc.flush();
if(d.length > 0){
buffer.push(new Int8Array(d));
}

console.log('done encoding, size=', buffer.length);
var blob = new Blob(buffer, {type: 'audio/mp3'});

关于javascript - 用 arrayBuffer 录制 mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65495721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com