1

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?

  • In your numerical example, after scrolling zero times, the window is 0-3, after scrolling one time, the window is 3-6, after scrolling two times, the window is 6-9, and after scrolling three times, the window in 9-12. (Notice that the endpoints of the window are always a multiple of canvasWidth.) How do you get 8-11? – Eric Towers Jul 04 '19 at 20:13
  • @EricTowers not 9-12 the entire span is only 11 seconds long: it must therefore must stop scrolling at 8 to fit the whole window. That's what's tricky – Seth Kitchen Jul 04 '19 at 20:14
  • Why can't the window show blank space after spaceNeeded? What do you expect to see in a page layout editor if you scroll past the edge of the page? – Eric Towers Jul 04 '19 at 20:17
  • @EricTowers I guess that's a good point I could do that. Seems kind of gross to have a whole new scroll for the .0001 seconds of excess audio. Then 99% of the window is blank space. – Seth Kitchen Jul 04 '19 at 20:20
  • Shouldn't that happen ${}\leq 1%$ of the time? – Eric Towers Jul 04 '19 at 20:21
  • @EricTowers Well it depends where your grossness level happens. If I could solve this it would be 0% of the time right? – Seth Kitchen Jul 04 '19 at 20:26

2 Answers2

1

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.

Eric Towers
  • 67,037
0

I played around with it and this is what worked... still not sure why...

If not fully scrolled:

$cursorPosition=(currentAudioTime∗oneTickWidth) \mod canvasWidth$

If fully scrolled:

$extra=scrollAmountInPixels \mod canvasWidth$ $cursorPosition=(currentAudioTime∗oneTickWidth-extra) \mod canvasWidth$