I have a collection of short WAV files that I would like to process in Java using various digital signal processing algorithms. I need to get an array of values ββwith many values ββfor this purpose, encoded at a frame rate of 11025 Hz.
The source files have several different sampling rates, including 11025 Hz and 44100 Hz. Here's the code I'm trying to use to read them:
// read the WAV file FileInputStream fileInputStream = new FileInputStream(new File("test.wav")); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(fileInputStream ); // copy the AudioInputStream to a byte array called buffer ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] data = new byte[4096]; int tempBytesRead = 0; int byteCounter = 0; while ((tempBytesRead = audioInputStream.read(data, 0, data.length)) != -1) { bos.write(data, 0, tempBytesRead); byteCounter += tempBytesRead; } bos.close(); byte[] buffer = bos.toByteArray(); AudioFileFormat audioFileFormat = new AudioFileFormat(AudioFileFormat.Type.WAVE, audioInputStream.getFormat(), (int)audioInputStream.getFrameLength()); // get the resulting sample array int[] samples = new int[audioFileFormat.getFrameLength()]; for (int i = 0; i < samples.length; i++) { samples[i] = getSampleValue(i); // the getSampleValue method reads the sample values from the "buffer" array, handling different encoding types like PCM unsigned/signed, mono/stereo, 8 bit/16 bit } // RESULT: the "samples" array
The problem is that the code does not correctly handle different sample rates. Therefore, for a frame rate of 44100 Hz, I get four times more samples than for a frame rate of 11025 Hz. I would like the resulting sample array to use a frame rate of 11025 Hz, regardless of the frame rate of the source file. I tried to get Java to convert the frame rate for me while reading AudioInputStream, but I get an exception similar to the following:
java.lang.IllegalArgumentException: Unsupported conversion: PCM_SIGNED 11025.0 Hz, 16 bit, mono, 2 bytes/frame, 44100.0 frames/second, little-endian from PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:955)
I read the Java Sound API API tutorial: http://java.sun.com/docs/books/tutorial/sound/converters.html . It seems that the Java Sound API does not support such a conversion of my operating system (Windows 7). And I would like to avoid dependencies on any external libraries. Is there a way to do the sampling rate conversion myself?
java windows audio signal-processing javasound
pako
source share