3

I'm working on a project where the user have to record his/her voice, and submit it to server. But before submitting the user might need to play the recorded sound.

The application has a recording and playing capabilities with SPEEX codec. But what i found strange and difficult is when i the user plays back the recorded audio, the playing speed is faster or slower than normal that it cannot be understood. As if its fast forwarding.

Here is the sample code:

private var mic:Microphone;  
private var rec:ByteArray;  
private var snd:Sound;  
private var channel:SoundChannel; 

protected function recBtn_clickHandler(event:MouseEvent):void
{  

     rec = new ByteArray();
     mic = Microphone.getMicrophone();
     mic.setLoopBack(false);
     mic.setUseEchoSuppression(true);
     mic.gain = 50;
     mic.setSilenceLevel(5, 1000);
     mic.codec = SoundCodec.SPEEX;  

     mic.addEventListener(SampleDataEvent.SAMPLE_DATA, getMicAudio);  

}  

protected function plyBtn_clickHandler(event:MouseEvent):void
{  

     snd.addEventListener(SampleDataEvent.SAMPLE_DATA, playRecorded);  

     channel = snd.play();              
}     

private function getMicAudio(e:SampleDataEvent): void  
{

     rec.writeBytes(e.data);

}  

private function playRecorded(e:SampleDataEvent): void
{  

     if (!rec.bytesAvailable > 0) return;

     for (var i:int = 0; i < 2048; i++){
          var sample:Number = 0;  
          if (rec.bytesAvailable > 0) sample = rec.readFloat(); 

          for (var j:uint = 0; j < 6; j++) { 
               e.data.writeFloat(sample);
          }
     }  
}

This scenario only happens when:

  1. mic.codec = SoundCodec.SPEEX;
  2. mic.rate = 16

I went through a lot of forums, but could not find any solution for Microphone playback with SPEEX codec or microphone.rate = 16;

Matthias
  • 15,919
  • 5
  • 39
  • 84
user663219
  • 31
  • 1
  • 4

2 Answers2

2

In flash, a sound object plays at 44khz. Since you're sampling at 16khz, you're sending data through the SampleDataEvent Event handler 2.75 faster then you are getting that data.

That is, if you were sending it twice.

But you're actually attempting to solve this problem by writing 3 times faster than what you're recording. This is still not optimum, you'll get a slowed down version of the recording, just a bit, because you're now sending data as if it were recorded at 48 khz, yet you're sending it as 44khz.

There are only two things you can do, and I think you're doing them already.

either adjust how many writes you do per iteration in that for loop. or adjust the max increment(2048) to a higher number, but it can't exceed 8192, I believe.

I had the same problem when i recorded in speex.

Prasad
  • 31
  • 5
1
e.data.writeFloat(sample);

e.data.writeFloat(sample);

e.data.writeFloat(sample);

e.data.writeFloat(sample);
if (i%3)
{
    e.data.writeFloat(sample);
    e.data.writeFloat(sample);
}
RomanGuzi
  • 11
  • 1