Log in

View Full Version : *Depreciated* How can I debug the funtcion I wrote: AudioBlend?


StifflerStealth
12th December 2007, 02:40
EDIT: I stopped working on this and now decided to work on DissolveMod. Search for it. :)

I am still new to AVS and I have read avisynth.org and looked at the internal filters, but I do no know where to begin on validating a complex function. I need to know if all the frames are properly cut and aligned, the values of the functions, and so forth. What are y'all's suggestions on how to start writing debug statements for this?

I guess I should explain the function before I show it. This relates to a previous post I made about wanting help on how to have video cut, but have the audio blend. I wanted more features than just what dissolve had. My attempt at this function is to combine three fairly simple, specific functions into one. The goal was to create a function that will take audio from from clip_1 and have it stop at the video cut point, or fade down to the cut point, but allow audio from clip_2 to be blended into the last part of clip_1, or have the audio from clip one to keep playing past the cut point into clip_2 where clip_2's audio would start at the cut point or fade in from the cut point, or have the audio from both clips be blended with each other, or if you want, have the audio from clip_1 stop playing before the cut point and the audio from clip_2 to start playing long after the cut point ... well ... it doesn't matter where they start and stop as long as there is a blank spot in between. This function would allow me the freedom to choose how much audio gets blended before and after the main video cut point. Sounds complex? I think you may be able to see how I tried to write, but I hope that people here can give me pointers on how to 1) simplify this thing and 2) make sure it is working perfectly.

My main problem is that I have audio going here, so I can't tell which frame the audio is starting and stopping at. Is there a way to throw debug statements onto the screen that come up when the audio is blended? I need to make sure my math was correct.

The function *I take no responsibility if you get a headache from trying to understand this one :P *:
function AudioBlend(clip cLeft, clip cRight,
\ int cLeftStartFadeOut, int cLeftEndFadeOut, int cLeftVidCut,
\ int cRightStartFadeIn, int cRightEndFadeIn, int CRightVidCut,
\ float fps)
{
# This function allows audio from one clip to bleed into another clip.
# The video is cut with no transition. Maybe in a future version of this function.
# NOTE: The cLeft audio can fade out before the end of the cut and the
# cRight audio can start the fade in after the cut. That is why
# I have the fadein end and the fadeout begin variables rather than
# always using the end and beginning of the clips.
# cLeft = The video/audio clip that is on the left side of the cut.
# cRight = The video/audio clip that is on the right side of the cut.
# cLeftStartFadeOut = The number of frames from the _end_ of the clip the fade is to begin.
# cLeftEndFadeOut = The number of frames from the _end_ of the clip the fade ends.
# cLeftVidCut = The number of frames from the _end_ of the clip the video is cut.
# cRightStartFadeIn = The number of frames from the _beginning_ of the clip the fade is to begin.
# cRightEndFadeIn = The number of frames from the _beginning_ of the clip the fade ends.
# cRightVidCut = The number of frames from the _beginning_ of the clip the video is cut.

# Frame counts needed for calculations. Last frame of clip is FC* - 1.
FCLeft = FrameCount(cLeft)
FCRight = FrameCount(cRight)

TotalLen = (FCLeft - cLeftVidCut) + (FCRight - cRightVidCut)
cLeftOffset = TotalLen - (FCLeft - cLeftEndFadeOut)
cRightOffset = TotalLen - FCRight + cRightStartFadeIn

fps = cLeft.FrameRate()

# Convert the frame pointers
cLeftStartFadeOut = FCLeft - cLeftStartFadeOut - 1
cLeftEndFadeOut = FCLeft - cLeftEndFadeOut - 1
cLeftVidCut = FCLeft - cLeftVidCut -1

cRightStartFadeIn = cRightOffset
cRightEndFadeIn = TotalLen - FCRight + cRightEndFadeIn
cRightVidCut = TotalLen - FCRight + cRightVidCut

cLeftFL = cLeftEndFadeOut - cLeftStartFadeOut
cRightFL = cRightEndFadeIn - cRightStartFadeIn

cLeft = cLeft + BlankClip(cLeft,length=(TotalLen - FCLeft),color=$000000)
cRight = BlankClip(cRight,length=(TotalLen - FCRight),color=$000000) + cRight

cLeftAudio = FadeOut0(cLeft.Trim(0,cLeftEndFadeOut),cLeftFL,0,fps) +
\ BlankClip(cLeft,length=cLeftOffset,color=$000000)
cRightAudio = BlankClip(cRight,length=cRightOffset,color=$000000) +
\ FadeIn0(cRight.Trim(cRightStartFadeIn,0),cRightFL,0,fps)

# Overlap occures if the right clip's fade starts before the left clip ends.
# If no overlap, then there is a gap in Audio and that must be taken into account.
# Break audio up into 3 blocks. The middle block has the offset or audio gap.
# MixAudio takes a long time for me, so that's why I want to break things up.
Block1End = (cRightStartFadeIn < cLeftEndFadeOut) ? cRightStartFadeIn - 1 : cLeftEndFadeOut
Block2End = (cRightStartFadeIn < cLeftEndFadeOut) ? cLeftEndFadeOut : cRightStartFadeIn - 1

# Construct the now audio clip with only one use of MixAudio for the overlapping part. :)
Block2Audio = (Block1End == Block2End) ?
\ BlankClip(cLeft, length=0) :
\ MixAudio(cLeftAudio.Trim(Block1End + 1,Block2End),cRightAudio.Trim(Block1End + 1,Block2End),1,1)

V = cLeft.Trim(0,cLeftVidCut) + cRight.Trim(cRightVidCut,0)
A = cLeftAudio.Trim(0,Block1End) + Block2Audio + cRightAudio.Trim(Block2End + 1, 0)

return AudioDub(V,A)
}

EDIT: This should be fully working, but I would like test cases to prove it. :) If this works, you all are welcome to use it in your projects or improve upon it or make it part of AVS. :P

stickboy
12th December 2007, 11:34
If you want to test your math, one easy thing you can do is to generate a test clip, use Tone() on it to make a clearly identifiable audio track, blend it with a clip with no audio, and inspect it with the AudioGraph filter. Then you should test again with the argument clips in the opposite order.

Incidentally--and I haven't looked at your script code carefully to know if this actually matters in your case--but I recommend that you do not call Trim directly in user-defined functions (http://forum.doom9.org/showthread.php?s=&threadid=58358). There are a lot of pitfalls for edge cases.

I also recommend that you not be conservative with assert()s and perform strict validation of the arguments supplied to your function. Bad arguments won't cause AviSynth to crash; instead they'll let people spend a few hours encoding garbage, which is worse.

Also, why do you take fps as an argument when you could easily use whatever the framerate is of the left (or right) clip?

StifflerStealth
12th December 2007, 17:19
Thanks for your reply. I need to read on how to use that AudioGraph filter that you mentioned. I found I could use a function that showed frames, but that was for video and not audio, which is why I requested help.

The reason I input the fps is because I didn't know how to extract the fps info out of the clips for for use in the fadein fadeout functions. Is there a way of doing that? I read that fade functions default to 24 fps when there is no value given, so I have always input the fps for them.

I did have validation at one point but that turned into a long line of ? : type functions that was longer than the function itself. :P How would I use the asserts.

EDIT: There's a function called Trim2? :S I did not know that. I didn't see it in the documentation. I need to google that.

I know I ask a lot of questions, but I really want to learn. :) I hope this doesn't annoy people too much. :$

Thanks for all your help.

sh0dan
12th December 2007, 19:16
fps = float(FrameRateNumerator(clip)) / float(FrameRateDenominator(clip))

stickboy
12th December 2007, 21:13
I did have validation at one point but that turned into a long line of ? : type functions that was longer than the function itself. :P How would I use the asserts.Look at my functions (http://www.avisynth.org/) for examples.
EDIT: There's a function called Trim2? :S I did not know that. I didn't see it in the documentation. I need to google that.If you follow the link in the thread I referenced, then you'll see that Trim2 is a function I wrote.

IanB
12th December 2007, 21:15
fps = FrameRate(clip):p

StifflerStealth
13th December 2007, 03:35
I was able to use AudioGraph to map out the audio to see if I did things right. I was able to turn ShowFrameNumber on and then used my math to check if the frames lined up because I used the same math to cut the audio so it would all line up.

However, I think I noticed something weird with the MixAudio function. I used split vertical function to show one audio stream at a time and offset them so that they would show where they were overlapped. When I finally used MixAudio on overlap section, the audio was greatly reduced. I then used blank audio on the second clip and used MixAudio again, but that fade was still reduced. I have two screenshots. The first one is two sections joined together with using the same cut points but without the second audio stream. The second shot is with using MixAudio with part of the fade and the blank audio stream from the second clip. Just to point out, the same thing happens as with using the tone audio for the second audio, the reduction is still the same. You will see by the screen shots what I mean by reduction.

Screen 1:
http://img.photobucket.com/albums/v193/StifflerStealth/avs/NoMixAudio.gif

Screen 2:
http://img.photobucket.com/albums/v193/StifflerStealth/avs/WithMixAudio.gif

With the second audio file being the tone, the reduction starts right there at the cut and looks the same and the line is about the same thickness since there is audio fading in.

Before the merger of the audio, the split screen I had looked like this:
http://img.photobucket.com/albums/v193/StifflerStealth/avs/FadeOverlap.gif

That shows that both were smooth fades, and then they go through MixAudio and get drastically reduced in volume. :S

I have updated the first post with my latest source. There is no error checking yet, but I wanted to get my algorithm working perfectly. The script should be correct now and the math should be spot on. I was able to simplify that script a great deal from what I had before. It should work for everyone that needs special fading.

This is what it looks like when the fade starts right where the first one ended and at the video cut point, though the video is always black. :P Frame 0 would be next because that's the next clip.
http://img.photobucket.com/albums/v193/StifflerStealth/avs/FadeLineUp.gif

Whew. That took me awhile to figure out. Thanks for everyone's suggestions. They helped me out a lot. Thanks Ian for pointing out that function. I hope someone can answer my concerns on MixAudio.

Is there a way to merge audio streams that doesn't involve loss like that? To overlap them as if they are playing at the same time?

Cheers

IanB
13th December 2007, 06:30
MixAudio, default mixing factor is 0.5/0.5

StifflerStealth
13th December 2007, 19:58
Thanks Ian. You are right. I overlooked that bit. This can be confusing at times. :P

Anyways, the script in it's present form is now mostly error free, but error prone if incorrect values are entered. I need to ad some error checking to it now, which will take awhile. Also, I have been thinking of changing some of the inputs.

Couple remaining questions for you all: Is this a good script idea? Has it been done before? I looked but haven't seen anything like this.

Thanks for all your help and patience. I have learned a lot by working on this. :)

And one more thing I'd like to say to the Devs: AviSynth actually kept the audio in sync. Adobe gradually grew out of sync the closer it got the end. It was a good 5 seconds off by the end of the cut. -_- It took 8 hours of precessing with no filters to do that too. I used filters: Shapeners, deblockers, smoothers, and whatnot with AVS and it was in sync. I'm pissed at Adobe now.

Cheers.