Darnell Christian
Darnell Christian

Reputation: 1

Combine 2 wav files as one simultaneous song

I have a program that records input from a mic while music is playing. When done, the voice is saved as a wav file. I would like to combine that wav file with the wav file song that was playing during recording. Example: song1.wav plays in the background as person is singing. The recording of that person singing is now recording1.wav. I want to combine song1.wav and recording1.wav to play simultaneously and become one song song recording finalsong1.wav. I am a beginner in Java and have yet to find a solution, or even a starting point beside how to concatenate them, which is the opposite of what I'd like to do.

Upvotes: 0

Views: 150

Answers (1)

Phil Freihofner
Phil Freihofner

Reputation: 7910

I am going to list the steps involved. These steps have been covered multiple times and should be straightforward to research.

  • For both wav files, read in using a AudioInputStream.

  • As the data arrives, convert the bytes to signed PCM. If the format is 16-bit, the PCM can be signed shorts, or scaled/normalized to floats that range from -1 to 1.

  • Use addition to combine the corresponding data (e.g., frame 1 right channel of voice with frame 1 right channel of music). If the signals are too "hot" it may be necessary to enforce a min and max function to prevent the data from exceeding the bounds of the range, as this sounds really terrible when it happens.

  • Convert the summed, signed PCM data back to bytes according to the audio format.

  • Write to a wav file.

I know of tools that can mix tracks for playback, but I don't recall one that will automatically save the results to wav. One likely exists somewhere, or should be easy to write or commission.

Upvotes: 1

Related Questions