0

I'd like to add javascript (preferably without a library) to alter the pitch of the users voice to when using Twilios Webcall.

I used the following code in an attempt to modify the voice, however, did not note any differencing in the stream which I am seeking a voicedrop in pitch. I've tried with the following attempts without success. Anyone have a recommendation for a simpler solution? Much appreciated!

//https://stackoverflow.com/questions/52512823/is-it-possible-to-do-voice-/**
 * Update the stream source with the new input audio stream.
 * @param {MediaStream} stream
 * @private
 */
PeerConnection.prototype._updateInputStreamSource = function (stream) {
  if (this._inputStreamSource) {
    this._inputStreamSource.disconnect();
  }

  this._inputStreamSource = this._audioContext.createMediaStreamSource(stream);

  const processorNode = this._audioContext.createScriptProcessor(4096, 1, 1);


  var grainSize = 8192;
  var overlapRatio = 0;
  var grainWindow = hannWindow(grainSize);
      processorNode.buffer = new Float32Array(grainSize * 2);
      processorNode.onaudioprocess = function (event) {

          var pitchRatio = 0.5;

          var inputData = event.inputBuffer.getChannelData(0);
          var outputData = event.outputBuffer.getChannelData(0);

          for (i = 0; i < inputData.length; i++) {

              // Apply the window to the input buffer
              inputData[i] *= grainWindow[i];

              // Shift half of the buffer
              this.buffer[i] = this.buffer[i + grainSize];

              // Empty the buffer tail
              this.buffer[i + grainSize] = 0.0;
          }

          // Calculate the pitch shifted grain re-sampling and looping the input
          var grainData = new Float32Array(grainSize * 2);
          for (var i = 0, j = 0.0;
               i < grainSize;
               i++, j += pitchRatio) {

              var index = Math.floor(j) % grainSize;
              var a = inputData[index];
              var b = inputData[(index + 1) % grainSize];
              grainData[i] += linearInterpolation(a, b, j % 1.0) * grainWindow[i];
          }

          // Copy the grain multiple times overlapping it
          for (i = 0; i < grainSize; i += Math.round(grainSize * (1 - overlapRatio))) {
              for (j = 0; j <= grainSize; j++) {
                  this.buffer[i + j] += grainData[j];
              }
          }

          // Output the first half of the buffer
          for (i = 0; i < grainSize; i++) {
              outputData[i] = this.buffer[i];
          }
      };


  this._inputStreamSource.connect(this._inputAnalyser);
  this._inputStreamSource.connect(this._inputAnalyser2);

    // Connect the nodes
  this._inputStreamSource.connect(processorNode);
  processorNode.connect(this._audioContext.destination);
};

0 Answers0