// SPDX-License-Identifier: BSD-2-Clause // Copyright (c) 2019-2020, Paul Ferrand // All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "Buffer.h" #include "SIMDHelpers.h" #include #include #include "ghc/filesystem.hpp" #include "Oversampler.h" #include "AudioBuffer.h" #include #include constexpr std::array coeffsStage2x { 0.036681502163648017, 0.13654762463195771, 0.27463175937945411, 0.42313861743656667, 0.56109869787919475, 0.67754004997416162, 0.76974183386322659, 0.83988962484963803, 0.89226081800387891, 0.9315419599631839, 0.96209454837808395, 0.98781637073289708 }; constexpr std::array coeffsStage4x { 0.042448989488488006, 0.17072114107630679, 0.39329183835224008, 0.74569514831986694 }; constexpr std::array coeffsStage8x { 0.055748680811302048, 0.24305119574153092, 0.6466991311926823 }; #if defined(__x86_64__) || defined(__i386__) #include "hiir/Upsampler2xSse.h" using Upsampler2x = hiir::Upsampler2xSse; using Upsampler4x = hiir::Upsampler2xSse; using Upsampler8x = hiir::Upsampler2xSse; #elif defined(__arm__) || defined(__aarch64__) #include "hiir/Upsampler2xNeon.h" using Upsampler2x = hiir::Upsampler2xNeon; using Upsampler4x = hiir::Upsampler2xNeon; using Upsampler8x = hiir::Upsampler2xNeon; #else #include "hiir/Upsampler2xFpu.h" using Upsampler2x = hiir::Upsampler2xFpu; using Upsampler4x = hiir::Upsampler2xFpu; using Upsampler8x = hiir::Upsampler2xFpu; #endif class FileFixture : public benchmark::Fixture { public: void SetUp(const ::benchmark::State& /* state */) { rootPath = getPath() / "sample1.flac"; if (!ghc::filesystem::exists(rootPath)) { #ifndef NDEBUG std::cerr << "Can't find path" << '\n'; #endif std::terminate(); } sndfile = SndfileHandle(rootPath.c_str()); numFrames = static_cast(sndfile.frames()); output = std::make_unique>(sndfile.channels(), numFrames * 4); } void TearDown(const ::benchmark::State& state [[maybe_unused]]) { } ghc::filesystem::path getPath() { #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 ghc::filesystem::current_path(); #endif } std::unique_ptr> output; SndfileHandle sndfile; ghc::filesystem::path rootPath; size_t numFrames { 0 }; }; BENCHMARK_DEFINE_F(FileFixture, NoResampling)(benchmark::State& state) { for (auto _ : state) { sfz::Buffer buffer { numFrames * sndfile.channels() }; sndfile.readf(buffer.data(), sndfile.frames()); sfz::readInterleaved(buffer, output->getSpan(0), output->getSpan(1)); } } BENCHMARK_DEFINE_F(FileFixture, ResampleAtOnce)(benchmark::State& state) { for (auto _ : state) { sfz::Buffer buffer { numFrames * sndfile.channels() }; sfz::Buffer temp { numFrames * 4 }; Upsampler2x upsampler2x; Upsampler4x upsampler4x; upsampler2x.set_coefs(coeffsStage2x.data()); upsampler4x.set_coefs(coeffsStage4x.data()); sndfile.readf(buffer.data(), numFrames); sfz::readInterleaved(buffer, output->getSpan(0), output->getSpan(1)); upsampler2x.process_block(temp.data(), output->channelReader(0), static_cast(numFrames)); upsampler4x.process_block(output->channelWriter(0), temp.data(), static_cast(numFrames * 2)); upsampler2x.clear_buffers(); upsampler4x.clear_buffers(); upsampler2x.process_block(temp.data(), output->channelReader(1), static_cast(numFrames)); upsampler4x.process_block(output->channelWriter(1), temp.data(), static_cast(numFrames * 2)); } } BENCHMARK_DEFINE_F(FileFixture, ResampleInChunks)(benchmark::State& state) { for (auto _ : state) { auto chunkSize = static_cast(state.range(0)); sfz::Buffer buffer { numFrames * sndfile.channels() }; sfz::Buffer leftInput { chunkSize }; sfz::Buffer rightInput { chunkSize }; sfz::Buffer chunk { chunkSize * 2 }; sndfile.readf(buffer.data(), numFrames); auto bufferSpan = absl::MakeSpan(buffer); auto leftSpan = absl::MakeSpan(leftInput); auto rightSpan = absl::MakeSpan(rightInput); auto chunkSpan = absl::MakeSpan(chunk); Upsampler2x upsampler2xLeft; Upsampler2x upsampler2xRight; Upsampler4x upsampler4xLeft; Upsampler4x upsampler4xRight; upsampler2xLeft.set_coefs(coeffsStage2x.data()); upsampler2xRight.set_coefs(coeffsStage2x.data()); upsampler4xLeft.set_coefs(coeffsStage4x.data()); upsampler4xRight.set_coefs(coeffsStage4x.data()); size_t inputFrameCounter { 0 }; size_t outputFrameCounter { 0 }; while(inputFrameCounter < numFrames) { // std::cout << "Input frames: " << inputFrameCounter << "/" << numFrames << '\n'; const auto thisChunkSize = std::min(chunkSize, numFrames - inputFrameCounter); const auto bufferChunk = bufferSpan.subspan( inputFrameCounter * sndfile.channels(), thisChunkSize * sndfile.channels() ); sfz::readInterleaved(bufferChunk, leftSpan, rightSpan); upsampler2xLeft.process_block(chunkSpan.data(), leftSpan.data(), static_cast(thisChunkSize)); upsampler4xLeft.process_block(output->channelWriter(0) + outputFrameCounter, chunkSpan.data(), static_cast(thisChunkSize * 2)); upsampler2xRight.process_block(chunkSpan.data(), rightSpan.data(), static_cast(thisChunkSize)); upsampler4xRight.process_block(output->channelWriter(1) + outputFrameCounter, chunkSpan.data(), static_cast(thisChunkSize * 2)); inputFrameCounter += chunkSize; outputFrameCounter += chunkSize * 4; } } } BENCHMARK_REGISTER_F(FileFixture, NoResampling); BENCHMARK_REGISTER_F(FileFixture, ResampleAtOnce); BENCHMARK_REGISTER_F(FileFixture, ResampleInChunks)->RangeMultiplier(4)->Range((1 << 4), (1 << 16)); BENCHMARK_MAIN();