public static AudioInputStream convertBitsPerSample(AudioInputStream in, int bitsPerSample) throws IllegalArgumentException
 {
   AudioInputStream converted;
   AudioFormat sourceFormat, targetFormat;

   //check for not null
   if(in == null)
      throw new IllegalArgumentException("the inputstream must not be null values");

  //check number of bits per sample
  if(bitsPerSample!=8 && bitsPerSample!=16 && bitsPerSample!=24 && bitsPerSample!=32)
    throw new IllegalArgumentException("number of bits must be 8, 16, 24 or 32");

   converted = in;

   sourceFormat = in.getFormat();
   targetFormat = new AudioFormat(sourceFormat.getSampleRate(), bitsPerSample,
                               sourceFormat.getChannels(), true,
                               sourceFormat.isBigEndian());

   if (targetFormat.getSampleSizeInBits() != sourceFormat.getSampleSizeInBits())
   {
     if (AudioSystem.isConversionSupported(targetFormat, sourceFormat))
       converted = AudioSystem.getAudioInputStream(targetFormat, converted);
     else
       throw new IllegalArgumentException("conversion to specified sample size (bits) not supported");
   }

   return converted;
 }