Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Music: sticky timeline sound rows #49426

Merged
merged 8 commits into from
Dec 15, 2022
Merged

Conversation

breville
Copy link
Member

@breville breville commented Dec 10, 2022

The timeline UI now maintains the row allocated to a specific sound between renders whenever possible.

Previously, we had a simple heuristic which simply allocated sounds to rows in the order that they appeared in the array of song data events, which was sorted by the sound's start time. A side effect of this was that if new sounds were scheduled while the song was playing, existing sounds could get pushed to new rows:

musiclab_uniquesounds_before.mov

Now, we have a more sophisticated approach in which we examine the previous allocations and keep sounds in those rows. The effect of this new approach is that if a new sound is scheduled when a song is playing, even if it will be played before another sound, the existing sound will maintain its row in the timeline UI:

musiclab_uniquesounds_after.mov

It's worth noting that the new approach will remove empty rows, so it's still possible that rows will shift upward. In the future, we'll likely render each row in a way that it can still be correlated to its sound, so this won't be too severe.

Also, a useful behaviour of this implementation is that when a sound for a given play block is changed, the new sound will occupy the row taken by the old sound, so it will effectively stay in place in the timeline UI.

This PR also improves performance in the Timeline UI by caching the generated set of unique sounds at the beginning of each render so that a variety of helper functions don't need to keep regenerating it.

This PR also fixes an issue in which the Timeline's playhead can be momentarily seen at its last position when starting to play a song, before the first hit of a timer callback corrects it. Now, the playhead position is reset when playback begins.

This change was a great candidate for unit testing, so the first Music Lab unit testing is now here.

Copy link
Contributor

@sanchitmalhotra126 sanchitmalhotra126 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple questions on the logic but not blocking. Looks good!

Comment on lines 44 to 45
// ... put it back in that row.
outputSounds[i] = this.previousUniqueSounds[i];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you now remove this sound from uniqueSounds so that on the next pass below you don't have to check if the sound has already been added?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a neat idea. One thing I have been attempting to do is to keep the data somewhat immutable, so I wanted to leave that prior array intact while we construct the new one, though it does mean we have to do some extra checking.

@@ -0,0 +1,50 @@
/* Tests for music/utils/UniqueSounds.js */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First unit test! 😎

// to keep those entries in the same rows as they were before.
if (this.previousUniqueSounds) {
// For each of those previous entries...
for (let i = 0; i < this.previousUniqueSounds.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to iterate through uniqueSounds here instead and check this.previousUniqueSounds.indexOf(uniqueSounds[i])? That way you're not checking sounds that may no longer be in use?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question. I might be missing a good shortcut, but given we do two passes -- one to pull over sounds into their existing slots, and another to fill in the gaps with the remaining sounds -- I think the resulting code for that first pass is not greatly different, and is in fact is a tiny bit longer:

      // For each of the current sounds...
      for (let i = 0; i < currentUniqueSounds.length; i++) {
        // ...see if it already has a slot...
        const previousSlot = this.previousUniqueSounds.indexOf(
          currentUniqueSounds[i]
        );
        // ...and if so, put it back in there.
        if (previousSlot !== -1) {
          outputSounds[previousSlot] = this.previousUniqueSounds[previousSlot];
        }
      }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's fair. I suppose another slight optimization you could make here is to push any new sounds that weren't the previous list into a second array, and then iterate through that after, instead of going through the whole list again? At this scale though it's not gonna make a huge difference, so whatever is easiest.

Copy link
Member Author

@breville breville Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's a great idea, and is now implemented. As a result, I did follow your earlier suggestion and now loop through existing sounds rather than previous sounds. Thanks for the ideas here!

Comment on lines 63 to 65
outputSounds = outputSounds.filter(s => {
return s !== undefined;
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, how would we end up with empty rows here if the original array was the length of uniqueSounds?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that was a mistake. In the updated implementation we clearly handle the reality that the array before filtering is sparse and can contain entries beyond that length, particularly in the case when we have some entries that were previously in high indices and then we remove one or more entries in lower indices.

@breville breville merged commit f8985e0 into staging Dec 15, 2022
@breville breville deleted the music-sticky-timeline-sound-rows branch December 15, 2022 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants