오디오가 서버 소켓에서 GSM
bytray의 오디오 데이터를 가져 오는 중입니다. GSMDecoder를 사용하여 PCM
으로 디코딩하고 재생합니다. 디코더는 길이가 33
인 바이트 배열로 gsm 데이터를 취해 길이 배열 160
의 int 배열을 반환합니다.AudioTrack을 사용하여 PCM을 재생하는 경우 다소 시끄럽습니다.
우선 (for for 루프에서) 저는 GSM bytearray의 길이를 33으로 자르고 디코더에주었습니다.
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);
그런 방법 writeToAudioTrack에 전달되는 PCM 데이터를 포함하는 오디오 스트림()에 : 디코더로부터의 리턴 값은 PCM의 int 배열 된 다음 I 커스텀의 InputStream라는 오디오 스트림을 각각 복호화 청크 (pcmChunk)을 첨가 한 놀이.
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 이미 설정되었습니다
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;
}
}
리턴 값() 메소드
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
그리고
r >> 4에서 r >> 8으로 변경하면 결과가 더 시끄 럽습니다.
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
나는 엔디안 문제라고 생각합니다.