edimuj / cordova-plugin-audioinput

This iOS/Android Cordova/PhoneGap plugin enables audio capture from the device microphone, by in near real-time forwarding audio to the web layer of your application. A typical usage scenario for this plugin would be to use the captured audio as source for a web audio node chain, where it then can be analyzed, manipulated and/or played.
https://github.com/edimuj/app-audioinput-demo
MIT License
161 stars 88 forks source link

Support recording directly to a given fileUrl #52

Closed robertfromont closed 6 years ago

robertfromont commented 7 years ago

As an alternative to event-based capture of audio data, these changes allow the plugin to save audio directly to a file.

To enable this option, add a fileUrl setting to the capture config passed into the start method - e.g.

var fileUrl = cordova.file.dataDirectory + "audio.wav";
var captureCfg = {
  sampleRate: 16000,
  bufferSize: 8192,
  channels: 1,
  format: audioinput.FORMAT.PCM_16BIT,
  audioSourceType: audioinput.AUDIOSOURCE_TYPE.DEFAULT,
  fileUrl: fileUrl
};
audioinput.start(captureCfg);

When stop is called, recording is stopped and the data written to the specified file, and then an 'audioinputfinished' event is raised, which includes the original file URL:

window.addEventListener('audioinputfinished', function(e) { 
  doSomethingWithFile(e.file);
}, false);

NB there is a request for saving audio input to files - these changes don't exactly fulfil this request, as here the audio is saved to a file instead of (rather than as well as) being passed in events, and the target format is not the requested M4A. I have tested these changes with WAV files, as this is what my project requires.

edimuj commented 6 years ago

Thanks @robertfromont Nice work extending the plugin.