Phonegap mixing audio files

后端 未结 2 2043
既然无缘
既然无缘 2020-12-30 12:06

I\'m building a karaoke app using Phonegap for Ios.

I have audio files in the www/assets folder that I am able to play using the media.play()function

This al

相关标签:
2条回答
  • 2020-12-30 12:28

    The solution was to use the offlineAudioContext

    The steps were: 1. Load the two files as buffers using the BufferLoader 2. Create an OfflineAudioContext 3. connect the two buffers to the OfflineAudioContext 4. start the two buffers 5. use the offline startRendering function 6. Set the offfline.oncomplete function to get a handle on the renderedBuffer.

    Here's the code:

    offline = new webkitOfflineAudioContext(2, voice.buffer.length, 44100);
    vocalSource = offline.createBufferSource();
    vocalSource.buffer = bufferList[0];
    vocalSource.connect(offline.destination);
    
    backing = offline.createBufferSource();
    backing.buffer = bufferList[1];
    backing.connect(offline.destination);
    
    vocalSource.start(0);
    backing.start(0);
    
    offline.oncomplete = function(ev){
        alert(bufferList);
        playBackMix(ev);
        console.log(ev.renderedBuffer);
        sendWaveToPost(ev);
    }
    offline.startRendering();
    
    0 讨论(0)
  • 2020-12-30 12:42

    I would suggest to mix the PCM directly. If you initialize a buffer that overlaps the time frames of both tracks, then the formula is additive:

    mix(a,b) = a+b - a*b/65535.

    This formula depends on unsigned 16bit integers. Here's an example:

    SInt16 *bufferA, SInt16 *bufferB;
    NSInteger bufferLength;
    SInt16 *outputBuffer;
    
    for ( NSInteger i=0; i<bufferLength; i++ ) {
      if ( bufferA[i] < 0 && bufferB[i] < 0 ) {
        // If both samples are negative, mixed signal must have an amplitude between 
        // the lesser of A and B, and the minimum permissible negative amplitude
        outputBuffer[i] = (bufferA[i] + bufferB[i]) - ((bufferA[i] * bufferB[i])/INT16_MIN);
      } else if ( bufferA[i] > 0 && bufferB[i] > 0 ) {
        // If both samples are positive, mixed signal must have an amplitude between the greater of
        // A and B, and the maximum permissible positive amplitude
        outputBuffer[i] = (bufferA[i] + bufferB[i]) - ((bufferA[i] * bufferB[i])/INT16_MAX);
      } else {
        // If samples are on opposite sides of the 0-crossing, mixed signal should reflect 
        // that samples cancel each other out somewhat
        outputBuffer[i] = bufferA[i] + bufferB[i];
      }
    }
    

    This can be a very effective way to handle signed 16 bit audio. Go here for the source.

    0 讨论(0)
提交回复
热议问题