Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Voice are not clear. Audio having distortion #62

Open
durgadp opened this issue Jul 27, 2019 · 16 comments
Open

Voice are not clear. Audio having distortion #62

durgadp opened this issue Jul 27, 2019 · 16 comments

Comments

@durgadp
Copy link

durgadp commented Jul 27, 2019

I am using your lamejs for recording voice and convert into mp3.
Below is audio link what i am exactly facing the problem after recording
Distorted Audio : https://www.ptemocktest.com/pte/uploaddata/answer-audio/d3cd0a153620a3ddb12e09c31c3f7bbd.mp3

This is configuration i am using to record

var mp3Encoder, maxSamples = 1152, samplesMono, config, dataBuffer;
var clearBuffer = function () {
dataBuffer = [];
};

var appendToBuffer = function (mp3Buf) {
dataBuffer.push(new Int8Array(mp3Buf));
};

var init = function (prefConfig) {
config = prefConfig || {debug: true};
mp3Encoder = new lamejs.Mp3Encoder(1, config.sampleRate || 48000, config.bitRate || 128);
clearBuffer();
};

Please help where i am doing wrong with configuration.

@zhuker
Copy link
Owner

zhuker commented Jul 30, 2019

i cant tell from looking at the code
but it looks like you are not feeding the encoder properly

@durgadp
Copy link
Author

durgadp commented Jul 30, 2019

Please let me help what i need to be code for encoder properly

@zhuker
Copy link
Owner

zhuker commented Jul 30, 2019

sounds like you are calling flush() too often, try calling it only at the end of encode

@durgadp
Copy link
Author

durgadp commented Jul 31, 2019

Can I share the code file with you? You want me to call flush after sending to the server or before.

@zhuker
Copy link
Owner

zhuker commented Jul 31, 2019

Can I share the code file with you? You want me to call flush after sending to the server or before.

Post the most relevant snippet here, i'll take a look

@durgadp
Copy link
Author

durgadp commented Jul 31, 2019 via email

@zhuker
Copy link
Owner

zhuker commented Jul 31, 2019

    var data = new Float32Array(arrayBuffer);
    var out = new Int16Array(arrayBuffer.length);

change to

    var data = new Float32Array(arrayBuffer);
    var out = new Int16Array(data.length);

@durgadp
Copy link
Author

durgadp commented Sep 16, 2019

I have tried by replace this code but still facing same issue. Please let me know where i am doing wrong with code.

@zhuker
Copy link
Owner

zhuker commented Sep 16, 2019

can you create an end-to-end example i can reproduce here?
i mean no worker code, no messages

@durgadp
Copy link
Author

durgadp commented Sep 16, 2019

Above is my file please let me know which code i need to remove from it.

@zhuker
Copy link
Owner

zhuker commented Sep 16, 2019 via email

@zhuker
Copy link
Owner

zhuker commented Sep 17, 2019

arrayBuffer passed to your var encode = function (arrayBuffer) {
must be of size divisible by 1152 * 4
if size is different encoder will produce the clicks you are hearing

@durgadp
Copy link
Author

durgadp commented Sep 19, 2019

What i need to do for 1152 * 4 into my encode function so it will ignore

@lostrepo
Copy link

lostrepo commented Oct 6, 2019

@durgadp I'm testing lamejs as fallback for vmsgjs right now.
Here is my handling of this limitation (mono sound example).

// assumes that t - is some sort of object, in my case it's vmsgjs Recorder instance
t.len = 0;
// List of valid buffSize values can be found here:
// https://developer.mozilla.org/en-US/docs/Web/API/BaseAudioContext/createScriptProcessor
// To use buffSize = 256 change t.samplesInFrame to 18. In other cases you don't change it
t.buffSize = 1024;
t.samplesInFrame = 9;
t.sampleFrameSize = t.buffSize * t.samplesInFrame;
t.unsentSamples = [];

function startRecording(){
  // assumes that
  //  t.encNode = (t.audioCtx.createScriptProcessor || t.audioCtx.createJavaScriptNode)
  //              .call(t.audioCtx, t.buffSize, inputChannelsNumber, outputChannelsNumber)
  t.encNode.onaudioprocess = function(e){
   // create new array so data won't be corrupted, references suck sometimes
   t.unsentSamples[t.len++]	=	new Float32Array(e.inputBuffer.getChannelData(0));
    if (!(t.len % t.samplesInFrame)){
      t.worker.postMessage({
         cmd: 'encode',
         data: { buf: concatSamples(t.unsentSamples, t.sampleFrameSize) }
      });
      t.len = 0;
      t.unsentSamples = [];
    }
  }
  t.encNode.connect(t.audioCtx.destination);
}

function stopRecording(){
  t.len = 0;
  t.unsentSamples = [];
  t.encNode.onaudioprocess = null;
  t.unsentSamples && t.unsentSamples.length && t.worker.postMessage({
    cmd: 'encode',
    data: {
       buf: concatSamples(t.unsentSamples, t.sampleFrameSize)
    }
 });
 t.worker.postMessage({ cmd: 'finish' });
 // disconnect Nodes, destroy stuff
}

// concatenate audio samples
// if not enough samples passed we will still return valid Float32Array
// when totalLength passed divisible by 4608 = 1152*4
// samples - array of Float32Arrays
// totalLength - combined length of Float32Arrays in samples
function concatSamples(samples, totalLength){
  var l = samples.length,
  len = 0,
  res = new Float32Array(totalLength);		
  for (var i = 0; i < l; i++){
    res.set(samples[i], len);
    len += samples[i].length;
  }
  return res;
}

Hope this helps others to work around this limitation.

@durgadp
Copy link
Author

durgadp commented Feb 14, 2020

sounds like you are calling flush() too often, try calling it only at the end of encode

What do you mean by calling flush() too often?

@durgadp
Copy link
Author

durgadp commented Feb 14, 2020

t.sampleFrameSize

var remaining = samplesMono.length;
I am getting 2048 length here which is divisible by 4. Help me if i am doing any thing wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants