I am currently recieving audio from a netty channel, i get a ByteBuff object type, and then write a file, byte by byte first into a byte array and then in a file, to do that i have the following function (consider that the offset of the first 22 bytes it's because i need the to identify the type of message in my program you can ignore it
public byte[] knowAMR (ByteBuf in){
byte[] bytes = null;
if(in.capacity() > 22) {
// from 22 is where we must find "TK,"
char T = (char) in.getByte(20);
char K = (char) in.getByte(21);
char coma = (char) in.getByte(22);
if (T == 'T' && K == 'K' && coma == ',') {
bytes = new byte[in.capacity() - 22];
for (int i = 23; i < in.capacity() - 22; i++) {
bytes[i - 23] = in.getByte(i);
}
//We are at the start of a new audio, so we start over
try {
String fileName = "/tmp/ranneke_audio/audio_" + this.brace_id;//brace id is a String as a property of the object
Path path = Paths.get(fileName);
if (Files.exists(path) && bytes != null) {
Files.delete(path);
log.info("Starting new audio file");
}
if (bytes != null) {
log.info("writing new audio file");
Files.write(path, bytes);
}
}
catch (IOException IOe){
IOe.printStackTrace();
}
}
}
//In case that the audio message is too long we proceed to check every header to know if we need to append to the file
if ((char)in.getByte(0) != '[' && (char)in.getByte(1) != '3' && (char)in.getByte(2) != 'G')
{
//we continue our previus audio but this time there's no header, so we just buffer
bytes = new byte[in.capacity()];
for(int i=0; i<in.capacity();i++)
{
bytes[i] = in.getByte(i);
}
try {
//if it already exists we buffer it
String fileName = "/tmp/ranneke_audio/audio_" + this.brace_id;
Path path = Paths.get(fileName);
if (Files.exists(path) && bytes != null) {
log.info("Agregado datos al audio");
Files.write(path, bytes, StandardOpenOption.APPEND);
}
}
catch (IOException IOe){
IOe.printStackTrace();
}
}
return bytes;
}
This is written for springframework under java 11
The output of the log seems like this (Encoded under UTF-8):
[3G, 5740129922, 008E, TK,#!AMR
May 30 14:05:15 ranneke-01 java[2671176]: }#004��|
May 30 14:05:15 ranneke-01 java[2671176]: �#001���/P�&/���`�R}#004��|
May 30 14:05:15 ranneke-01 java[2671176]: �#001���/P�&/���`�R}#004��|
May 30 14:05:15 ranneke-01 java[2671176]: �#001���/P�&/���`�R}#004��2#006�#001<��zD�#031O��#021#014��}#004��j#026�
May 30 14:05:15 ranneke-01 java[2671176]: ���#020(�\.�y^#014�p}#004��@=��kN�z���}#001#017K5m#015(]
And the binary file seems like this:
2321 414d 520a 7d04 9ec0 7c00 ff01 fcb9
a32f 508b 262f b3f5 8360 af52 7d04 9ec0
7c00 ff01 fcb9 a32f 508b 262f b3f5 8360
af52 7d04 9ec0 7c00 ff01 fcb9 a32f 508b
262f b3f5 8360 af52 7d04 a6a3 3206 e001
3c92 f77a 44ff 194f c9f4 110c d5ec 7d04
85a9 6a16 8000 99ba f910 28f6 5c2e 8879
5e0c 8d70 7d04 a294 403d 8ea0 6b4e cf7a
81a8 f37d 010f 4b35 6d0d 285d 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000
What am i doing wrong?
i expect to play the audio correctly right now i can just hear noise, in the other hand, i have tested the microphone (it works well) and i tested encoding it in UTF-8 leave it as bytebuff and put it byte by byte in the file
I'm lost please help