简体   繁体   中英

How to read compressed .wav audio files in Java?

I would like to read an audio file which is downsampled from 22050Hz to 8000Hz. The buffer size was 512 for 22050Hz. Is there any solution for this or any other approach to read the downsampled .wav files in java?

The approach to read the audio file was implemented using the ReadExample class from http://www.labbookpages.co.uk/audio/javaWavFiles.html which worked properly for 22050Hz original audio.
When I downsampled the audio using Python's librosa and wrote the file at 8000Hz, to read this file using the ReadExample method it threw an exception -

Error :WavFileException: Compression Code 3 not supported WavFileException: Compression Code 3 not supported

Process finished with exit code -1


code is -

import java.io.*;

public class ReadExample
{
   public static void main(String[] args)
   {
      try
      {

         WavFile wavFile = WavFile.openWavFile(new File(args[0]); //22050Hz filepath

         wavFile.display();
         int numChannels = wavFile.getNumChannels(); //numChannels = 1

         double[] buffer = new double[512 * numChannels];

         int framesRead;
         double min = Double.MAX_VALUE;
         double max = Double.MIN_VALUE;

         do
         {

            framesRead = wavFile.readFrames(buffer, 512);

            for (int s=0 ; s<framesRead * numChannels ; s++)
            {
               if (buffer[s] > max) max = buffer[s];
               if (buffer[s] < min) min = buffer[s];
            }
         }
         while (framesRead != 0);
         wavFile.close();
         System.out.printf("Min: %f, Max: %f\n", min, max);
      }
      catch (Exception e)
      {
         System.err.println(e);
      }
    }
}

The expected result was to obtain values in the buffer as usual.

The issue is perhaps you have not used java to downsample audio.
Either

downsample .wav using java and then check its working. Good reference for Java-based downsampling and this

OR

resample .wav using third party libraries and test. Reference for Re-sample using third-party library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM