I am Writing a web based Voice Recorder with a Javascript/HTML5/JSF frontend, and a Glassfish (Java) backend.
I need to save the recorded .WAV file with ULAW encoding. However as far as I can tell the only way to record audio in HTML5/Javascript (with getUserMedia()
) is in PCM encoding. I would love a simple way to capture the client side recording in ULAW, but have been unable to find any way to do this.
So for now what I've been trying to do is:
Record in PCM wav (client side)
Upload to server using JSP
Pass the received FileItem into a JAVA converter method that returns a byte array in ULAW
I found the following article of someone trying to do this in Android:
Android PCM to Ulaw encoding wav file
However the class this article references is either not working or I am not using it properly.
My current convertPCMtoULAW(FileItem file)
Java method:
//read the FileItem's InputStream into a byte[]
InputStream uploadedStream = file.getInputStream();
byte[] pcmBytes = new byte[(int) file.getSize()];
uploadedStream.read(pcmBytes);
//extract the number of PCM Samples from the header
int offset =40;
int length =4;
int pcmSamples = 0;
for (int i = 0; i < length; i++)
{
pcmSamples += ((int) pcmBytes[offset+i] & 0xffL) << (8 * i);
}
//create the UlawEncoderInputStream (class in link above)
is = new UlawEncoderInputStream(
file.getInputStream(),
UlawEncoderInputStream.maxAbsPcm(pcmBytes, 44, pcmSamples/2)
);
//read from the created InputStream into another byte[]
byteLength = is.read(ulawBytes);
//I then add the ULAW header to the beginning of the byte[]
//and pass the entire byte[] through a pre-existing Wav Verifier method
//which serializes the byte[] later on
(all code compiles, the above is simplified to include the necessary parts)
I am consistently getting back only 512 bytes read in the variable byteLength.
I know I'm uploading a correct PCM WAV to Glassfish, because I can download and listen to my recording directly from the Javascript side.
I get an error when opening the file on the Server side after I have attempted to encode it.
My main question is: Has/Can anyone successfully use the class in the linked page to encode from PCM to ULAW?