Im getting audio data in GSM
bytearray from server socket and I'm using this GSMDecoder to decode it to PCM
and play. The decoder takes gsm data in byte array of length 33
and returns int array of length 160
.
First of all (in for loop) I have chopped the GSM bytearray to length of 33 and gave it to the decoder. The returned value from decoder is pcm int array, then I have added each decoded chunk (pcmChunk) to a custom InputStream named AudioStream:
int x = 33;
//p is the GSM bytearray
int len = p.length;
byte[] gsmChunk;
int[] pcmChunk;
for (int i = 0; i < len - x + 1; i += x) {
gsmChunk= Arrays.copyOfRange(p, i, i + x);
pcmChunk= gsmDecoder.decode(gsmChunk);
audioStream.addData(pcmChunk);
}
if (len % x != 0) {//end of array
gsmChunk= Arrays.copyOfRange(p, len - len % x, len);
pcmChunk= gsmDecoder.decode(gsmChunk);
audioStream.addData(pcmChunk);
}
writeToAudioTrack(audioStream);
Then audioStream which contains the pcm data is being passed to method writeToAudioTrack() to play.
private void writeToAudioTrack(InputStream is) throws IOException {
int i;
byte[] s = new byte[BUFFER_SIZE]; //BUFFER_SIZE = 64
while ((i = is.read(s, 0, BUFFER_SIZE)) > -1) {
audioTrack.write(s, 0, i);
}
}
AudioTrack has already been set:
int sampleRate = 4200;
int minBufferSize = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
AudioStream.java:
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class AudioStream extends InputStream {
private ArrayList<Integer> pcm;
public AudioStream() {
pcm = new ArrayList<>();
}
public void addData(int data[]){
for(int i=0 ; i<data.length ; i++){
pcm.add(data[i]);
}
}
@Override
public int read() throws IOException {
if(pcm.size()>0) {
int r= pcm.remove(0);
return (r>>4) & 0xFF;
}
return -1;
}
}
The returned values in read() method of AudioStream:
12-17 18:59:20.788 : 247
12-17 18:59:20.788 : 249
12-17 18:59:20.788 : 252
12-17 18:59:20.788 : 0
12-17 18:59:20.788 : 3
12-17 18:59:20.788 : 247
12-17 18:59:20.788 : 241
12-17 18:59:20.788 : 249
12-17 18:59:20.788 : 6
12-17 18:59:20.788 : 11
12-17 18:59:20.788 : 5
12-17 18:59:20.788 : 249
12-17 18:59:20.788 : 242
12-17 18:59:20.788 : 249
12-17 18:59:20.788 : 5
12-17 18:59:20.788 : 9
12-17 18:59:20.788 : 3
12-17 18:59:20.788 : 247
12-17 18:59:20.788 : 244
12-17 18:59:20.788 : 252
12-17 18:59:20.788 : 3
12-17 18:59:20.788 : 6
12-17 18:59:20.788 : 0
12-17 18:59:20.788 : 252
12-17 18:59:20.788 : 252
12-17 18:59:20.788 : 0
12-17 18:59:20.788 : 1
12-17 18:59:20.788 : 255
12-17 18:59:20.788 : 253
12-17 18:59:20.788 : 252
12-17 18:59:20.788 : 254
12-17 18:59:20.788 : 1
12-17 18:59:20.788 : 5
12-17 18:59:20.788 : 3
12-17 18:59:20.788 : 254
12-17 18:59:20.788 : 0
12-17 18:59:20.788 : 3
12-17 18:59:20.788 : 2
12-17 18:59:20.788 : 4
12-17 18:59:20.788 : 2
12-17 18:59:20.788 : 253
12-17 18:59:20.788 : 253
12-17 18:59:20.788 : 0
12-17 18:59:20.788 : 3
12-17 18:59:20.788 : 10
12-17 18:59:20.788 : 5
12-17 18:59:20.788 : 250
12-17 18:59:20.793 : 250
12-17 18:59:20.793 : 253
12-17 18:59:20.793 : 2
12-17 18:59:20.793 : 12
12-17 18:59:20.793 : 6
12-17 18:59:20.793 : 248
12-17 18:59:20.793 : 248
12-17 18:59:20.793 : 2
12-17 18:59:20.793 : 9
12-17 18:59:20.793 : 19
12-17 18:59:20.793 : 13
12-17 18:59:20.793 : 249
12-17 18:59:20.793 : 244
12-17 18:59:20.793 : 254
12-17 18:59:20.793 : 8
12-17 18:59:20.793 : 15
12-17 18:59:20.793 : 9
12-17 18:59:20.793 : 249
12-17 18:59:20.793 : 245
12-17 18:59:20.793 : 252
12-17 18:59:20.793 : 7
12-17 18:59:20.793 : 14
12-17 18:59:20.793 : 11
12-17 18:59:20.793 : 253
And if I change r>>4 to r>>8 the result is more noisy:
12-17 19:21:36.043 : 1
12-17 19:21:36.043 : 254
12-17 19:21:36.043 : 252
12-17 19:21:36.043 : 255
12-17 19:21:36.043 : 1
12-17 19:21:36.043 : 1
12-17 19:21:36.043 : 0
12-17 19:21:36.043 : 253
12-17 19:21:36.043 : 251
12-17 19:21:36.043 : 255
12-17 19:21:36.043 : 1
12-17 19:21:36.043 : 2
12-17 19:21:36.043 : 2
12-17 19:21:36.043 : 1
12-17 19:21:36.043 : 254
12-17 19:21:36.043 : 253
12-17 19:21:36.043 : 254
12-17 19:21:36.043 : 254
12-17 19:21:36.043 : 0
12-17 19:21:36.043 : 2
12-17 19:21:36.043 : 1
12-17 19:21:36.043 : 255
12-17 19:21:36.043 : 253
12-17 19:21:36.043 : 251
12-17 19:21:36.043 : 254
12-17 19:21:36.043 : 2
12-17 19:21:36.043 : 4
12-17 19:21:36.043 : 1
12-17 19:21:36.043 : 254
12-17 19:21:36.043 : 250
12-17 19:21:36.043 : 251
12-17 19:21:36.043 : 0
12-17 19:21:36.043 : 4
12-17 19:21:36.043 : 4
12-17 19:21:36.043 : 0
12-17 19:21:36.043 : 252
12-17 19:21:36.043 : 251
12-17 19:21:36.043 : 255
12-17 19:21:36.043 : 4
12-17 19:21:36.043 : 4
12-17 19:21:36.043 : 0
12-17 19:21:36.048 : 251
12-17 19:21:36.048 : 250
12-17 19:21:36.048 : 255
12-17 19:21:36.048 : 4
12-17 19:21:36.048 : 5
12-17 19:21:36.048 : 1
12-17 19:21:36.048 : 251
12-17 19:21:36.048 : 249
12-17 19:21:36.048 : 253
12-17 19:21:36.048 : 3
12-17 19:21:36.048 : 5
12-17 19:21:36.048 : 2
12-17 19:21:36.048 : 253
12-17 19:21:36.048 : 251
12-17 19:21:36.048 : 253
12-17 19:21:36.048 : 2
12-17 19:21:36.048 : 3
12-17 19:21:36.048 : 1
12-17 19:21:36.048 : 253
12-17 19:21:36.048 : 251
12-17 19:21:36.048 : 254
12-17 19:21:36.048 : 2
12-17 19:21:36.048 : 3
12-17 19:21:36.048 : 1
12-17 19:21:36.048 : 1
12-17 19:21:36.048 : 0
12-17 19:21:36.048 : 254
12-17 19:21:36.048 : 254
12-17 19:21:36.048 : 255
12-17 19:21:36.048 : 255
I guess it's an endianness problem.
I switched to this decoder and now it's working just fine without any noises!