From f389b85619f68f35658850b81e2be3a9337dde26 Mon Sep 17 00:00:00 2001 From: falkTX Date: Tue, 12 Mar 2024 02:47:01 +0100 Subject: [PATCH] More mac details Signed-off-by: falkTX --- Makefile | 1 + src/plugin/DesktopAudioDriver.cpp | 111 +++++++++++++++++++++++------- src/plugin/DesktopPlugin.cpp | 42 +++++++++-- src/plugin/Makefile | 2 +- src/plugin/SharedMemory.hpp | 6 +- 5 files changed, 131 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 6da06e7..5853e3f 100644 --- a/Makefile +++ b/Makefile @@ -101,6 +101,7 @@ TARGETS += build/mod-desktop.app/Contents/MacOS/jackd TARGETS += build/mod-desktop.app/Contents/MacOS/jack/jack-session.conf TARGETS += build/mod-desktop.app/Contents/MacOS/jack/jack_coreaudio.so TARGETS += build/mod-desktop.app/Contents/MacOS/jack/jack_coremidi.so +TARGETS += build/mod-desktop.app/Contents/MacOS/jack/jack_desktop.so TARGETS += build/mod-desktop.app/Contents/MacOS/jack/jack_dummy.so TARGETS += build/mod-desktop.app/Contents/MacOS/jack/mod-host.so TARGETS += build/mod-desktop.app/Contents/MacOS/jack/mod-midi-broadcaster.so diff --git a/src/plugin/DesktopAudioDriver.cpp b/src/plugin/DesktopAudioDriver.cpp index 4b59d24..1e24460 100644 --- a/src/plugin/DesktopAudioDriver.cpp +++ b/src/plugin/DesktopAudioDriver.cpp @@ -6,15 +6,20 @@ #include "JackEngineControl.h" #include "JackTools.h" -#if defined(__APPLE__) -#elif defined(_WIN32) +#ifdef _WIN32 #else -#include -#include -#include -#include -#include -#include +# include +# include +# ifdef __APPLE__ +# include +# include +# include +# else +# include +# include +# include +# include +# endif #endif namespace Jack @@ -26,29 +31,46 @@ class DesktopAudioDriver : public JackAudioDriver { struct Data { uint32_t magic; + int32_t padding1; + #if defined(__APPLE__) + char bootname1[32]; + char bootname2[32]; + #elif defined(_WIN32) + HANDLE sem1; + HANDLE sem2; + #else int32_t sem1; int32_t sem2; - int32_t padding1; - // uint8_t sem[32]; + #endif uint16_t midiEventCount; uint16_t midiFrames[511]; uint8_t midiData[511 * 4]; uint8_t padding2[4]; float audio[]; - }* data = nullptr; + }; static constexpr const size_t kDataSize = sizeof(Data) + sizeof(float) * 128 * 2; Data* fShmData; + #ifndef _WIN32 + int fShmFd; + #endif + #if defined(__APPLE__) + mach_port_t task = MACH_PORT_NULL; + semaphore_t sem1 = MACH_PORT_NULL; + semaphore_t sem2 = MACH_PORT_NULL; + void post() { + semaphore_signal(sem2); } bool wait() { - return false; + const mach_timespec timeout = { 1, 0 }; + return semaphore_timedwait(sem1, timeout) == KERN_SUCCESS; } #elif defined(_WIN32) void post() @@ -60,8 +82,6 @@ class DesktopAudioDriver : public JackAudioDriver return false; } #else - int fShmFd; - void post() { const bool unlocked = __sync_bool_compare_and_swap(&data->sem2, 0, 1); @@ -120,9 +140,7 @@ class DesktopAudioDriver : public JackAudioDriver DesktopAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table) : JackAudioDriver(name, alias, engine, table), fShmData(nullptr), - #if defined(__APPLE__) - #elif defined(_WIN32) - #else + #ifndef _WIN32 fShmFd(-1), #endif fIsProcessing(false), @@ -154,8 +172,7 @@ class DesktopAudioDriver : public JackAudioDriver return -1; } - #if defined(__APPLE__) - #elif defined(_WIN32) + #ifdef _WIN32 #else fShmFd = shm_open("/mod-desktop-test1", O_RDWR, 0); if (fShmFd < 0) @@ -165,7 +182,16 @@ class DesktopAudioDriver : public JackAudioDriver return -1; } - void* const ptr = ::mmap(nullptr, kDataSize, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fShmFd, 0); + void* ptr; + + #ifdef MAP_LOCKED + ptr = mmap(nullptr, kDataSize, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fShmFd, 0); + if (ptr == nullptr || ptr == MAP_FAILED) + #endif + { + ptr = mmap(nullptr, kDataSize, PROT_READ|PROT_WRITE, MAP_SHARED, fShmFd, 0); + } + if (ptr == nullptr || ptr == MAP_FAILED) { Close(); @@ -173,6 +199,10 @@ class DesktopAudioDriver : public JackAudioDriver return -1; } + #ifndef MAP_LOCKED + mlock(ptr, kDataSize); + #endif + fShmData = static_cast(ptr); #endif @@ -183,6 +213,28 @@ class DesktopAudioDriver : public JackAudioDriver return -1; } + #ifdef __APPLE__ + task = mach_task_self(); + + mach_port_t bootport1; + if (task_get_bootstrap_port(task, &bootport1) != KERN_SUCCESS || + bootstrap_look_up(bootport1, fShmData->bootname1, &sem1) != KERN_SUCCESS) + { + Close(); + jack_error("Can't open default MOD Desktop driver 4"); + return -1; + } + + mach_port_t bootport2; + if (task_get_bootstrap_port(task, &bootport2) != KERN_SUCCESS || + bootstrap_look_up(bootport2, fShmData->bootname2, &sem2) != KERN_SUCCESS) + { + Close(); + jack_error("Can't open default MOD Desktop driver 5"); + return -1; + } + #endif + return 0; } @@ -191,8 +243,21 @@ class DesktopAudioDriver : public JackAudioDriver printf("%03d:%s\n", __LINE__, __FUNCTION__); JackAudioDriver::Close(); - #if defined(__APPLE__) - #elif defined(_WIN32) + #ifdef __APPLE__ + if (sem1 != MACH_PORT_NULL) + { + semaphore_destroy(task, sem1); + sem1 = MACH_PORT_NULL; + } + + if (sem2 != MACH_PORT_NULL) + { + semaphore_destroy(task, sem2); + sem2 = MACH_PORT_NULL; + } + #endif + + #ifdef _WIN32 #else if (fShmData != nullptr) { @@ -220,7 +285,7 @@ class DesktopAudioDriver : public JackAudioDriver if (JackAudioDriver::Start() != 0) return -1; - #ifdef __APPLE__ + #if 0 // def __APPLE__ fEngineControl->fPeriod = fEngineControl->fPeriodUsecs * 1000; fEngineControl->fComputation = JackTools::ComputationMicroSec(fEngineControl->fBufferSize) * 1000; fEngineControl->fConstraint = fEngineControl->fPeriodUsecs * 1000; diff --git a/src/plugin/DesktopPlugin.cpp b/src/plugin/DesktopPlugin.cpp index da437bf..2702da2 100644 --- a/src/plugin/DesktopPlugin.cpp +++ b/src/plugin/DesktopPlugin.cpp @@ -34,17 +34,38 @@ class DesktopPlugin : public Plugin const String sampleRateStr(static_cast(getSampleRate())); const char* const jackd_args[] = { - P "/jackd", "-R", "-S", "-n", "mod-desktop", "-C", P "/jack/jack-session.conf", "-d", "desktop", "-r", sampleRateStr.buffer(), nullptr + #if defined(DISTRHO_OS_MAC) + P "/MacOS/jackd", + #else + P "/jackd", + #endif + "-R", + "-S", + "-n", "mod-desktop", "-C", + #ifdef DISTRHO_OS_MAC + P "/MacOS/jack/jack-session.conf", + #else + P "/jack/jack-session.conf", + #endif + "-d", "desktop", "-r", sampleRateStr.buffer(), nullptr }; const char* const mod_ui_args[] = { - P "/mod-ui", nullptr + #if defined(DISTRHO_OS_MAC) + P "/MacOS/mod-ui", + #else + P "/mod-ui", + #endif + nullptr }; - if (shm.init() && jackd.start(jackd_args) && mod_ui.start(mod_ui_args)) + if (shm.init() && jackd.start(jackd_args)) { processing = true; shm.getAudioData(shmBuffers); bufferSizeChanged(getBufferSize()); + + d_msleep(500); + mod_ui.start(mod_ui_args); } } @@ -284,7 +305,20 @@ class DesktopPlugin : public Plugin const String sampleRateStr(static_cast(sampleRate)); const char* const jackd_args[] = { - P "/jackd", "-R", "-S", "-n", "mod-desktop", "-C", P "/jack/jack-session.conf", "-d", "desktop", "-r", sampleRateStr.buffer(), nullptr + #if defined(DISTRHO_OS_MAC) + P "/MacOS/jackd", + #else + P "/jackd", + #endif + "-R", + "-S", + "-n", "mod-desktop", "-C", + #ifdef DISTRHO_OS_MAC + P "/MacOS/jack/jack-session.conf", + #else + P "/jack/jack-session.conf", + #endif + "-d", "desktop", "-r", sampleRateStr.buffer(), nullptr }; jackd.stop(); diff --git a/src/plugin/Makefile b/src/plugin/Makefile index 7fcc8ee..f5a4233 100644 --- a/src/plugin/Makefile +++ b/src/plugin/Makefile @@ -21,6 +21,6 @@ FILES_UI = \ include ../DPF/Makefile.plugins.mk -all: jack +all: jack vst2 # --------------------------------------------------------------------------------------------------------------------- diff --git a/src/plugin/SharedMemory.hpp b/src/plugin/SharedMemory.hpp index 796d9e4..177db7e 100644 --- a/src/plugin/SharedMemory.hpp +++ b/src/plugin/SharedMemory.hpp @@ -63,11 +63,11 @@ class SharedMemory DISTRHO_SAFE_ASSERT_RETURN(ptr != nullptr, fail_deinit()); DISTRHO_SAFE_ASSERT_RETURN(ptr != MAP_FAILED, fail_deinit()); - data = static_cast(ptr); - #ifndef MAP_LOCKED - mlock(data, kDataSize); + mlock(ptr, kDataSize); #endif + + data = static_cast(ptr); } #endif