Skip to content

sb2702/audioRecord.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

audioRecord.js

⚠️This project is from 2015 and is deprecated. See the MediaStream Recording API for a native browser recording API

A simplified audio Recorder for HTML5, which uses the WebAudio API to export microphone input to wav, mp3 or ogg files. Based on https://github.com/mattdiamond/Recorderjs, https://github.com/nusofthq/Recordmp3js and https://github.com/bjornm/libvorbis-js

Quick and dirty Usage

 audioRecorder.requestDevice(function(recorder){
  // Create a recorder object (this will ask browser for microphone access)

	recorder.start();         // Start recording

	setTimeout(function(){     // Stop recording after 5 seconds
      
		 recorder.stop();

		 recorder.exportMP3(function(mp3Blob){ // Export the recording as a Blob
			  
			   
			console.log("Here is your blob: " + URL.createObjectURL(mp3Blob));
			//Do something with your blob
		       
		 
		 });
		  
	}, 5000);       

        
 });

Syntax

Constructor

Basic / Async Construction The recorder object is instatiated asynchronously as shown below. Calling the recorder function will ask the client for access to the device's microphone. Once the client agrees, the callback is called, providing the recorder object

     audioRecorder.requestDevice(function(recorder){
      // Create a recorder object (this will ask browser for microphone access)

     } [,config]);

From Media Source / Sync Construction The recorder object can be instatiated synchronously if you want to specify the media source manually.

  var recorder = audioRecorder.fromSource(source [,config]);

Example:

  window.AudioContext = window.AudioContext || window.webkitAudioContext;
	  navigator.getUserMedia = ( navigator.getUserMedia ||
	  navigator.webkitGetUserMedia ||
	  navigator.mozGetUserMedia ||
	  navigator.msGetUserMedia);
	  window.URL = window.URL || window.webkitURL;

	  audio_context = new AudioContext;


	  navigator.getUserMedia({audio: true}, function(stream){

	          var source = audio_context.createMediaStreamSource(stream);

		  var recorder  = audioRecorder.fromSource(source);

	  }, function(e) {

	       //Error handling
		
	  });
  • config - (optional) A configuration object (see config section below)

Config

  • workerPath - Path to recorder.js worker script. Defaults to 'js/recorderjs/recorderWorker.js'
  • mp3LibPath - Name of the mp3 conversion library, relative to the location of "workerPath". Defaults to 'libmp3lame.min.js'
  • vorbisLibPath - Name of the vorbis conversion library, relative to the location of "workerPath". Defaults to 'libvorbis.module.min.js'. If you specify a non-default path, be sure to include 'libvorbis.module.min.jis.mem' in the same path
  • bufferLen - The length of the buffer that the internal JavaScriptNode uses to capture the audio. Can be tweaked if experiencing performance issues. Defaults to 4096.
  • callback - A default callback to be used with exportWAV.
  • type - The type of the Blob generated by exportWAV. Defaults to 'audio/wav'.
  • recordAsMP3 - Binary: if true - will continuously encode the stream into mp3 format, making MP3 exports much faster, especially for recordings of over 10 seconds. Defaults to false

TODO continuously encoding OGG provides a codec error, figure out why


Instance Methods

rec.record()
rec.stop()

Pretty self-explanatory... record will begin capturing audio and stop will cease capturing audio. Subsequent calls to record will add to the current recording.

rec.clear()

This will clear the recording.

rec.exportWAV([callback][, type])

This will generate a Blob object containing the recording in WAV format. The callback will be called with the Blob as its sole argument. If a callback is not specified, the default callback (as defined in the config) will be used. If no default has been set, an error will be thrown.

rec.exportMP3([callback])

This will generate a Blob object containing the recording in MP3 format. The callback will be called with the Blob as its sole argument. If a callback is not specified, the default callback (as defined in the config) will be used. If no default has been set, an error will be thrown.

rec.exportOGG([callback])

This will generate a Blob object containing the recording in OGG format. The callback will be called with the Blob as its sole argument. If a callback is not specified, the default callback (as defined in the config) will be used. If no default has been set, an error will be thrown.

In addition, you may specify the type of Blob to be returned (defaults to 'audio/wav').

rec.getBuffer([callback])

This will pass the recorded stereo buffer (as an array of two Float32Arrays, for the separate left and right channels) to the callback. It can be played back by creating a new source buffer and setting these buffers as the separate channel data:

function getBufferCallback( buffers ) {
	var newSource = audioContext.createBufferSource();
	var newBuffer = audioContext.createBuffer( 2, buffers[0].length, audioContext.sampleRate );
	newBuffer.getChannelData(0).set(buffers[0]);
	newBuffer.getChannelData(1).set(buffers[1]);
	newSource.buffer = newBuffer;

	newSource.connect( audioContext.destination );
	newSource.start(0);
}

This sample code will play back the stereo buffer.

rec.configure(config)

This will set the configuration for Recorder by passing in a config object.

Utility Methods (static)

Recorder.forceDownload(blob[, filename])

This method will force a download using the new anchor link download attribute. Filename defaults to 'output.wav'.

License

The MIT License (MIT)

Copyright (c) 2015 Sam Bhattacharyya

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Disclaimer

MP3 encoding/decoding technology may be governed by MP3 patents in some countries.

This project is based off of https://github.com/nusofthq/Recordmp3js and Based on https://github.com/mattdiamond/Recorderjs

About

Easily record audio in the browser, export as WAV, MP3 or OGG

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages