Java resample .wav soundfile without third party library

巧了我就是萌 提交于 2019-12-10 14:58:11

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/15410725/java-resample-wav-soundfile-without-third-party-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!