2 Replies Latest reply on Nov 2, 2013 10:02 AM by botchniaque

    how can i play flex microphone 8 bit rate stream on server?

    kamiomar

      Hi,

       

      My objective is to record microphone stream from the client (design in flex) and send it to the server (design in java).

       

      My microphone setting that I define in the flex are following:

       

      mic = Microphone.getMicrophone();

                  Security.showSettings("2");

                  if (mic != null) {

                      mic = Microphone.getMicrophone();

                      //mic.setLoopBack(false);

                      mic.encodeQuality = 6;

                      //mic.setSilenceLevel( 0, 6000 );

                      mic.gain = 100;

                      mic.rate = 8;

                  }

       

       

      Before sending it to the server I encode the stream into wave file. The code is following:

       

       

      public function encode( samples:ByteArray, channels:int=1, bits:int=8, rate:int=8000 ):ByteArray

                          {

                                    var data:ByteArray = create( samples );

       

                                    _bytes.length = 0;

                                    _bytes.endian = Endian.LITTLE_ENDIAN;

       

                                    _bytes.writeUTFBytes( WaveEncoder.RIFF );

                                    _bytes.writeInt( uint( data.length + 44 ) );

                                    _bytes.writeUTFBytes( WaveEncoder.WAVE );

                                    _bytes.writeUTFBytes( WaveEncoder.FMT );

                                    _bytes.writeInt( uint( 8 ) );

                                    _bytes.writeShort( uint( 1 ) );

                                    _bytes.writeShort( channels );

                                    _bytes.writeInt( rate );

                                    _bytes.writeInt( uint( rate * channels * ( bits >> 3 ) ) );

                                    _bytes.writeShort( uint( channels * ( bits >> 3 ) ) );

                                    _bytes.writeShort( bits );

                                    _bytes.writeUTFBytes( WaveEncoder.DATA );

                                    _bytes.writeInt( data.length );

                                    _bytes.writeBytes( data );

                                    _bytes.position = 0;

       

                                    return _bytes;

                          }

       

                          private function create( bytes:ByteArray ):ByteArray

                          {

                                    _buffer.endian = Endian.LITTLE_ENDIAN;

                                    _buffer.length = 0;

                                    bytes.position = 0;

       

                                    while( bytes.bytesAvailable )

                                              _buffer.writeShort( bytes.readFloat() * (0x7fff * _volume) );

                                    return _buffer;

                          }


       

      The problem is that instead of voice and I am listening noise on server.

      How can I encode microphone 8 bit rate stream into mono, 8 bit rate and 8000khz stream?

        • 1. Re: how can i play flex microphone 8 bit rate stream on server?
          Claudiu Ursica Level 4

          Why can't you stream it to the server?

          C

          • 2. Re: how can i play flex microphone 8 bit rate stream on server?
            botchniaque

            Hi Kamiomar,

            that was exactly my problem I have just had. It's been a long time since you posted the question, so it's probably no use for you but it may help others (I was looking for help here ). 

             

            I needed to format the samples to 8bit 8kHz wav format. Like you I am also using org.bytearray.micrecorder.encoder.WaveEncoder and I found a bug there. I have posted an issue http://code.google.com/p/micrecorder/issues/detail?id=1 for it. Hopefully the library will be fixed one day

             

            The below code encodes correctly wav as 8 and 16 bit:

             

            public function encode( samples:ByteArray, channels:int=2, bits:int=16, rate:int=44100 ):ByteArray

                {

                    var data:ByteArray = create(samples, bits);

             

             

                    _bytes.length = 0;

                    _bytes.endian = Endian.LITTLE_ENDIAN;

             

             

                    _bytes.writeUTFBytes( WaveEncoder.RIFF );

                    _bytes.writeInt( uint( data.length + 44 ) );

                    _bytes.writeUTFBytes( WaveEncoder.WAVE );

                    _bytes.writeUTFBytes( WaveEncoder.FMT );

                    _bytes.writeInt( uint( 16 ) );  // You also changed this value, but this one describes the length of data following it (channels, bitrate info etc.)

                    _bytes.writeShort( uint( 1 ) );

                    _bytes.writeShort( channels );

                    _bytes.writeInt( rate );

                    _bytes.writeInt( uint( rate * channels * ( bits >> 3 ) ) );

                    _bytes.writeShort( uint( channels * ( bits >> 3 ) ) );

                    _bytes.writeShort( bits );

                    _bytes.writeUTFBytes( WaveEncoder.DATA );

                    _bytes.writeInt( data.length );

                    _bytes.writeBytes( data );

                    _bytes.position = 0;

             

             

                    return _bytes;

                }

             

             

                private function create(bytes:ByteArray, bits:int):ByteArray

                {

                    _buffer.endian = Endian.LITTLE_ENDIAN;

                    _buffer.length = 0;

                    bytes.position = 0;

             

                    // The problem was that 8bit sample depth means storing an unsigned byte, whereas 16bit sample is a signed short

                    if (bits == 16) {

                        while( bytes.bytesAvailable ){

                            _buffer.writeShort(readSignedShort(bytes) );

                        }

                    } else if (bits == 8){

                        while( bytes.bytesAvailable ) {

                            _buffer.writeByte(signedShortToUnsignedByte(readSignedShort(bytes)));

                        }

                    }

                    return _buffer;

                }

             

                private function signedShortToUnsignedByte(signedShort:int):int {

                    var signedByte:int = signedShort >> 8;

                    var unsignedByte:int = signedByte + 0x7f;

                    return unsignedByte;

                }

             

                private function readSignedShort(bytes:ByteArray):Number {

                    return bytes.readFloat() * (0x7fff * _volume);

                }