Class: MicLevelsAnalysis

MicLevelsAnalysis

mmir.env.media.MicLevelsAnalysis

MicLevelsAnalysis is a plugin/module for generating "microphone input levels changed events" for ASR (speech recognition) modules based on Web Audio Input (i.e. analyze audio from getUserMedia) The plugin triggers events miclevelchanged on listeners registered to the MediaManager. In addition, if the mic-levels-audio plugin starts its own audio-stream, an webaudioinputstarted event is trigger, when the plugin starts.
See:
Example
////////////////////////////////  within media plugin: load analyzer //////////////////////////////////
//within audio-input plugin that uses Web Audio: load mic-levels-analyzer plugin

//first: check, if the analyzer plugin is already loaded (should be loaded only once)
if(!mediaManager.micLevelsAnalysis){

	//set marker so that other plugins may know that the analyzer will be loaded:
	mediaManager.micLevelsAnalysis = true;

	//load the analyzer
	mediaManager.loadPlugin(micLevelsImplFile, function success(){

		//... finish the audio-plugin initialization, e.g. invoke initializer-callback

	}, function error(err){

	  	// ... in case the analyzer could not be loaded:
		// do some error handling ...

		//... and supply a stub-implementation for the analyzer module:
	  	mediaManager.micLevelsAnalysis = {
	  		_active: false,
	  		start: function(){
	  			console.info('STUB::micLevelsAnalysis.start()');
	  		},
	  		stop: function(){
	  			console.info('STUB::micLevelsAnalysis.stop()');
	  		},
	  		enable: function(enable){
	  			console.info('STUB::micLevelsAnalysis.enable('+(typeof enable === 'undefined'? '': enable)+') -> false');
	  			return false;//<- the stub can never be enabled
	  		},
	  		active: function(active){
	  			this._active = typeof active === 'undefined'? this._active: active;
	  			console.info('STUB::micLevelsAnalysis.active('+(typeof active === 'undefined'? '': active)+') -> ' + this._active);
	  			return active;//<- must always return the input-argument's value
	  		}
	  	};

	  	//... finish the audio-plugin initialization without the mic-levels-analyzer, e.g. invoke initializer-callback

	  });
} else {

	//if analyzer is already loaded/loading: just finish the audio-plugin initialization,
	//										 e.g. invoke initializer-callback

}


////////////////////////////////  use of mic-levels-analysis events //////////////////////////////////
//in application code: listen for mic-level-changes

mmir.media.on('miclevelchange', function(micValue){

});

Requires

  • module:HTML5
  • module:HTML5

Methods

Getter/Setter for ASR-/recording-active state. This function should be called with true when ASR starts and with false when ASR stops. NOTE setting the active state allows the analyzer to start processing when a listener for miclevelchanged is added while ASR/recording is already active (otherwise the processing would not start immediately, but when the ASR/recording is started the next time).
Name Type Description
active Boolean optional if active is provided, then the mic-level-analysis' (recording) active-state is set to this value.
Returns:
Type Description
Boolean the mic-level-analysis' (recording) active-state. If argument active was supplied, then the return value will be the same as this input value.
Get/set the mic-level-analysis' enabled-state: If the analysis is disabled, then start will not active the analysis (and currently running analysis will be stopped). This function is getter and setter: if an argument enable is provided, then the mic-level-analysis' enabled-state will be set, before returning the current value of the enabled-state (if omitted, just the enabled-state will be returned)
Name Type Description
enable Boolean optional OPTIONAL if enable is provided, then the mic-level-analysis' enabled-state is set to this value.
Returns:
Type Description
Boolean the mic-level-analysis' enabled-state

start(audioInputData)

Start the audio analysis for generating "microphone levels changed" events. This functions should be called, when ASR is starting / receiving the audio audio stream. When the analysis has started, listeners of the MediaManager for event miclevelchanged will get notified, when the mic-levels analysis detects changes in the microphone audio input levels.
Name Type Description
audioInputData AudioInputData optional If provided, the analysis will use these audio input objects instead of creating its own audio-input via getUserMedia. The AudioInputData object must have 2 properties: { inputSource: MediaStreamAudioSourceNode (HTML5 Web Audio API) audioContext: AudioContext (HTML5 Web Audio API) } If this argument is omitted, then the analysis will create its own audio input stream via getUserMedia
Stops the audio analysis for "microphone levels changed" events. This functions should be called, when ASR has stopped / closed the audio input stream.