Audio - Compute samples for a given length with a start and end tempo

48 Views Asked by At

I'm working on some audio analysis code and I'm currently trying to determine the number of audio samples that would be needed in a segment of audio in which the tempo changes linearly. I know start tempo, end tempo, and the length of time which the change occurs for. Below I'll state the problem in the simplest form possible (removing all real-world units):


Suppose the relationship $s = \frac{l}{t}$, where

s is a number of audio samples

l is a length of time

t is a tempo.

Derive a formula to compute samples s for any given l, tstart and tend, where tempo t varies through l with a linear f(x) such that:

tstart = f(0), tend = f(l).


So for example, I could put the following conditions into the formula to find the number of audio samples needed for a segment of length 100 (seconds, or whatever unit) where tempo increases from 30 to 150 (BPM, or whatever unit).

tstart = 30, tend = 150, l = 100

And in that case since I'm only interested in a linear tempo change, we'd have

$f(x) = t_{start}+ \frac{t_{end} - t_{start}}{l}x = 30 + 1.2 x$


Now where I'm hung up is the fact that we have tempo and sample count having an inverse relationship. The naive method I tried at first of simply averaging tstart and tend through the window didn't work as a result (as sample count is actually not changing linearly throughout that time). I then thought that perhaps I'd have to integrate this formula with respect to t (which gives $l \ln t$) but then I got hung up on how to apply that to find the final sample count.

I'm probably just a few steps away here. Any push in the right direction would be great!

(Edit: it may be more prudent to call the variable "l" a number of 'beats' or 'bars', to confer the point that it is a fixed quantity and does not vary as tempo changes the actual length in time the segment would take to change by affecting sample count.)

1

There are 1 best solutions below

0
On BEST ANSWER

I found out the answer here. Luckily the solution wasn't so straightforward as just multiplying by the right thing in the right place, so I feel a bit justified in getting stumped for a while!

So, the relationship

$s = \frac{l}{t}$

gives a total sample count for a given l and t. This is useful when l and t are constants, because then we can just directly compute s. It is less useful if one of them varies, as we have for t.

In this case, it is more useful to compute a step function that gives the number of samples per an arbitrary dx, then integrate that from 0 to l. The variable x represents a position along the length l. As t can be expressed in terms of this position x as written in my initial post, we can express everything as:

$s = \int_0^l \frac{1}{\frac{t_{end} - t_{start}}{l} x + t_{start}} dx$

Then, the integrated value:

$s = \frac{ l \ln{( t_{start} l - t_{start} x + t_{end} x )} }{t_{end} - t_{start}} |_{0}^{l}$

Which, when evaluated, simplifies to:

$s = l ( \frac{ \ln{(t_{end} \, l)} - \ln{(t_{start} \, l)} }{ t_{end} - t_{start} } )$

And that, finally, is a formulaic expression of s for a given l, tstart, and tend. Phew!

By the way, to test out this formula yourself, you can use a WolframAlpha query formatted similar to this, where L is length l, A is start tempo tstart, and B is end tempo tend:

Solve L*(ln(B*L) - ln(A*L))/(B-A) where A = 30, B = 150, L = 100

Some properties such a formula should exhibit is that it always returns a positive number for a positive L because sample count is a physical quantity related to a play time, and flipping tstart and tend should return the same value (as flipping start and end tempo shouldn't affect overall sample count).

Note that the formula is undefined at tstart = tend, in this case the simpler formula ($s = \frac{l}{t}$) can be used instead as t is constant.