[英]mixing two audio file using mixer
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();
}
}
I just found a link http://www.jsresources.org/examples/AudioConcat.html 我刚刚找到了一个链接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.
希望这可以帮助你。
what @Vossi said is true. @Vossi说的是真的。 Use jresources.org to help use.
使用jresources.org来帮助使用。 You could use their example: http://www.jsresources.org/examples/MixingAudioInputStream.java.html .
您可以使用他们的示例: 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.
对于org.tritonus.share.sampled.TConversionTool包,如果您在使用他们的库时遇到问题,请下载他们的源代码:它是开源的,并尝试使用它。 I tried it and it worked for me :D http://sourceforge.net/projects/tritonus/files/ I'm grateful for those guys!
我尝试了它,它对我有用:D http://sourceforge.net/projects/tritonus/files/我很感激那些家伙!
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
. 您需要从两个流中读取样本样本,添加这些样本(注意溢出),然后将新添加的样本存储到新的
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. 检查此处以了解如何将
OutputStream
转换为InputStream
,此时您将能够创建另一个AudioInputStream
并保存音频文件。
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. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.