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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a5b2f4c873
commit
7f231c8f79
+16
-10
@@ -2,16 +2,22 @@
|
||||
#pragma once
|
||||
#include <AudioToolbox/AudioToolbox.h>
|
||||
|
||||
typedef struct { OSType subtype; const char *toolboxType; const char *name; } SizzleComponent;
|
||||
typedef struct { OSType subtype; OSType type; const char *toolboxType; const char *name; } SizzleComponent;
|
||||
|
||||
static const SizzleComponent kSizzleComponents[] = {
|
||||
{'sBcr', "BITCRUSHER", "Bitcrusher"},
|
||||
{'sFlt', "FILTER", "Filter"},
|
||||
{'sEqx', "EQ10", "10-Band EQ"},
|
||||
{'sDly', "DELAY", "Tape Delay"},
|
||||
{'sMxt', "MIX_TIGHTENER", "Mix Tightener"},
|
||||
{'sTpe', "TAPE_ENGINE", "Tape Engine"},
|
||||
{'sGrn', "GRANULAR", "Granular Glitch"},
|
||||
{'sAmb', "AMBIENCE", "Ambience Reverb"}
|
||||
{'sBcr', 'aufx', "BITCRUSHER", "Bitcrusher"},
|
||||
{'sFlt', 'aufx', "FILTER", "Filter"},
|
||||
{'sEqx', 'aufx', "EQ10", "10-Band EQ"},
|
||||
{'sDly', 'aufx', "DELAY", "Tape Delay"},
|
||||
{'sMxt', 'aufx', "MIX_TIGHTENER", "Mix Tightener"},
|
||||
{'sTpe', 'aufx', "TAPE_ENGINE", "Tape Engine"},
|
||||
{'sGrn', 'aufx', "GRANULAR", "Granular Glitch"},
|
||||
{'sAmb', 'aufx', "AMBIENCE", "Ambience Reverb"},
|
||||
{'sSyn', 'aumu', "NES_SYNTH", "Synth"},
|
||||
{'sMph', 'aumu', "MORPH_PAD", "Morph Pad"},
|
||||
{'sWav', 'aumu', "WAVETABLE", "Wavetable"},
|
||||
{'sGrc', 'aumu', "GRAIN_CLOUD", "Grain Cloud"},
|
||||
{'sSmp', 'aumu', "SAMPLER", "Sampler"},
|
||||
{'sSf2', 'aumu', "SOUNDFONT", "SF2-XI Loader"}
|
||||
};
|
||||
static const int kSizzleComponentCount = 8;
|
||||
static const int kSizzleComponentCount = 14;
|
||||
|
||||
@@ -106,6 +106,72 @@
|
||||
<key>type</key><string>aufx</string>
|
||||
<key>version</key><integer>65536</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>description</key><string>Sizzle Synth</string>
|
||||
<key>factoryFunction</key><string>SizzleFXViewController</string>
|
||||
<key>manufacturer</key><string>Tsys</string>
|
||||
<key>name</key><string>Tired Sysadmin: Sizzle Synth</string>
|
||||
<key>sandboxSafe</key><true/>
|
||||
<key>subtype</key><string>sSyn</string>
|
||||
<key>tags</key><array><string>Synthesizer</string></array>
|
||||
<key>type</key><string>aumu</string>
|
||||
<key>version</key><integer>65536</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>description</key><string>Sizzle Morph Pad</string>
|
||||
<key>factoryFunction</key><string>SizzleFXViewController</string>
|
||||
<key>manufacturer</key><string>Tsys</string>
|
||||
<key>name</key><string>Tired Sysadmin: Sizzle Morph Pad</string>
|
||||
<key>sandboxSafe</key><true/>
|
||||
<key>subtype</key><string>sMph</string>
|
||||
<key>tags</key><array><string>Synthesizer</string></array>
|
||||
<key>type</key><string>aumu</string>
|
||||
<key>version</key><integer>65536</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>description</key><string>Sizzle Wavetable</string>
|
||||
<key>factoryFunction</key><string>SizzleFXViewController</string>
|
||||
<key>manufacturer</key><string>Tsys</string>
|
||||
<key>name</key><string>Tired Sysadmin: Sizzle Wavetable</string>
|
||||
<key>sandboxSafe</key><true/>
|
||||
<key>subtype</key><string>sWav</string>
|
||||
<key>tags</key><array><string>Synthesizer</string></array>
|
||||
<key>type</key><string>aumu</string>
|
||||
<key>version</key><integer>65536</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>description</key><string>Sizzle Grain Cloud</string>
|
||||
<key>factoryFunction</key><string>SizzleFXViewController</string>
|
||||
<key>manufacturer</key><string>Tsys</string>
|
||||
<key>name</key><string>Tired Sysadmin: Sizzle Grain Cloud</string>
|
||||
<key>sandboxSafe</key><true/>
|
||||
<key>subtype</key><string>sGrc</string>
|
||||
<key>tags</key><array><string>Synthesizer</string></array>
|
||||
<key>type</key><string>aumu</string>
|
||||
<key>version</key><integer>65536</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>description</key><string>Sizzle Sampler</string>
|
||||
<key>factoryFunction</key><string>SizzleFXViewController</string>
|
||||
<key>manufacturer</key><string>Tsys</string>
|
||||
<key>name</key><string>Tired Sysadmin: Sizzle Sampler</string>
|
||||
<key>sandboxSafe</key><true/>
|
||||
<key>subtype</key><string>sSmp</string>
|
||||
<key>tags</key><array><string>Synthesizer</string></array>
|
||||
<key>type</key><string>aumu</string>
|
||||
<key>version</key><integer>65536</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>description</key><string>Sizzle SF2-XI Loader</string>
|
||||
<key>factoryFunction</key><string>SizzleFXViewController</string>
|
||||
<key>manufacturer</key><string>Tsys</string>
|
||||
<key>name</key><string>Tired Sysadmin: Sizzle SF2-XI Loader</string>
|
||||
<key>sandboxSafe</key><true/>
|
||||
<key>subtype</key><string>sSf2</string>
|
||||
<key>tags</key><array><string>Synthesizer</string></array>
|
||||
<key>type</key><string>aumu</string>
|
||||
<key>version</key><integer>65536</integer>
|
||||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
<key>NSExtensionPointIdentifier</key><string>com.apple.AudioUnit-UI</string>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// For subclasses of SizzleDeviceAU only (Objective-C++).
|
||||
|
||||
#import "SizzleDeviceAU.h"
|
||||
|
||||
#include <memory>
|
||||
#include "SizzleDeviceKernel.hpp"
|
||||
|
||||
@interface SizzleDeviceAU ()
|
||||
|
||||
/// Subclasses: build the render-side kernel (called once from init).
|
||||
- (std::unique_ptr<sizzle::DeviceKernel>)makeKernel;
|
||||
/// The kernel (owned by the base).
|
||||
@property (nonatomic, readonly) sizzle::DeviceKernel *kernel;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,36 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#import <AudioToolbox/AudioToolbox.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/// What every Sizzle AUv3 shares: the device (a ToolboxType, e.g. "DELAY" or
|
||||
/// "WAVETABLE") and its parameters from the generated specs, the hand-over of
|
||||
/// settings to the render thread, factory presets (Android's PresetLibrary set),
|
||||
/// user presets and saved state. Subclasses add busses and the render block:
|
||||
/// SizzleEffectAU (aufx) and SizzleInstrumentAU (aumu). Pick the right one with
|
||||
/// +classForComponentType:.
|
||||
@interface SizzleDeviceAU : AUAudioUnit
|
||||
|
||||
/// Standard initializer: the device comes from the component's subtype
|
||||
/// (components.txt). Used by AU registration (in-process and in the extension).
|
||||
- (nullable instancetype)initWithComponentDescription:(AudioComponentDescription)desc
|
||||
options:(AudioComponentInstantiationOptions)options
|
||||
error:(NSError **)outError;
|
||||
|
||||
/// The AU class for a component type: kAudioUnitType_MusicDevice → instruments,
|
||||
/// anything else → effects.
|
||||
+ (Class)classForComponentType:(OSType)type;
|
||||
|
||||
/// The device's name as shown in Sizzle ("Tape Delay").
|
||||
@property (nonatomic, readonly) NSString *displayName;
|
||||
/// Its ToolboxType name ("DELAY").
|
||||
@property (nonatomic, readonly) NSString *toolboxType;
|
||||
|
||||
/// Formats a parameter value the way Sizzle's editor does (units, %, choices).
|
||||
- (NSString *)stringForParameter:(AUParameter *)param value:(AUValue)value;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,221 @@
|
||||
// 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];
|
||||
}
|
||||
|
||||
#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
|
||||
@@ -0,0 +1,106 @@
|
||||
// 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
|
||||
@@ -1,26 +1,13 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#import <AudioToolbox/AudioToolbox.h>
|
||||
#import "SizzleDeviceAU.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/// One Sizzle audio effect as an AUv3 (`aufx`): wraps a C++ `sizzle::Effect`
|
||||
/// chosen by its ToolboxType name ("DELAY", "FILTER", …). Parameters come from the
|
||||
/// generated device specs; each channel runs its own mono effect instance.
|
||||
@interface SizzleEffectAU : AUAudioUnit
|
||||
|
||||
- (nullable instancetype)initWithComponentDescription:(AudioComponentDescription)desc
|
||||
toolboxType:(const char *)toolboxType
|
||||
displayName:(NSString *)displayName
|
||||
error:(NSError **)outError;
|
||||
|
||||
/// The device's name as shown in Sizzle ("Tape Delay").
|
||||
@property (nonatomic, readonly) NSString *displayName;
|
||||
|
||||
/// Formats a parameter value the way Sizzle's editor does (units, %, choices).
|
||||
- (NSString *)stringForParameter:(AUParameter *)param value:(AUValue)value;
|
||||
|
||||
/// One Sizzle audio effect as an AUv3 (`aufx`): a C++ `sizzle::Effect` per channel
|
||||
/// (the effects are mono, as in Sizzle's mixer: stereo runs dual mono).
|
||||
@interface SizzleEffectAU : SizzleDeviceAU
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
+19
-285
@@ -2,137 +2,45 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#import "SizzleEffectAU.h"
|
||||
#import "Components.gen.h"
|
||||
#import "SizzleDeviceAU+Private.h"
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "sizzle/DeviceSpecs.gen.hpp"
|
||||
#include "sizzle/Effect.hpp"
|
||||
|
||||
using namespace sizzle;
|
||||
|
||||
namespace {
|
||||
|
||||
/**
|
||||
* Everything the render thread touches, owned by the AU.
|
||||
*
|
||||
* Two ways settings reach the effect, neither of them locking:
|
||||
* - AU parameters (host automation, the UI) 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 Effect::update only when something changed.
|
||||
*/
|
||||
struct Kernel {
|
||||
const DeviceSpec *spec = nullptr;
|
||||
std::string type;
|
||||
struct EffectKernel final : DeviceKernel {
|
||||
std::unique_ptr<Effect> fx[2];
|
||||
int channels = 0;
|
||||
|
||||
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;
|
||||
std::atomic<bool> bypass{false};
|
||||
std::vector<float> inBuf[2];
|
||||
AUHostMusicalContextBlock musicalContext = nil;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/** Parameter i's value 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Called at the top of each render cycle. */
|
||||
void refresh(bool tempoChanged) {
|
||||
uint32_t ms = mailboxSeq.load(std::memory_order_acquire);
|
||||
uint32_t vs = valuesSeq.load(std::memory_order_acquire);
|
||||
if (ms == seenMailbox && vs == seenValues && !tempoChanged) return;
|
||||
if (ms != seenMailbox) live = mailbox[mailboxIdx.load(std::memory_order_acquire)];
|
||||
seenMailbox = ms;
|
||||
seenValues = vs;
|
||||
overlay(live);
|
||||
for (int c = 0; c < channels; ++c) fx[c]->update(live, (float)tempo);
|
||||
void apply(const Params &p, float tempoBpm) override {
|
||||
for (int c = 0; c < channels; ++c) fx[c]->update(p, tempoBpm);
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@implementation SizzleEffectAU {
|
||||
std::unique_ptr<Kernel> _k;
|
||||
AUAudioUnitBusArray *_inputs;
|
||||
AUAudioUnitBusArray *_outputs;
|
||||
AUAudioUnitBus *_inBus;
|
||||
AUAudioUnitBus *_outBus;
|
||||
AUParameterTree *_tree;
|
||||
Params _base; // main thread: the full settings (incl. hidden keys)
|
||||
NSArray<AUAudioUnitPreset *> *_factoryPresets;
|
||||
AUAudioUnitPreset *_currentPreset;
|
||||
NSString *_displayName;
|
||||
}
|
||||
|
||||
/// The standard initializer (used by AUAudioUnit registration and the Mac test):
|
||||
/// picks the device from the component's subtype in components.txt.
|
||||
- (std::unique_ptr<DeviceKernel>)makeKernel { return std::make_unique<EffectKernel>(); }
|
||||
- (EffectKernel *)fxKernel { return static_cast<EffectKernel *>(self.kernel); }
|
||||
|
||||
- (instancetype)initWithComponentDescription:(AudioComponentDescription)desc
|
||||
options:(AudioComponentInstantiationOptions)options
|
||||
error:(NSError **)outError {
|
||||
for (int i = 0; i < kSizzleComponentCount; ++i)
|
||||
if (kSizzleComponents[i].subtype == desc.componentSubType)
|
||||
return [self initWithComponentDescription:desc toolboxType:kSizzleComponents[i].toolboxType
|
||||
displayName:@(kSizzleComponents[i].name) error:outError];
|
||||
if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:kAudioUnitErr_InvalidElement userInfo:nil];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (instancetype)initWithComponentDescription:(AudioComponentDescription)desc
|
||||
toolboxType:(const char *)toolboxType
|
||||
displayName:(NSString *)displayName
|
||||
error:(NSError **)outError {
|
||||
self = [super initWithComponentDescription:desc options:0 error:outError];
|
||||
self = [super initWithComponentDescription:desc options:options error:outError];
|
||||
if (!self) return nil;
|
||||
_displayName = displayName;
|
||||
_k = std::make_unique<Kernel>();
|
||||
_k->type = toolboxType;
|
||||
_k->spec = findDevice(toolboxType);
|
||||
if (!_k->spec) {
|
||||
if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:kAudioUnitErr_InvalidProperty userInfo:nil];
|
||||
return nil;
|
||||
}
|
||||
|
||||
AVAudioFormat *fmt = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100 channels:2];
|
||||
_inBus = [[AUAudioUnitBus alloc] initWithFormat:fmt error:outError];
|
||||
_outBus = [[AUAudioUnitBus alloc] initWithFormat:fmt error:outError];
|
||||
@@ -141,162 +49,9 @@ int choiceIndex(const ParamSpec &s, const char *name) {
|
||||
_outBus.maximumChannelCount = 2;
|
||||
_inputs = [[AUAudioUnitBusArray alloc] initWithAudioUnit:self busType:AUAudioUnitBusTypeInput busses:@[_inBus]];
|
||||
_outputs = [[AUAudioUnitBusArray alloc] initWithAudioUnit:self busType:AUAudioUnitBusTypeOutput busses:@[_outBus]];
|
||||
|
||||
[self buildParameterTree];
|
||||
_base.fill(*_k->spec);
|
||||
[self pushBase];
|
||||
[self buildFactoryPresets];
|
||||
self.maximumFramesToRender = 4096;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)displayName { return _displayName; }
|
||||
|
||||
#pragma mark - parameters
|
||||
|
||||
- (void)buildParameterTree {
|
||||
const DeviceSpec *spec = _k->spec;
|
||||
NSMutableArray<AUParameter *> *params = [NSMutableArray array];
|
||||
for (int i = 0; i < spec->paramCount; ++i) {
|
||||
const ParamSpec &s = spec->params[i];
|
||||
AUParameter *p;
|
||||
AudioUnitParameterOptions flags = kAudioUnitParameterFlag_IsReadable | kAudioUnitParameterFlag_IsWritable;
|
||||
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];
|
||||
|
||||
Kernel *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 SizzleEffectAU *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];
|
||||
}
|
||||
|
||||
#pragma mark - presets and state
|
||||
|
||||
- (void)buildFactoryPresets {
|
||||
NSMutableArray *list = [NSMutableArray array];
|
||||
if (_k->type == "MIX_TIGHTENER") {
|
||||
for (int i = 0; gen::mixTightenerPresets[i]; ++i) {
|
||||
AUAudioUnitPreset *p = [AUAudioUnitPreset new];
|
||||
p.number = i;
|
||||
p.name = @(gen::mixTightenerPresets[i][0]);
|
||||
[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)_factoryPresets.count && _k->type == "MIX_TIGHTENER") {
|
||||
const char *const *kv = gen::mixTightenerPresets[preset.number];
|
||||
_base.fill(*_k->spec);
|
||||
for (int i = 1; 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;
|
||||
}
|
||||
}
|
||||
|
||||
static NSString *const kStateKey = @"sizzleValues";
|
||||
|
||||
- (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"] = @(_k->type.c_str());
|
||||
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 - busses and render
|
||||
|
||||
- (AUAudioUnitBusArray *)inputBusses { return _inputs; }
|
||||
- (AUAudioUnitBusArray *)outputBusses { return _outputs; }
|
||||
- (NSArray<NSNumber *> *)channelCapabilities { return @[@1, @1, @2, @2]; }
|
||||
@@ -304,19 +59,18 @@ static NSString *const kStateKey = @"sizzleValues";
|
||||
|
||||
- (void)setShouldBypassEffect:(BOOL)bypass {
|
||||
[super setShouldBypassEffect:bypass];
|
||||
_k->bypass.store(bypass);
|
||||
self.fxKernel->bypass.store(bypass);
|
||||
}
|
||||
|
||||
- (BOOL)allocateRenderResourcesAndReturnError:(NSError **)outError {
|
||||
if (![super allocateRenderResourcesAndReturnError:outError]) return NO;
|
||||
AVAudioChannelCount chIn = _inBus.format.channelCount, chOut = _outBus.format.channelCount;
|
||||
if (chIn != chOut || chOut < 1 || chOut > 2) {
|
||||
if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:kAudioUnitErr_FailedInitialization userInfo:nil];
|
||||
self.renderResourcesAllocated = NO;
|
||||
return NO;
|
||||
}
|
||||
if (![super allocateRenderResourcesAndReturnError:outError]) return NO;
|
||||
EffectKernel *k = self.fxKernel;
|
||||
int sr = (int)std::lround(_outBus.format.sampleRate);
|
||||
Kernel *k = _k.get();
|
||||
k->channels = (int)chOut;
|
||||
for (int c = 0; c < 2; ++c) {
|
||||
k->fx[c] = c < k->channels ? Effect::create(k->type.c_str(), sr) : nullptr;
|
||||
@@ -324,22 +78,19 @@ static NSString *const kStateKey = @"sizzleValues";
|
||||
}
|
||||
if (!k->fx[0]) {
|
||||
if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:kAudioUnitErr_FailedInitialization userInfo:nil];
|
||||
self.renderResourcesAllocated = NO;
|
||||
[super deallocateRenderResources];
|
||||
return NO;
|
||||
}
|
||||
k->musicalContext = self.musicalContextBlock;
|
||||
k->seenValues = k->seenMailbox = 0; // force update() on the first render
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)deallocateRenderResources {
|
||||
for (auto &f : _k->fx) f.reset();
|
||||
_k->musicalContext = nil;
|
||||
for (auto &f : self.fxKernel->fx) f.reset();
|
||||
[super deallocateRenderResources];
|
||||
}
|
||||
|
||||
- (AUInternalRenderBlock)internalRenderBlock {
|
||||
Kernel *k = _k.get();
|
||||
EffectKernel *k = self.fxKernel;
|
||||
return ^AUAudioUnitStatus(AudioUnitRenderActionFlags *actionFlags, const AudioTimeStamp *timestamp,
|
||||
AVAudioFrameCount frameCount, NSInteger outputBusNumber,
|
||||
AudioBufferList *outputData, const AURenderEvent *events,
|
||||
@@ -360,28 +111,11 @@ static NSString *const kStateKey = @"sizzleValues";
|
||||
AUAudioUnitStatus err = pullInputBlock(&pullFlags, timestamp, frameCount, 0, &in.list);
|
||||
if (err != noErr) return err;
|
||||
|
||||
// Host automation arrives as render events: apply at block start.
|
||||
bool changed = false;
|
||||
for (const AURenderEvent *e = events; e; e = e->head.next) {
|
||||
if (e->head.eventType == AURenderEventParameter || e->head.eventType == AURenderEventParameterRamp) {
|
||||
AUParameterAddress a = e->parameter.parameterAddress;
|
||||
if (a < (AUParameterAddress)k->spec->paramCount) {
|
||||
k->values[a].store(e->parameter.value, std::memory_order_relaxed);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changed) k->valuesSeq.fetch_add(1, std::memory_order_release);
|
||||
|
||||
bool tempoChanged = false;
|
||||
if (k->musicalContext) {
|
||||
double bpm = 0;
|
||||
if (k->musicalContext(&bpm, nullptr, nullptr, nullptr, nullptr, nullptr) && bpm > 0 && bpm != k->tempo) {
|
||||
k->tempo = bpm;
|
||||
tempoChanged = true;
|
||||
}
|
||||
}
|
||||
k->refresh(tempoChanged);
|
||||
// Host automation: applied at block start.
|
||||
for (const AURenderEvent *e = events; e; e = e->head.next)
|
||||
if (e->head.eventType == AURenderEventParameter || e->head.eventType == AURenderEventParameterRamp)
|
||||
k->setValue(e->parameter.parameterAddress, e->parameter.value);
|
||||
k->refresh(k->pollTempo());
|
||||
|
||||
bool bypass = k->bypass.load(std::memory_order_relaxed);
|
||||
for (int c = 0; c < k->channels && c < (int)outputData->mNumberBuffers; ++c) {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#import <CoreAudioKit/CoreAudioKit.h>
|
||||
#import "SizzleDeviceAU.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/// The extension's principal class (the AU factory) and the generic device editor.
|
||||
/// The app uses the same editor for the devices it hosts itself.
|
||||
@interface SizzleFXViewController : AUViewController <AUAudioUnitFactory>
|
||||
/// Show the editor for an AU created elsewhere (the in-app host). Call once.
|
||||
- (void)attachAudioUnit:(SizzleDeviceAU *)au;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -1,8 +1,9 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// The FX extension's principal class: the factory for every effect listed in
|
||||
// components.txt (picked by subtype) and a generic editor for its parameters
|
||||
// The extension's principal class: the factory for every device listed in
|
||||
// components.txt (picked by subtype; instruments and effects) and a generic editor
|
||||
// for its parameters
|
||||
// (sliders, segmented switches for short choice lists, menus for long ones).
|
||||
// The card-style editors of the Android app come later; this keeps every
|
||||
// parameter reachable meanwhile.
|
||||
@@ -10,16 +11,13 @@
|
||||
#import <CoreAudioKit/CoreAudioKit.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "Components.gen.h"
|
||||
#import "SizzleEffectAU.h"
|
||||
|
||||
@interface SizzleFXViewController : AUViewController <AUAudioUnitFactory>
|
||||
@end
|
||||
#import "SizzleFXViewController.h"
|
||||
|
||||
static UIColor *Amber(void) { return [UIColor colorWithRed:0.96 green:0.76 blue:0.29 alpha:1]; }
|
||||
static UIColor *Dim(void) { return [UIColor colorWithWhite:0.62 alpha:1]; }
|
||||
|
||||
@implementation SizzleFXViewController {
|
||||
SizzleEffectAU *_au;
|
||||
SizzleDeviceAU *_au;
|
||||
UIStackView *_stack;
|
||||
NSMutableDictionary<NSNumber *, UIControl *> *_controls;
|
||||
NSMutableDictionary<NSNumber *, UILabel *> *_valueLabels;
|
||||
@@ -30,18 +28,15 @@ static UIColor *Dim(void) { return [UIColor colorWithWhite:0.62 alpha:1]; }
|
||||
#pragma mark - factory
|
||||
|
||||
- (AUAudioUnit *)createAudioUnitWithComponentDescription:(AudioComponentDescription)desc error:(NSError **)error {
|
||||
for (int i = 0; i < kSizzleComponentCount; ++i) {
|
||||
if (kSizzleComponents[i].subtype == desc.componentSubType) {
|
||||
_au = [[SizzleEffectAU alloc] initWithComponentDescription:desc
|
||||
toolboxType:kSizzleComponents[i].toolboxType
|
||||
displayName:@(kSizzleComponents[i].name)
|
||||
error:error];
|
||||
dispatch_async(dispatch_get_main_queue(), ^{ [self buildIfReady]; });
|
||||
return _au;
|
||||
}
|
||||
}
|
||||
if (error) *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:kAudioUnitErr_InvalidElement userInfo:nil];
|
||||
return nil;
|
||||
_au = [[[SizzleDeviceAU classForComponentType:desc.componentType] alloc]
|
||||
initWithComponentDescription:desc options:0 error:error];
|
||||
if (_au) dispatch_async(dispatch_get_main_queue(), ^{ [self buildIfReady]; });
|
||||
return _au;
|
||||
}
|
||||
|
||||
- (void)attachAudioUnit:(SizzleDeviceAU *)au {
|
||||
_au = au;
|
||||
[self buildIfReady];
|
||||
}
|
||||
|
||||
#pragma mark - view
|
||||
@@ -96,7 +91,7 @@ static UIColor *Dim(void) { return [UIColor colorWithWhite:0.62 alpha:1]; }
|
||||
presets.tintColor = Amber();
|
||||
presets.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
|
||||
NSMutableArray<UIAction *> *items = [NSMutableArray array];
|
||||
__weak SizzleEffectAU *au = _au;
|
||||
__weak SizzleDeviceAU *au = _au;
|
||||
__weak UIButton *weakButton = presets;
|
||||
for (AUAudioUnitPreset *p in _au.factoryPresets) {
|
||||
[items addObject:[UIAction actionWithTitle:p.name image:nil identifier:nil handler:^(UIAction *a) {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#import "SizzleDeviceAU.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/// One Sizzle instrument as an AUv3 (`aumu`): a C++ `sizzle::Instrument` (8 voices,
|
||||
/// as one Sizzle mixer channel) played by MIDI, sample-accurately. The instruments
|
||||
/// are mono; the output carries the same signal on both channels.
|
||||
@interface SizzleInstrumentAU : SizzleDeviceAU
|
||||
|
||||
/// Where the Sampler, SF2/XI loader and Grain Cloud find samples (a
|
||||
/// `sizzle::sampler::SampleStore *`; it must outlive the AU). Set before
|
||||
/// allocateRenderResources. Without one those instruments stay silent.
|
||||
@property (nonatomic, nullable) void *sampleStore;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,139 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#import "SizzleInstrumentAU.h"
|
||||
#import "SizzleDeviceAU+Private.h"
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include "sizzle/Instrument.hpp"
|
||||
|
||||
using namespace sizzle;
|
||||
|
||||
namespace {
|
||||
|
||||
struct InstrumentKernel final : DeviceKernel {
|
||||
std::unique_ptr<Instrument> inst;
|
||||
std::vector<float> outBuf; // for hosts that pass null output buffers
|
||||
|
||||
void apply(const Params &p, float tempoBpm) override { if (inst) inst->update(p, tempoBpm); }
|
||||
|
||||
/** One MIDI message (any channel). */
|
||||
void midi(const uint8_t *d, int len) {
|
||||
if (!inst || len < 1) return;
|
||||
uint8_t status = d[0] & 0xF0;
|
||||
if (status == 0x90 && len >= 3 && d[2] > 0) inst->noteOn(d[1], d[2]);
|
||||
else if ((status == 0x80 || status == 0x90) && len >= 3) inst->noteOff(d[1]);
|
||||
else if (status == 0xB0 && len >= 3) {
|
||||
if (d[1] == 123) inst->allNotesOff(); // all notes off
|
||||
else if (d[1] == 120) inst->kill(); // all sound off
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
@implementation SizzleInstrumentAU {
|
||||
AUAudioUnitBusArray *_outputs;
|
||||
AUAudioUnitBus *_outBus;
|
||||
}
|
||||
|
||||
- (std::unique_ptr<DeviceKernel>)makeKernel { return std::make_unique<InstrumentKernel>(); }
|
||||
- (InstrumentKernel *)instKernel { return static_cast<InstrumentKernel *>(self.kernel); }
|
||||
|
||||
- (instancetype)initWithComponentDescription:(AudioComponentDescription)desc
|
||||
options:(AudioComponentInstantiationOptions)options
|
||||
error:(NSError **)outError {
|
||||
self = [super initWithComponentDescription:desc options:options error:outError];
|
||||
if (!self) return nil;
|
||||
AVAudioFormat *fmt = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100 channels:2];
|
||||
_outBus = [[AUAudioUnitBus alloc] initWithFormat:fmt error:outError];
|
||||
if (!_outBus) return nil;
|
||||
_outBus.maximumChannelCount = 2;
|
||||
_outputs = [[AUAudioUnitBusArray alloc] initWithAudioUnit:self busType:AUAudioUnitBusTypeOutput busses:@[_outBus]];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (AUAudioUnitBusArray *)outputBusses { return _outputs; }
|
||||
- (NSArray<NSNumber *> *)channelCapabilities { return @[@0, @1, @0, @2]; }
|
||||
|
||||
- (BOOL)allocateRenderResourcesAndReturnError:(NSError **)outError {
|
||||
if (![super allocateRenderResourcesAndReturnError:outError]) return NO;
|
||||
InstrumentKernel *k = self.instKernel;
|
||||
k->inst = Instrument::create(k->type.c_str(), (int)std::lround(_outBus.format.sampleRate),
|
||||
static_cast<sampler::SampleStore *>(self.sampleStore));
|
||||
k->outBuf.assign(self.maximumFramesToRender, 0.f);
|
||||
if (!k->inst) {
|
||||
if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:kAudioUnitErr_FailedInitialization userInfo:nil];
|
||||
[super deallocateRenderResources];
|
||||
return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)deallocateRenderResources {
|
||||
self.instKernel->inst.reset();
|
||||
[super deallocateRenderResources];
|
||||
}
|
||||
|
||||
- (AUInternalRenderBlock)internalRenderBlock {
|
||||
InstrumentKernel *k = self.instKernel;
|
||||
return ^AUAudioUnitStatus(AudioUnitRenderActionFlags *actionFlags, const AudioTimeStamp *timestamp,
|
||||
AVAudioFrameCount frameCount, NSInteger outputBusNumber,
|
||||
AudioBufferList *outputData, const AURenderEvent *events,
|
||||
AURenderPullInputBlock pullInputBlock) {
|
||||
Instrument *inst = k->inst.get();
|
||||
if (!inst) return kAudioUnitErr_Uninitialized;
|
||||
UInt32 nb = outputData->mNumberBuffers;
|
||||
if (nb < 1) return noErr;
|
||||
if (frameCount > k->outBuf.size()) return kAudioUnitErr_TooManyFramesToProcess;
|
||||
// Hosts may pass null buffers: then the AU supplies its own.
|
||||
if (!outputData->mBuffers[0].mData) outputData->mBuffers[0].mData = k->outBuf.data();
|
||||
float *out0 = (float *)outputData->mBuffers[0].mData;
|
||||
|
||||
k->refresh(k->pollTempo());
|
||||
inst->blockStart((int)frameCount);
|
||||
|
||||
// Render between events so each note starts on its own sample.
|
||||
const AUEventSampleTime start = (AUEventSampleTime)timestamp->mSampleTime;
|
||||
const AURenderEvent *e = events;
|
||||
AVAudioFrameCount frame = 0;
|
||||
while (frame < frameCount) {
|
||||
while (e && e->head.eventSampleTime - start <= (AUEventSampleTime)frame) {
|
||||
switch (e->head.eventType) {
|
||||
case AURenderEventMIDI:
|
||||
k->midi(e->MIDI.data, e->MIDI.length);
|
||||
break;
|
||||
case AURenderEventParameter:
|
||||
case AURenderEventParameterRamp:
|
||||
k->setValue(e->parameter.parameterAddress, e->parameter.value);
|
||||
k->refresh(false);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
e = e->head.next;
|
||||
}
|
||||
AVAudioFrameCount end = frameCount;
|
||||
if (e) {
|
||||
AUEventSampleTime t = e->head.eventSampleTime - start;
|
||||
if (t < (AUEventSampleTime)end) end = (AVAudioFrameCount)std::max<AUEventSampleTime>(t, frame + 1);
|
||||
}
|
||||
for (AVAudioFrameCount i = frame; i < end; ++i) {
|
||||
float y = inst->render();
|
||||
out0[i] = std::isfinite(y) ? y : 0.f;
|
||||
}
|
||||
frame = end;
|
||||
}
|
||||
outputData->mBuffers[0].mDataByteSize = frameCount * sizeof(float);
|
||||
for (UInt32 c = 1; c < nb; ++c) {
|
||||
if (!outputData->mBuffers[c].mData) outputData->mBuffers[c].mData = out0;
|
||||
else std::memcpy(outputData->mBuffers[c].mData, out0, frameCount * sizeof(float));
|
||||
outputData->mBuffers[c].mDataByteSize = frameCount * sizeof(float);
|
||||
}
|
||||
(void)actionFlags; (void)outputBusNumber; (void)pullInputBlock;
|
||||
return noErr;
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user