sfizz/benchmarks/BM_wavfile.cpp

110 lines
2.9 KiB
C++
Raw Permalink Normal View History

2020-01-25 10:04:31 +01:00
// SPDX-License-Identifier: BSD-2-Clause
2020-01-25 13:13:07 +01:00
// This code is part of the sfizz library and is licensed under a BSD 2-clause
// license. You should have receive a LICENSE.md file along with the code.
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
2020-01-04 17:38:51 +01:00
#include "Buffer.h"
2019-12-21 22:05:32 +01:00
#include <benchmark/benchmark.h>
#if defined(SFIZZ_USE_SNDFILE)
2019-12-21 22:05:32 +01:00
#include <sndfile.hh>
#endif
2019-12-21 22:05:32 +01:00
#define DR_WAV_IMPLEMENTATION
#include "dr_wav.h"
#include "ghc/fs_std.hpp"
2020-03-11 01:11:25 +01:00
#include "absl/memory/memory.h"
#if 0
#include "libnyquist/Decoders.h"
#endif
2019-12-21 22:05:32 +01:00
#ifndef NDEBUG
#include <iostream>
#endif
#include <unistd.h> // readlink
2019-12-21 22:05:32 +01:00
class FileFixture : public benchmark::Fixture {
public:
2020-03-11 01:11:25 +01:00
void SetUp(const ::benchmark::State& /* state */) {
2020-01-05 10:51:06 +01:00
filePath1 = getPath() / "sample1.wav";
filePath2 = getPath() / "sample2.wav";
filePath3 = getPath() / "sample3.wav";
if ( !fs::exists(filePath1)
|| !fs::exists(filePath2)
|| !fs::exists(filePath3)) {
2019-12-21 22:05:32 +01:00
#ifndef NDEBUG
std::cerr << "Can't find path" << '\n';
#endif
std::terminate();
}
}
void TearDown(const ::benchmark::State& /* state */) {
2019-12-21 22:05:32 +01:00
}
fs::path getPath()
2019-12-21 22:05:32 +01:00
{
#ifdef __linux__
char buf[PATH_MAX + 1];
if (readlink("/proc/self/exe", buf, sizeof(buf) - 1) == -1)
return {};
std::string str { buf };
return str.substr(0, str.rfind('/'));
#elif _WIN32
return fs::current_path();
2019-12-21 22:05:32 +01:00
#endif
}
std::unique_ptr<sfz::Buffer<float>> buffer;
fs::path filePath1;
fs::path filePath2;
fs::path filePath3;
2019-12-21 22:05:32 +01:00
};
#if defined(SFIZZ_USE_SNDFILE)
2019-12-21 22:05:32 +01:00
BENCHMARK_DEFINE_F(FileFixture, SndFile)(benchmark::State& state) {
for (auto _ : state)
{
2020-01-05 10:51:06 +01:00
SndfileHandle sndfile(filePath1.c_str());
2020-03-11 01:11:25 +01:00
buffer = absl::make_unique<sfz::Buffer<float>>(sndfile.channels() * sndfile.frames());
2019-12-21 22:05:32 +01:00
sndfile.readf(buffer->data(), sndfile.frames());
}
}
#endif
2019-12-21 22:05:32 +01:00
BENCHMARK_DEFINE_F(FileFixture, DrWav)(benchmark::State& state) {
for (auto _ : state)
{
drwav wav;
2020-01-05 10:51:06 +01:00
if (!drwav_init_file(&wav, filePath2.c_str(), nullptr)) {
2019-12-21 22:05:32 +01:00
#ifndef NDEBUG
std::cerr << "Can't find path" << '\n';
#endif
std::terminate();
}
2020-03-11 01:11:25 +01:00
buffer = absl::make_unique<sfz::Buffer<float>>(wav.channels * wav.totalPCMFrameCount);
2019-12-21 22:05:32 +01:00
drwav_read_pcm_frames_f32(&wav, wav.totalPCMFrameCount, buffer->data());
}
}
#if 0
BENCHMARK_DEFINE_F(FileFixture, LibNyquist)(benchmark::State& state) {
for (auto _ : state)
{
nqr::AudioData data;
nqr::NyquistIO loader;
loader.Load(&data, filePath3.string());
benchmark::DoNotOptimize(data);
}
}
#endif
2020-01-05 10:51:06 +01:00
#if defined(SFIZZ_USE_SNDFILE)
2019-12-21 22:05:32 +01:00
BENCHMARK_REGISTER_F(FileFixture, SndFile);
#endif
2019-12-21 22:05:32 +01:00
BENCHMARK_REGISTER_F(FileFixture, DrWav);
#if 0
BENCHMARK_REGISTER_F(FileFixture, LibNyquist);
#endif
2019-12-21 22:05:32 +01:00
BENCHMARK_MAIN();