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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <cstdint>
|
| 4 |
+
#include <cstring>
|
| 5 |
+
#include <atomic>
|
| 6 |
+
#include <string>
|
| 7 |
+
|
| 8 |
+
#ifdef __APPLE__
|
| 9 |
+
#include <sys/mman.h>
|
| 10 |
+
#include <sys/stat.h>
|
| 11 |
+
#include <fcntl.h>
|
| 12 |
+
#include <unistd.h>
|
| 13 |
+
#include <semaphore.h>
|
| 14 |
+
#include <dispatch/dispatch.h>
|
| 15 |
+
#endif
|
| 16 |
+
|
| 17 |
+
// Shared memory layout for audio transport between Plugin and Helper
|
| 18 |
+
// Zero-copy: both processes mmap the same region
|
| 19 |
+
|
| 20 |
+
struct SharedAudioHeader {
|
| 21 |
+
std::atomic<uint32_t> sampleRate{44100};
|
| 22 |
+
std::atomic<uint32_t> blockSize{512};
|
| 23 |
+
std::atomic<uint32_t> numChannels{2};
|
| 24 |
+
std::atomic<uint32_t> state{0}; // 0=idle, 1=input_ready, 2=output_ready
|
| 25 |
+
std::atomic<uint32_t> helperAlive{0}; // heartbeat from helper
|
| 26 |
+
uint32_t padding[3]; // align to 32 bytes
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
+
// State machine:
|
| 30 |
+
// Parent: writes input → sets state=1 → signals helper
|
| 31 |
+
// Helper: reads input → processBlock → writes output → sets state=2 → signals parent
|
| 32 |
+
// Parent: reads output → sets state=0
|
| 33 |
+
|
| 34 |
+
static constexpr uint32_t kStateIdle = 0;
|
| 35 |
+
static constexpr uint32_t kStateInputReady = 1;
|
| 36 |
+
static constexpr uint32_t kStateOutputReady = 2;
|
| 37 |
+
|
| 38 |
+
// Max supported: stereo, 4096 samples per block
|
| 39 |
+
static constexpr size_t kMaxChannels = 2;
|
| 40 |
+
static constexpr size_t kMaxBlockSize = 4096;
|
| 41 |
+
static constexpr size_t kAudioDataSize = kMaxChannels * kMaxBlockSize * sizeof(float);
|
| 42 |
+
static constexpr size_t kSharedMemSize = sizeof(SharedAudioHeader) + (kAudioDataSize * 2); // input + output
|
| 43 |
+
|
| 44 |
+
class SharedAudioBuffer {
|
| 45 |
+
public:
|
| 46 |
+
SharedAudioBuffer() = default;
|
| 47 |
+
~SharedAudioBuffer() { destroy(); }
|
| 48 |
+
|
| 49 |
+
// Create (parent) or open (helper) shared memory
|
| 50 |
+
bool create(const std::string& name)
|
| 51 |
+
{
|
| 52 |
+
shmName = "/" + name;
|
| 53 |
+
|
| 54 |
+
// Remove if exists from previous crash
|
| 55 |
+
shm_unlink(shmName.c_str());
|
| 56 |
+
|
| 57 |
+
fd = shm_open(shmName.c_str(), O_CREAT | O_RDWR, 0666);
|
| 58 |
+
if (fd < 0) return false;
|
| 59 |
+
|
| 60 |
+
if (ftruncate(fd, kSharedMemSize) != 0) {
|
| 61 |
+
close(fd);
|
| 62 |
+
return false;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
ptr = (uint8_t*)mmap(nullptr, kSharedMemSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
| 66 |
+
if (ptr == MAP_FAILED) {
|
| 67 |
+
close(fd);
|
| 68 |
+
return false;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// Zero the whole region
|
| 72 |
+
memset(ptr, 0, kSharedMemSize);
|
| 73 |
+
|
| 74 |
+
header = reinterpret_cast<SharedAudioHeader*>(ptr);
|
| 75 |
+
inputBuffer = reinterpret_cast<float*>(ptr + sizeof(SharedAudioHeader));
|
| 76 |
+
outputBuffer = reinterpret_cast<float*>(ptr + sizeof(SharedAudioHeader) + kAudioDataSize);
|
| 77 |
+
|
| 78 |
+
isOwner = true;
|
| 79 |
+
return true;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
bool open(const std::string& name)
|
| 83 |
+
{
|
| 84 |
+
shmName = "/" + name;
|
| 85 |
+
|
| 86 |
+
fd = shm_open(shmName.c_str(), O_RDWR, 0666);
|
| 87 |
+
if (fd < 0) return false;
|
| 88 |
+
|
| 89 |
+
ptr = (uint8_t*)mmap(nullptr, kSharedMemSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
| 90 |
+
if (ptr == MAP_FAILED) {
|
| 91 |
+
close(fd);
|
| 92 |
+
return false;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
header = reinterpret_cast<SharedAudioHeader*>(ptr);
|
| 96 |
+
inputBuffer = reinterpret_cast<float*>(ptr + sizeof(SharedAudioHeader));
|
| 97 |
+
outputBuffer = reinterpret_cast<float*>(ptr + sizeof(SharedAudioHeader) + kAudioDataSize);
|
| 98 |
+
|
| 99 |
+
isOwner = false;
|
| 100 |
+
return true;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
void destroy()
|
| 104 |
+
{
|
| 105 |
+
if (ptr && ptr != MAP_FAILED) {
|
| 106 |
+
munmap(ptr, kSharedMemSize);
|
| 107 |
+
ptr = nullptr;
|
| 108 |
+
}
|
| 109 |
+
if (fd >= 0) {
|
| 110 |
+
close(fd);
|
| 111 |
+
fd = -1;
|
| 112 |
+
}
|
| 113 |
+
if (isOwner && !shmName.empty()) {
|
| 114 |
+
shm_unlink(shmName.c_str());
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
// --- Audio data access ---
|
| 119 |
+
|
| 120 |
+
SharedAudioHeader* getHeader() { return header; }
|
| 121 |
+
|
| 122 |
+
void writeInput(const float* const* channelData, int numChannels, int numSamples)
|
| 123 |
+
{
|
| 124 |
+
int ch = std::min(numChannels, (int)kMaxChannels);
|
| 125 |
+
int ns = std::min(numSamples, (int)kMaxBlockSize);
|
| 126 |
+
for (int c = 0; c < ch; ++c)
|
| 127 |
+
memcpy(inputBuffer + c * kMaxBlockSize, channelData[c], ns * sizeof(float));
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
void readOutput(float* const* channelData, int numChannels, int numSamples)
|
| 131 |
+
{
|
| 132 |
+
int ch = std::min(numChannels, (int)kMaxChannels);
|
| 133 |
+
int ns = std::min(numSamples, (int)kMaxBlockSize);
|
| 134 |
+
for (int c = 0; c < ch; ++c)
|
| 135 |
+
memcpy(channelData[c], outputBuffer + c * kMaxBlockSize, ns * sizeof(float));
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
void readInput(float* const* channelData, int numChannels, int numSamples)
|
| 139 |
+
{
|
| 140 |
+
int ch = std::min(numChannels, (int)kMaxChannels);
|
| 141 |
+
int ns = std::min(numSamples, (int)kMaxBlockSize);
|
| 142 |
+
for (int c = 0; c < ch; ++c)
|
| 143 |
+
memcpy(channelData[c], inputBuffer + c * kMaxBlockSize, ns * sizeof(float));
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
void writeOutput(const float* const* channelData, int numChannels, int numSamples)
|
| 147 |
+
{
|
| 148 |
+
int ch = std::min(numChannels, (int)kMaxChannels);
|
| 149 |
+
int ns = std::min(numSamples, (int)kMaxBlockSize);
|
| 150 |
+
for (int c = 0; c < ch; ++c)
|
| 151 |
+
memcpy(outputBuffer + c * kMaxBlockSize, channelData[c], ns * sizeof(float));
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
bool isValid() const { return ptr != nullptr && ptr != MAP_FAILED; }
|
| 155 |
+
|
| 156 |
+
private:
|
| 157 |
+
uint8_t* ptr = nullptr;
|
| 158 |
+
int fd = -1;
|
| 159 |
+
bool isOwner = false;
|
| 160 |
+
std::string shmName;
|
| 161 |
+
|
| 162 |
+
SharedAudioHeader* header = nullptr;
|
| 163 |
+
float* inputBuffer = nullptr;
|
| 164 |
+
float* outputBuffer = nullptr;
|
| 165 |
+
};
|
| 166 |
+
|
| 167 |
+
// --- Semaphore wrapper (macOS uses dispatch semaphores since POSIX named sems are deprecated) ---
|
| 168 |
+
|
| 169 |
+
class SharedSemaphore {
|
| 170 |
+
public:
|
| 171 |
+
SharedSemaphore() = default;
|
| 172 |
+
~SharedSemaphore() { destroy(); }
|
| 173 |
+
|
| 174 |
+
bool create(const std::string& name)
|
| 175 |
+
{
|
| 176 |
+
semName = name;
|
| 177 |
+
// macOS: use named POSIX semaphore (still works despite deprecation warning)
|
| 178 |
+
sem = sem_open(("/" + name).c_str(), O_CREAT, 0666, 0);
|
| 179 |
+
return sem != SEM_FAILED;
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
bool open(const std::string& name)
|
| 183 |
+
{
|
| 184 |
+
semName = name;
|
| 185 |
+
sem = sem_open(("/" + name).c_str(), 0);
|
| 186 |
+
return sem != SEM_FAILED;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
void signal()
|
| 190 |
+
{
|
| 191 |
+
if (sem != SEM_FAILED)
|
| 192 |
+
sem_post(sem);
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
bool wait(int timeoutMs = 100)
|
| 196 |
+
{
|
| 197 |
+
if (sem == SEM_FAILED) return false;
|
| 198 |
+
|
| 199 |
+
// macOS doesn't support sem_timedwait, use polling
|
| 200 |
+
for (int i = 0; i < timeoutMs; ++i) {
|
| 201 |
+
if (sem_trywait(sem) == 0)
|
| 202 |
+
return true;
|
| 203 |
+
usleep(1000); // 1ms
|
| 204 |
+
}
|
| 205 |
+
return false;
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
bool tryWait()
|
| 209 |
+
{
|
| 210 |
+
if (sem == SEM_FAILED) return false;
|
| 211 |
+
return sem_trywait(sem) == 0;
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
void destroy()
|
| 215 |
+
{
|
| 216 |
+
if (sem != SEM_FAILED) {
|
| 217 |
+
sem_close(sem);
|
| 218 |
+
sem_unlink(("/" + semName).c_str());
|
| 219 |
+
sem = SEM_FAILED;
|
| 220 |
+
}
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
private:
|
| 224 |
+
sem_t* sem = SEM_FAILED;
|
| 225 |
+
std::string semName;
|
| 226 |
+
};
|