View Full Version : Mixing audio levels over time
daniforever
11th March 2014, 15:13
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!
Guest
11th March 2014, 15:51
Did you do:
return result
at the end of your script?
daniforever
11th March 2014, 16:06
I'm sorry. I just wrote the code describing the problem... thanks.
Did you do:
return result
at the end of your script?
Gavino
11th March 2014, 17:18
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.
result = Dissolve(music, voice, 15, fps=1).AudioTrim(0, 3)
daniforever
12th March 2014, 17:25
Gavino, thank you for your help. I'll give Dissolve() a try.
:) Dr
raffriff42
12th March 2014, 23:59
Searching on this problem seems to point to SoxFilter (http://forum.doom9.org/showthread.php?t=104792), so I gave it a try: # 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.
Gavino
13th March 2014, 01:17
Also see here, especially if you need a non-linear crossfade.
http://forum.videohelp.com/threads/355061-Avisynth-Crossfades
raffriff42
13th March 2014, 02:28
Gavino, it was your post in that thread (http://forum.videohelp.com/threads/355061-Avisynth-Crossfades?p=2278794&viewfull=1#post2278794) that got me interested in trying Sox - thanks!
I've wrapped the above code in a function now: 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
raffriff42
16th March 2014, 01:04
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.
Ghitulescu
16th March 2014, 09:09
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.
vBulletin® v3.8.11, Copyright ©2000-2025, vBulletin Solutions Inc.