从 Java 中加入两个 WAV 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/653861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:26:27  来源:igfitidea点击:

Join two WAV files from Java?

javaaudiojavasound

提问by krosenvold

What's the simplest way to concatenate two WAVfiles in Java 1.6? (Equal frequency and all, nothing fancy.)

在 Java 1.6 中连接两个WAV文件的最简单方法是什么?(等频率和所有,没什么特别的。)

(This is probably sooo simple, but my Google-fuseems weak on this subject today.)

(这可能太简单了,但我的Google-fu今天在这个主题上似乎很弱。)

采纳答案by James Van Huis

Here is the barebones code:

这是准系统代码:

import java.io.File;
import java.io.IOException;
import java.io.SequenceInputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class WavAppender {
    public static void main(String[] args) {
        String wavFile1 = "D:\wav1.wav";
        String wavFile2 = "D:\wav2.wav";

        try {
            AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1));
            AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wavFile2));

            AudioInputStream appendedFiles = 
                            new AudioInputStream(
                                new SequenceInputStream(clip1, clip2),     
                                clip1.getFormat(), 
                                clip1.getFrameLength() + clip2.getFrameLength());

            AudioSystem.write(appendedFiles, 
                            AudioFileFormat.Type.WAVE, 
                            new File("D:\wavAppended.wav"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

回答by schnaader

The WAV header should be not be too hard to parse, and if I read this header descriptioncorrectly, you can just strip the first 44 bytes from the second WAV and simply append the bytes to the first one. After that, you should of course change some of the header fields of the first WAV so that they contain the correct new length.

WAV 标头应该不会太难解析,如果我正确阅读了此标头描述,您只需从第二个 WAV 中去除前 44 个字节,然后简单地将字节附加到第一个。之后,您当然应该更改第一个 WAV 的一些头字段,以便它们包含正确的新长度。

回答by McDowell

I found this(AudioConcat) via the "Code Samples & Apps" link on here.

我发现这个通过“代码示例和应用程序”链接(AudioConcat)在这里

回答by ReinstateMonica Larry Osterman

Your challenge though occurs if the two WAV files don't have the exact same format in the wave header.

如果两个 WAV 文件在波形标头中没有完全相同的格式,则会出现您的挑战。

If the wave formats on the two files aren't the same, you're going to have to find a way to transmogrify them so they match.

如果两个文件上的波形格式不同,您将不得不找到一种方法来变形它们以使它们匹配。

That may involve an MP3 transcode or other kinds of transcoding (if one of them is encoded with an MP3 codec).

这可能涉及 MP3 转码或其他类型的转码(如果其中之一使用 MP3 编解码器进行编码)。