Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android PCM to Ulaw encoding wav file

I'm trying to encode raw pcm data as uLaw to save on the bandwidth required to transmit speech data.

I have come across a class called UlawEncoderInputStream on This page but there is no documentation! :(

The constructor takes an input stream and a max pcm value (whatever that is).

  /**
     * Create an InputStream which takes 16 bit pcm data and produces ulaw data.
     * @param in InputStream containing 16 bit pcm data.
     * @param max pcm value corresponding to maximum ulaw value.
     */
    public UlawEncoderInputStream(InputStream in, int max) {

After looking through the code, I suspect that i should calculate this "max" value using a supplied function: maxAbsPcm. Problem is, i dont really understand what I'm meant to pass into it! I am recording my raw pcm to a file on the sdcard so I dont have one continuous memory resident array of data to pass to this.

  /**
     * Compute the maximum of the absolute value of the pcm samples.
     * The return value can be used to set ulaw encoder scaling.
     * @param pcmBuf array containing 16 bit pcm data.
     * @param offset offset of start of 16 bit pcm data.
     * @param length number of pcm samples (not number of input bytes)
     * @return maximum abs of pcm data values
     */
    public static int maxAbsPcm(byte[] pcmBuf, int offset, int length) {

Another problem I have using this code is I am unsure what values to write out for the header for uLaw data. How do i determine how much less byte data there is after encoding with uLaw?

I have listened to one of the (potentially) uLaw encoded files that I created in VLC media player (the only player i have that will attempt to read the file) and its sounds nasty, broken and clicky but can still make out the voice.

I am writing my wave header using code similar to a class I found called WaveHeader which can be found Here!

If anyone has any thoughts on this matter I would be most grateful to hear them!:)

Many thanks Dexter

like image 224
Chris Birch Avatar asked Nov 03 '11 10:11

Chris Birch


1 Answers

The max in the constructor is the maximum amplitude in the PCM data. It is used to scale the input before generating the output. If the input is very loud you need a higher value, if it's quiet you need a lower one. If you pass in 0 the encoder will use 8192 by default, which may be good enough.

The length in the other method is the number of 16-bit samples from which you want to find the maximum amplitude. This class assumes that the input PCM data is always encoded with 16-bit samples, which means that each sample spans two bytes: if your input is 2000 bytes long you have 1000 samples.

The encoder in this class produces one 8-bit µ-Law sample for every 16-bit PCM sample, so the size in bytes is halved.

like image 150
Joni Avatar answered Oct 01 '22 18:10

Joni