/*
multimux-0.2.5 copyright 2003-always Jan Panteltje (panteltje@yahoo.com).

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

Multimux allows you to add up to 8 mono channels into a wave file.
This wave file can then be used to make an AC3 file with the MS windows
program AC3Machine (GUI for BeSweet), or in Linux with ffmpeg.

So for example:
multimux -d 0,0,0,0  English.wav Spanish.wav French.wav Italian.wav > english-spanish-french-italian.wav

will create a wave file with an English, Spanish, French and Italian channel.
When then encoded with AC3Machine, you would get:
front left   English
front right  Spanish
rear left    French
rear right   Italian

You can then encode to ac3 in Linux with ffmpeg:
ffmpeg -i english-spanish-french-italian.wav -ab 256 -ac 4 -acodec ac3 english-spanish-french-italian.ac3

Doing it without a multichannel wave file:
 If you do not want the intermediate wave file (no space for example), you can try this:
 multimux -f file1.wav file2.wav file3.wav file4.wav file5.wav
 This will call multimux recursively piping through ffmpeg.

Output files > 4GB:
 If you expect an output wave file that is longer then 4GB, the Microsoft wave format can no
 longer be used (it is limited to 4 GB).
 In such a case use the -f flag) to pipe through ffmpeg to make AC3 without an intermediate wave file.

 In raw mode multimux will stop when it sees an EOF or error on ALL input channels, it will
 ignore any size fields in the input wave files, and no output wave header is written.


IMPORTANT:
Input channels MUST be the same format, and mono, only 8 and 16 bit per sample files have
been tested (1 and 2 bytes per sample).
The bitrates must be the same too.

-a bitrate
Normally, when the -f flag is used, multimux will calculate the AC3 bitrate passes to ffmpeg as 64 x number of input files.
(So 320 kb/s for 5 input files).
The -a option lets you force to pass a different bitrate to ffmpeg, but ffmpeg -must_ support that bitrate.

-d delay1,....,delayn
The -d options are optional delays in milliseconds for each channel.

These serve to align the audio, for example in case of translation
often the original language is also audible, and needs to be exactly the
same in each channel (in sync).

You can find out the delays needed with the combine_wave program.
combine_wave can be found on http://panteltje.com/panteltje/dvd/

The delays are the SAME as needed for the multiplexer tcmplex-panteltje,
also on that site.

Often it works like this:
Get a good sync with the video for channel 1, (use the windows program
PVAStrumento to demux for European DVB-s satellite programs for example,
this will fix any delays).

Use:
combine_wave -a -l English.wav -r Spanish.wav -s 30000000
Align audio an write down delay reported.
Now try for next channel:
combine_wave -a -l English.wav -r French.wav -s 30000000 
write down delay and repeat process for all channels

Take the LONGEST delay found, and then add a bit more to the video delay
for the multiplexer.
So it the longest delay is 560 ms, for example use 1000 ms as video delay.
Then calculate the difference:
Measured delays:
0, 732, 748, 964
Use 1000 ms (> 964) for video
Calculate the differences with this:
1000, 268, 252, 36

Subtract 180 ms from the video delay for the DVD standard audio decoding time:
Then in the multiplex use:
-D 820,1000,268,252,36

And in combine wave use:
-d 1000,268,252,36

Convert to ac3 5.1 with AC3Machine in MS windows (uses BeSweet).
Now multiplex the ac3 to the video, and use in tcmplex-panteltje
-D 820,0

Note:
The Microsoft wave file format file size is limited to 4GB, it uses a 32 bit integer for
the data length and the file length:
So the maximum file length is 2^32 = 4 294 967 296 Bytes.
When using the -r flag, piping through ffmpeg, or -f flag, and encoding to AC3 there is no limit.


