简体   繁体   English

没有第三方库的Java重新采样.wav声音文件

[英]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? 是否可以在不使用任何第三方库的情况下将java中的.wav文件从22050 khz重新采样到44100 khz?

Maybe using AudioInputStream ? 也许使用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. 有很多第三方库允许你重新采样音频wav文件,对我来说,在我的情况下(我最近使用过)最友好的第三方库是Jave所有你需要做的就是包括jar 。 no more tedious installation like other libraries 没有像其他库那样繁琐的安装

Jave has a method named Jave有一个名为的方法

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: 只需要在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. 无论您是否希望提高音频质量,您都需要使用原始样本创建新的.wav文件。 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. 然而,有一个相当简单的“改善”质量的算法:读取三个连续的样本,让我们现在将它们命名为a,b和c。 Let z be an extra output sample between a and b. 设z是a和b之间的额外输出样本。

Let z=(a+b)/2. 设z =(a + b)/ 2。 Now replace b with (a + 2b + c)/4. 现在用(a + 2b + c)/ 4替换b。 Remember to calculate z using b's old value! 记得用b的旧值计算z! Now only write a, z and the new b to the output. 现在只将a,z和新b写入输出。

Read one more sample, let's call it d. 再读一个样本,我们称之为d。 Calculate the next extra output sample, y, using (b+c)/2. 使用(b + c)/ 2计算下一个额外输出样本y。 Use b's old value. 使用b的旧值。 Recalculate c, let it equal to (b + 2c + d)/4. 重新计算c,让它等于(b + 2c + d)/ 4。 Write b's new value and y to the output. 将b的新值和y写入输出。

You can figure out the guess, read and repeat while !EOF. 你可以弄清楚猜测,阅读和重复!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. 不...您可以尝试编写新的wav文件,其中新样本按相邻样本的平均值计算。 Crude, but maybe it will be sufficient for your purposes. 原油,但也许它足以满足您的需求。

If you are accepting third-party libraries, JSSRC seems like a good option. 如果您接受第三方库, JSSRC似乎是一个不错的选择。 You need to build the .jar by yourself, however. 但是,您需要自己构建.jar。

Yes, using AudioInputstream works. 是的,使用AudioInputstream工作。 You can catch some sample code here: 你可以在这里找到一些示例代码:

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

Don't worry about Tritonius, that information is no longer relevant to newer versions of Java. 不要担心Tritonius,该信息不再与较新版本的Java相关。 You can resample with native Java, without any third party libraries as long as you stick to *.wav files. 只要您坚持使用* .wav文件,就可以使用本机Java重新采样,无需任何第三方库。

Additionally, most audio related questions in Java are answered by this FAQ. 此外,本FAQ中回答了Java中大多数与音频相关的问题。 It may not be up to date but most of the information is still relevant. 它可能不是最新的,但大多数信息仍然相关。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用Java获取第三方库版本 - get third party library versions in Java 将第三方库添加到 Java 小程序 - Adding a Third Party Library to Java Applet 有没有一种简单的方法可以将 java 字符串转换为包括变音符号在内的标题大小写,无需第三方库 - Is there a simple way to convert java string to title case including diacritics, without third party library 是否可以在没有任何第三方库的情况下使用@RequestBody将JSON转换为Java bean - Is it possible to covert JSON to java bean with @RequestBody without any third party library 如果没有第三方库,您可以在Java 7中进行基本的EC操作吗? - Can you do basic EC operations in Java 7 without a third-party library? 使用第三方Java库重新实现AES加密,不受美国法律限制 - Reimplement AES encryption using third-party Java library without US law limitations 如何在不使用第三方库的情况下使用Java中另一个CSV文件中的值创建CSV文件 - How to create a CSV file using the values from another CSV file in Java without using third party library 没有Proguard规则的第三方库会导致Proguard错误 - Third party library without Proguard rules causing Proguard error 没有第三方库,如何完整解析HTML? - How can I full parsing HTML without third party library? Kotlin-Android序列化json不使用第三方库 - Kotlin-Android serialize json without using third party library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM