Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 11th March 2014, 15:13   #1  |  Link
daniforever
Registered User
 
Join Date: Aug 2013
Posts: 6
Mixing audio levels over time

HI all,

I want to play music alone for a clip and gradually reduce the volume of this music and make voice appear while music plays in background. I'm trying this using Animate() and MixAudio() but there's not effect on the final audio output.

This is my code for a three seconds audio levels ramp effect:


mp3_sourceA=DirectShowSource("song1.mp3")
mp3_sourceB=DirectShowSource("song2.mp3")

voice=AudioTrim(mp3_sourceA,0,3)
music=AudioTrim(mp3_sourceB,0,3)

result=Animate(0,75,"MixAudio",voice,music,0,1, voice,music,.8,.2)

return result


Thank you in advance!

Last edited by daniforever; 11th March 2014 at 16:09. Reason: completing code
daniforever is offline   Reply With Quote
Old 11th March 2014, 15:51   #2  |  Link
Guest
Guest
 
Join Date: Jan 2002
Posts: 21,923
Did you do:

return result

at the end of your script?
Guest is offline   Reply With Quote
Old 11th March 2014, 16:06   #3  |  Link
daniforever
Registered User
 
Join Date: Aug 2013
Posts: 6
I'm sorry. I just wrote the code describing the problem... thanks.

Quote:
Originally Posted by neuron2 View Post
Did you do:

return result

at the end of your script?
daniforever is offline   Reply With Quote
Old 11th March 2014, 17:18   #4  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,380
Unfortunately, Animate() does not support audio filters.

However, you can do an audio crossfade using Dissolve().
Since you want to bring the second clip up from 0 to 0.2 (rather than a complete crossfade), you need to take the first 3 seconds of a Dissolve() five times as long (15 seconds) to get the required 20% increase.
Code:
result = Dissolve(music, voice, 15, fps=1).AudioTrim(0, 3)
__________________
GScript and GRunT - complex Avisynth scripting made easier
Gavino is offline   Reply With Quote
Old 12th March 2014, 17:25   #5  |  Link
daniforever
Registered User
 
Join Date: Aug 2013
Posts: 6
Gavino, thank you for your help. I'll give Dissolve() a try.

Dr
daniforever is offline   Reply With Quote
Old 12th March 2014, 23:59   #6  |  Link
raffriff42
Retried Guesser
 
raffriff42's Avatar
 
Join Date: Jun 2012
Posts: 1,365
Searching on this problem seems to point to SoxFilter, so I gave it a try:
Code:
# http://forum.doom9.org/showthread.php?t=104792
LoadPlugin(pathBase + "SoxFilter\soxfilter.dll")

## video source
V=Colorbars(pixel_type="YV12").Trim(0, 299).Info 

## audio source #1
A1=V.AmplifyDB(-18)

## audio source #2 (same as #1 with pitch shift & channel swap)
A2=MergeChannels(A1.GetRightChannel, A1.GetleftChannel).TimeStretch(pitch=50.0)

## linear fade out starting at 3 sec, duration 5 sec, silence thereafter
##     fade [type] 0.0 stop-time fade-out-length
A1=A1.AudioTrim(0, -8).SoxFilter("fade t 0.0 8.0 5.0")
\ +A1.AudioTrim(8, -0).Amplify(0)

## silence for 3 sec, then linear fade in, 5 sec duration:
##     fade [type] fade-in-length
A2=A2.AudioTrim(0, -3).Amplify(0)
\ +A2.AudioTrim(3, -0).SoxFilter("fade t 5.0")

## mix both sources:
return V.AudioDub(MixAudio(A1, A2, 1, 1))
\  .Histogram(mode="Audiolevels")
...it works, but attempting to rewind in VirtualDub sometimes causes hangups.

Last edited by raffriff42; 13th March 2014 at 00:03. Reason: tweak
raffriff42 is offline   Reply With Quote
Old 13th March 2014, 01:17   #7  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,380
Also see here, especially if you need a non-linear crossfade.
http://forum.videohelp.com/threads/3...nth-Crossfades
__________________
GScript and GRunT - complex Avisynth scripting made easier
Gavino is offline   Reply With Quote
Old 13th March 2014, 02:28   #8  |  Link
raffriff42
Retried Guesser
 
raffriff42's Avatar
 
Join Date: Jun 2012
Posts: 1,365
Gavino, it was your post in that thread that got me interested in trying Sox - thanks!

I've wrapped the above code in a function now:
Code:
V=Colorbars(pixel_type="YV12").Trim(0, 299).Info 

A1=V.AmplifyDB(-18)
A2=MergeChannels(A1.GetRightChannel, A1.GetleftChannel).TimeStretch(pitch=50.0)

return V.AudioDub(SoxAudioDissolve(A1, A2, starttime=3, fadetime=5))
\  .Histogram(mode="Audiolevels") 

##################################
### cross fade from A1 to A2 
### note sample rates etc must match - see MixAudio
##
## @ starttime  - start of the dissolve in seconds
## @ fadetime   - duration of the dissolve in seconds
## @ fade1_type - per Sox documentation: 
##      "Choices are 'q' for quarter of a sinewave, 'h' for half
##       sinewave, 't' for a linear slope, 'l' for logarithmic, and 
##       'p' for inverted parabola. Default is 't', linear slope"
## @ fade2_type   - see fade1_type
## @ clip1_factor - see MixAudio; default=1.0
## @ clip2_factor - see MixAudio; default=1.0
##
function SoxAudioDissolve(clip A1, clip A2, 
\    float "starttime", float "fadetime", 
\    string "fade1_type", string "fade2_type",
\    float "clip1_factor", float "clip2_factor")
{
    Assert(A1.HasAudio, "SoxAudioDissolve: source A1 has no audio")
    Assert(A2.HasAudio, "SoxAudioDissolve: source A2 has no audio")

    starttime  = Float(Max(0.05, Default(starttime, 0)))
    fadetime   = Float(Max(0.05, Default(fadetime,  1)))
    fade1_type = Default(fade1_type, "t")
    fade2_type = Default(fade2_type, "t")

    clip1_factor = Float(Default(clip1_factor, 1.0))
    clip2_factor = Float(Default(clip2_factor, 1.0))

    tottime   = starttime + fadetime
    stottime  = _FormatTime(0, 0, tottime)
    sfadetime = _FormatTime(0, 0, fadetime)

    Assert(A1.AudioDuration>=tottime, "SoxAudioDissolve: source A1 too short")
    Assert(A2.AudioDuration>=tottime, "SoxAudioDissolve: source A2 too short")

    ## fade [type] 0.0 stop-time fade-out-length
    A1 = A1.AudioTrim(0, -tottime)
    \      .SoxFilter("fade "+fade1_type+" 0.0 "
    \         +stottime+" "+sfadetime)
    \  + A1.AudioTrim(tottime, 0)
    \      .Amplify(0)

    ## fade [type] fade-in-length
    A2 = A2.AudioTrim(0, -starttime)
    \      .Amplify(0)
    \  + A2.AudioTrim(starttime, 0)
    \      .SoxFilter("fade "+fade2_type+" "+sfadetime)

    return MixAudio(A1, A2, clip1_factor, clip2_factor)
}

##################################
## format hours, minutes, seconds as hh:mm:ss.frac
##
function _FormatTime(int t_hours, int t_min, float t_secs)
{
    t_secs = t_secs + t_min*60 + t_hours*60*60

    t_int  = Floor(t_secs)
    t_frac = t_secs - Float(t_int)
    hh     = t_int / (60 * 60)
    mm     = (t_int % (60 * 60)) / 60
    ss     = t_int % 60

    shh = (hh<=0) ? "" : String(hh)+ ":"

    smm = (mm<=0 && shh.StrLen==0) ? "" : String(mm) + ":"
    smm = (smm.StrLen>2 || smm.StrLen==0) ? smm : "0" + smm

    sss = String(ss) + "."
    sss = (sss.StrLen>2 || smm.StrLen==0) ? sss : "0" + sss

    sff = MidStr(String(t_frac, "%1.3f"), 3)

    return (t_secs<0) 
    \  ? "-" + _FormatTime(0, 0, -t_secs) 
    \  : shh + smm + sss + sff
}
...not thoroughly tested. It will probably fail somehow with invalid input; for example if starttime + fadetime add up to be longer than the source audio duration. Unknown behavior on mixed sample rates & channel counts.

And then there is the rewind issue, as mentioned above and in the Sox docs.

EDIT

Last edited by raffriff42; 16th March 2014 at 01:09. Reason: rename functions
raffriff42 is offline   Reply With Quote
Old 16th March 2014, 01:04   #9  |  Link
raffriff42
Retried Guesser
 
raffriff42's Avatar
 
Join Date: Jun 2012
Posts: 1,365
For me, Dissolve() creates audio popping, due to periodic phase inversion (?)
This occurs with any audio format or sample rate.

EDIT - sorry, source was pitch shifted with TimeStretch().
Lesson learned: don't do that; Dissolve works fine otherwise.

Last edited by raffriff42; 16th March 2014 at 01:41.
raffriff42 is offline   Reply With Quote
Old 16th March 2014, 09:09   #10  |  Link
Ghitulescu
Registered User
 
Ghitulescu's Avatar
 
Join Date: Mar 2009
Location: Germany
Posts: 5,555
Just wonder why you would use a video editor for audio specific actions ....

Demux the audio, perform any modifications you want in an audio editor that provides the function you want, remux the audio back. Bingo.
__________________
Born in the USB (not USA)
Ghitulescu is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 16:16.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.