# Upmix Library: increase number of audio channels by smart mixing
# Written by tebasuna51 and manolito (http://forum.doom9.org/showthread.php?p=1733767#post1733767) (August 2015)
# Updated by MrC (September 2015)


function UmixMonoStereo(clip a)
{
 l = GetChannels(a, 1)
 r = GetChannels(a, 1)
 return MergeChannels(l, r)
}


function UmixStereoPseudo4(clip a)
{
 sl = MixAudio(GetLeftChannel(a), GetRightChannel(a), 0.473, -0.473) # max 0.5 to avoid clip
 sr = MixAudio(GetRightChannel(a), GetLeftChannel(a), 0.473, -0.473)
 sl = DelayAudio(sl, 0.02)
 sr = DelayAudio(sr, 0.02)
 return MergeChannels(a, sl, sr)
}


function UmixStereoPseudo5(clip a, bool "NormalizeSurround")
{
 NormalizeSurround = default(NormalizeSurround, false)
 cc = Amplify(GetLeftChannel(a), 0)
 sl = MixAudio(GetLeftChannel(a), GetRightChannel(a), 0.473, -0.473) # max 0.5 to avoid clip
 sr = MixAudio(GetRightChannel(a), GetLeftChannel(a), 0.473, -0.473)
 sl = DelayAudio(sl, 0.02)
 sr = DelayAudio(sr, 0.02)
 surr = MergeChannels(sl, sr)
 surr = (NormalizeSurround == false) ? surr : surr.Normalize(0.8)
 return MergeChannels(a, cc, surr)
}


function UmixStereoPseudo6(clip a, bool "NormalizeSurround")
{
 NormalizeSurround = default(NormalizeSurround, false)
 c_lfe = Amplify(a, 0)
 sl = MixAudio(GetLeftChannel(a), GetRightChannel(a), 0.473, -0.473) # max 0.5 to avoid clip
 sr = MixAudio(GetRightChannel(a), GetLeftChannel(a), 0.473, -0.473)
 sl = DelayAudio(sl, 0.02)
 sr = DelayAudio(sr, 0.02)
 surr = MergeChannels(sl, sr)
 surr = (NormalizeSurround == false) ? surr : surr.Normalize(0.8)
 return MergeChannels(a, c_lfe, surr)
}
