Skip to content

Commit

Permalink
🤏 Use double instead of float to represent time
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFouchy committed May 26, 2024
1 parent c565cca commit 06340b0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void Player::set_audio_data(AudioData data)
if (backend().isStreamOpen())
backend().closeStream(); // Otherwise data race with the audio thread that is reading _audio_data. Could cause crashes.

float const current_time = get_time();
double const current_time = get_time();

_data = std::move(data);
set_time(current_time); // Need to adjust the _next_frame_to_play so that we will be at the same point in time in both audios even if they have different sample rates.
Expand All @@ -123,23 +123,23 @@ void Player::pause()
_is_playing = false;
}

auto Player::set_time(float time_in_seconds) -> bool
auto Player::set_time(double time_in_seconds) -> bool
{
auto const next_frame_to_play = static_cast<int64_t>(
static_cast<float>(_data.sample_rate)
static_cast<double>(_data.sample_rate)
* time_in_seconds
);
bool const has_changed = next_frame_to_play != _next_frame_to_play;
_next_frame_to_play = next_frame_to_play;
return has_changed;
}

auto Player::get_time() const -> float
auto Player::get_time() const -> double
{
if (_data.sample_rate == 0)
return 0.f;
return static_cast<float>(_next_frame_to_play)
/ static_cast<float>(_data.sample_rate);
return 0.;
return static_cast<double>(_next_frame_to_play)
/ static_cast<double>(_data.sample_rate);
}

static auto mod(int64_t a, int64_t b) -> int64_t
Expand Down
4 changes: 2 additions & 2 deletions src/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class Player {
[[nodiscard]] auto is_playing() const -> bool { return _is_playing; }
/// Makes the player jump to a specific moment in time.
/// Return true iff the time has actually changed (i.e. the time that was passed to the function is different from the time that was currently set).
auto set_time(float time_in_seconds) -> bool;
auto set_time(double time_in_seconds) -> bool;
/// Returns the moment in time the player is currently playing.
[[nodiscard]] auto get_time() const -> float;
[[nodiscard]] auto get_time() const -> double;

/// Checks if the default device has changed (e.g. the user has just plugged in some headphones)
/// and switches device accordingly.
Expand Down

0 comments on commit 06340b0

Please sign in to comment.