TimeStretch

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
m (added category)
(formatting, links, phrasing, link fixes *** TODO default values ***)
Line 1: Line 1:
{{Template:FuncDef|TimeStretch(clip ''clip'' [, float ''tempo'', float ''rate'', float ''pitch'', int ''sequence'', int ''seekwindow'', int ''overlap'', bool ''quickseek'', int ''aa''])}}
+
Change the audio speed and/or [[Wikipedia:Pitch_(music)|pitch]]:
 +
{| border="0"
 +
|style="line-height:1.2em"|{{BoldColor|blue|100|•}} {{FuncArg|tempo}}
 +
|style="line-height:1.2em"|adjusts speed while maintaining pitch.
 +
|-
 +
|style="line-height:1.2em"|{{BoldColor|blue|100|•}} {{FuncArg|rate}}
 +
|style="line-height:1.2em"|adjusts speed while allowing pitch to rise or fall.
 +
|-
 +
|style="line-height:1.2em"|{{BoldColor|blue|100|•}} {{FuncArg|pitch}}
 +
|style="line-height:1.2em"|adjusts pitch while maintaining speed.
 +
|}
 +
You can use these parameters in any combination – for example, 104% {{FuncArg|tempo}} with 95% {{FuncArg|pitch}}.
  
TimeStretch allows changing the sound tempo, pitch and playback rate parameters independently from each other, i.e.:
 
  
* Sound tempo can be increased or decreased while maintaining the original pitch.
+
__TOC__
* Sound pitch can be increased or decreased while maintaining the original tempo.
+
* Change playback rate that affects both tempo and pitch at the same time.
+
* Choose any combination of tempo/pitch/rate.
+
  
'''Parameters:'''
+
==== Syntax and Parameters ====
 +
<div style="max-width:62em" >
 +
{{FuncDef
 +
|TimeStretch(clip ''clip'' [, float ''tempo'', float ''rate'', float ''pitch'', <br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int ''sequence'', int ''seekwindow'', int ''overlap'', bool ''quickseek'', int ''aa'' ] )
 +
}}
  
The speed parameters are percentages, and defaults to 100. If tempo is 200 it will play twice as fast, if it is 50, it will play at half the speed. Adjusting rate is equivalent to using [[AssumeSampleRate]] and [[ResampleAudio]].
+
:{{FuncArg|clip|clip|}}
 +
::Audio sample type is automatically converted to [[Float]].
 +
::If {{FuncArg|clip}}.[[Clip_properties|AudioChannels]]=2, special processing is used to preserve [[Wikipedia:Stereo_imaging|stereo imaging]]. Otherwise, channels are processed independently. Independent processing works well for unrelated audio tracks, for not very well for [[Wikipedia:5.1_surround_sound|surround sound]]; for that, try [[TimeStretchPlugin]] instead.
  
The time-stretch algorithm has a few parameters that can be tuned to optimize sound quality for certain applications. The current default parameters have been chosen by iterative if-then analysis (read: "trial and error") to obtain the best subjective sound quality in pop/rock music processing, but in applications processing different kind of sound the default parameter set may result in a sub-optimal result.
+
<div style="margin:0 0 0 0.5em" >
 +
===== &nbsp;''Tempo, Rate and Pitch'' =====
 +
</div>
 +
:{{Par2|tempo|float|100.0}}
 +
::Changes speed and {{FuncArg|clip}}.[[Clip_properties|AudioLength]] while maintaining the original [[Wikipedia:Pitch_(music)|pitch]].
 +
::If {{FuncArg|tempo}}=200, the audio will play twice (200%) as fast; if {{FuncArg|tempo}}=50, the audio will play half (50%) as fast.
  
The time-stretch algorithm default parameter values are
+
:{{Par2|rate|float|100.0}}
 +
::Changes speed and {{FuncArg|clip}}.[[Clip_properties|AudioLength]] while allowing [[Wikipedia:Pitch_(music)|pitch]] to rise or fall.
 +
::Adjusting {{FuncArg|rate}} is equivalent to adjusting the speed of a [[Wikipedia:Phonograph|record player]] or [[Wikipedia:Tape_recorder|tape recorder]].
 +
::If {{FuncArg|rate}}=200, the audio will play twice (200%) as fast; if {{FuncArg|rate}}=50, the audio will play half (50%) as fast.
  
  Sequence    82
+
::By the way, the following script:
  SeekWindow  28
+
<div {{BoxWidthIndent|24|6}} >
  Overlap      12
+
  TimeStretch(rate=105)
 +
</div>
 +
::is equivalent to
 +
<div {{BoxWidthIndent|24|6}} >
 +
  ar=[[Clip_properties|AudioRate]]
 +
  [[AssumeSampleRate]](Round(ar*1.05))
 +
[[ResampleAudio]](ar)
 +
</div>
  
These parameters affect the time-stretch algorithm as follows:
+
:{{Par2|pitch|float|100.0}}
 +
::Changes [[Wikipedia:Pitch_(music)|pitch]] while maintaining the length of the original sample: ''{{FuncArg|clip}}.[[Clip_properties|AudioLength]]'' is not changed.
 +
::(within a small tolerance &ndash; see [[#Notes|Notes]] below)
  
* '''Sequence''': This is the length of a single processing sequence in milliseconds which determines how the original sound is chopped in the time-stretch algorithm. Larger values mean fewer sequences are used in processing. In principle a larger value sounds better when slowing down the tempo, but worse when increasing the tempo and vice versa.
+
:{{FuncArg|Tempo}}, {{FuncArg|rate}} and {{FuncArg|pitch}} can all be adjusted independently, in which case their effects are added together.
* '''SeekWindow''': The seeking window length in milliseconds is for the algorithm that searches for the best possible overlap location. This determines from how wide a sample "window" the algorithm can use to find an optimal mixing location when the sound sequences are to be linked back together. The bigger this window setting is, the higher the possibility of finding a better mixing position becomes, but at the same time large values may cause a "drifting" sound artifact because neighboring sequences may be chosen at more uneven intervals. If there's a disturbing artifact that sounds as if a constant frequency was drifting around, try reducing this setting.
+
* '''Overlap''': The overlap length in milliseconds. When the sound sequences are mixed back together to form a continuous sound stream again, this parameter defines how much of the ends of the consecutive sequences will be overlapped. This shouldn't be that critical parameter. If you reduce the Sequence setting by a large amount, you might wish to try a smaller value on this.
+
* '''QuickSeek''': The time-stretch routine has a 'quick' mode that substantially speeds up the algorithm but may degrade the sound quality.
+
* '''aa''': Controls the number of tap the Anti-alias filter uses for the rate changer. Set to 0 to disable the filter. The value must be a multiple of 4.
+
  
The table below summarizes how the parameters can be adjusted for different applications:
+
<div style="margin:0 0 0 0.5em" >
 +
===== &nbsp;''Advanced Parameters'' =====
 +
</div>
 +
:The time-stretch algorithm has a few parameters that can be tuned to optimize sound quality for certain applications. The current default parameters have been chosen by iterative if-then analysis (read: "trial and error") to obtain the best subjective sound quality in pop/rock music processing, but in applications processing different kind of sound the default parameter set may return a sub-optimal result.
  
{| border="1"
+
:{{Par2|sequence|int|82 *}}
 +
::This is the length of a single ''processing sequence'' in milliseconds, which determines how the original sound is chopped in the time-stretch algorithm. Larger values mean fewer, and longer, sequences are used. In general,
 +
::* a larger {{FuncArg|sequence}} value sounds better with a lower {{FuncArg|tempo}} and/or {{FuncArg|pitch}};
 +
::* a smaller {{FuncArg|sequence}} value sounds better with a higher {{FuncArg|tempo}} and/or {{FuncArg|pitch}}.
 +
 
 +
:{{Par2|seekwindow|int|28 *}}
 +
::The length in milliseconds for the algorithm that searches for the best possible overlap location. For larger {{FuncArg|seekwindow}} values, the possibility of finding a better mixing position increases, but too-large {{FuncArg|seekwindow}} values may cause a "drifting" sound (a disturbing artifact where what ''should'' be steady frequencies &ndash; such as piano notes &ndash; seem to drifting around) because neighboring sequences may be chosen at more uneven intervals.
 +
 
 +
:{{Par2|overlap|int|12 *}}
 +
::The overlap length in milliseconds. When the sound sequences are mixed back together to form a continuous sound stream again, {{FuncArg|overlap}} defines how much of the ends of the consecutive sequences will be overlapped. This shouldn't be a critical parameter. If you reduce the {{FuncArg|sequence}} by a large amount, you might wish to try a smaller {{FuncArg|overlap}}.
 +
 
 +
:{{Par2|quickseek|bool|true? *}}
 +
::The time-stretch routine has a 'quick' mode that substantially speeds up the algorithm but may degrade the sound quality.
 +
 
 +
:{{Par2|aa|int|? *}}
 +
::Controls the number of ''taps'' the [[Wikipedia:Finite_impulse_response|anti-alias filter]] uses. Set to 0 to disable the filter. Must be a multiple of 4.
 +
 
 +
:'''*''' [[TODO]] ''( default values are under investigation at the time of updating this documentation )''
 +
 
 +
 
 +
:This table summarizes how these parameters can be adjusted for different applications:
 +
:{| class="wikitable"
 
|-
 
|-
| '''Parameter name'''
+
| '''Parameter'''
| '''Default value magnitude'''
+
| '''Default value'''
| '''Larger value affects ...'''
+
| '''If larger...'''
| '''Smaller value affects ...'''
+
| '''If smaller...'''
 
| '''Music'''
 
| '''Music'''
 
| '''Speech'''
 
| '''Speech'''
| '''Effect in CPU burden'''
+
| '''CPU burden'''
 
|-
 
|-
| Sequence
+
|style="vertical-align:top"| {{FuncArg|sequence}}
| Default value is relatively large, chosen for slowing down music tempo.
+
|style="vertical-align:top"| Relatively large, chosen for slowing music tempo.
| Larger value is usually better for slowing down tempo. Growing the value decelerates the "echoing" artifact when slowing down the tempo.
+
|style="vertical-align:top"| Usually better for slowing tempo. You might need less {{FuncArg|overlap}}.
| Smaller value might be better for speeding up tempo. Reducing the value accelerates the "echoing" artifact when slowing down the tempo.
+
|style="vertical-align:top"| Accelerates "echoing" artifact when slowing down the tempo.
| Default value usually good.
+
|style="vertical-align:top"| Default value usually good.
| A smaller value than default might be better.
+
|style="vertical-align:top"| A smaller value might be better.
| Increasing the parameter value reduces computation burden.
+
|style="vertical-align:top"| Smaller value increases CPU burden.
 
|-
 
|-
| SeekWindow
+
|style="vertical-align:top"| {{FuncArg|seekwindow}}&nbsp;
| Default value is relatively large, chosen for slowing down music tempo.
+
|style="vertical-align:top"| Relatively large, chosen for slowing music tempo.
| Larger value eases finding a good mixing position, but may cause a "drifting" artifact.
+
|style="vertical-align:top"| Eases finding a good mixing position, but may cause "drifting" artifact.
| Smaller reduce possibility to find a good mixing position, but reduce the "drifting" artifact.  
+
|style="vertical-align:top"| Makes finding a good mixing position more difficult.  
| Default value usually good, unless a "drifting" artifact is disturbing.
+
|style="vertical-align:top"| Default usually good, unless "drifting" is a problem.
| Default value usually good.
+
|style="vertical-align:top"| Default usually good.
| Increasing the parameter value increases computation burden.
+
|style="vertical-align:top"| Larger value increases CPU burden.
 
|-
 
|-
| Overlap
+
|style="vertical-align:top"| {{FuncArg|overlap}}
| Default value is relatively large, chosen to suit with above parameters.
+
|style="vertical-align:top"| Relatively large, chosen to suit above parameters.
 +
|
 
|  
 
|  
| If you reduce the "sequence ms" setting, you might wish to try a smaller value.
 
 
|
 
|
 
|  
 
|  
| Increasing the parameter value increases computation burden.
+
|style="vertical-align:top"| Larger value increases CPU burden.
 
|}
 
|}
 +
</div>
  
'''Notes:'''
 
  
* This is NOT a sample exact plugin. If you use it, slight inaccuracies might occur. Since we are dealing with float values rounding errors might occur, especially on large samples. In general however inaccuracies should not exceed a few 10's of milliseconds for movielength samples.
+
==== Notes ====
 +
<div style="max-width:62em" >
 +
* This is NOT a sample exact plugin. If you use it, slight inaccuracies might occur.{{Clarify}} Since we are dealing with float values rounding errors might occur, especially on large samples. In general however inaccuracies should not exceed a few 10's of milliseconds for movielength samples.
  
* In 2.61 an updated SoundTouch library is used which supports multichannel audio. Prior versions supported stereo only. See this thread for details :- [http://forum.doom9.org/showthread.php?t=71632 TimeStretch in AVISynth 2.5.5 Alpha - Strange stereo effects ?]
+
* In '''TimeStretch''' 2.61 an updated [http://www.surina.net/soundtouch/ SoundTouch] library is used which supports multichannel audio. Prior versions supported stereo only. See this thread for details :- [http://forum.doom9.org/showthread.php?t=71632 ''TimeStretch in AVISynth 2.5.5 Alpha - Strange stereo effects?'']
 +
</div>
  
* SoundTouch is used in float sample mode.
+
==== Examples ====
 +
<div style="max-width:62em" >
 +
* Raise pitch one octave, while preserving the length of the original sample.
 +
<div {{BoxWidthIndent|36|3}} >
 +
TimeStretch(pitch=200)
 +
</div>
  
'''Examples:'''
+
* Raise [[Wikipedia:Pitch_(music)|pitch]] one semi-tone, while preserving the length of the original sample.
 +
<div {{BoxWidthIndent|36|3}} >
 +
## (pitch changes
 +
TimeStretch(pitch=100.0*pow(2.0, 1.0/12.0))
 +
</div>
  
  TimeStretch(pitch = 200)  
+
* Change playback tempo from NTSC Film speed ([[ShowFrameNumber#Drop-Frame_versus_Non-Drop-Frame_Time_Code|23.97 fps]]) to PAL speed (25 fps) without changing pitch.
 +
<div {{BoxWidthIndent|36|3}} >
 +
  TimeStretch(tempo=100.0*25.0/(24000.0/1001.0))
 +
</div>
 +
</div>
  
This will raise the pitch one octave, while preserving the length of the original sample.
+
==== Credits ====
 
+
<div style="max-width:62em" >
TimeStretch(pitch = 100.0*pow(2.0, 1.0/12.0))
+
This function uses '''SoundTouch Audio Processing Library''' <br>
 
+
Copyright © Olli Parviainen <br>
This will raise the pitch one semi-tone, while preserving the length of the original sample.
+
[http://www.surina.net/soundtouch surina.net/soundtouch]
 
+
</div>
TimeStretch(tempo = 25.0/(24000.0/1001.0)*100.0)
+
 
+
This will change the tempo from Film speed to PAL speed without changing the pitch.
+
 
+
'''Credits:'''
+
 
+
This function uses:
+
 
+
SoundTouch library Copyright (c) Olli Parviainen 2002-2006
+
 
+
[http://www.iki.fi/oparviai/soundtouch] <br>
+
[http://www.surina.net/soundtouch]
+
 
+
'''Changelog:'''
+
  
 +
==== Changelog ====
 +
<div style="max-width:62em" >
 
{| border="1"
 
{| border="1"
 
|-
 
|-
| v2.55
+
| v2.61
| Initial Release (based on SoundTouch library 1.4.0?)
+
| Updated SoundTouch library to 1.9.0. Fixes multichannel issues.
 
|-
 
|-
 
| v2.57
 
| v2.57
 
| Expose soundtouch parameters
 
| Expose soundtouch parameters
 
|-
 
|-
| v2.61
+
| v2.55
| Updated SoundTouch library to 1.9.0. Fixes multichannel issues.
+
| Initial Release (based on SoundTouch library 1.4.0?)
 
|}
 
|}
 +
</div>
  
 
[[Category:Internal filters]]
 
[[Category:Internal filters]]
 
[[Category:Audio_filters]]
 
[[Category:Audio_filters]]

Revision as of 05:36, 19 February 2016

Change the audio speed and/or pitch:

tempo adjusts speed while maintaining pitch.
rate adjusts speed while allowing pitch to rise or fall.
pitch adjusts pitch while maintaining speed.

You can use these parameters in any combination – for example, 104% tempo with 95% pitch.


Contents


Syntax and Parameters

TimeStretch(clip clip [, float tempo, float rate, float pitch,
      int sequence, int seekwindow, int overlap, bool quickseek, int aa ] )

clip
Audio sample type is automatically converted to Float.
If clip.AudioChannels=2, special processing is used to preserve stereo imaging. Otherwise, channels are processed independently. Independent processing works well for unrelated audio tracks, for not very well for surround sound; for that, try TimeStretchPlugin instead.
 Tempo, Rate and Pitch
float  tempo = 100.0
Changes speed and clip.AudioLength while maintaining the original pitch.
If tempo=200, the audio will play twice (200%) as fast; if tempo=50, the audio will play half (50%) as fast.
float  rate = 100.0
Changes speed and clip.AudioLength while allowing pitch to rise or fall.
Adjusting rate is equivalent to adjusting the speed of a record player or tape recorder.
If rate=200, the audio will play twice (200%) as fast; if rate=50, the audio will play half (50%) as fast.
By the way, the following script:
TimeStretch(rate=105)
is equivalent to
ar=AudioRate 
AssumeSampleRate(Round(ar*1.05))
ResampleAudio(ar)
float  pitch = 100.0
Changes pitch while maintaining the length of the original sample: clip.AudioLength is not changed.
(within a small tolerance – see Notes below)
Tempo, rate and pitch can all be adjusted independently, in which case their effects are added together.
 Advanced Parameters
The time-stretch algorithm has a few parameters that can be tuned to optimize sound quality for certain applications. The current default parameters have been chosen by iterative if-then analysis (read: "trial and error") to obtain the best subjective sound quality in pop/rock music processing, but in applications processing different kind of sound the default parameter set may return a sub-optimal result.
int  sequence = 82 *
This is the length of a single processing sequence in milliseconds, which determines how the original sound is chopped in the time-stretch algorithm. Larger values mean fewer, and longer, sequences are used. In general,
  • a larger sequence value sounds better with a lower tempo and/or pitch;
  • a smaller sequence value sounds better with a higher tempo and/or pitch.
int  seekwindow = 28 *
The length in milliseconds for the algorithm that searches for the best possible overlap location. For larger seekwindow values, the possibility of finding a better mixing position increases, but too-large seekwindow values may cause a "drifting" sound (a disturbing artifact where what should be steady frequencies – such as piano notes – seem to drifting around) because neighboring sequences may be chosen at more uneven intervals.
int  overlap = 12 *
The overlap length in milliseconds. When the sound sequences are mixed back together to form a continuous sound stream again, overlap defines how much of the ends of the consecutive sequences will be overlapped. This shouldn't be a critical parameter. If you reduce the sequence by a large amount, you might wish to try a smaller overlap.
bool  quickseek = true? *
The time-stretch routine has a 'quick' mode that substantially speeds up the algorithm but may degrade the sound quality.
int  aa = ? *
Controls the number of taps the anti-alias filter uses. Set to 0 to disable the filter. Must be a multiple of 4.
* TODO ( default values are under investigation at the time of updating this documentation )


This table summarizes how these parameters can be adjusted for different applications:
Parameter Default value If larger... If smaller... Music Speech CPU burden
sequence Relatively large, chosen for slowing music tempo. Usually better for slowing tempo. You might need less overlap. Accelerates "echoing" artifact when slowing down the tempo. Default value usually good. A smaller value might be better. Smaller value increases CPU burden.
seekwindow  Relatively large, chosen for slowing music tempo. Eases finding a good mixing position, but may cause "drifting" artifact. Makes finding a good mixing position more difficult. Default usually good, unless "drifting" is a problem. Default usually good. Larger value increases CPU burden.
overlap Relatively large, chosen to suit above parameters. Larger value increases CPU burden.


Notes

  • This is NOT a sample exact plugin. If you use it, slight inaccuracies might occur.[clarification needed] Since we are dealing with float values rounding errors might occur, especially on large samples. In general however inaccuracies should not exceed a few 10's of milliseconds for movielength samples.

Examples

  • Raise pitch one octave, while preserving the length of the original sample.
TimeStretch(pitch=200) 
  • Raise pitch one semi-tone, while preserving the length of the original sample.
## (pitch changes 
TimeStretch(pitch=100.0*pow(2.0, 1.0/12.0)) 
  • Change playback tempo from NTSC Film speed (23.97 fps) to PAL speed (25 fps) without changing pitch.
TimeStretch(tempo=100.0*25.0/(24000.0/1001.0))

Credits

This function uses SoundTouch Audio Processing Library
Copyright © Olli Parviainen
surina.net/soundtouch

Changelog

v2.61 Updated SoundTouch library to 1.9.0. Fixes multichannel issues.
v2.57 Expose soundtouch parameters
v2.55 Initial Release (based on SoundTouch library 1.4.0?)
Personal tools