Device editors ask BespokeEditors for headers, whole bodies, drawings and card actions; DeviceDataSource gives them DSP data and hidden settings, backed by app slots (SlotDataSource) or AUs (AUDataSource, hidden values in SizzleDeviceAU). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
249 lines
10 KiB
Plaintext
249 lines
10 KiB
Plaintext
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#import "SizzleDeviceAU+Private.h"
|
|
#import "Components.gen.h"
|
|
#import "SizzleEffectAU.h"
|
|
#import "SizzleInstrumentAU.h"
|
|
|
|
#include <cstring>
|
|
#include <vector>
|
|
#include "sizzle/DeviceSpecs.gen.hpp"
|
|
|
|
using namespace sizzle;
|
|
|
|
static int choiceIndex(const ParamSpec &s, const char *name) {
|
|
for (int i = 0; s.choices[i]; ++i) if (std::strcmp(s.choices[i], name) == 0) return i;
|
|
return 0;
|
|
}
|
|
|
|
static NSString *const kStateKey = @"sizzleValues";
|
|
|
|
@implementation SizzleDeviceAU {
|
|
std::unique_ptr<DeviceKernel> _k;
|
|
AUParameterTree *_tree;
|
|
Params _base; // main thread: the full settings (incl. hidden keys)
|
|
NSArray<AUAudioUnitPreset *> *_factoryPresets;
|
|
std::vector<int> _factoryIndex; // AU preset number → gen::factoryPresets index
|
|
AUAudioUnitPreset *_currentPreset;
|
|
NSString *_displayName;
|
|
NSString *_toolboxType;
|
|
}
|
|
|
|
+ (Class)classForComponentType:(OSType)type {
|
|
return type == kAudioUnitType_MusicDevice ? SizzleInstrumentAU.class : SizzleEffectAU.class;
|
|
}
|
|
|
|
- (instancetype)initWithComponentDescription:(AudioComponentDescription)desc
|
|
options:(AudioComponentInstantiationOptions)options
|
|
error:(NSError **)outError {
|
|
const SizzleComponent *comp = nullptr;
|
|
for (int i = 0; i < kSizzleComponentCount; ++i)
|
|
if (kSizzleComponents[i].subtype == desc.componentSubType) comp = &kSizzleComponents[i];
|
|
const DeviceSpec *spec = comp ? findDevice(comp->toolboxType) : nullptr;
|
|
if (!spec) {
|
|
if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:kAudioUnitErr_InvalidElement userInfo:nil];
|
|
return nil;
|
|
}
|
|
self = [super initWithComponentDescription:desc options:options error:outError];
|
|
if (!self) return nil;
|
|
_displayName = @(comp->name);
|
|
_toolboxType = @(comp->toolboxType);
|
|
_k = [self makeKernel];
|
|
_k->spec = spec;
|
|
_k->type = comp->toolboxType;
|
|
[self buildParameterTree];
|
|
_base.fill(*spec);
|
|
[self pushBase];
|
|
[self buildFactoryPresets];
|
|
self.maximumFramesToRender = 4096;
|
|
return self;
|
|
}
|
|
|
|
- (std::unique_ptr<DeviceKernel>)makeKernel { return nullptr; } // subclasses
|
|
- (DeviceKernel *)kernel { return _k.get(); }
|
|
- (NSString *)displayName { return _displayName; }
|
|
- (NSString *)toolboxType { return _toolboxType; }
|
|
|
|
#pragma mark - parameters
|
|
|
|
- (void)buildParameterTree {
|
|
const DeviceSpec *spec = _k->spec;
|
|
NSMutableArray<AUParameter *> *params = [NSMutableArray array];
|
|
AudioUnitParameterOptions flags = kAudioUnitParameterFlag_IsReadable | kAudioUnitParameterFlag_IsWritable;
|
|
for (int i = 0; i < spec->paramCount; ++i) {
|
|
const ParamSpec &s = spec->params[i];
|
|
AUParameter *p;
|
|
if (s.isEnum()) {
|
|
NSMutableArray<NSString *> *names = [NSMutableArray array];
|
|
for (int c = 0; s.choices[c]; ++c) [names addObject:@(s.choices[c])];
|
|
p = [AUParameterTree createParameterWithIdentifier:@(s.key) name:@(s.label) address:(AUParameterAddress)i
|
|
min:0 max:(AUValue)(names.count - 1)
|
|
unit:kAudioUnitParameterUnit_Indexed unitName:nil
|
|
flags:flags valueStrings:names dependentParameters:nil];
|
|
p.value = (AUValue)s.def;
|
|
} else {
|
|
p = [AUParameterTree createParameterWithIdentifier:@(s.key) name:@(s.label) address:(AUParameterAddress)i
|
|
min:s.min max:s.max
|
|
unit:s.isInt ? kAudioUnitParameterUnit_Indexed : kAudioUnitParameterUnit_Generic
|
|
unitName:nil flags:flags valueStrings:nil dependentParameters:nil];
|
|
p.value = s.def;
|
|
}
|
|
_k->values[i].store(p.value);
|
|
[params addObject:p];
|
|
}
|
|
_tree = [AUParameterTree createTreeWithChildren:params];
|
|
|
|
DeviceKernel *k = _k.get();
|
|
_tree.implementorValueObserver = ^(AUParameter *param, AUValue value) {
|
|
if (param.address < (AUParameterAddress)k->spec->paramCount) {
|
|
k->values[param.address].store(value, std::memory_order_relaxed);
|
|
k->valuesSeq.fetch_add(1, std::memory_order_release);
|
|
}
|
|
};
|
|
_tree.implementorValueProvider = ^AUValue(AUParameter *param) {
|
|
return param.address < (AUParameterAddress)k->spec->paramCount ? k->values[param.address].load() : 0;
|
|
};
|
|
__weak SizzleDeviceAU *weakSelf = self;
|
|
_tree.implementorStringFromValueCallback = ^NSString *(AUParameter *param, const AUValue *value) {
|
|
return [weakSelf stringForParameter:param value:value ? *value : param.value] ?: @"";
|
|
};
|
|
}
|
|
|
|
- (AUParameterTree *)parameterTree { return _tree; }
|
|
|
|
/// Publish [_base] to the render thread and move the AU parameters to match it.
|
|
- (void)pushBase {
|
|
const DeviceSpec *spec = _k->spec;
|
|
for (int i = 0; i < spec->paramCount; ++i) {
|
|
const ParamSpec &s = spec->params[i];
|
|
AUValue v = s.isEnum() ? (AUValue)choiceIndex(s, _base.s(s.key, s.choices[0])) : _base.f(s.key, s.def);
|
|
[_tree parameterWithAddress:(AUParameterAddress)i].value = v; // notifies UI + kernel
|
|
}
|
|
_k->publish(_base);
|
|
}
|
|
|
|
/// Bring [_base] up to date with the AU parameters (they're the source of truth for
|
|
/// the visible keys; _base also holds hidden ones).
|
|
- (void)pullBase { _k->overlay(_base); }
|
|
|
|
- (NSString *)stringForParameter:(AUParameter *)param value:(AUValue)value {
|
|
if (param.address >= (AUParameterAddress)_k->spec->paramCount) return @"";
|
|
const ParamSpec &s = _k->spec->params[param.address];
|
|
if (s.isEnum()) {
|
|
int n = s.choiceCount(), i = (int)std::lround(value);
|
|
return @(s.choices[i < 0 ? 0 : (i >= n ? n - 1 : i)]);
|
|
}
|
|
const char *label = s.label;
|
|
if (strstr(label, "(dB)")) return [NSString stringWithFormat:@"%.1f dB", value];
|
|
if (strstr(label, "(ms)")) return [NSString stringWithFormat:@"%.0f ms", value];
|
|
if (s.isInt) return s.min < 0 ? [NSString stringWithFormat:@"%+d", (int)std::lround(value)]
|
|
: [NSString stringWithFormat:@"%d", (int)std::lround(value)];
|
|
if (s.min == 0 && s.max == 1) return [NSString stringWithFormat:@"%d%%", (int)std::lround(value * 100)];
|
|
if (s.min == -1 && s.max == 1) return [NSString stringWithFormat:@"%+d%%", (int)std::lround(value * 100)];
|
|
return [NSString stringWithFormat:@"%.2f", value];
|
|
}
|
|
|
|
- (AUValue)defaultValueForParameter:(AUParameter *)param {
|
|
if (param.address >= (AUParameterAddress)_k->spec->paramCount) return 0;
|
|
return _k->spec->params[param.address].def;
|
|
}
|
|
|
|
- (BOOL)isIntegerParameter:(AUParameter *)param {
|
|
if (param.address >= (AUParameterAddress)_k->spec->paramCount) return NO;
|
|
const ParamSpec &s = _k->spec->params[param.address];
|
|
return s.isInt || s.isEnum();
|
|
}
|
|
|
|
- (NSString *)specLabelForParameter:(AUParameter *)param {
|
|
if (param.address >= (AUParameterAddress)_k->spec->paramCount) return @"";
|
|
return @(_k->spec->params[param.address].label);
|
|
}
|
|
|
|
- (NSString *)hiddenValueForKey:(NSString *)key {
|
|
[self pullBase];
|
|
return _base.has(key.UTF8String) ? @(_base.s(key.UTF8String)) : nil;
|
|
}
|
|
|
|
- (void)setHiddenValue:(NSString *)value forKey:(NSString *)key {
|
|
[self pullBase];
|
|
_base.set(key.UTF8String, value.UTF8String);
|
|
_k->publish(_base);
|
|
}
|
|
|
|
#pragma mark - presets and state
|
|
|
|
- (void)buildFactoryPresets {
|
|
NSMutableArray *list = [NSMutableArray array];
|
|
for (int i = 0; i < gen::factoryPresetCount; ++i) {
|
|
if (_k->type != gen::factoryPresets[i].type) continue;
|
|
AUAudioUnitPreset *p = [AUAudioUnitPreset new];
|
|
p.number = (NSInteger)_factoryIndex.size();
|
|
p.name = @(gen::factoryPresets[i].name);
|
|
_factoryIndex.push_back(i);
|
|
[list addObject:p];
|
|
}
|
|
_factoryPresets = list;
|
|
}
|
|
|
|
- (NSArray<AUAudioUnitPreset *> *)factoryPresets { return _factoryPresets; }
|
|
- (BOOL)supportsUserPresets { return YES; }
|
|
- (AUAudioUnitPreset *)currentPreset { return _currentPreset; }
|
|
|
|
- (void)setCurrentPreset:(AUAudioUnitPreset *)preset {
|
|
if (preset == nil) { _currentPreset = nil; return; }
|
|
if (preset.number >= 0) {
|
|
if (preset.number < (NSInteger)_factoryIndex.size()) {
|
|
const char *const *kv = gen::factoryPresets[_factoryIndex[(size_t)preset.number]].values;
|
|
_base.fill(*_k->spec);
|
|
for (int i = 0; kv[i] && kv[i + 1]; i += 2) _base.set(kv[i], kv[i + 1]);
|
|
[self pushBase];
|
|
}
|
|
_currentPreset = preset;
|
|
} else {
|
|
NSError *err = nil;
|
|
NSDictionary *state = [self presetStateFor:preset error:&err];
|
|
if (state) self.fullState = state;
|
|
_currentPreset = preset;
|
|
}
|
|
}
|
|
|
|
- (NSDictionary<NSString *, id> *)fullState {
|
|
NSMutableDictionary *state = [NSMutableDictionary dictionaryWithDictionary:[super fullState] ?: @{}];
|
|
[self pullBase];
|
|
NSMutableDictionary *values = [NSMutableDictionary dictionary];
|
|
for (int i = 0; i < _base.size(); ++i) values[@(_base.keyAt(i))] = @(_base.valueAt(i));
|
|
state[kStateKey] = values;
|
|
state[@"sizzleType"] = _toolboxType;
|
|
return state;
|
|
}
|
|
|
|
- (void)setFullState:(NSDictionary<NSString *, id> *)fullState {
|
|
[super setFullState:fullState];
|
|
NSDictionary *values = fullState[kStateKey];
|
|
if (![values isKindOfClass:[NSDictionary class]]) return;
|
|
_base.fill(*_k->spec);
|
|
for (NSString *key in values) {
|
|
id v = values[key];
|
|
if ([key isKindOfClass:[NSString class]] && [v isKindOfClass:[NSString class]])
|
|
_base.set(key.UTF8String, ((NSString *)v).UTF8String);
|
|
}
|
|
[self pushBase];
|
|
}
|
|
|
|
#pragma mark - render resources
|
|
|
|
- (BOOL)allocateRenderResourcesAndReturnError:(NSError **)outError {
|
|
if (![super allocateRenderResourcesAndReturnError:outError]) return NO;
|
|
_k->musicalContext = self.musicalContextBlock;
|
|
_k->seenValues = _k->seenMailbox = 0; // apply settings on the first render
|
|
return YES;
|
|
}
|
|
|
|
- (void)deallocateRenderResources {
|
|
_k->musicalContext = nil;
|
|
[super deallocateRenderResources];
|
|
}
|
|
|
|
@end
|