Java Record / Mix Two Audio Streams

I have a java application that records audio from a mixer and saves it in a byte array or saves it to a file. I need to get the sound from two mixers at the same time, and save it to an audio file (I'm trying to use .wav). The fact is that I can get two byte arrays, but I don’t know how to combine them (by "merging" I do not mean concatenation). To be specific, this is an application that processes conversations via a USB modem, and I need to record them (streams are voices for each speaker, already printed for recording them separately).

Are there any tips on how to do this?

Here is my code:

import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.Path; public class FileMixer { Path path1 = Paths.get("/file1.wav"); Path path2 = Paths.get("/file2.wav"); byte[] byte1 = Files.readAllBytes(path1); byte[] byte2 = Files.readAllBytes(path2); byte[] out = new byte[byte1.length]; public FileMixer() { byte[] byte1 = Files.readAllBytes(path1); byte[] byte2 = Files.readAllBytes(path2); for (int i=0; i<byte1.Length; i++) out[i] = (byte1[i] + byte2[i]) >> 1; } } 

Thanks in advance

+4
source share
1 answer

To mix sound waves digitally, you add each corresponding data point from two files together.

 for (int i=0; i<source1.length; i++) result[i] = (source1[i] + source2[i]) >> 1; 

In other words, you take element 0 from byte array 1 and element 0 from byte array two, add them together and put the resulting number in element 0 of your result array. Repeat for other values. To prevent overload, you may need to split each resulting value into two.

+4
source

Source: https://habr.com/ru/post/1499035/


All Articles