Monday 4 January 2021

record mp3 with arrayBuffer

I try a lot of things with Stackoverflow and I can't get it . So I try to record audio from raspberry pi with nodejs (1). After this stream goes through a websocket server(I don't put this code because it's just a redirection). And last a websocket in vuejs listen the stream. And after I want to record this stream in mp3 (2). But I have noise or nothing. 1 - Raspberry pi :

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- Part vuejs

  • Socket :

     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) {
    
    
      }
    
  • part arraybuffer to 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);
    

And I don't understand why I have arraybuffer with 16384 size buffer. I'm really open to any solution, but I don't want to do it on server side. Thanks



from record mp3 with arrayBuffer

No comments:

Post a Comment