jeudi 25 mai 2017

Silent microphone audio getUserMedia

I've been banging my head against a wall on this for two days now, and I really hope someone can help on this.

I've taken some code for a getUserMedia microphone recorder from http://ift.tt/2r2tZg7 + http://ift.tt/2s0BAc7 here. I've stripped out the components I don't need - and somehow, in the process, I've managed to make it so that there is no audio coming through on the generated file.

It looks like it formats correctly - but is completely silent. I'm getting 0 errors to work from.

// navigator.getUserMedia shim
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
// URL shim
window.URL = window.URL || window.webkitURL;

// audio context + .createScriptProcessor shim
var audioContext = new AudioContext;
if (audioContext.createScriptProcessor == null) {
  audioContext.createScriptProcessor = audioContext.createJavaScriptNode;
}

// selectors
var $microphone = $('#microphone');
var $cancel = $('#cancel');
var $recordingList = $('#recording-list');
var $timeDisplay = $('#time-display');
var $microphoneLevel = $('#microphone-level');

var microphone = undefined;
var input = audioContext.createGain();
var mixer = audioContext.createGain();
var microphoneLevel = audioContext.createGain();
microphoneLevel.gain.value = 0;
microphoneLevel.connect(mixer);
var processor = undefined;
var startTime = null;
var encoder = undefined;

// obtaining microphone input
$microphone.click(function() {
    navigator.getUserMedia({ audio: true },
      function(stream) {
        microphone = audioContext.createMediaStreamSource(stream);
        microphone.connect(microphoneLevel);
        console.log(microphone);
      },
      function(error) {
        window.alert("Could not get audio input");
      });
});

// start/stop recording
$microphone.click(function() {
  if (startTime != null) {
    stopRecording(true);
  } else {
    startRecording();
  }
});

// cancel recording (without saving)
$cancel.click(function() {
  stopRecording(false);
});

// microphone level slider
$microphoneLevel.on('input', function() {
  var level = $microphoneLevel[0].valueAsNumber / 100;
  microphoneLevel.gain.value = level * level;
});

function startRecording() {
  startTime = Date.now();
  $microphone.html('Stop');
  $cancel.removeClass("hidden");
  startRecordingProcess();
}

function startRecordingProcess() {
  processor = audioContext.createScriptProcessor(1024, 2, 2);
  input.connect(processor);
  processor.connect(audioContext.destination);
  // wav encoder
  encoder = new WavAudioEncoder(audioContext.sampleRate, 2);
  processor.onaudioprocess = function(event) {
    encoder.encode(getBuffers(event));
  };
}

function getBuffers(event) {
  var buffers = [];
  for (var ch = 0; ch < 2; ++ch) {
    buffers[ch] = event.inputBuffer.getChannelData(ch);
  }
  return buffers;
}

function stopRecording(finish) {
  startTime = null;
  $timeDisplay.html('00:00');
  $microphone.html('<i class="start fa fa-microphone fa-5x" aria-hidden="true"></i>');
  $cancel.addClass('hidden');
  stopRecordingProcess(finish);
}

function stopRecordingProcess(finish) {
  input.disconnect();
  processor.disconnect();

  if (finish) { // if microphone pressed
    saveRecording(encoder.finish());
  } else { // if cancel pressed
    encoder.cancel();
  }
}

function saveRecording(blob) {
  var url = URL.createObjectURL(blob);
  var html = "<p class='recording' recording='" + url + "'><a class='btn btn-default' href='" + url + "' download='recording.wav'>Save Recording</a></p>";
  $recordingList.prepend($(html));

  // once we have done all the processing, upload the file to beyond verbal
  // uploadFile(blob);
}

// update the recording timer
function minuteSeconds(n) { return (n < 10 ? "0" : "") + n; }
function updateDateTime() {
  if (startTime !== null) {
    var sec = Math.floor((Date.now() - startTime) / 1000);
    $timeDisplay.html(minuteSeconds(sec / 60 | 0) + ":" + minuteSeconds(sec % 60));
  }
}
window.setInterval(updateDateTime, 200);

If anyone has run into this before, I'd be really appreciative of a fix.

Thank you all for your time, and have a nice day/night




Aucun commentaire:

Enregistrer un commentaire