PDA

View Full Version : Simple Audio Script Function Challenge


Umamio
14th October 2008, 19:44
Following from this post (http://forum.doom9.org/showthread.php?p=1178485#post1178485) I was wondering if anyone knew how to turn this pseudo-code into a working avisynth script:

introVideo = AviSource("Intro.avi")
introStereoAudio =WavSource("IntroAudio-Stereo.wav")
introSixChannelAudio = WavSource("IntroAudio-51.wav")

filmVideo = dgdecode_mpeg2source("Wahwahwah.d2v")
filmAudio = ac3Source("SourceAudioOfDoom.ac3")

function AudioMaker(int channels)
{
if channels>0
{
if (channels > 5) # 5.1 and above
{
if (channels == 6)
audio = introSixChannelAudio
else
audio = MergeChannels(introSixChannelAudio, AudioMaker(channels-6))
}
else
{
if ((channels%2) == 1) #OddNumber of Channels
{
if (channels == 1)
audio = introStereoAudio.ConvertToMono
else
audio = MergeChannels(introStereoAudio , AudioMaker(channels-2))
}
else #Even Channels
{
if (channels == 2)
audio = introStereoAudio
else
audio = MergeChannels(introStereoAudio , AudioMaker(channels-2))
}
}
}
else { assert("No audio, fool! Avisynth explosion imminent") }

return audio
}

AudioDub(introVideo, AudioMaker(filmAudio.audioChannels)) ++ AudioDub(filmVideo, filmAudio)

The idea is, I have an intro video with a stereo source and a 5.1 source (fake 5.1, the stereo was run through besweet and converted to 5.1) and I want to be able to join the intro to films that can have any number of audio channels (>0 , <infinity)

The original idea was:
"I am thinking of making a poor man's audio mixer for the intro audio. Along the lines of:
If film has 1 audio channel, Merge Stereo -> Mono
If film has 2 audio channels, Use stereo
If film has 3 audio channels, Use stereo (left and right) channels + used merged audio on centre channel
if film has 4 audio channels, use stereo on front 2 and back 2,
if film has 5 audio channels, use stereo on front 2 and back 2, use merged stereo on centre
if film has 6 audio channels, use 5.1 source"
But the position of the audio does not matter so much at the moment.

Thanks for any help!

gzarkadas
17th October 2008, 21:05
This untested function (hopefully :)) reproduces the logic in your post, after some logically equivalent rearrangements made that you can easily identify. I have made the audio clips parameters; this frees the code from global dependencies.

function AudioMaker(int channels, clip stereo, clip sixchannel) {
Assert(channels > 0, "AudioMaker: cannot produce audio with less than 1 channel(s)")
return channels > 6 \
? MergeChannels(sixchannel, AudioMaker(channels - 6, stereo, sixchannel)) \
: (channels == 6 \
? sixchannel \
: (channels > 2 \
? MergeChannels(stereo, AudioMaker(channels - 2, stereo, sixchannel)) \
: (channels == 2 \
? stereo \
: \
stereo.ConvertToMono() \
)))
}

Umamio
21st October 2008, 20:10
It works! Thank you very much.
I did not realise that you could use MergeChannels to combine more than just 2 mono channels. I should have tested before making a post... whoops. That will teach me to not read the wiki properly.

Also, thank you for streamlining the redundancies out of my recursive function!