Skip to content

Add AudioSinkPlayback::position #19173

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Weihnachtsbaum
Copy link

@Weihnachtsbaum Weihnachtsbaum commented May 11, 2025

Objective

  • Allow users to get the playback position of playing audio.

Solution

  • Add a position method to AudioSinkPlayback
  • Implement it for AudioSink and SpatialAudioSink

Testing

  • Updated audio_control example to show playback position

Copy link
Contributor

@mgi388 mgi388 left a comment

Choose a reason for hiding this comment

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

You might like to do a quick audit of Bevy code to confirm the best name here: at a glance it looks like Bevy would name this position() rather than get_pos().

@Weihnachtsbaum
Copy link
Author

@mgi388 I'm not entirely sure which Bevy code you are referring to. I definitely agree that the get_ prefix is weird, as other methods on AudioSinkPlayback don't have it (volume(), speed()), but the methods of AudioSinkPlayback correspond almost 1:1 to the methods of rodio::Sink, and it also uses the name get_pos. While it's weird, I'd just use the name rodio uses here, as it makes it easier to understand what corresponds to what.

@mgi388
Copy link
Contributor

mgi388 commented May 11, 2025

@mgi388 I'm not entirely sure which Bevy code you are referring to. I definitely agree that the get_ prefix is weird, as other methods on AudioSinkPlayback don't have it (volume(), speed()), but the methods of AudioSinkPlayback correspond almost 1:1 to the methods of rodio::Sink, and it also uses the name get_pos. While it's weird, I'd just use the name rodio uses here, as it makes it easier to understand what corresponds to what.

@Weihnachtsbaum sorry for the confusion. I’m saying amongst bevy code in other crates I think we see methods that get the position of something named position rather than get_pos.

I’m also not sure if 1:1 mapping to Rodio is a goal especially because there’s a medium to high chance that Rodio won’t be the audio backend for Bevy in the near future (see Bettwr Audio Working Group on Discord).

@Weihnachtsbaum Weihnachtsbaum changed the title Add AudioSinkPlayback::get_pos Add AudioSinkPlayback::position May 11, 2025
@Weihnachtsbaum
Copy link
Author

@mgi388 That's a fair point, I changed it to position now.

@hukasu
Copy link
Contributor

hukasu commented May 12, 2025

things like this usually use the terms seek and tell, i'd say, we already have the seek methods and this would be tell

@mgi388
Copy link
Contributor

mgi388 commented May 12, 2025

things like this usually use the terms seek and tell, i'd say, we already have the seek methods and this would be tell

@hukasu do you have a reference for that? I don't have a strong opinion on it really, but if I saw code that said music_controller.tell().as_secs_f32() I'm not sure it would be that intuitive to me compared to music_controller.position().as_secs_f32().

@hukasu
Copy link
Contributor

hukasu commented May 12, 2025

yeah, maybe not for audio, both kira and rodio use position
because it uses seek i just assumed the counterpart would be tell like file descriptors

@mgi388
Copy link
Contributor

mgi388 commented May 12, 2025

@Weihnachtsbaum I ran the example and pressed spacebar to pause the audio and the progress keeps going presumably due to floating point things. I'm surprised that it fluctuates so much though. I wonder if it's expected from Rodio's side to fluctuate so much? Like even if you rendered it at 2 decimal places e.g. 11.43s, in the example below it would still fluctuate. Any ideas?

Screen.Recording.2025-05-12.at.11.41.07.am.mov

@Weihnachtsbaum
Copy link
Author

@mgi388 I'm not sure but it might be because in example the playback speed is changing all the time. I'll have a look at it later today.

@mgi388
Copy link
Contributor

mgi388 commented May 12, 2025

@mgi388 I'm not sure but it might be because in example the playback speed is changing all the time. I'll have a look at it later today.

@Weihnachtsbaum ah that's probably it. It's probably fine then. A solution might be to update the example to only update the speed if it's not paused. I don't think that would have any negative effects on the example, see what you think.

@janhohenheim janhohenheim added A-Audio Sounds playback and modification D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 12, 2025
@Weihnachtsbaum
Copy link
Author

A solution might be to update the example to only update the speed if it's not paused.

@mgi388 Done 👍

Copy link
Contributor

@mgi388 mgi388 left a comment

Choose a reason for hiding this comment

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

LGTM. I tested the example again and the progress pauses when you press spacebar.

@mgi388 mgi388 requested a review from SolarLiner May 13, 2025 00:31
Copy link
Contributor

@SolarLiner SolarLiner left a comment

Choose a reason for hiding this comment

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

LGTM! A small update directly exposing more of rodio's API surface.

@mgi388 mgi388 added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Audio Sounds playback and modification D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

5 participants