# synchronizing two music tracks

I would like to synchronize two different music tracks (and maintain synchronization) as a DJ would with two records.

So far, I have the beats mapped for each song, so I am able to adjust their rates and time align them accordingly. What I've found though, with many sample tracks, is that the two tracks will drift out of sync after a while due to the tempos of the songs not remaining constant. I've tried compensating for this by periodically checking the tempos of the songs and re-adjusting their rates so that they are synchronized again by the start of the following measure.

Is there a common (or better) approach of synchronizing two music tracks?

• Are you just computing a tempo or doing actual beat-tracking (getting the position within the song of each beat)? Once you know the temporal position of each beat synchronization is not an issue. – pichenettes Nov 26 '13 at 22:31
• I am doing actual beat tracking. At the moment, one track is played back at a constant rate, while the rate of the other track is adjusted periodically so that the beats remain aligned. In searching the internet, I didn't find any descriptions of how to sync two tracks, so I just naïvely took this approach. So, the original question was: is there a common approach to solve this problem? or a better one? - thanks in advance – user931563 Nov 28 '13 at 17:26
• If you have timestamps for beats on both tracks, where is the problem exactly? You know the current location of the play head t_a in track A and t_b in track B. You know that the next beat is at t_a' in track A and t_b' in track B. You set the stretch rate for the duration of the current beat as (t_b' - t_b) / (t_a' - t_a) until the play-head reaches t_a' (if your time-stretcher is accurate, your play-head will reach t_b' at the same time). If this causes wobbly playing because of irregularity in beat detection, you low-pass filter your sequence of stretch rates. – pichenettes Nov 28 '13 at 20:34
• thanks for the suggestion @pichenettes . I will try adjusting the rates for each beat (had been doing it once per measure) and perform some smoothing of the stretch rates. – user931563 Nov 29 '13 at 10:12

well, it depends on how similar the audio signal from the two tracks are. i dunno if the two tracks are identical, to within some degree of error or noise, and to within some degree of time alignment, but somehow, in a manner that is salient to your model or to your problem, you have to have a measure of something in the two signals to align.

so let

$$a_x(t) = f\left\{ x(t) \right\}$$

$$a_y(t) = f\left\{ y(t) \right\}$$

be that something that defines what it is you're trying to synchronize. so you want to find a delay offset $\tau$, that maximizes the cross-correlation between $a_x(t)$ and $a_y(t)$

$$R_{xy}(\tau) = \int_{-\infty}^{+\infty} a_x\left(t+\frac{\tau}{2}\right) a_y\left(t-\frac{\tau}{2}\right) w(t) \ dt$$

with whatever window function $w(t) \ge 0$ you like best. find the $\tau$ that maximizes $R_{xy}(\tau)$

so i'm not telling you how to do this using discrete-time code. you get to figure that out.

I would suggest you to take a closer look on this publications:

MATCH: A Music Alignment Tool Chest

Live Tracking of Musical Performances Using on-line Time Warping

Shortly speaking, algorithm is following::

• Extract temporal features of your signals (Audio Spectral Flatness, MFCC's, Onset, etc.).
• Using Dynamic Time Warping with some constraints, i.e. path must be monotonically increasing and be bounded.
• Apply the warping to the signal and you are done.

This algorithm might be bit of overkill as it's mainly used for synchronization of two the same songs played by different artist. Although you can successfully use it (worked in my case) to perform time alignment between your tracks.

For some implementations please refer to: