From e17c99dcdf9711e86c188d5b53052ff12fe3a046 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sun, 22 May 2022 19:01:50 -0500 Subject: [PATCH] allow building Furnace without SDL2 and libsndfile for eventual libfurnace --- CMakeLists.txt | 142 +++++++++++++++++++++++----------------- src/engine/engine.cpp | 31 ++++++++- src/engine/playback.cpp | 2 + src/engine/sample.cpp | 7 ++ 4 files changed, 121 insertions(+), 61 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e58a2794..5b07e05e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,8 @@ set(CMAKE_PROJECT_VERSION_MINOR 6) set(CMAKE_PROJECT_VERSION_PATCH 0) set(BUILD_GUI_DEFAULT ON) +set(USE_SDL2_DEFAULT ON) +set(USE_SNDFILE_DEFAULT ON) set(SYSTEM_SDL2_DEFAULT OFF) if (ANDROID) @@ -35,7 +37,9 @@ else() endif() option(BUILD_GUI "Build the tracker (disable to build only a headless player)" ${BUILD_GUI_DEFAULT}) -option(USE_RTMIDI "Build with MIDI support using RtMidi. Currently unfinished." ${USE_RTMIDI_DEFAULT}) +option(USE_RTMIDI "Build with MIDI support using RtMidi." ${USE_RTMIDI_DEFAULT}) +option(USE_SDL2 "Build with SDL2. Required to build with GUI." ${USE_SDL2_DEFAULT}) +option(USE_SNDFILE "Build with libsndfile. Required in order to work with audio files." ${USE_SNDFILE_DEFAULT}) option(WITH_JACK "Whether to build with JACK support. Auto-detects if JACK is available" ${WITH_JACK_DEFAULT}) option(SYSTEM_FMT "Use a system-installed version of fmt instead of the vendored one" OFF) option(SYSTEM_LIBSNDFILE "Use a system-installed version of libsndfile instead of the vendored one" OFF) @@ -93,25 +97,30 @@ else() message(STATUS "Using vendored fmt") endif() -if (SYSTEM_LIBSNDFILE) - find_package(PkgConfig REQUIRED) - pkg_check_modules(LIBSNDFILE REQUIRED sndfile) - list(APPEND DEPENDENCIES_INCLUDE_DIRS ${LIBSNDFILE_INCLUDE_DIRS}) - list(APPEND DEPENDENCIES_COMPILE_OPTIONS ${LIBSNDFILE_CFLAGS_OTHER}) - list(APPEND DEPENDENCIES_LIBRARIES ${LIBSNDFILE_LIBRARIES}) - list(APPEND DEPENDENCIES_LIBRARY_DIRS ${LIBSNDFILE_LIBRARY_DIRS}) - list(APPEND DEPENDENCIES_LINK_OPTIONS ${LIBSNDFILE_LDFLAGS_OTHER}) - list(APPEND DEPENDENCIES_LEGACY_LDFLAGS ${LIBSNDFILE_LDFLAGS}) - message(STATUS "Using system-installed libsndfile") +if (USE_SNDFILE) + list(APPEND DEPENDENCIES_DEFINES HAVE_SNDFILE) + if (SYSTEM_LIBSNDFILE) + find_package(PkgConfig REQUIRED) + pkg_check_modules(LIBSNDFILE REQUIRED sndfile) + list(APPEND DEPENDENCIES_INCLUDE_DIRS ${LIBSNDFILE_INCLUDE_DIRS}) + list(APPEND DEPENDENCIES_COMPILE_OPTIONS ${LIBSNDFILE_CFLAGS_OTHER}) + list(APPEND DEPENDENCIES_LIBRARIES ${LIBSNDFILE_LIBRARIES}) + list(APPEND DEPENDENCIES_LIBRARY_DIRS ${LIBSNDFILE_LIBRARY_DIRS}) + list(APPEND DEPENDENCIES_LINK_OPTIONS ${LIBSNDFILE_LDFLAGS_OTHER}) + list(APPEND DEPENDENCIES_LEGACY_LDFLAGS ${LIBSNDFILE_LDFLAGS}) + message(STATUS "Using system-installed libsndfile") + else() + set(BUILD_TESTING OFF CACHE BOOL "aaaaaa" FORCE) + set(BUILD_PROGRAMS OFF CACHE BOOL "aaa" FORCE) + set(BUILD_EXAMPLES OFF CACHE BOOL "a" FORCE) + set(ENABLE_EXTERNAL_LIBS OFF CACHE BOOL "come on" FORCE) + set(ENABLE_MPEG OFF CACHE BOOL "come on" FORCE) + add_subdirectory(extern/libsndfile EXCLUDE_FROM_ALL) + list(APPEND DEPENDENCIES_LIBRARIES sndfile) + message(STATUS "Using vendored libsndfile") + endif() else() - set(BUILD_TESTING OFF CACHE BOOL "aaaaaa" FORCE) - set(BUILD_PROGRAMS OFF CACHE BOOL "aaa" FORCE) - set(BUILD_EXAMPLES OFF CACHE BOOL "a" FORCE) - set(ENABLE_EXTERNAL_LIBS OFF CACHE BOOL "come on" FORCE) - set(ENABLE_MPEG OFF CACHE BOOL "come on" FORCE) - add_subdirectory(extern/libsndfile EXCLUDE_FROM_ALL) - list(APPEND DEPENDENCIES_LIBRARIES sndfile) - message(STATUS "Using vendored libsndfile") + message(STATUS "Not using libsndfile") endif() if (USE_RTMIDI) @@ -154,58 +163,71 @@ else() message(STATUS "Using vendored zlib") endif() -if (SYSTEM_SDL2) - if (PKG_CONFIG_FOUND) - pkg_check_modules(SDL2 sdl2>=${SYSTEM_SDL_MIN_VER}) - if (SDL2_FOUND) - list(APPEND DEPENDENCIES_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS}) - list(APPEND DEPENDENCIES_COMPILE_OPTIONS ${SDL2_CFLAGS_OTHER}) - list(APPEND DEPENDENCIES_LIBRARIES ${SDL2_LIBRARIES}) - list(APPEND DEPENDENCIES_LIBRARY_DIRS ${SDL2_LIBRARY_DIRS}) - list(APPEND DEPENDENCIES_LINK_OPTIONS ${SDL2_LDFLAGS_OTHER}) - list(APPEND DEPENDENCIES_LEGACY_LDFLAGS ${SDL2_LDFLAGS}) +if (USE_SDL2) + if (SYSTEM_SDL2) + if (PKG_CONFIG_FOUND) + pkg_check_modules(SDL2 sdl2>=${SYSTEM_SDL_MIN_VER}) + if (SDL2_FOUND) + list(APPEND DEPENDENCIES_DEFINES HAVE_SDL2) + list(APPEND DEPENDENCIES_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS}) + list(APPEND DEPENDENCIES_COMPILE_OPTIONS ${SDL2_CFLAGS_OTHER}) + list(APPEND DEPENDENCIES_LIBRARIES ${SDL2_LIBRARIES}) + list(APPEND DEPENDENCIES_LIBRARY_DIRS ${SDL2_LIBRARY_DIRS}) + list(APPEND DEPENDENCIES_LINK_OPTIONS ${SDL2_LDFLAGS_OTHER}) + list(APPEND DEPENDENCIES_LEGACY_LDFLAGS ${SDL2_LDFLAGS}) + endif() endif() + if (NOT SDL2_FOUND) + find_package(SDL2 ${SYSTEM_SDL_MIN_VER} REQUIRED) + list(APPEND DEPENDENCIES_DEFINES HAVE_SDL2) + list(APPEND DEPENDENCIES_INCLUDE_DIRS ${SDL2_INCLUDE_DIR}) + list(APPEND DEPENDENCIES_LIBRARIES ${SDL2_LIBRARY}) + endif() + message(STATUS "Using system-installed SDL2") + else() + if (ANDROID) + set(SDL_SHARED ON CACHE BOOL "Force no dynamically-linked SDL" FORCE) + set(SDL_STATIC OFF CACHE BOOL "Force statically-linked SDL" FORCE) + else() + set(SDL_SHARED OFF CACHE BOOL "Force no dynamically-linked SDL" FORCE) + set(SDL_STATIC ON CACHE BOOL "Force statically-linked SDL" FORCE) + endif() + # https://github.com/libsdl-org/SDL/issues/1481 + # On 2014-06-22 17:15:50 +0000, Sam Lantinga wrote: + # If you link SDL statically, you also need to define HAVE_LIBC so it builds with the C runtime that your application uses. + # This should probably go in a FAQ. + set(SDL_LIBC ON CACHE BOOL "Tell SDL that we want it to use our C runtime (required for proper static linking)" FORCE) + add_subdirectory(extern/SDL EXCLUDE_FROM_ALL) + list(APPEND DEPENDENCIES_DEFINES HAVE_SDL2) + list(APPEND DEPENDENCIES_INCLUDE_DIRS extern/SDL/include) + if (ANDROID) + list(APPEND DEPENDENCIES_LIBRARIES SDL2) + else() + list(APPEND DEPENDENCIES_LIBRARIES SDL2-static) + endif() + # Work around add_subdirectory'd SDL not propagating HAVE_LIBC to MSVC furnace build + if (MSVC) + list(APPEND DEPENDENCIES_COMPILE_OPTIONS "/DHAVE_LIBC") + endif() + message(STATUS "Using vendored SDL2") endif() - if (NOT SDL2_FOUND) - find_package(SDL2 ${SYSTEM_SDL_MIN_VER} REQUIRED) - list(APPEND DEPENDENCIES_INCLUDE_DIRS ${SDL2_INCLUDE_DIR}) - list(APPEND DEPENDENCIES_LIBRARIES ${SDL2_LIBRARY}) - endif() - message(STATUS "Using system-installed SDL2") else() - if (ANDROID) - set(SDL_SHARED ON CACHE BOOL "Force no dynamically-linked SDL" FORCE) - set(SDL_STATIC OFF CACHE BOOL "Force statically-linked SDL" FORCE) - else() - set(SDL_SHARED OFF CACHE BOOL "Force no dynamically-linked SDL" FORCE) - set(SDL_STATIC ON CACHE BOOL "Force statically-linked SDL" FORCE) + message(STATUS "Not using SDL2") + if (BUILD_GUI) + message(FATAL_ERROR "SDL2 is required in order to build with GUI! Disable BUILD_GUI otherwise.") endif() - # https://github.com/libsdl-org/SDL/issues/1481 - # On 2014-06-22 17:15:50 +0000, Sam Lantinga wrote: - # If you link SDL statically, you also need to define HAVE_LIBC so it builds with the C runtime that your application uses. - # This should probably go in a FAQ. - set(SDL_LIBC ON CACHE BOOL "Tell SDL that we want it to use our C runtime (required for proper static linking)" FORCE) - add_subdirectory(extern/SDL EXCLUDE_FROM_ALL) - list(APPEND DEPENDENCIES_INCLUDE_DIRS extern/SDL/include) - if (ANDROID) - list(APPEND DEPENDENCIES_LIBRARIES SDL2) - else() - list(APPEND DEPENDENCIES_LIBRARIES SDL2-static) - endif() - # Work around add_subdirectory'd SDL not propagating HAVE_LIBC to MSVC furnace build - if (MSVC) - list(APPEND DEPENDENCIES_COMPILE_OPTIONS "/DHAVE_LIBC") - endif() - message(STATUS "Using vendored SDL2") endif() set(AUDIO_SOURCES src/audio/abstract.cpp src/audio/midi.cpp -src/audio/sdl.cpp ) -if(WITH_JACK) +if (USE_SDL2) + list(APPEND AUDIO_SOURCES src/audio/sdl.cpp) +endif() + +if (WITH_JACK) find_package(PkgConfig REQUIRED) pkg_check_modules(JACK REQUIRED jack) list(APPEND AUDIO_SOURCES src/audio/jack.cpp) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 2307d198..5a17cd3c 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -23,7 +23,9 @@ #include "safeReader.h" #include "../ta-log.h" #include "../fileutils.h" +#ifdef HAVE_SDL2 #include "../audio/sdl.h" +#endif #include #ifndef _WIN32 #include @@ -34,7 +36,9 @@ #include "../audio/jack.h" #endif #include +#ifdef HAVE_SNDFILE #include +#endif #include void process(void* u, float** in, float** out, int inChans, int outChans, unsigned int size) { @@ -187,6 +191,7 @@ bool DivEngine::isExporting() { #define EXPORT_BUFSIZE 2048 +#ifdef HAVE_SNDFILE void DivEngine::runExportThread() { switch (exportMode) { case DIV_EXPORT_MODE_ONE: { @@ -438,8 +443,16 @@ void DivEngine::runExportThread() { } stopExport=false; } +#else +void DivEngine::runExportThread() { +} +#endif bool DivEngine::saveAudio(const char* path, int loops, DivAudioExportModes mode) { +#ifndef HAVE_SNDFILE + logE("Furnace was not compiled with libsndfile. cannot export!"); + return false; +#else exportPath=path; exportMode=mode; if (exportMode!=DIV_EXPORT_MODE_ONE) { @@ -461,6 +474,7 @@ bool DivEngine::saveAudio(const char* path, int loops, DivAudioExportModes mode) remainingLoops=loops; exportThread=new std::thread(_runExportThread,this); return true; +#endif } void DivEngine::waitAudioFile() { @@ -2015,6 +2029,10 @@ int DivEngine::addSampleFromFile(const char* path) { } } +#ifndef HAVE_SNDFILE + lastError="Furnace was not compiled with libsndfile!"; + return -1; +#else SF_INFO si; SNDFILE* f=sf_open(path,SFM_READ,&si); if (f==NULL) { @@ -2093,6 +2111,7 @@ int DivEngine::addSampleFromFile(const char* path) { renderSamples(); BUSY_END; return sampleCount; +#endif } void DivEngine::delSample(int index) { @@ -2743,13 +2762,23 @@ bool DivEngine::initAudioBackend() { logE("Furnace was not compiled with JACK support!"); setConf("audioEngine","SDL"); saveConf(); +#ifdef HAVE_SDL2 output=new TAAudioSDL; +#else + logE("Furnace was not compiled with SDL support either!"); + output=new TAAudio; +#endif #else output=new TAAudioJACK; #endif break; case DIV_AUDIO_SDL: +#ifdef HAVE_SDL2 output=new TAAudioSDL; +#else + logE("Furnace was not compiled with SDL support!"); + output=new TAAudio; +#endif break; case DIV_AUDIO_DUMMY: output=new TAAudio; @@ -2844,7 +2873,7 @@ bool DivEngine::init() { // init config #ifdef _WIN32 configPath=getWinConfigPath(); -#elif defined(ANDROID) +#elif defined(IS_MOBILE) configPath=SDL_GetPrefPath("tildearrow","furnace"); #else struct stat st; diff --git a/src/engine/playback.cpp b/src/engine/playback.cpp index f8c3847f..49151570 100644 --- a/src/engine/playback.cpp +++ b/src/engine/playback.cpp @@ -23,7 +23,9 @@ #include "engine.h" #include "../ta-log.h" #include +#ifdef HAVE_SNDFILE #include +#endif constexpr int MASTER_CLOCK_PREC=(sizeof(void*)==8)?8:0; diff --git a/src/engine/sample.cpp b/src/engine/sample.cpp index a6df2f06..53441095 100644 --- a/src/engine/sample.cpp +++ b/src/engine/sample.cpp @@ -21,7 +21,9 @@ #include "../ta-log.h" #include #include +#ifdef HAVE_SNDFILE #include +#endif #include "filter.h" extern "C" { @@ -37,6 +39,10 @@ DivSampleHistory::~DivSampleHistory() { } bool DivSample::save(const char* path) { +#ifndef HAVE_SNDFILE + logE("Furnace was not compiled with libsndfile!"); + return false; +#else SNDFILE* f; SF_INFO si; memset(&si,0,sizeof(SF_INFO)); @@ -76,6 +82,7 @@ bool DivSample::save(const char* path) { sf_close(f); return true; +#endif } // 16-bit memory is padded to 512, to make things easier for ADPCM-A/B.