Seamless loops - freepats/documentation GitHub Wiki

After recording a series of samples from a musical instrument, the next step is processing the sounds. And among all tasks, loops are frequently the most time consuming part of sample editing.

This document tries to answer a few tipical questions of loop creation. Since it's a huge topic, we won't be able to cover everything, though it will be expanded as time allows.

When are loops needed

Some instruments produce sounds with limited duration. It's the case of the piano, acoustic guitar, drums, xylophone, and most percussive instruments. Those instruments can be recorded at full length, until they become inaudible due to the natural decay of their notes, and loops won't be needed.

Sometimes an instrument may have a long decay, such as piano bass notes. Loops can be created to make a smaller version of the full sound bank; they are not needed but sometimes can be useful.

There are instruments which their sound don't decay among time, such as a pipe organ, and it's also the case of most wind and bowed string instruments. Even if some of them can't actually produce an infinite sound without introducing small pauses (the musician has to breathe), it's commonly expected that their digital recreation could seamless sustain forever. Loops are needed for them.

Loop creation

Let's look at the visual representation of a sound, as it's commonly displayed by audio editing programs.

The attack part may have a percussive or accented sound, and is usually fast ascending in amplitude. Depending on the instrument, it may be folowed by a decay (descending amplitude). When the sound becomes more stable, the sustain part starts. The release part happens at the note off, and it's much influenced by room's reverberation.

Loops are created within the sustain part, the middle and most stable part of the sample.

Methods for creation

There are several different methods. One method may give good results for a particular sound and very bad for others. Even with the same instrument, one method may be good for bass notes and other may be better for treble. Some methods require experience in sound editing, and high levels of patience and perseverance.

We could search for natural loop points, either manually or with the help of specialized programs. This method tends to work better on some synthesized sounds, but it is unlikely to return good results with most acoustic instruments, where the sound is continuosly changing.

It's not possible to detail everything here, because we would need to write a whole book. We are just going to provide a few examples for one of the most popular methods: crossfading samples. It can work in many scenarios, and it's one of the easiest methods. On the other side, sometimes it can mangle the quality of the samples and requires a lot of trial and error, which can become tedious.

Crossfading a sample with itself

Crossfading is a popular method commonly used with music loops, which are used to create compositions by using repeated patterns. While the concept is similar, creating a loop for one sample is different to creating a music loop, and it may not always work.

By using this method, we are creating loops "by force". This means that part of the loop has to be modified, to ensure that the ending matches the starting point and provides a smooth transition.

There are a few inconvenients: crossfading an individual sample with itself may produce audio phase cancellation and other problems that could affect the quality. We should try to avoid those artifacts whenever possible, though a certain degradation is unavoidable, specially on sounds with rich harmonic content.

Note that the term "sample" can be confusing, because it may refer to two different things: in the context of sound banks, we usually use the term "sample" to mean an individual note recorded from a musical instruments. In the context of audio editors, the term "sample" refers to each individual digitized value of the waveform, usually represented as a dot or vertical line when zooming in. To avoid confusion, I'll use the term "waveform" to refer to the whole audio piece being loaded into the editor (one note of the instrument) along next sections.

Begin by cutting the start of the waveform

It's not strictly required to cut the waveform first, but if you plan to do it, it's better to do it now. If done at a later time, remember to substract the number of removed samples from loop values.

Duplicate and displace the waveform

Create a copy of the waveform into a new audio track. These examples are made with Audacity, you can use any other multi-track audio editor that you are comfortable with.

Displace the second audio track to the right until the desired end of loop in the first track is close to the start of loop in the second. Select the crossfade region, it will become the transition area, blended between the two tracks. Check the following image for reference.

Take note of the start and end loop points, we'll need them later. They should be expressed in samples, not seconds, because we need them to point out exact positions within the waveform.

How big the crossfade region should be?

It depends on the instrument and the pitch of the note beign edited. The crossfade area can introduce ugly artifacts in the sound, so it's preferred to keep it as small as possible. But if the crossfade area is too small, the transition between the end and start of the loop will become abrupt, and a larger crossfade will be preferred instead.

If you don't know where to start, try with a region around 150ms first. Many tries may be needed. When experience is gained, by editing thousand of instruments, some intuition will be gained as well that will help you choose good values with less tries. There is many information that can be inferred by carefully listening to the sound, and also analyzing it's waveform and harmonic distribution in the editor may help. The intuition is never perfect, though. A few tries can be expected, as the crossfading method is quite unstable on it's results.

Crossfade tracks

Delete the tail of the first track (everything after the selected portion) and delete or silence the heading of the second track (everything before the selection). You can crossfade both tracks now; on Audacity the option is called "Crossfade Tracks" in the Effect menu.

In the crossfade dialog, use these options:

  • Fade type: Constant Gain

  • Fade direction: Alternating Out / In

Delete the remaining waveform

Afer the crossfade is done, you should cut and delete the remaining of the waveform. When loop's end point is reached, the sound will go back to the start point and continue from there. The remaining waveform won't be played and it shouldn't be needed, except for a few extra samples past the end.

You may be wondering why are those extra samples needed: the SF2 specification requires at least 8 samples after loop's end, for compatibility with legacy hardware. They are not required by other formats, but please keep them, the space used by those extra samples is negligible and the instrument will be compatible with the SF2 format which continues to be very popular nowadays.

Merge and testing

Merge both tracks into one. On Audacity, the operation is called "Mix and Render" (Tracks menu, Mix submenu).

To test the loop, use the loop's start and end numbers that you wrote earlier, and select exactly that region. An offset of just one sample will very likely introduce a "click" or other artifacts into the sound. On Audacity, the operation is called "Loop Play" (Transport menu, Playing submenu), or you can use the shortcut Shift + Space.

Listen carefully to the loop, paying special attention to the crossfaded part and the transition at the end. It should be as smooth as possible, with no abrupt changes or noticeable variation of the sound.

It's usually very rare to get a good result on the first try, most likely you'll notice that something is not good during the crossfade zone or transition. Depending on the instrument, it can be easy, very hard, or near to impossible to get a clean crossfade. But, why are those artifacts happening? Can we do something to avoid them? We'll try to provide a few hints in the next sections.

Repetitions shouldn't be much noticeable either. Our ears are very sensitive to detecting repetitive patterns, if you can easily notice a pattern, it will be noticed by the users of the sound bank as well, and appear into the music made with it. Creating longer loops is the easier way to avoid the issue, repetitions become much harder to notice when the loop is longer than 5 seconds. Depending on the instrument, up to 10 seconds may be needed. If the size of the sound bank is not a concern, you can go with long loops since the first try and avoid spending a lot of time finding the optimal loop length.

Phase cancellation

If you have worked with audio before, it's quite possible that you've encountered phase cancellation issues already. Otherwise, please search for this subject to get a more in-depth description. It frecuently happens when more than one microphone is recording the same audio signal, and that's what makes recording a single instrument in stereo very delicate. It will also happen when mixing the same sound twice, and we are doing that for loops.

It's not possible to completely avoid phase issues during the crossfade region, some harmonics or overtones may be in phase and others out of phase at the same time. We should aim to make phase issues as less noticeable as possible, and specially trying to preserve undamaged the main frequency of the note beign played.

To put it simple, by aligning the main waveform visually in both tracks, before doing the crossfade, we are reducing the risk of phase cancellation of its main frequencies. The main frequencies of a sound usually define its main shape, as they contain more energy than the others. Just don't take it as a proven rule, because it's a simplification: the waveform displayed by the editor is actually the sum of many other waves of different frequencies and phases, and results can be sometimes counterintuitive.

The waveform resulting from joining discrete sample points with lines it's very common on editors because it's light and fast (in computing requirements) and a convenient way to display data on a computer screen, but it should not be interpreted literally. There is much more data that can't be inferred by simply looking at the shape of the compound waveform. A spectrum analyzer would decompose a waveform into a series of pure frequencies and their energy, which is a better picture of the content that will reach to our ears, but it neither will help with phase alignment.

In short, a loop transition may seem smooth and pleasing to our eyes when viewed in the editor, but that doesn't mean that it'll be equally pleasant to our ears. Please use your ears for judgement; how good a sound does look doesn't really matter.

If the result is not as smooth as you'd like to be, try again with a different region, different parameters, or different alignment. A waveform is not always constant, the larger the crossfade zone, the most likely that part of it becomes misaligned. Optionally save the sound before starting a new one, because it can be useful to compare results or going back in case the newer is worse.

Chorus effect

When two or more voices play the same note at the same time, they are perceived as a chorus because they are slightly different. Unless the voices are synthesized by a computer algorithm, it's impossible to make their waveform perfectly syncronized, and those small differences are responsible of our perception of a set of voices instead of one louder voice.

When crossfading a sound with itself, two instances of the same sound will be playing together during the crossfade region. Depending on the instrument and the length of the crossfade, it may not be noticeable. Once again, carefully listen to the result.

TO DO: Continue writing this article from here.

Other problems that may arise

Release sounds