0

I would have a problem on converting data from a byte array to a float array. After a certain operation it have to go back to a byte array.

My application is to record audio and so far no problem. Now I would also like to include filters. For the filters I found this class available to everyone:

https://cardinalpeak.com/blog/a-c-class-to-implement-low-pass-high-pass-and-band-pass-filters/

and here on stackoverflow its conversion to java:

Implementing a High Pass filter to an audio signal

I know the filters want float values. I used the methods I knew, found even on stackoverflow, on converting from byte to float, and from float to byte, but nothing worked. These are the methods I know:

ByteArrayInputStream bufferIN = new ByteArrayInputStream(audioArray);
DataInputStream dataIN = new DataInputStream(bufferIN);
float[] floatAudio = new float[audioArray.length / 4];
for (int i = 0; i < floatAudio.length; i++) {
     floatAudio[i] = dataIN.readFloat();
}


ByteArrayOutputStream bufferOUT = new ByteArrayOutputStream();
DataOutputStream dataOUT = new DataOutputStream(bufferOUT);
for (float i : floatAudio)
     dataOUT.writeFloat(i);
audioArray = bufferOUT.toByteArray();

Searching I find this discussion where for the bytes from AudioRecord one has to do a double operation since they are PCM data.

How to convert 16-bit PCM audio byte-array to double or float array?

I did it but the problem is I do not know how to continue because the conversion from float to bytes, in the audio, give problems with the usual methods. I think it's not the fault of the filter class because for testing, I made a byte conversion to float and immediately after by bytes to float but the audio is distorted. So what should I do? What would be the right conversion to do so that I can work on bytes in the AudioRecord field?

I leave part of my code (I remove the declarations, are the classic ones) where registration is done.

byte audioArray[] = new byte[dimBufferAR];

FileOutputStream audioOUT = null;

try {
   audioOUT = new FileOutputStream(pathFile);

   while (REC) {

      AR.read(audioArray, 0, dimBufferAR);

      // Conversion should take place here

      audioOUT.write(audioArray, 0, dimBufferAR);
   }

  audioOUT.close();

}

Thanks for your attention.

Fantastico
  • 31
  • 7

1 Answers1

0

Try : Jumping to next 4 bytes for each .readFloat().

float[] floatAudio = new float[audioArray.length / 4];
for (int i = 0; i < floatAudio.length; i=i) 
{
     floatAudio[i] = dataIN.readFloat();
     i += 4;
}
VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Hi, thank you for having responded to me. I implemented your idea but unfortunately did not work. Both with and without the filter. The result is squeaky sound. I have a question. From float[] to byte[], I have implemented the code I showed in the post of the question, did I have to try something else or was that okay? Thank you. – Fantastico Oct 26 '17 at 15:10