Skip to content

Commit

Permalink
✨ Added sample_unaltered_volume()
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFouchy committed Oct 20, 2023
1 parent 64664d4 commit ec6e34f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,15 @@ static auto mod(int64_t a, int64_t b) -> int64_t
return res;
}

auto Player::sample(int64_t frame_index, int64_t channel_index) -> float
auto Player::sample(int64_t frame_index, int64_t channel_index) const -> float
{
if (_properties.is_muted)
return 0.f;
return _properties.volume * sample_unaltered_volume(frame_index, channel_index);
}

auto Player::sample_unaltered_volume(int64_t frame_index, int64_t channel_index) const -> float
{
auto const sample_index = frame_index * _data.channels_count
+ channel_index % _data.channels_count;
if ((sample_index < 0
Expand All @@ -161,8 +165,7 @@ auto Player::sample(int64_t frame_index, int64_t channel_index) -> float
&& !_properties.does_loop)
return 0.f;

return _data.samples[static_cast<size_t>(mod(sample_index, static_cast<int64_t>(_data.samples.size())))]
* _properties.volume;
return _data.samples[static_cast<size_t>(mod(sample_index, static_cast<int64_t>(_data.samples.size())))];
}

void set_error_callback(RtAudioErrorCallback callback)
Expand Down
4 changes: 3 additions & 1 deletion src/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class Player {
auto has_audio_data() const -> bool;

/// Returns the value of the audio data at the given position, while taking all the player properties into account.
auto sample(int64_t frame_index, int64_t channel_index) -> float;
auto sample(int64_t frame_index, int64_t channel_index) const -> float;
/// Returns the value of the audio data at the given position, while ignoring `volume` and `is_muted` properties. It still takes `does_loop` into account.
auto sample_unaltered_volume(int64_t frame_index, int64_t channel_index) const -> float;

/// Used to get and set the properties.
auto properties() -> PlayerProperties& { return _properties; }
Expand Down

0 comments on commit ec6e34f

Please sign in to comment.