在Java中混合音频文件

在Java中混合音频文件

本文介绍了在Java中混合音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java创建此程序,该程序将导入X个音频文件并将它们混合在1个音频文件中.

I am creating this program in Java that import X number of Audio files and mix them in 1 audio file.

示例:

导入:"Audio1.wav","Audio2.wav".
混合.
导出:"Result.wav"

Import: "Audio1.wav", "Audio2.wav".
Mix them.
Export: "Result.wav"

直到现在我有了导入和导出方法,我的问题是将文件混合成1个文件.

Until now i have the import and export methods, my problem is mixing the files into 1 file.

一些代码.

private static File openDialog(){
    JFileChooser open = new JFileChooser();
    int returnVal = open.showOpenDialog(open);
    if (returnVal == JFileChooser.APPROVE_OPTION){
        return open.getSelectedFile();
    }
    return open.getSelectedFile();
}
private static File saveDialog(){
    JFileChooser save = new JFileChooser();
    FileFilter filter = new FileNameExtensionFilter("Audio files", ".wav");
    save.setFileFilter(filter);
    //save.addChoosableFileFilter(new AudioFilter());
    int returnVal = save.showSaveDialog(save);
    if (returnVal == JFileChooser.APPROVE_OPTION){
        return save.getSelectedFile();
    }
    return save.getSelectedFile();
}

private static List<File> importFile(File file){
    files.add(file);
    audioElements();
    return files;
}

这是我导入文件并保存结果的方式.

This is how I import the files and save the result.

推荐答案

混合两个音频流就像获取样本总和一样容易,即

Mixing two audio streams can be as easy as taking the sum of the samples, that is

result[i] = audio1[i] + audio2[i];

这是假设音频在LPCM中以相同的样本大小和频率进行编码.如果音频不是LPCM(例如µ-law或A-law),则需要一个考虑了非线性编码的公式.如果样本大小不同,则必须转换为相同大小.如果采样频率不同,则必须重新采样.

This is assuming the audio is encoded in LPCM with the same sample size and frequency. If the audio is not LPCM (like µ-law, or A-law) you need a formula that takes the non-linear encoding into account. If the sample sizes are different you have to convert to the same size. If the sample frequencies are different you have to resample.

这篇关于在Java中混合音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 00:57