8/18/2010 11:11:19 PM
Title:
capturing microphone and playing at lower sampling rate.
i am having problem on capturing and playing microphone.
i am using eclipse(helios) with flex builder 4 plugin. and running on win7-64bit machine.
it works fine with capturing at 44 khz.
But when i capture the audio from microphone at lower rate less than 44, it captures it
sucessfully (size of byteArray becomes less for same duration of recording) but when i play, it speeds up and cannot be understood.
is there any way to play the captured sound at the same rate as it was sampled.
also i want to know the exact audio format of recorded audio in byteArray so that i can upload it to J2EE server and play it on server.
i have inserted my code below:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" width="445" height="274">
<fx:Script>
<![CDATA[
private var mic:Microphone;
private var rec:ByteArray;
private var snd:Sound;
protected function recBtn_clickHandler(event:MouseEvent):void{
if (event.currentTarget.selected == true){
event.currentTarget.label = "Stop";
rec=new ByteArray();
mic = Microphone.getMicrophone();
mic.setUseEchoSuppression(true);
mic.setSilenceLevel(10,5);
mic.rate=16;//it creates
mic.addEventListener(SampleDataEvent.SAMPLE_DATA, getMicAudio);
ply_Btn.enabled=false;
}
else{
mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, getMicAudio);
event.currentTarget.label = "Record";
ply_Btn.enabled=true;
}
}
protected function plyBtn_clickHandler(event:MouseEvent):void{
if (event.currentTarget.selected == true){
event.currentTarget.label = "Stop";
rec.position = 0;
snd = new Sound();
snd.addEventListener(SampleDataEvent.SAMPLE_DATA, playRecorded);
var channel:SoundChannel;
channel = snd.play();
channel.addEventListener(Event.SOUND_COMPLETE, stopPlayback);
}
else{
event.currentTarget.label = "Play";
snd.close();
}
}
private function getMicAudio(e:SampleDataEvent): void
{ rec.writeBytes(e.data);//capture
lbl.text = String(rec.length);//show byteArray length
}
private function playRecorded(e:SampleDataEvent): void{
//what must be done, for playing at same rate.
if (!rec.bytesAvailable > 0) return;
for (var i:int = 0; i < 8192; i++){//why 8192??
var sample:Number = 0;
if (rec.bytesAvailable > 0) sample = rec.readFloat();
e.data.writeFloat(sample);
e.data.writeFloat(sample);
}
}
private function stopPlayback(e:Event): void{
ply_Btn.selected=false;
ply_Btn.label="play";
}
protected function changeValues():void{
for(var i:int=0;i<rec.length;i++){
if(rec[i]<100){
rec[i]=rec[i]+3;//what should be done
}
}
}
]]>
</fx:Script>
<s:Panel id="recordPanel" x="48" y="41" width="349" height="171" title="Sound Record and Play">
<s:ToggleButton id="rec_Btn" x="20" y="25" width="80" label="Record" click="recBtn_clickHandler(event);"/>
<s:ToggleButton id="ply_Btn" x="20" y="60" width="80" label="Play" click="plyBtn_clickHandler(event);" enabled="false"/>
<s:Label id ="lbl" x="176" y="23" width="165" height="48" fontSize="20" fontFamily="Courier New" verticalAlign="middle"/>
<s:Button x="198" y="91" label="Change Value +30" click="changeValues()"/>
</s:Panel>
</s:Application>
Rayan
Points: 700
Posts:0
8/20/2010 7:26:56 AM
check this url to see recording and play back your audio using adobe air
http://www.adobe.com/devnet/air/flex/articles/using_mic_api.html
8/23/2010 1:43:35 PM
Adobe Flex always wants samples at 44.1khz. While you can set the microphone rate to something other than 44, when you do so you need to scale the output by the same factor.
Basically you always want to output 16384 samples for every call to your sample data event handler. In lots of example code you'll see a loop of 8192 times with two calls to writeFloat. If you sample at 22khz, you should loop 4096 times and call writeFloat 4 times. It's easiest to accomplish this with a second inner loop that calls writeFloat twice for 8192 / (44/rate) times.
8/24/2010 6:29:47 AM
thanks.
It worked. Now my project smooths up.
8/24/2010 6:39:13 AM
i captured at 22khz and made wave file(file bytearray ,in fact) by using AIR's wavwriter class. Uploaded bytearray of file to j2ee by using blazeds rpc. Read the wave file's pcm amplitude content. Also saved the wave file.
I am happy now.