Skip to content

Commit

Permalink
✨ Added compute_volume()
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFouchy committed Oct 23, 2023
1 parent b05f621 commit 2ea44b1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion include/Audio/Audio.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "../../src/load_audio_file.h"
#include "../../src/compute_volume.hpp"
#include "../../src/load_audio_file.hpp"
#include "RtAudioWrapper/RtAudioWrapper.hpp"
2 changes: 1 addition & 1 deletion lib/RtAudioWrapper
Submodule RtAudioWrapper updated 1 files
+1 −0 src/Player.hpp
26 changes: 26 additions & 0 deletions src/compute_volume.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "compute_volume.hpp"
#include <cstdint>

namespace Cool {

auto compute_volume(RtAudioW::Player const& player, float average_duration_in_seconds /*TODO(Audio) better name*/) -> float
{
if (!player.has_audio_data())
return 0.f;

auto const width = static_cast<int64_t>(static_cast<float>(player.audio_data().sample_rate) * average_duration_in_seconds);

float volume{0.f};
for (int64_t i = 0; i < width; ++i)
{
int64_t const offset = i - width / 2;
for (int64_t channel_index = 0; channel_index < player.audio_data().channels_count; ++channel_index)
{
// TODO(Audio) When the audio loops, we might start sampling the next play of the audio. But since the width is small, this is probably not a noticeable problem ?
volume += std::abs(player.sample_unaltered_volume(player.current_frame_index() + offset, channel_index));
}
}
return volume / static_cast<float>(width) / static_cast<float>(player.audio_data().channels_count);
}

} // namespace Cool
9 changes: 9 additions & 0 deletions src/compute_volume.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include "RtAudioWrapper/RtAudioWrapper.hpp"

namespace Cool {

/// Returns a number between 0 and 1.
auto compute_volume(RtAudioW::Player const& player, float average_duration_in_seconds) -> float;

} // namespace Cool
2 changes: 1 addition & 1 deletion src/load_audio_file.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "load_audio_file.h"
#include "load_audio_file.hpp"
#include "libnyquist/Common.h"
#include "libnyquist/Decoders.h"

Expand Down
File renamed without changes.

0 comments on commit 2ea44b1

Please sign in to comment.