Files
sizzletracker-android/ios/app/FX/SizzleDeviceKernel.hpp
T
ReactorcoremeltdownandClaude Opus 5.5 7f231c8f79 iOS 0.2.0: instruments as AUv3, in-app host with keyboard, MIDI in and benchmark
Shared SizzleDeviceAU base for effects and instruments; sizzle::Instrument voice
pools; Android's factory presets generated into the specs header; the app hosts
every device in-process (no dependence on the system plugin registry).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 22:17:47 +01:00

107 lines
3.7 KiB
C++

// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
// The audio-thread side of a Sizzle AUv3 (SizzleDeviceAU and its subclasses).
// Objective-C++ only (holds an AU host block).
#import <AudioToolbox/AudioToolbox.h>
#include <atomic>
#include <cmath>
#include <string>
#include "sizzle/Params.hpp"
#include "sizzle/Specs.hpp"
namespace sizzle {
/**
* Everything the render thread touches, owned by the AU.
*
* Two ways settings reach the device, neither of them locking:
* - AU parameters (host automation, the editor) are written into [values] (one
* atomic float per parameter, address = index in the spec) and bump [valuesSeq];
* - whole settings (presets, restored state, hidden keys such as the Mix
* Tightener's) are written by the main thread into the spare half of [mailbox]
* and published with [mailboxIdx] / [mailboxSeq].
* The render thread copies the published mailbox into [live], overlays the
* parameter values, and calls apply() only when something changed or the tempo moved.
*/
struct DeviceKernel {
const DeviceSpec *spec = nullptr;
std::string type;
std::atomic<float> values[Params::kMax];
std::atomic<uint32_t> valuesSeq{1};
Params mailbox[2];
std::atomic<int> mailboxIdx{0};
std::atomic<uint32_t> mailboxSeq{1};
// render thread only
Params live;
uint32_t seenValues = 0, seenMailbox = 0;
double tempo = 120.0;
AUHostMusicalContextBlock musicalContext = nil;
virtual ~DeviceKernel() = default;
/** New settings for the device (render thread). */
virtual void apply(const Params &p, float tempoBpm) = 0;
/** Main thread: hand whole settings to the render thread. */
void publish(const Params &p) {
int next = 1 - mailboxIdx.load(std::memory_order_acquire);
mailbox[next] = p;
mailboxIdx.store(next, std::memory_order_release);
mailboxSeq.fetch_add(1, std::memory_order_release);
}
/** The parameter values as Params text: a choice name or a number. */
void overlay(Params &p) {
for (int i = 0; i < spec->paramCount; ++i) {
const ParamSpec &s = spec->params[i];
float v = values[i].load(std::memory_order_relaxed);
if (s.isEnum()) {
int n = s.choiceCount(), idx = (int)std::lround(v);
p.set(s.key, s.choices[idx < 0 ? 0 : (idx >= n ? n - 1 : idx)]);
} else {
p.set(s.key, s.isInt ? std::round(v) : v);
}
}
}
/** Render thread: a host parameter event (automation). */
void setValue(AUParameterAddress a, AUValue v) {
if (a < (AUParameterAddress)spec->paramCount) {
values[a].store(v, std::memory_order_relaxed);
valuesSeq.fetch_add(1, std::memory_order_release);
}
}
/** Render thread: pick up the host tempo; true if it changed. */
bool pollTempo() {
if (!musicalContext) return false;
double bpm = 0;
if (musicalContext(&bpm, nullptr, nullptr, nullptr, nullptr, nullptr) && bpm > 0 && bpm != tempo) {
tempo = bpm;
return true;
}
return false;
}
/** Render thread: apply new settings if anything changed. */
void refresh(bool force) {
uint32_t ms = mailboxSeq.load(std::memory_order_acquire);
uint32_t vs = valuesSeq.load(std::memory_order_acquire);
if (ms == seenMailbox && vs == seenValues && !force) return;
if (ms != seenMailbox) live = mailbox[mailboxIdx.load(std::memory_order_acquire)];
seenMailbox = ms;
seenValues = vs;
overlay(live);
apply(live, (float)tempo);
}
};
} // namespace sizzle