File size: 6,659 Bytes
a4a5519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#pragma once

#include <cstdint>
#include <cstring>
#include <atomic>
#include <string>

#ifdef __APPLE__
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <semaphore.h>
#include <dispatch/dispatch.h>
#endif

// Shared memory layout for audio transport between Plugin and Helper
// Zero-copy: both processes mmap the same region

struct SharedAudioHeader {
    std::atomic<uint32_t> sampleRate{44100};
    std::atomic<uint32_t> blockSize{512};
    std::atomic<uint32_t> numChannels{2};
    std::atomic<uint32_t> state{0};        // 0=idle, 1=input_ready, 2=output_ready
    std::atomic<uint32_t> helperAlive{0};  // heartbeat from helper
    uint32_t padding[3];                    // align to 32 bytes
};

// State machine:
// Parent: writes input → sets state=1 → signals helper
// Helper: reads input → processBlock → writes output → sets state=2 → signals parent
// Parent: reads output → sets state=0

static constexpr uint32_t kStateIdle = 0;
static constexpr uint32_t kStateInputReady = 1;
static constexpr uint32_t kStateOutputReady = 2;

// Max supported: stereo, 4096 samples per block
static constexpr size_t kMaxChannels = 2;
static constexpr size_t kMaxBlockSize = 4096;
static constexpr size_t kAudioDataSize = kMaxChannels * kMaxBlockSize * sizeof(float);
static constexpr size_t kSharedMemSize = sizeof(SharedAudioHeader) + (kAudioDataSize * 2); // input + output

class SharedAudioBuffer {
public:
    SharedAudioBuffer() = default;
    ~SharedAudioBuffer() { destroy(); }

    // Create (parent) or open (helper) shared memory
    bool create(const std::string& name)
    {
        shmName = "/" + name;

        // Remove if exists from previous crash
        shm_unlink(shmName.c_str());

        fd = shm_open(shmName.c_str(), O_CREAT | O_RDWR, 0666);
        if (fd < 0) return false;

        if (ftruncate(fd, kSharedMemSize) != 0) {
            close(fd);
            return false;
        }

        ptr = (uint8_t*)mmap(nullptr, kSharedMemSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        if (ptr == MAP_FAILED) {
            close(fd);
            return false;
        }

        // Zero the whole region
        memset(ptr, 0, kSharedMemSize);

        header = reinterpret_cast<SharedAudioHeader*>(ptr);
        inputBuffer = reinterpret_cast<float*>(ptr + sizeof(SharedAudioHeader));
        outputBuffer = reinterpret_cast<float*>(ptr + sizeof(SharedAudioHeader) + kAudioDataSize);

        isOwner = true;
        return true;
    }

    bool open(const std::string& name)
    {
        shmName = "/" + name;

        fd = shm_open(shmName.c_str(), O_RDWR, 0666);
        if (fd < 0) return false;

        ptr = (uint8_t*)mmap(nullptr, kSharedMemSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        if (ptr == MAP_FAILED) {
            close(fd);
            return false;
        }

        header = reinterpret_cast<SharedAudioHeader*>(ptr);
        inputBuffer = reinterpret_cast<float*>(ptr + sizeof(SharedAudioHeader));
        outputBuffer = reinterpret_cast<float*>(ptr + sizeof(SharedAudioHeader) + kAudioDataSize);

        isOwner = false;
        return true;
    }

    void destroy()
    {
        if (ptr && ptr != MAP_FAILED) {
            munmap(ptr, kSharedMemSize);
            ptr = nullptr;
        }
        if (fd >= 0) {
            close(fd);
            fd = -1;
        }
        if (isOwner && !shmName.empty()) {
            shm_unlink(shmName.c_str());
        }
    }

    // --- Audio data access ---

    SharedAudioHeader* getHeader() { return header; }

    void writeInput(const float* const* channelData, int numChannels, int numSamples)
    {
        int ch = std::min(numChannels, (int)kMaxChannels);
        int ns = std::min(numSamples, (int)kMaxBlockSize);
        for (int c = 0; c < ch; ++c)
            memcpy(inputBuffer + c * kMaxBlockSize, channelData[c], ns * sizeof(float));
    }

    void readOutput(float* const* channelData, int numChannels, int numSamples)
    {
        int ch = std::min(numChannels, (int)kMaxChannels);
        int ns = std::min(numSamples, (int)kMaxBlockSize);
        for (int c = 0; c < ch; ++c)
            memcpy(channelData[c], outputBuffer + c * kMaxBlockSize, ns * sizeof(float));
    }

    void readInput(float* const* channelData, int numChannels, int numSamples)
    {
        int ch = std::min(numChannels, (int)kMaxChannels);
        int ns = std::min(numSamples, (int)kMaxBlockSize);
        for (int c = 0; c < ch; ++c)
            memcpy(channelData[c], inputBuffer + c * kMaxBlockSize, ns * sizeof(float));
    }

    void writeOutput(const float* const* channelData, int numChannels, int numSamples)
    {
        int ch = std::min(numChannels, (int)kMaxChannels);
        int ns = std::min(numSamples, (int)kMaxBlockSize);
        for (int c = 0; c < ch; ++c)
            memcpy(outputBuffer + c * kMaxBlockSize, channelData[c], ns * sizeof(float));
    }

    bool isValid() const { return ptr != nullptr && ptr != MAP_FAILED; }

private:
    uint8_t* ptr = nullptr;
    int fd = -1;
    bool isOwner = false;
    std::string shmName;

    SharedAudioHeader* header = nullptr;
    float* inputBuffer = nullptr;
    float* outputBuffer = nullptr;
};

// --- Semaphore wrapper (macOS uses dispatch semaphores since POSIX named sems are deprecated) ---

class SharedSemaphore {
public:
    SharedSemaphore() = default;
    ~SharedSemaphore() { destroy(); }

    bool create(const std::string& name)
    {
        semName = name;
        // macOS: use named POSIX semaphore (still works despite deprecation warning)
        sem = sem_open(("/" + name).c_str(), O_CREAT, 0666, 0);
        return sem != SEM_FAILED;
    }

    bool open(const std::string& name)
    {
        semName = name;
        sem = sem_open(("/" + name).c_str(), 0);
        return sem != SEM_FAILED;
    }

    void signal()
    {
        if (sem != SEM_FAILED)
            sem_post(sem);
    }

    bool wait(int timeoutMs = 100)
    {
        if (sem == SEM_FAILED) return false;

        // macOS doesn't support sem_timedwait, use polling
        for (int i = 0; i < timeoutMs; ++i) {
            if (sem_trywait(sem) == 0)
                return true;
            usleep(1000); // 1ms
        }
        return false;
    }

    bool tryWait()
    {
        if (sem == SEM_FAILED) return false;
        return sem_trywait(sem) == 0;
    }

    void destroy()
    {
        if (sem != SEM_FAILED) {
            sem_close(sem);
            sem_unlink(("/" + semName).c_str());
            sem = SEM_FAILED;
        }
    }

private:
    sem_t* sem = SEM_FAILED;
    std::string semName;
};