/*
multimux  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 -o all.wav English.wav Spanish.wav French.wav 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 also 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 from a script:

# make a fifo (fifo is a 'first in first out' type buffer, multimux writes to it, ffmpeg reads from it):
mkfifo ff

# note the '&', multimux operates in the background,
# you can stop it with kill -KILL pid (get pid first with ps -x).
multimux -o ff \
english.wav \
spanish.wav \
french.wav \
italian.wav \
dutch.wav &

# ffmpeg reads from the fifo
ffmpeg -y \
-i ff \
-ab 320 \
-ac 5 \
-acodec ac3 \
english-spanish-french-italian-dutch.ac3



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

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

