public static AudioInputStream convertChannels(AudioInputStream in, int channels) 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 channels
    if(channels < 1)
      throw new IllegalArgumentException("the number of channels must be greater than one");

    converted = in;

    //convert channels
    sourceFormat = in.getFormat();
    targetFormat = new AudioFormat(sourceFormat.getSampleRate(),
                                   sourceFormat.getSampleSizeInBits(),
                                   channels,
                                   true,
                                   sourceFormat.isBigEndian());

    if (sourceFormat.getChannels() != targetFormat.getChannels())
    {
      if (AudioSystem.isConversionSupported(targetFormat, sourceFormat))
        converted = AudioSystem.getAudioInputStream(targetFormat, converted);
      else
        throw new IllegalArgumentException("Conversion to specified number of channels not supported.");
    }

    return converted;
 }