使用混音器混合两个音频文件

时间:2012-02-10 11:10:47

标签: java audio javasound mixer

如何将两个音频文件混合到一个文件中,以便生成的文件可以同时播放两个文件?请帮助..这里我正在做的是我正在拿两个文件并将它们连接到另一个文件..但我希望文件同时播放..

    private void saveAudio1() {
    try {                                      

        AudioInputStream clip1 = AudioSystem.getAudioInputStream(file1);
        AudioInputStream clip2 = AudioSystem.getAudioInputStream(file2);
        Collection list=new ArrayList();

        AudioInputStream appendedFiles =
                new AudioInputStream(
                new SequenceInputStream(clip1, clip2),
                clip1.getFormat(),
                clip1.getFrameLength() + clip2.getFrameLength());
        if (dlgOpenFile == null) {
            dlgOpenFile = new FileDialog(this, "Save As...", FileDialog.SAVE);
        }
        if (cfgJMApps != null) {
            nameFile = cfgJMApps.getLastOpenFile();
        }
        if (nameFile != null) {
            dlgOpenFile.setFile(nameFile);
        }

        dlgOpenFile.show();
        nameFile = dlgOpenFile.getFile();
        if (nameFile == null) {
            return;
        }

        nameFile = dlgOpenFile.getDirectory() + nameFile;
        if (cfgJMApps != null) {
            cfgJMApps.setLastOpenFile(nameFile);
        }


        AudioSystem.write(appendedFiles,
                AudioFileFormat.Type.WAVE,
                new File(nameFile));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

4 个答案:

答案 0 :(得分:1)

我刚刚找到一个链接 http://www.jsresources.org/examples/AudioConcat.html

好像他正在这样做..源代码可以在页面上找到!希望这会帮助你。

答案 1 :(得分:0)

您需要从两个流中读取样本样本,添加这些样本(小心溢出),然后将新添加的样本存储到新AudioInputStream。检查here以了解如何将OutputStream转换为InputStream,此时您还可以制作另一个AudioInputStream并保存音频文件。

答案 2 :(得分:0)

@Vossi说的是真的。使用jresources.org来帮助使用。您可以使用他们的示例:http://www.jsresources.org/examples/MixingAudioInputStream.java.html。对于org.tritonus.share.sampled.TConversionTool包,如果您在使用他们的库时遇到问题,请下载他们的源代码:它是开源的,并尝试使用它。我尝试了它,它对我有用:D http://sourceforge.net/projects/tritonus/files/我很感激那些人!

答案 3 :(得分:-1)

这是我上过的课程:

class MixedSound extends Thread {
    protected AudioFormat format; //Both streams must have same format
    protected AudioInputStream sound1;
    protected AudioInputStream sound2;
    protected static SourceDataLine ausgabe;
    protected DataLine.Info data;

    public MixedSound(String path1, String path2) {
        try {
        sound1 = AudioSystem.getAudioInputStream(new File(path1));
        sound2 = AudioSystem.getAudioInputStream(new File(path2));
        format=sound1.getFormat(); //I assume both streams have same format
        data=new DataLine.Info(SourceDataLine.class,format);
        ausgabe=(SourceDataLine) AudioSystem.getLine(data);
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException bug) {
            System.err.println(bug);
        }
    }
    public synchronized void play() throws IOException, LineUnavailableException {
        ausgabe.open(format,1024);
        ausgabe.start();
        byte[] buffer1=new byte[1024];
        byte[] buffer2=new byte[1024];
        byte[] mixed=new byte[1024];
        int bytes_sound1=sound1.read(buffer1,0,buffer1.length);
        int bytes_sound2=sound2.read(buffer2,0,buffer2.length);
        while (bytes_sound1 != -1 || bytes_sound2 != -1) {
            for (int i=0; i < mixed.length; i++) {
                mixed[i]=(byte)Math.min(0.999f,((float)buffer1[i]+(float)buffer2[i])); //Mix them
            }
            ausgabe.write(mixed, 0, Math.max(bytes_sound1, bytes_sound2));
            buffer1=new byte[1024]; //Clean buffer
            buffer2=new byte[1024]; //Clean buffer
            bytes_sound1=sound1.read(buffer1,0,buffer1.length);
            bytes_sound2=sound2.read(buffer2,0,buffer2.length);
        }
        ausgabe.drain();
        ausgabe.close();
    }
    @Override
    public synchronized void run() {
        try {
            play();
        } catch (IOException | LineUnavailableException ex) {
            System.err.println(ex);
        }
    }
}

通过添加音量来混合两种声音。

使用它:

MixedSound sound=new Sound("sound1.wav","sound2.wav");
sound.start(); //Play it
System.out.println("Started playing sound"); //Do stuff at same time

希望它有所帮助。

相关问题