ruffle icon indicating copy to clipboard operation
ruffle copied to clipboard

Use `std::time::Duration` for time intervals

Open Herschel opened this issue 3 years ago • 8 comments

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.

Herschel avatar Jan 17 '22 00:01 Herschel

Hi @Herschel I like to work on this.

udai1931 avatar Jan 17 '22 10:01 udai1931

@Udai1931 you still one this, i like to have a knack at it

Mekomancer avatar Jan 26 '22 20:01 Mekomancer

@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.

stevenhuyn avatar Jan 29 '22 19:01 stevenhuyn

@Herschel This, issue has been without activity for a long time, can I try to work on it?

higumachan avatar Jul 01 '22 15:07 higumachan

@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 avatar Jul 07 '22 09:07 n0samu

@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.

  1. create an original Duration type that is compatible for ruffle
  2. discard compatibility

Which do you think is better, or do you have any other good ideas?

higumachan avatar Jul 24 '22 03:07 higumachan

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_secs etc. methods directly onto this type.
  • Having duration_helper as a separate crate is overkill; Duration could live in its own module in ruffle_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 avatar Jul 26 '22 23:07 Herschel

@Herschel

Thank you for response!

OK, I would like to implement the policy of adding Duration(f64) in ruffle_core.

higumachan avatar Jul 27 '22 12:07 higumachan