title: Convert PCM WAV to ULAW WAV in JAVA
body: <p>I am Writing a web based Voice Recorder with a Javascript/HTML5/JSF frontend, and a Glassfish (Java) backend.</p>
  
  <p>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 <code>getUserMedia()</code>) 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.</p>
  
  <p>So for now what I've been trying to do is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I found the following article of someone trying to do this in Android:</p>
  
  <p><a href="https://stackoverflow.com/questions/7993507/android-pcm-to-ulaw-encoding-wav-file">Android PCM to Ulaw encoding wav file</a></p>
  
  <p>However the class this article references is either not working or I am not using it properly.</p>
  
  <p>My current <code>convertPCMtoULAW(FileItem file)</code> Java method:</p>
  
  <pre><code>//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 &lt; length; i++)
  {
     pcmSamples += ((int) pcmBytes[offset+i] &amp; 0xffL) &lt;&lt; (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
  </code></pre>
  
  <p>(all code compiles, the above is simplified to include the necessary parts)</p>
  
  <p>I am consistently getting back only 512 bytes read in the variable byteLength.</p>
  
  <p>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.</p>
  
  <p>I get an error when opening the file on the Server side after I have attempted to encode it.</p>
  
  <p>My main question is:  <strong>Has/Can anyone successfully use the class in the linked page to encode from PCM to ULAW?</strong></p>