-
Notifications
You must be signed in to change notification settings - Fork 39
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
Bump Ratio to 3.0 #438
Bump Ratio to 3.0 #438
Changes from 3 commits
f15745f
838bc79
25d2b39
504f1ff
0a26238
d65d679
3276e55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,9 +212,9 @@ defmodule Membrane.Clock do | |
end | ||
|
||
defp handle_clock_update(till_next, state) do | ||
use Ratio | ||
use Numbers, overload_operators: true | ||
|
||
if till_next < 0 do | ||
if Ratio.lt?(till_next, 0) do | ||
raise "Clock update time cannot be negative, received: #{inspect(till_next)}" | ||
end | ||
|
||
|
@@ -227,10 +227,10 @@ defmodule Membrane.Clock do | |
end | ||
|
||
defp do_handle_clock_update(till_next, state) do | ||
use Ratio | ||
use Numbers, overload_operators: true | ||
%{till_next: from_previous, clock_time: clock_time} = state | ||
clock_time = clock_time + from_previous | ||
ratio = clock_time / (state.time_provider.() - state.init_time) | ||
ratio = Ratio.div(Ratio.new(clock_time), Ratio.new(state.time_provider.() - state.init_time)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as above |
||
state = %{state | clock_time: clock_time, till_next: till_next} | ||
broadcast_and_update_ratio(ratio, state) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ defmodule Membrane.Core.Timer do | |
end | ||
|
||
def tick(timer) do | ||
use Ratio | ||
use Numbers, overload_operators: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's be consistent and not use that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
%__MODULE__{ | ||
id: id, | ||
|
@@ -62,7 +62,9 @@ defmodule Membrane.Core.Timer do | |
|
||
# Next tick time converted to BEAM clock time | ||
beam_next_tick_time = | ||
(init_time + next_tick_time / ratio) |> Ratio.floor() |> Time.round_to_milliseconds() | ||
(init_time + Ratio.new(next_tick_time, ratio)) | ||
|> Ratio.floor() | ||
|> Time.round_to_milliseconds() | ||
|
||
timer_ref = | ||
Process.send_after(self(), Message.new(:timer_tick, id), beam_next_tick_time, abs: true) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ defmodule Membrane.Time do | |
that do not touch hardware clock, you should use Membrane units for consistency. | ||
""" | ||
|
||
require Ratio | ||
|
||
@compile {:inline, | ||
native_units: 1, native_unit: 0, nanoseconds: 1, nanosecond: 0, second: 0, seconds: 1} | ||
|
||
|
@@ -30,7 +32,7 @@ defmodule Membrane.Time do | |
# Difference between 01.01.1900 (start of NTP epoch) and 01.01.1970 (start of Unix epoch) in seconds | ||
@ntp_unix_epoch_diff 2_208_988_800 | ||
|
||
@two_to_pow_32 Ratio.pow(2, 32) | ||
@two_to_pow_32 Ratio.pow(Ratio.new(2, 1), 32) |> Ratio.trunc() | ||
|
||
@doc """ | ||
Checks whether a value is `Membrane.Time.t`. | ||
|
@@ -260,12 +262,10 @@ defmodule Membrane.Time do | |
end | ||
|
||
# credo:disable-for-next-line Credo.Check.Readability.Specs | ||
def unquote(unit.plural)(number) do | ||
if not Ratio.is_rational?(number) do | ||
raise "Only integers and rationals can be converted with Membrane.Time.#{unquote(unit.plural)}" | ||
end | ||
def unquote(unit.plural)(number) when Ratio.is_rational(number) do | ||
use Numbers, overload_operators: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done here and also in clock_test.exs |
||
|
||
Ratio.*(number, unquote(unit.duration)) | ||
(number * unquote(unit.duration)) | ||
|> round_rational() | ||
end | ||
|
||
|
@@ -288,7 +288,7 @@ defmodule Membrane.Time do | |
@spec unquote(as_fun_name)(t) :: integer | Ratio.t() | ||
# credo:disable-for-next-line Credo.Check.Readability.Specs | ||
def unquote(as_fun_name)(time) when is_time(time) do | ||
Ratio./(time, unquote(unit.duration)) | ||
Ratio.new(time, unquote(unit.duration)) | ||
end | ||
end) | ||
|
||
|
@@ -298,7 +298,6 @@ defmodule Membrane.Time do | |
end | ||
|
||
defp round_rational(ratio) do | ||
ratio = make_rational(ratio) | ||
trunced = Ratio.trunc(ratio) | ||
|
||
if 2 * sign_of_rational(ratio) * | ||
|
@@ -308,14 +307,6 @@ defmodule Membrane.Time do | |
else: trunced | ||
end | ||
|
||
defp make_rational(number) do | ||
if Ratio.is_rational?(number) do | ||
number | ||
else | ||
%Ratio{numerator: number, denominator: 1} | ||
end | ||
end | ||
|
||
defp sign_of_rational(ratio) do | ||
if ratio.numerator == 0, do: 0, else: Ratio.sign(ratio) | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we overload operators if we don't use them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we are using them - the overloaded operators are not only comparison operators, but also arithmetic operators