how to know ~waveform selection length

Arttu Hirsilä's icon

Umm I need help since I forgot the expression for calculating the duration of time between two points… Here you can see a waveform~ that has a selection from (a) (selection start) to (b) (selection end) (highlighted in yellow and controlled with mouse by a hidden rslider with hi/lo value scaled to the sample length). The info~ tells the buffer length (c) of the sample which the waveform and slider are scaled and linked to. The thing I’d like to know is the length of the selection (a) to (b) and get a *~ to stop playback just a bit before playback ends (1-2 milliseconds for adding a fade out to avoid clicks). I skipped a lot of maths classes so this might be a ridiculously easy to solve. Also, what would y'all recommend for fading out audio (that is playing back) from the current amplitude to zero in real time?

Source Audio's icon

b - a = length.
But depending on what object you use for playback,
setting fadeout point would differ.

I usually us matrix~ for audio level control.
Depending on audio played at least 10 ms could be needed for decent fadeout.
You can also combine ramp time with level mesages
ramp 10, 0 0 1 - fade input 0 to output 0 with 10 ms ramp time
or ramp 100, clear - fade out all channels with 100 ms ramp
-------
For that simple playback as you ask, I would use line~ driven play~ object

Max Patch
Copy patch and select New From Clipboard in Max.