0 Replies Latest reply on Feb 11, 2011 7:05 AM by Marcidus

    Microphone live feedback

    Marcidus Level 1

      Hello, i have been trying to work out a way to get microphone feedback through the Sound object in a simular way to one of the examples on the api.  only without the 4 second delay.

       

      I want to convert microphone data into a bytearray (as shown below) to be sent accross a socket (which i can handle).  However the problem is playing it.  There does not seem to be a simple way to put the mic data straight into the Sound object on the fly, or rather, i cant think of a good way of doing it.  The code below is essentially the same as the api example, but without the 4 second time, in an attempt the make the playback live.  The problem is that the feedback sounds completely wrong and occassionally causes the program to freeze.

       

      private var mic:Microphone = Microphone.getMicrophone();
      private var soundBytes:ByteArray = new ByteArray();
      
      private function init():void {
          mic.setUseEchoSuppression(true);
          mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
      }
      
      private function micSampleDataHandler(event:SampleDataEvent):void {
          while(event.data.bytesAvailable) {
              var sample:Number = (event.data as ByteArray).readFloat();
              soundBytes.writeFloat(sample);
          }
          
          soundBytes.position = 0;
          
          var sound:Sound = new Sound();
          sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
          sound.play();
      }
      
      private function playbackSampleHandler(event:SampleDataEvent):void {
          for (var i:int = 0; i < 8192 && soundBytes.bytesAvailable > 0; i++) {
              var sample:Number = soundBytes.readFloat();
              event.data.writeFloat(sample);
              event.data.writeFloat(sample);
          }
      }
      

       

      What is have figured out is that the sound.play() function is called to often and the start position is always out of sync.  it tried to get around this by using a channel and making it play only when the previous channel is complete, but that caused most of the audio to go missing.

       

      Has anyone found an effective way to do this?

       

      Thank you

       

      marcidus