ruffle
ruffle copied to clipboard
Use `std::time::Duration` for time intervals
Time durations in Ruffle are often stored as floats (example), and it's difficult to know which units the values are in (secs, ms, ...).
It'd be nice to switch these over to use std::time::Duration for clarity.
Hi @Herschel I like to work on this.
@Udai1931 you still one this, i like to have a knack at it
@Herschel Are you like suggesting every place where durations are used should be switched out to durations? Example
Or more like all functions' input and return types should be std::time::Duration. Also is this change limited to this file or there would be a lot of places?
Also note the example you linked now uses a Duration and not float.
@Herschel This, issue has been without activity for a long time, can I try to work on it?
@higumachan Yes, please feel free to do so!
Also, for anyone wondering, these are the lines of code that Mike was linking to. The file has since changed, so his link highlights the wrong lines now. You can see that the function Mike was talking about (and many others) are still representing time with floats.
@n0samu @Herschel
I tried use std::time::Duration
However, as noted in this PR, the cargo:test://regression_tests::as3_sound_embeddedprops test case fails.
https://github.com/higumachan/ruffle/pull/1
One possible reason for this is that the original floating-point data was used to handle the data, so when division is performed, values finer than microseconds are being generated.
My solution, as I see it, is as follows.
- create an original Duration type that is compatible for ruffle
- discard compatibility
Which do you think is better, or do you have any other good ideas?
std::time::Duration only uses nanosecond resolution (~10 significant figures), so it loses some precision compared to calculating the sound sample rate directly (~16 sig figures in f64).
I was only really thinking about dt and the timers when I wrote this issue and didn't really consider the audio position -- sorry about that!
Some thoughts on the best way to proceed:
- Seems like the easiest path forward would be to make our own
struct Duration(f64)type. - This also avoids the need for
SignedDuration(we'll just always be signed). - We can add
Duration::as_secsetc. methods directly onto this type. - Having
duration_helperas a separate crate is overkill;Durationcould live in its own module inruffle_core.
Basically the idea is to make things clearer+easier; if it gets more complicated, then it's more trouble than it's worth and we're better off sticking with f64.
Thanks for your work! Sorry for the delay in responding.
@Herschel
Thank you for response!
OK, I would like to implement the policy of adding Duration(f64) in ruffle_core.