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

Example for using Kali in real time that produce reasonably good audio #5

Open
Willena opened this issue Jan 14, 2017 · 4 comments
Open

Comments

@Willena
Copy link

Willena commented Jan 14, 2017

Hi !
I am currently trying to use your project in a realtime project. Audio should be processed and pushed to speakers without the latency of processing the whole track.

I am using the scriptPrecessorNode witch is feeding the Kali instance with the buffer given by the scriptProcessor.
Actualy it is working but the sound is very bad. You can listen the audio sample to understand a bit more what it sound like.

Here the code I use :

this.kali = new Kali(2);
this.setup(audioContext.sampleRate, 0.5);
input = audioContext.createScriptProcessor(4096, 1, 1);
that = this;
input.onaudioprocess = function (audioProcessingEvent) {
      var inputBuffer = audioProcessingEvent.inputBuffer;
      var outputBuffer = audioProcessingEvent.outputBuffer;

      var inputData = inputBuffer.getChannelData(0);

      var numInputFrames = inputData.length;
      var numChannels = 1;
      var stretchFactor = 1;

      var completed = new Float32Array(numInputFrames * numChannels + 1);

      that.kali.input(dataToInput);
      that.kali.process();
      that.kali.output(complete)
      that.kali.flush();

    outputBuffer.copyToChannel(completed, 0, 0);

  };

Any ideas of what I am doing wrong ?

@Willena Willena changed the title Example for using Kali in real time that produce resonably good audio ? Example for using Kali in real time that produce reasonably good audio Jan 14, 2017
@saste
Copy link

saste commented Mar 3, 2017

You should not flush data if not after you finish to consume the input data, otherwise you will drop audio data and produce signal discontinuities.

@Willena
Copy link
Author

Willena commented Mar 6, 2017

Thanks, I removed the kali.flush(), but still the same problem. the only thing I think is that I drop some part of the audio between two AudioBuffers.

Edit : It seems to be good with only one channel

@saste
Copy link

saste commented Mar 7, 2017

@Willena: from my experiments kali+real time streaming is fine, at least if you correctly manage the AudioBuffer. For example in my case I have something like this for each processed chunk of input data:

        var kaliOutputData = new Float32Array(Math.round(chunkSize / TEMPO_RATIO));
        kali.input(chunk);
        kali.process();
        
        var kaliOutputSize = kali.output(kaliOutputData);
        console.log("kali in:" + chunk.length + " out:" + kaliOutputSize + " time:" + timeOff);

        if (kaliOutputSize === 0) {
            continue;
        }

        // fill channel with interleaved data
        source = audioCtx.createBufferSource(channels);
        outputBuffer = audioCtx.createBuffer(channels, kaliOutputSize / channels, audioCtx.sampleRate);
        for (var i = 0; i < kaliOutputSize / channels; i++) {
            for (var j = 0; j < channels; j++) {
                outputBuffer.getChannelData(j)[i] = kaliOutputData[i * channels + j];
            }
        }
        
        source.buffer = outputBuffer;
        source.connect(audioCtx.destination);
        source.start(timeOff, 0, outputBuffer.duration);

The issue you noticed might be due to #6 in case it depends on the number of channels.
HTH

@Willena
Copy link
Author

Willena commented Mar 7, 2017

Yes I've seen #6 and I will have a look at your code.
Thank for the help !

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

2 participants