Hillel
Hillel

Reputation: 331

AudioRecorder quality degradation when change from byte to short and back to byte

I am writing a recording app for android using AudioRecorder Class. The code records audio raw data in a byte array then saves it to a WAV file till now all is well. Here is my problem: I need to change each 2 bytes to one short so i can work on audio data then change it back to bytes so I can write to output stream file. For now there is NO work on audio data just moving from byte to short and back again but for some reason the there is a bad degradation in audio quality here is the code for moving from byte to short and back again:

//16 bit per sample to get correct value of sample convert to short
private short[] ByteToShort (byte[] test)
{
    short[] Sh  = new short[Alldata.size()/2];

    for(int ii=0;ii<Alldata.size()/2;ii++)
    {
        //Sh[ii] = (short) ((short)Alldata.get(2*ii)  | ((short)Alldata.get(2*ii+1))<<8);
        Sh[ii] = (short) ((short)test[2*ii]  | ((short)test[2*ii+1])<<8);

    }
    return Sh;
}
//change back to bytes for input/output streamfiles 
byte[] ShortToByte(short[] data)
{
    byte[] dataByte = new byte[(int) (data.length*2)];
    for(int ii=0;ii<data.length;ii++)
    {
        dataByte[2*ii] = (byte)(data[ii] & 0x00ff);
        dataByte[2*ii+1] = (byte)((data[ii] & 0xff00) >> 8);
    }

    return dataByte;
}

Upvotes: 0

Views: 141

Answers (1)

Gabriel Negut
Gabriel Negut

Reputation: 13960

You have to take into account that byte is signed.

Sh[ii] = (short) (test[2 * ii] & 0xFF | (test[2 * ii + 1] & 0xFF) << 8);

Upvotes: 1

Related Questions