Tracking sound offset with a float

I’m mixing samples from multiple sounds into a single stream. For the sounds, I use whole numbers to track the current sample. What I am considering is tracking the amount of time elapsed with a float, and then calculating the current frame with something like “time / sound sample rate”. The benefit of this method is that I can use any sample rates I would like for sounds, and even change the playback speed. Would this be a good idea, or is the loss in precision critical for clear audio? I will also note that I increment the elapsed time with a float as well, so “elapsed += 1.0 / stream sample rate”.

I created a script to try to see how much precision is lost (44100 samples at 44100 Hz returned 1.0000000000004803), but I’m not currently able to tell if it is significant. The final results were close, but now I worry that maybe the precision of floats is not linear, but ‘saws’ or something, which would cause uneven playback.

The smallest “time unit” for digital audio is the sample period. When mixing multiple streams the sample rates need to match, or be converted to a common sample rate for the output. For large time periods it is acceptable to represent time as a floating point value as any error in “time / sound sample rate” will be insignificant, however you will need to be careful about cumulative errors. For example, if you calculate the time period of 1 sample at 44.1 kHz as being 0.000022676, the binary representation is not exact, so over several million samples there could be a significant error. (in single precision, 44100 x 60 x 0.000022676 = 60.000696 which is a cumulative error of about 30 samples for 1 minute of audio). Playback rates and DSP calculations should generally be based on an integer sample basis as that is unambiguous.