From b0e4fb316a6ef199c2dae29ff39f37e771281c17 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Tue, 23 Mar 2021 13:28:07 +0100 Subject: [PATCH] sfizz_render without sndfile --- clients/CMakeLists.txt | 2 +- clients/sfizz_render.cpp | 31 +++++++++++++++++++++++-------- cmake/SfizzDeps.cmake | 2 +- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/clients/CMakeLists.txt b/clients/CMakeLists.txt index 367acc04..37e6ed77 100644 --- a/clients/CMakeLists.txt +++ b/clients/CMakeLists.txt @@ -8,7 +8,7 @@ endif() if(SFIZZ_RENDER) add_executable(sfizz_render MidiHelpers.h sfizz_render.cpp) - target_link_libraries(sfizz_render PRIVATE sfizz::internal sfizz::fmidi sfizz::sndfile sfizz::cxxopts) + target_link_libraries(sfizz_render PRIVATE sfizz::internal sfizz::fmidi sfizz::cxxopts st_audiofile_formats) sfizz_enable_lto_if_needed(sfizz_render) install(TARGETS sfizz_render DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT "render" OPTIONAL) endif() diff --git a/clients/sfizz_render.cpp b/clients/sfizz_render.cpp index bd03ea20..6b646b4c 100644 --- a/clients/sfizz_render.cpp +++ b/clients/sfizz_render.cpp @@ -1,11 +1,11 @@ -#include #include "sfizz/Synth.h" #include "sfizz/MathHelpers.h" #include "sfizz/SfzHelpers.h" #include "sfizz/SIMDHelpers.h" #include "MidiHelpers.h" -#include "cxxopts.hpp" +#include +#include #include #include @@ -158,14 +158,27 @@ int main(int argc, char** argv) LOG_INFO("-- Cutting the rendering at the last MIDI End of Track message"); } - SndfileHandle outputFile (outputPath.u8string(), SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 2, sampleRate); - ERROR_IF(outputFile.error() != 0, "Error writing out the wav file: " << outputFile.strError()); + drwav outputFile; + drwav_data_format outputFormat {}; + outputFormat.container = drwav_container_riff; + outputFormat.format = DR_WAVE_FORMAT_PCM; + outputFormat.channels = 2; + outputFormat.sampleRate = sampleRate; + outputFormat.bitsPerSample = 16; + +#if !defined(_WIN32) + drwav_bool32 outputFileOk = drwav_init_file_write(&outputFile, outputPath.c_str(), &outputFormat, nullptr); +#else + drwav_bool32 outputFileOk = drwav_init_file_write_w(&outputFile, outputPath.c_str(), &outputFormat, nullptr); +#endif + ERROR_IF(!outputFileOk, "Error opening the wav file for writing"); auto sampleRateDouble = static_cast(sampleRate); const double increment { 1.0 / sampleRateDouble }; - int numFramesWritten { 0 }; + uint64_t numFramesWritten { 0 }; sfz::AudioBuffer audioBuffer { 2, blockSize }; sfz::Buffer interleavedBuffer { 2 * blockSize }; + sfz::Buffer interleavedPcm { 2 * blockSize }; fmidi_player_u midiPlayer { fmidi_player_new(midiFile.get()) }; CallbackData callbackData { synth, 0, false }; @@ -178,7 +191,8 @@ int main(int argc, char** argv) fmidi_player_tick(midiPlayer.get(), increment); synth.renderBlock(audioBuffer); sfz::writeInterleaved(audioBuffer.getConstSpan(0), audioBuffer.getConstSpan(1), absl::MakeSpan(interleavedBuffer)); - numFramesWritten += outputFile.writef(interleavedBuffer.data(), blockSize); + drwav_f32_to_s16(interleavedPcm.data(), interleavedBuffer.data(), 2 * blockSize); + numFramesWritten += drwav_write_pcm_frames(&outputFile, blockSize, interleavedPcm.data()); } if (!useEOT) { @@ -186,12 +200,13 @@ int main(int argc, char** argv) while (averagePower > 1e-12f) { synth.renderBlock(audioBuffer); sfz::writeInterleaved(audioBuffer.getConstSpan(0), audioBuffer.getConstSpan(1), absl::MakeSpan(interleavedBuffer)); - numFramesWritten += outputFile.writef(interleavedBuffer.data(), blockSize); + drwav_f32_to_s16(interleavedPcm.data(), interleavedBuffer.data(), 2 * blockSize); + numFramesWritten += drwav_write_pcm_frames(&outputFile, blockSize, interleavedPcm.data()); averagePower = sfz::meanSquared(interleavedBuffer); } } - outputFile.writeSync(); + drwav_uninit(&outputFile); LOG_INFO("Wrote " << numFramesWritten << " frames of sound data in" << outputPath.string()); return 0; diff --git a/cmake/SfizzDeps.cmake b/cmake/SfizzDeps.cmake index 9f30e111..e6f07c1c 100644 --- a/cmake/SfizzDeps.cmake +++ b/cmake/SfizzDeps.cmake @@ -57,7 +57,7 @@ add_library(sfizz::cxxopts ALIAS sfizz_cxxopts) target_include_directories(sfizz_cxxopts INTERFACE "external/cxxopts") # The sndfile library -if(SFIZZ_USE_SNDFILE OR SFIZZ_DEMOS OR SFIZZ_DEVTOOLS OR SFIZZ_BENCHMARKS OR SFIZZ_RENDER) +if(SFIZZ_USE_SNDFILE OR SFIZZ_DEMOS OR SFIZZ_DEVTOOLS OR SFIZZ_BENCHMARKS) add_library(sfizz_sndfile INTERFACE) add_library(sfizz::sndfile ALIAS sfizz_sndfile) if(SFIZZ_USE_VCPKG OR CMAKE_CXX_COMPILER_ID MATCHES "MSVC")