java stereo to mono conversion unsupported conversion error

I am trying to convert a stereo wav file into mono here is my code:
AudioInputStream inputAIS = AudioSystem.getAudioInputStream(sourceFile);

if (inputAIS.getFormat().getChannels() == 2)
		{
			
			inputAIS = convertChannels(1, inputAIS);
			if (DEBUG) out("stream: " + inputAIS);
			
		}



 private static AudioInputStream convertChannels(
            int nChannels,
            AudioInputStream sourceStream) {
        AudioFormat sourceFormat = sourceStream.getFormat();
        AudioFormat targetFormat = new AudioFormat(
                sourceFormat.getEncoding(),
                sourceFormat.getSampleRate(),
                sourceFormat.getSampleSizeInBits(),
                nChannels,
                calculateFrameSize(nChannels,
                sourceFormat.getSampleSizeInBits()),
                sourceFormat.getFrameRate(),
                sourceFormat.isBigEndian());
        return AudioSystem.getAudioInputStream(targetFormat, sourceStream);
    }

 private static int calculateFrameSize(int nChannels, int nSampleSizeInBits)
	{
		return ((nSampleSizeInBits + 7) / 8) * nChannels;
	}

Open in new window


And I am getting an error:

Exception in thread "main" java.lang.IllegalArgumentException: Unsupported conversion: PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian from PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
        at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:955)
        at sound.AudioDataBuffer.convertChannels(AudioDataBuffer.java:46)
        at sound.AudioDataBuffer.main(AudioDataBuffer.java:73)
Java Result: 1

Function convertChannels comes from here: http://www.jsresources.org/examples/AudioConverter.java.html
chudyksgAsked:
Who is Participating?
 
chudyksgConnect With a Mentor Author Commented:
I solved it. It needs Tritonous pcm2pcm converter installed.
0
 
chudyksgAuthor Commented:
I found the solution on my own
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.