0 Replies Latest reply on Aug 18, 2010 11:20 PM by hsenag_tiwari

    sound record and play at same sampling rate

    hsenag_tiwari

      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:


      <code>
      <?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>

      </code>