[music-dsp] Algorithms for finding seamless loops in audio

Ross Bencina rossb-lists at audiomulch.com
Thu Nov 25 07:43:39 EST 2010

Element Green wrote:
> I'm the author of a SoundFont instrument editing application called
> Swami (http://swami.sourceforge.net).  A while back an interested
> developer added a loop finding algorithm which I integrated into the
> application.  This feature is supposed to generate a list of start/end
> loop points which are optimal for "seamless loops".

What kind of loops are you talking about here? loops in pitched musical 
instrument samples or drum/percussive rhythm loops?

> The original algorithm was based on autocorrelation.  There were many
> bugs in the implementation and I was having trouble understanding how
> it functioned, so I wrote a new algorithm which currently does not use
> autocorrelation.  The new algorithm seems to come up with good
> candidates for "seamless loops", but is slower than the old algorithm
> by a factor of 5, but at least it does not suffer from bugs, which
> often resulted in unpredictable results.

Assumng you're talking about musical instrument samples then autocorrelation 
(or AMDF) is one way to do it.

You need to get clear about whether your only goal is "seamless" or if you 
want to preserve the pitch of the original accurately too -- that's pretty 
important for musical instrument samples :-) and will be more relevant for 
short loops. In that case you may be better off with a good spectral 
fundamental frequency estimator like this one to choose the loop length:

That could be combined with autocorrelation/AMDF and/or crossfading to 
choose where to place the loop.

Note that there are a number of different ways you can perform the crossfade 
if you choose to do that (linear, equal power, I remember my Ensoniq EPS 
even had some weird ensemble  "bow-tie" crossfade options).

For longish loops on musical/tonal/synth sounds that are spectrally dynamic 
(ie they have lots of phasing or lfo stuff going on) you could also use 
spectral descriptors to find good match points (you probably want to combine 
this with autocorrelation/AMDF to get both the time domain and spectral 
aspects correct).

A while ago James Chandler Jr gave a really good description of an off-line 
time stretching algorithm, part of which did looping for pitched sounds --  
you might want to have a look in the list archives that.. it's probably of 
interest to you. From memory one of the points he made was that one of the 
loop points within a few samples either side of the one automatically chosen 
might actually sound better.. so you could offer a few options to the user 
+/- 1 or two samples.

> I have limited knowledge in the area of DSP, so I thought I would seek
> advice on this list to determine if the new algorithm makes sense, if
> anyone has any ideas on ways to optimize it or if there are better
> ways of tackling this task.

I think AMDF is a reasonable approach for a simple algorithm. You could take 
it a lot further using the techniques above -- not sure they're that useful 

> First off, is autocorrelation even a solution for this?  That is what
> the old algorithm used and it seemed to me that sample points closer
> to the loop start or end should be given higher priority in the
> "quality" calculation, which was not done in that case.

You could limit the autocorrelation to a window around the candidate loop 
point to maximise waveform similarity in that region.

Weighting at the loop point might help if you're not doing any crossfading, 
but using a larger, evenly weighted window makes it more likely that your 
algorithm will choose a good global match based on the longer term evolution 
of the sound (including it's pitch and spectral evolution).


More information about the music-dsp mailing list