Log in

View Full Version : AudioChannels() in an avsi file


Mr_Odwin
24th June 2006, 18:30
I'm using Avisynth 2.56a
I have a clip with no audio and I normally use an avsi file that I've written to create a blank soundtrack (or to downmix to stereo if the file requires).

Here is my avsi file:


Function FAVCStereo(clip Video, clip Audio) {

Dubbed = Video

Chan0 = (AudioChannels(Audio)==0) ? AudioDub(Video, BlankClip(Video, audio_rate=48000, stereo=true)) : nop()
Dubbed = (AudioChannels(Audio)==0) ? Chan0 : Dubbed

Audio = (AudioChannels(Audio)>=1) ? ConvertAudioToFloat(Audio) : nop()

Chan1 = (AudioChannels(Audio)==1) ? Audiodub(Video, GetChannel(Audio, 1, 1)) : nop()
Dubbed = (AudioChannels(Audio)==1) ? Chan1 : Dubbed

Chan2 = (AudioChannels(Audio)==2) ? Audiodub(Video, Audio) : nop()
Dubbed = (AudioChannels(Audio)==2) ? Chan2 : Dubbed

FrontLeft = (AudioChannels(Audio)==3) ? MixAudio(GetChannel(Audio, 1), GetChannel(Audio, 3), 0.5, 0.5) : nop()
FrontRight = (AudioChannels(Audio)==3) ? MixAudio(GetChannel(Audio, 2), GetChannel(Audio, 3), 0.5, 0.5) : nop()
Chan3 = (AudioChannels(Audio)==3) ? Audiodub(Video, MergeChannels(FrontLeft, FrontRight)) : nop()
Dubbed = (AudioChannels(Audio)==3) ? Chan3 : Dubbed

TotalLeft = (AudioChannels(Audio)==4) ? MixAudio(GetChannel(Audio, 1), GetChannel(Audio, 3), 0.5, 0.5) : nop()
TotalRight = (AudioChannels(Audio)==4) ? MixAudio(GetChannel(Audio, 2), GetChannel(Audio, 4), 0.5, 0.5) : nop()
Chan4 = (AudioChannels(Audio)==4) ? Audiodub(Video, MergeChannels(TotalLeft, TotalRight)) : nop()
Dubbed = (AudioChannels(Audio)==4) ? Chan4 : Dubbed

FrontLeft = (AudioChannels(Audio)==5) ? MixAudio(GetChannel(Audio, 1), GetChannel(Audio, 3), 0.3694, 0.2612) : nop()
FrontRight = (AudioChannels(Audio)==5) ? MixAudio(GetChannel(Audio, 2), GetChannel(Audio, 3), 0.3694, 0.2612) : nop()
TotalLeft = (AudioChannels(Audio)==5) ? MixAudio(FrontLeft, GetChannel(Audio, 4), 1, 0.3694) : nop()
TotalRight = (AudioChannels(Audio)==5) ? MixAudio(FrontRight, GetChannel(Audio, 5), 1, 0.3694) : nop()
Chan5 = (AudioChannels(Audio)==5) ? Audiodub(Video, MergeChannels(TotalLeft, TotalRight)) : nop()
Dubbed = (AudioChannels(Audio)==5) ? Chan5 : Dubbed

#The following works with DirectShowSource and NicAudio
BackLeft = (AudioChannels(Audio)>=6) ? MixAudio(GetChannel(Audio, 5), GetChannel(Audio, 4), 0.2698, 0.2698) : nop()
BackRight = (AudioChannels(Audio)>=6) ? MixAudio(GetChannel(Audio, 6), GetChannel(Audio, 4), 0.2698, 0.2698) : nop()
FrontLeft = (AudioChannels(Audio)>=6) ? MixAudio(GetChannel(Audio, 1), GetChannel(Audio, 3), 0.2698, 0.1906) : nop()
FrontRight = (AudioChannels(Audio)>=6) ? MixAudio(GetChannel(Audio, 2), GetChannel(Audio, 3), 0.2698, 0.1906) : nop()
TotalLeft = (AudioChannels(Audio)>=6) ? MixAudio(FrontLeft, BackLeft, 1, 1) : nop()
TotalRight = (AudioChannels(Audio)>=6) ? MixAudio(FrontRight, BackRight, 1, 1) : nop()
Chan6 = (AudioChannels(Audio)>=6) ? Audiodub(Video, MergeChannels(TotalLeft, TotalRight)) : nop()
Dubbed = (AudioChannels(Audio)>=6) ? Chan6 : Dubbed


Dubbed = (AudioChannels(Audio)>=1) ? Normalize(Dubbed) : nop()
Dubbed = (AudioChannels(Audio)>=1) ? SSRC(Dubbed, 48000) : nop()
Dubbed = (AudioChannels(Audio)>=1) ? ConvertAudioTo16bit(Dubbed) : nop()

return(Dubbed)
}


Here is my script:
Import("C:\Documents and Settings\James\My Documents\FAVC\Avisynth Plugins\FAVCStereo.avsi")
AVISource("C:\Documents and Settings\James\My Documents\FAVC\To Do\RS_NoAudio.avi").EnsureVBRMP3Sync
FAVCStereo(Last,Last)

Here is the resulting error:

http://homepages.nildram.co.uk/~pike/FS_Error.jpg

Line 10 of the avsi? Is that not weird?

Here is the video clip (http://homepages.nildram.co.uk/~pike/RS_NoAudio.avi) if anyone cares to test it out. But I've tried this with other clips that have no audio and it also doesn't work. The avsi file works great for clips that have audio. No errors at all.

If anyone can shed some light on this I'd much appreciate it. Some of you may be thinking "well just omit the line in the avs script and all will be well", but this script is produced as part of an automated process and knowing where the avsi file is going wrong would help me a lot.

Wilbert
24th June 2006, 19:49
Use HasAudio().

Mr_Odwin
24th June 2006, 20:12
Cheers, use it where?

stickboy
24th June 2006, 22:02
AudioChannels seems to be unreliable for clips witsh no audio.
MessageClip(String(BlankClip().KillAudio().AudioChannels()))gives me 1.
MessageClip(String(BlankClip(stereo=true).KillAudio().AudioChannels()))gives me 2.
MessageClip(String(AVISource("silent.avi").AudioChannels()))gives me 0.

Is there any reason for AudioChannels not to return 0 in all cases?

Anyway, I haven't been able to reproduce Mr_Odwin's error independently with AviSynth 2.5.6a, and I don't think AudioChannels should be failing in the first place.

IanB
25th June 2006, 03:04
AVSValue AudioChannels(AVSValue args, void*, IScriptEnvironment* env) { return VI(args[0]).nchannels; }Yes a minor bug. nchannels has crap in it when there is no audio. It will be fixed in the next 2.5.7 release.

Mr_Odwin
25th June 2006, 20:30
It will be fixed in the next 2.5.7 release.

Great stuff.

stickboy
26th June 2006, 01:33
Well, in the meantime, you can do:
function MyAudioChannels(clip c)
{
return c.HasAudio() ? c.AudioChannels() : 0
}and then use MyAudioChannels everywhere.

Mr_Odwin
29th June 2006, 10:01
Well, in the meantime, you can do:
function MyAudioChannels(clip c)
{
return c.HasAudio() ? c.AudioChannels() : 0
}and then use MyAudioChannels everywhere.

Trying this, the following avsi file:

function MyAudioChannels(clip c) {
return c.HasAudio() ? c.AudioChannels() : 0
}

Function FAVCStereo(clip Video, clip Audio) {

Dubbed = Video

Chan0 = (MyAudioChannels(Audio)==0) ? AudioDub(Video, BlankClip(Length=FrameCount(Video), FPS=FrameRate(Video), Audio_Rate=48000, Stereo=True, Sixteen_Bit=True)) : nop()
Dubbed = (MyAudioChannels(Audio)==0) ? Chan0 : Dubbed

Audio = (MyAudioChannels(Audio)>=1) ? ConvertAudioToFloat(Audio) : nop()

Chan1 = (MyAudioChannels(Audio)==1) ? Audiodub(Video, GetChannel(Audio, 1, 1)) : nop()
Dubbed = (MyAudioChannels(Audio)==1) ? Chan1 : Dubbed

Chan2 = (MyAudioChannels(Audio)==2) ? Audiodub(Video, Audio) : nop()
Dubbed = (MyAudioChannels(Audio)==2) ? Chan2 : Dubbed

FrontLeft = (MyAudioChannels(Audio)==3) ? MixAudio(GetChannel(Audio, 1), GetChannel(Audio, 3), 0.5, 0.5) : nop()
FrontRight = (MyAudioChannels(Audio)==3) ? MixAudio(GetChannel(Audio, 2), GetChannel(Audio, 3), 0.5, 0.5) : nop()
Chan3 = (MyAudioChannels(Audio)==3) ? Audiodub(Video, MergeChannels(FrontLeft, FrontRight)) : nop()
Dubbed = (MyAudioChannels(Audio)==3) ? Chan3 : Dubbed

TotalLeft = (MyAudioChannels(Audio)==4) ? MixAudio(GetChannel(Audio, 1), GetChannel(Audio, 3), 0.5, 0.5) : nop()
TotalRight = (MyAudioChannels(Audio)==4) ? MixAudio(GetChannel(Audio, 2), GetChannel(Audio, 4), 0.5, 0.5) : nop()
Chan4 = (MyAudioChannels(Audio)==4) ? Audiodub(Video, MergeChannels(TotalLeft, TotalRight)) : nop()
Dubbed = (MyAudioChannels(Audio)==4) ? Chan4 : Dubbed

FrontLeft = (MyAudioChannels(Audio)==5) ? MixAudio(GetChannel(Audio, 1), GetChannel(Audio, 3), 0.3694, 0.2612) : nop()
FrontRight = (MyAudioChannels(Audio)==5) ? MixAudio(GetChannel(Audio, 2), GetChannel(Audio, 3), 0.3694, 0.2612) : nop()
TotalLeft = (MyAudioChannels(Audio)==5) ? MixAudio(FrontLeft, GetChannel(Audio, 4), 1, 0.3694) : nop()
TotalRight = (MyAudioChannels(Audio)==5) ? MixAudio(FrontRight, GetChannel(Audio, 5), 1, 0.3694) : nop()
Chan5 = (MyAudioChannels(Audio)==5) ? Audiodub(Video, MergeChannels(TotalLeft, TotalRight)) : nop()
Dubbed = (MyAudioChannels(Audio)==5) ? Chan5 : Dubbed

BackLeft = (MyAudioChannels(Audio)>=6) ? MixAudio(GetChannel(Audio, 5), GetChannel(Audio, 4), 0.2698, 0.2698) : nop()
BackRight = (MyAudioChannels(Audio)>=6) ? MixAudio(GetChannel(Audio, 6), GetChannel(Audio, 4), 0.2698, 0.2698) : nop()
FrontLeft = (MyAudioChannels(Audio)>=6) ? MixAudio(GetChannel(Audio, 1), GetChannel(Audio, 3), 0.2698, 0.1906) : nop()
FrontRight = (MyAudioChannels(Audio)>=6) ? MixAudio(GetChannel(Audio, 2), GetChannel(Audio, 3), 0.2698, 0.1906) : nop()
TotalLeft = (MyAudioChannels(Audio)>=6) ? MixAudio(FrontLeft, BackLeft, 1, 1) : nop()
TotalRight = (MyAudioChannels(Audio)>=6) ? MixAudio(FrontRight, BackRight, 1, 1) : nop()
Chan6 = (MyAudioChannels(Audio)>=6) ? Audiodub(Video, MergeChannels(TotalLeft, TotalRight)) : nop()
Dubbed = (MyAudioChannels(Audio)>=6) ? Chan6 : Dubbed


Dubbed = (MyAudioChannels(Audio)>=1) ? Normalize(Dubbed) : nop()
Dubbed = (MyAudioChannels(Audio)>=1) ? SSRC(Dubbed, 48000) : nop()
Dubbed = (MyAudioChannels(Audio)>=1) ? ConvertAudioTo16bit(Dubbed) : nop()

return(Dubbed)
}


works perfectly with files that have audio, but a file with no audio produces this error:

http://img220.imageshack.us/img220/5628/error20dh.jpg

saying there is an error on line 14 (The 'Chan1' line). An error with MyAudioChannels, but the function has obviously worked well enough in the lines of code just above it. Weird?


Edit: Oh man, I just figured it out. I don't know if it's me being dumb or if it's a bug. If there was no audio and a line was used like

Audio = (AudioChannels(Audio)>=1) ? Normalize(Dubbed) : nop()
return(Audio)


Then it balks, but
Audio = (AudioChannels(Audio)>=1) ? Normalize(Dubbed) : Audio
return(Audio)


works fine.

stickboy
29th June 2006, 11:10
Audio = (AudioChannels(Audio)>=1) ? Normalize(Dubbed) : nop()
return(Audio)


Then it balks, but
Audio = (AudioChannels(Audio)>=1) ? Normalize(Dubbed) : Audio
return(Audio)


works fine.Oh. Yeah, that'd be a problem. Assigning nop() to anything is pretty nonsensical.

IanB
29th June 2006, 14:15
A point of noteBlankClip(... FPS=FrameRate(Video), ...)may cause problems with mismatching framerates, due to the inexact single precision floating point representation from FrameRate().

To produce a BlankClip() clone of another clip useBlankClip(video)This will use the parameters of the clip video as the template (including the exact framerate ratio), any specified parameter will override the template parameter.