简体   繁体   中英

Java resample .wav soundfile without third party library

Is it possible to resample a .wav file from 22050 khz to 44100 khz in java without the use of any third party library?

Maybe using AudioInputStream ?

edit: since it seems that without a third party library it isnt possible easily, what third party libraries do exist to accomplish a resampling?

Since you are now accepting Third Party libraries, here is my suggestion

There are a lot of third party libraries that allows you to resample an audio wav file, For me, in my case(And I've recently used) the most user friendly third party library is Jave all you need to do is include the jar. no more tedious installation like other libraries

Jave has a method named

public void setSamplingRate(java.lang.Integer bitRate)

Which allows you to resample audio with the given bitrate.

Its possible, quick and dirty. Just needs some diging in the javax.sound API:

import java.io.File;

import javax.sound.sampled.AudioFileFormat.Type;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import com.sun.media.sound.WaveFileReader;
import com.sun.media.sound.WaveFileWriter;

public class Resample {

public static void main(String[] argv) {
    try {
        File wavFile = new File("C:\\Temp\\test.wav");
        File dstFile = new File("C:\\Temp\\test_half.wav");
        WaveFileReader reader = new WaveFileReader();
        AudioInputStream audioIn = reader.getAudioInputStream(wavFile);
        AudioFormat srcFormat = audioIn.getFormat();

        AudioFormat dstFormat = new AudioFormat(srcFormat.getEncoding(),
                srcFormat.getSampleRate() / 2,
                srcFormat.getSampleSizeInBits(),
                srcFormat.getChannels(),
                srcFormat.getFrameSize(),
                srcFormat.getFrameRate() / 2,
                srcFormat.isBigEndian());

        AudioInputStream convertedIn = AudioSystem.getAudioInputStream(dstFormat, audioIn);

        WaveFileWriter writer = new WaveFileWriter();
        writer.write(convertedIn, Type.WAVE, dstFile);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

}

This short hacked example will create a copy with halved sample rate of the file specified in the source. I think its pretty self explanatory.

Whether you wish to enhance audio quality or not, either way you need to create a new .wav file with samples from the original one. If you want to keep the quality of the original file, you can write every sample twice.

However there is a rather simple algorithm for "improving" quality: read three consecutive samples, let's name them a, b and c for now. Let z be an extra output sample between a and b.

Let z=(a+b)/2. Now replace b with (a + 2b + c)/4. Remember to calculate z using b's old value! Now only write a, z and the new b to the output.

Read one more sample, let's call it d. Calculate the next extra output sample, y, using (b+c)/2. Use b's old value. Recalculate c, let it equal to (b + 2c + d)/4. Write b's new value and y to the output.

You can figure out the guess, read and repeat while !EOF. Remember to calculate using old values, but output new values.

No... You can try writing new wav file with new samples computed as average from adjacent samples. Crude, but maybe it will be sufficient for your purposes.

If you are accepting third-party libraries, JSSRC seems like a good option. You need to build the .jar by yourself, however.

Yes, using AudioInputstream works. You can catch some sample code here:

http://www.jsresources.org/examples/SampleRateConverter.html

Don't worry about Tritonius, that information is no longer relevant to newer versions of Java. You can resample with native Java, without any third party libraries as long as you stick to *.wav files.

Additionally, most audio related questions in Java are answered by this FAQ. It may not be up to date but most of the information is still relevant.

http://www.jsresources.org/faq.html

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