Skip to content

Commit

Permalink
Merge branch 'main' into chore/add-valgrind-memory-leak-check
Browse files Browse the repository at this point in the history
  • Loading branch information
BayernMuller committed Nov 4, 2024
2 parents 6175cb1 + 9f2f856 commit 6d50c28
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ project(vibra)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -pedantic -O2 -fno-strict-aliasing")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -pedantic -fno-strict-aliasing")
include_directories(${CMAKE_SOURCE_DIR}/lib)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
endif()

# build verbose output
# set(CMAKE_VERBOSE_MAKEFILE ON)

Expand Down
27 changes: 18 additions & 9 deletions lib/utils/ffmpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ namespace ffmpeg

constexpr const char *DEFAULT_FFMPEG_PATHS[] = {"ffmpeg", "ffmpeg.exe"};
constexpr const char FFMPEG_PATH_ENV[] = "FFMPEG_PATH";
constexpr int EXPECTED_DURATION = 5 * 60; // 5 minutes

class FFmpegWrapper
{
public:
FFmpegWrapper() = delete;
static int ConvertToWav(const std::string &input_file, LowQualityTrack *pcm);
static LowQualityTrack ConvertToLowQaulityPcm(
std::string input_file, std::uint32_t start_seconds, std::uint32_t duration_seconds);

private:
static std::string getFFmpegPath();
static bool isWindows();
};

int FFmpegWrapper::ConvertToWav(const std::string &input_file, LowQualityTrack *pcm)
LowQualityTrack FFmpegWrapper::ConvertToLowQaulityPcm(
std::string input_file, std::uint32_t start_seconds, std::uint32_t duration_seconds)
{
static std::string ffmpeg_path = FFmpegWrapper::getFFmpegPath();
if (ffmpeg_path.empty())
Expand All @@ -46,7 +47,10 @@ int FFmpegWrapper::ConvertToWav(const std::string &input_file, LowQualityTrack *
<< "pcm_s" << LOW_QUALITY_SAMPLE_BIT_WIDTH << "le";
ss << " -ar " << LOW_QUALITY_SAMPLE_RATE;
ss << " -ac " << 1;
ss << " - 2>/dev/null"; // suppress std
ss << " -ss " << start_seconds;
ss << " -t " << duration_seconds;
ss << " -"; // stdout
ss << " 2>/dev/null"; // suppress std

std::FILE *pipe = popen(ss.str().c_str(), "r");
if (!pipe)
Expand All @@ -57,16 +61,21 @@ int FFmpegWrapper::ConvertToWav(const std::string &input_file, LowQualityTrack *
std::array<std::int16_t, 4096> buffer;
size_t bytes_read;

pcm->reserve(EXPECTED_DURATION * LOW_QUALITY_SAMPLE_RATE);
LowQualityTrack pcm;
pcm.reserve(duration_seconds * LOW_QUALITY_SAMPLE_RATE);

while ((bytes_read = fread(buffer.data(), 1, buffer.size(), pipe)) != 0)
{
pcm->insert(pcm->end(), buffer.begin(),
buffer.begin() + (bytes_read / sizeof(LowQualitySample)));
pcm.insert(pcm.end(), buffer.begin(),
buffer.begin() + (bytes_read / sizeof(LowQualitySample)));
}

pclose(pipe);
return 0;
int exit_code = pclose(pipe);
if (exit_code != 0)
{
throw std::runtime_error("PCM Conversion Failed: " + std::to_string(exit_code));
}
return pcm;
}

std::string FFmpegWrapper::getFFmpegPath()
Expand Down
8 changes: 5 additions & 3 deletions lib/vibra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "audio/wav.h"
#include "utils/ffmpeg.h"

constexpr std::uint32_t MAX_DURATION_SECONDS = 12;

Fingerprint *_get_fingerprint_from_wav(const Wav &wav);

Fingerprint *_get_fingerprint_from_low_quality_pcm(const LowQualityTrack &pcm);
Expand All @@ -17,8 +19,8 @@ Fingerprint *vibra_get_fingerprint_from_music_file(const char *music_file_path)
return _get_fingerprint_from_wav(wav);
}

LowQualityTrack pcm;
ffmpeg::FFmpegWrapper::ConvertToWav(path, &pcm);
LowQualityTrack pcm =
ffmpeg::FFmpegWrapper::ConvertToLowQaulityPcm(path, 0, MAX_DURATION_SECONDS);
return _get_fingerprint_from_low_quality_pcm(pcm);
}

Expand Down Expand Up @@ -64,7 +66,7 @@ Fingerprint *_get_fingerprint_from_low_quality_pcm(const LowQualityTrack &pcm)
{
SignatureGenerator generator;
generator.FeedInput(pcm);
generator.set_max_time_seconds(12);
generator.set_max_time_seconds(MAX_DURATION_SECONDS);

Signature signature = generator.GetNextSignature();

Expand Down

0 comments on commit 6d50c28

Please sign in to comment.