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

发布于 2025-01-04 11:51:18 字数 1399 浏览 1 评论 0原文

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

    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();
    }
}

how can i mix two audio files into one file so that the resultant file can play two files simultaneously? please help.. here what i am doing is that i am taking two files and concat them into another file.. but i want the file to be played simultaneously..

    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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

缘字诀 2025-01-11 11:51:19

这是我制作的一个类:

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

希望有帮助。

Here is a class I made :

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);
        }
    }
}

It mixes two sounds by adding their volumes.

Use it like that :

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

Hope it helps.

好菇凉咱不稀罕他 2025-01-11 11:51:18

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

看来他正在做..页面上可以找到源代码!希望这对您有帮助。

I just found a link
http://www.jsresources.org/examples/AudioConcat.html

It seems like he is doing it.. The source code can be found on the page! Hope this helps you out.

心舞飞扬 2025-01-11 11:51:18

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

what @Vossi said is true. Use jresources.org to help use. You could use their example: http://www.jsresources.org/examples/MixingAudioInputStream.java.html. For the org.tritonus.share.sampled.TConversionTool package, if you have problems using their library, then download their source code: it's open source, and try using it. I tried it and it worked for me :D http://sourceforge.net/projects/tritonus/files/ I'm grateful for those guys!

星光不落少年眉 2025-01-11 11:51:18

您需要从两个流中逐个读取样本,执行这些样本的添加(小心溢出),然后将新添加的样本存储到新的 AudioInputStream 中。检查此处了解如何转换OutputStreamInputStream,当您能够创建另一个 AudioInputStream 并保存音频文件时。

You need to read sample by sample from both streams, perfrom addition of those samples (be carefull of overflow) and then store new added sample to new AudioInputStream. Check here to see how to convert OutputStream to InputStream, when you'll be able to make another AudioInputStream and save your audio file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文