Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Merging two .wav-files (both in same format) (Read 2622 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Merging two .wav-files (both in same format)

Hello,

i just would like to merge 2 .wav-files into one (not mix, merge!).

The got the same settings (KHz, Bit, ...).

Example:

1.wav (100.000 bytes)
2.wav (200.000 bytes)

I load in this 2 files and remove the first 44 bytes (wav-header)
from the 2.wav and append it to 1.wav. Then i write the new
size of the file into the first wav-header (Byte 4). In this case i would
be 299.964 bytes (300.000 bytes - 44 bytes (header of 2.wav) - 8 bytes).
   
Problem: The new .wav-file only plays the first part (1.wav) and not
the second part after that...

Must i change some things in the subchunks or remove other
tags in the end of both wav-files?

Merging two .wav-files (both in same format)

Reply #1
This is easy to accomplish a number of ways in an audio editor. If you are trying to say you want to write your own program, I guess that fact is no help unless you can get access to existing code.
In CoolEdit/Audition the three simplest ways are:

Open first file in single waveform view.
Open Append second file.
Save As a new file.

Open first file in single waveform view.
Place cursor at very end.
Open second file. This will be in a new window.
Copy second file.
Switch to first file's window
Paste second file.
Save As a new file.

Insert both files into multi-track view (as separate tracks).
Adjust track 2's start position to where you want it after the end of track 1.
Select All.
Mixdown.
Save new file.

Merging two .wav-files (both in same format)

Reply #2
Yep this solution is okay for 3-4 files, but if you have to merge ~50 files then it's really work :-)

So i need a own tool... merging works, but some little thing might be wrong...

Merging two .wav-files (both in same format)

Reply #3
I load in this 2 files and remove the first 44 bytes (wav-header)
from the 2.wav and append it to 1.wav. Then i write the new
size of the file into the first wav-header (Byte 4). In this case i would
be 299.964 bytes (300.000 bytes - 44 bytes (header of 2.wav) - 8 bytes).
   
Problem: The new .wav-file only plays the first part (1.wav) and not
the second part after that...

Must i change some things in the subchunks or remove other
tags in the end of both wav-files?

As Andy says, this is easy to do with an audio editor. But if you want to write code to do it, then here's what you  need to do:

1. Stripping off the first 44 bytes of file 2 is only correct if you know that there are no additional chunks before the data chunk.

2. The number to be written into bytes 4-7 of the new file (where the first byte is byte 0) is the number of bytes in the file minus 8. The file size field here *excludes* the RIFF header and the file size field itself.

3. You must also rewrite the datalength field. This is typically in bytes 40-43 (but could be elsewhere if there are other chunks - basically it's the last four bytes before the actual audio data begins, and immediately following the "data" chunk descriptor). It should contain the number of bytes of data.

4. If the first file had extra stuff at the end, after the audio data, you'll need to remove that. It's easy to tell if this is the case, by reading the datalength field, working out what file size that would imply, and checking whether the file size is greater than this.

Note: all size fields must be written in Intel format (least significant byte first).