[AS3] – MICROPHONE – RECORD AND SAVE AS WAVE FILE

April 14, 2011 at 1:37 AM (Actionscript) (, , , )

Adding save feature to my previous demo Record and play, it saves the recorded sound to a wave file.

For saving the sound data to wave file I’m using wave encoder from bytearray.org

click here for a DEMO
source files:
Link 1: microphone_recandsave.zip – 22.4 KB

import flash.media.*;
import flash.events.*;
import flash.utils.ByteArray;
import flash.net.FileReference;
import org.bytearray.micrecorder.encoder.WaveEncoder;

var enco:WaveEncoder=new WaveEncoder();
var file:FileReference=new FileReference()

var ch:SoundChannel;
//ByteArray in which the recorded sound data is stored
var soundBytes:ByteArray = new ByteArray();
//ByteArray from which the recorded sound data is played
var soundO:ByteArray = new ByteArray();
//Sound object which plays the recorded sound...
var sound:Sound= new Sound();

//Gets your default microphone
var mic:Microphone = Microphone.getMicrophone();
//To check whether the application is recording the sound or not
var recMode:Boolean=false;
//To check whether the application is playing the sound or not
var playMode:Boolean=false;
//function called at start of application
function init(){
	//Sets the minimum input level that should be considered sound
	mic.setSilenceLevel(0);
	//The amount by which the microphone boosts the signal.
	mic.gain = 50;
	//The rate at which the microphone is capturing sound, in kHz.
	mic.rate = 44;
}
//function called when start Record button is clicked
function startRecord():void
{
	mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
}
//function called when stop Record button is clicked
function stopRecord():void
{
	mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
	soundBytes.position = 0;
	soundO.length=0
	soundO.writeBytes(soundBytes);
	soundO.position = 0;
	soundBytes.length=0;
}

function micSampleDataHandler(event:SampleDataEvent):void
{
	while (event.data.bytesAvailable)
	{
		var sample:Number = event.data.readFloat();
		soundBytes.writeFloat(sample);
	}
}

function playSound():void
{
soundO.position = 0;
	sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
	ch=sound.play();
	ch.addEventListener(Event.SOUND_COMPLETE,onSC)
}
function stopSound():void
{
	sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
	ch.stop()
	ch.removeEventListener(Event.SOUND_COMPLETE,onSC)
}
function onSC(evt:Event):void
{
	trace("SOUND_COMPLETE")
	stopSound()
	soundO.position=0
	playMode=!true
		play_lab.gotoAndStop(1)
		play_mesg.gotoAndStop(1)
		rec_btn.visible=rec_lab.visible=!false
		save_btn.visible=save_lab.visible=true
}
function playbackSampleHandler(event:SampleDataEvent):void
{
	for (var i:int = 0; i < 8192; i++)
	{
		if (soundO.bytesAvailable < 4)
		{
			break
		}
		var sample:Number = soundO.readFloat();
		event.data.writeFloat(sample);
		event.data.writeFloat(sample);

	}
}
rec_lab.mouseEnabled=play_lab.mouseEnabled=save_lab.mouseEnabled=false
rec_btn.buttonMode=play_btn.buttonMode=save_btn.buttonMode=true
function showPlayUI(){
	play_btn.visible=play_lab.visible=save_btn.visible=save_lab.visible=true;
}
function hidePlayUI(){
	play_btn.visible=play_lab.visible=save_btn.visible=save_lab.visible=!true;
}
rec_btn.addEventListener(MouseEvent.CLICK,onRecord)
play_btn.addEventListener(MouseEvent.CLICK,onPlay)
save_btn.addEventListener(MouseEvent.CLICK,onSave)
function onSave(evt:MouseEvent=null):void
{
	hidePlayUI()
	soundO.position=0
	var o:ByteArray=enco.encode(soundO,1)
	file.save(o,"sample.wav")
	showPlayUI()
}
function onRecord(evt:MouseEvent=null):void
{
	if(!recMode){
		recMode=true
		rec_lab.gotoAndStop(2)
		rec_mesg.gotoAndPlay(2)
//--------------- Edit[2-11-2012] START -----------------------------//
                hidePlayUI()
//--------------- Edit[2-11-2012] END -----------------------------//
		startRecord()
	}else{
		recMode=!true
		rec_lab.gotoAndStop(1)
		rec_mesg.gotoAndStop(1)
		stopRecord()
		showPlayUI()
	}
}
function onPlay(evt:MouseEvent=null):void
{
	if(!playMode){
		playMode=true
		play_lab.gotoAndStop(2)
		play_mesg.gotoAndPlay(2)
		rec_btn.visible=rec_lab.visible=false
		save_btn.visible=save_lab.visible=!true
		playSound()
	}else{
		playMode=!true
		play_lab.gotoAndStop(1)
		play_mesg.gotoAndStop(1)
		rec_btn.visible=rec_lab.visible=!false
		save_btn.visible=save_lab.visible=true
		stopSound()
	}
}
hidePlayUI()
init()
Advertisements

Permalink 27 Comments

%d bloggers like this: