I'm creating an audio editor and the scroll/cursor position is going wonky at the end. Let me set up the scenario.
-I know how many pixels is equivalent to 1 second of audio time. Call it $oneTickWidth$ .
-I have a window that the viewer can see at a time that is $canvasWidth$ pixels.
Suppose the track I am editing is $duration$ seconds long. Then
$spaceNeeded=duration*oneTickWidth$
If
$spaceNeeded > canvasWidth$
then we must scroll the window each time we go over the window allotment. Therefore for the majority of the time (assuming I scroll an entire $canvasWidth$ at a time).
$cursorPosition = (currentAudioTime*oneTickWidth) \mod canvasWidth$
However, at the end of the track this is not the case. If I scroll all the way to the end there is some remainder that is tripping me up.
For example: Let $oneTickWidth=1, canvasWidth=3, duration=11$
Then at $currentAudioTime=5$,
$cursorPosition=(5*1) \mod 3= 2$
which is correct. We have scrolled 1 time and the cursor rests 2 seconds after the scroll. But let's check at $currentAudioTime=10$
$cursorPosition = (10*1) \mod 3 = 1$
which is incorrect. We have scrolled 3 times and the cursor should rest 2 seconds after the scroll (the window is 8-11). What can I add to my $cursorPosition$ equation to account for this?
Suppose you have a sample of $s$ pixels width, which we may index with the integers in $[0,s)$. Suppose you have a display window of $d$ pixels width, which we may index with the integers in $[0,d)$. Let $p$ be the index of the pixel in the sample that is shown at index $0$ in the display. You appear to want $$ 0 \leq p \leq s-d \text{.} $$ Suppose the cursor is at index $c$ in the sample. Then the cursor is at the (virtual) index $c-p$ in the window. If $c-p<0$, the cursor is to the left of the display. If $c-p \geq d$, the cursor is to the right of the display. Otherwise, the cursor is on the pixel(s) of index $c-p$ in the display.