Files
sizzletracker-android/ios/app/FX/SizzleFXViewController.mm
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

197 lines
8.5 KiB
Plaintext

// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// 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.
#import <CoreAudioKit/CoreAudioKit.h>
#import <UIKit/UIKit.h>
#import "Components.gen.h"
#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 {
SizzleDeviceAU *_au;
UIStackView *_stack;
NSMutableDictionary<NSNumber *, UIControl *> *_controls;
NSMutableDictionary<NSNumber *, UILabel *> *_valueLabels;
AUParameterObserverToken _token;
BOOL _built;
}
#pragma mark - factory
- (AUAudioUnit *)createAudioUnitWithComponentDescription:(AudioComponentDescription)desc error:(NSError **)error {
_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
- (void)viewDidLoad {
[super viewDidLoad];
self.preferredContentSize = CGSizeMake(520, 420);
self.view.backgroundColor = [UIColor colorWithRed:0.07 green:0.08 blue:0.09 alpha:1];
UIScrollView *scroll = [UIScrollView new];
scroll.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scroll];
_stack = [UIStackView new];
_stack.axis = UILayoutConstraintAxisVertical;
_stack.spacing = 10;
_stack.translatesAutoresizingMaskIntoConstraints = NO;
[scroll addSubview:_stack];
UILayoutGuide *g = self.view.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
[scroll.leadingAnchor constraintEqualToAnchor:g.leadingAnchor],
[scroll.trailingAnchor constraintEqualToAnchor:g.trailingAnchor],
[scroll.topAnchor constraintEqualToAnchor:g.topAnchor],
[scroll.bottomAnchor constraintEqualToAnchor:g.bottomAnchor],
[_stack.leadingAnchor constraintEqualToAnchor:scroll.contentLayoutGuide.leadingAnchor constant:14],
[_stack.trailingAnchor constraintEqualToAnchor:scroll.contentLayoutGuide.trailingAnchor constant:-14],
[_stack.topAnchor constraintEqualToAnchor:scroll.contentLayoutGuide.topAnchor constant:12],
[_stack.bottomAnchor constraintEqualToAnchor:scroll.contentLayoutGuide.bottomAnchor constant:-12],
[_stack.widthAnchor constraintEqualToAnchor:scroll.frameLayoutGuide.widthAnchor constant:-28],
]];
[self buildIfReady];
}
- (UILabel *)label:(NSString *)text size:(CGFloat)size color:(UIColor *)color {
UILabel *l = [UILabel new];
l.text = text;
l.font = [UIFont monospacedSystemFontOfSize:size weight:UIFontWeightRegular];
l.textColor = color;
return l;
}
- (void)buildIfReady {
if (_built || !_au || !_stack) return;
_built = YES;
_controls = [NSMutableDictionary dictionary];
_valueLabels = [NSMutableDictionary dictionary];
[_stack addArrangedSubview:[self label:_au.displayName.uppercaseString size:15 color:Amber()]];
if (_au.factoryPresets.count > 0) {
UIButton *presets = [UIButton buttonWithType:UIButtonTypeSystem];
[presets setTitle:@"PRESET ▾" forState:UIControlStateNormal];
presets.titleLabel.font = [UIFont monospacedSystemFontOfSize:13 weight:UIFontWeightRegular];
presets.tintColor = Amber();
presets.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
NSMutableArray<UIAction *> *items = [NSMutableArray array];
__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) {
au.currentPreset = p;
[weakButton setTitle:[NSString stringWithFormat:@"PRESET: %@ ▾", p.name] forState:UIControlStateNormal];
}]];
}
presets.menu = [UIMenu menuWithChildren:items];
presets.showsMenuAsPrimaryAction = YES;
[_stack addArrangedSubview:presets];
}
for (AUParameter *p in _au.parameterTree.allParameters) [self addRowFor:p];
__weak SizzleFXViewController *weakSelf = self;
_token = [_au.parameterTree tokenByAddingParameterObserver:^(AUParameterAddress address, AUValue value) {
dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf show:address value:value]; });
}];
}
- (void)addRowFor:(AUParameter *)p {
NSNumber *addr = @(p.address);
UIStackView *head = [UIStackView new];
head.axis = UILayoutConstraintAxisHorizontal;
[head addArrangedSubview:[self label:p.displayName.uppercaseString size:11 color:Dim()]];
UILabel *value = [self label:[_au stringForParameter:p value:p.value] size:11 color:UIColor.whiteColor];
value.textAlignment = NSTextAlignmentRight;
[head addArrangedSubview:value];
_valueLabels[addr] = value;
UIControl *control;
NSArray<NSString *> *choices = p.valueStrings;
NSUInteger chars = [[choices componentsJoinedByString:@""] length];
__weak SizzleFXViewController *weakSelf = self;
if (choices.count > 0 && choices.count <= 6 && chars <= 34) {
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:choices];
seg.selectedSegmentIndex = (NSInteger)lroundf(p.value);
seg.selectedSegmentTintColor = Amber();
[seg addAction:[UIAction actionWithHandler:^(UIAction *a) {
[weakSelf set:p value:(AUValue)seg.selectedSegmentIndex];
}] forControlEvents:UIControlEventValueChanged];
control = seg;
} else if (choices.count > 0) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];
b.tintColor = Amber();
b.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
NSMutableArray<UIAction *> *items = [NSMutableArray array];
[choices enumerateObjectsUsingBlock:^(NSString *name, NSUInteger i, BOOL *stop) {
[items addObject:[UIAction actionWithTitle:name image:nil identifier:nil handler:^(UIAction *a) {
[weakSelf set:p value:(AUValue)i];
}]];
}];
b.menu = [UIMenu menuWithChildren:items];
b.showsMenuAsPrimaryAction = YES;
[b setTitle:[NSString stringWithFormat:@"%@ ▾", choices[(NSUInteger)lroundf(p.value)]] forState:UIControlStateNormal];
control = b;
} else {
UISlider *s = [UISlider new];
s.minimumValue = p.minValue;
s.maximumValue = p.maxValue;
s.value = p.value;
s.minimumTrackTintColor = Amber();
BOOL isInt = p.unit == kAudioUnitParameterUnit_Indexed;
[s addAction:[UIAction actionWithHandler:^(UIAction *a) {
AUValue v = isInt ? roundf(s.value) : s.value;
[weakSelf set:p value:v];
}] forControlEvents:UIControlEventValueChanged];
control = s;
}
_controls[addr] = control;
UIStackView *row = [[UIStackView alloc] initWithArrangedSubviews:@[head, control]];
row.axis = UILayoutConstraintAxisVertical;
row.spacing = 4;
[_stack addArrangedSubview:row];
}
- (void)set:(AUParameter *)p value:(AUValue)v {
[p setValue:v originator:_token];
[self show:p.address value:v];
}
- (void)show:(AUParameterAddress)address value:(AUValue)value {
AUParameter *p = [_au.parameterTree parameterWithAddress:address];
if (!p) return;
_valueLabels[@(address)].text = [_au stringForParameter:p value:value];
UIControl *c = _controls[@(address)];
if ([c isKindOfClass:[UISlider class]]) {
if (!c.isTracking) ((UISlider *)c).value = value;
} else if ([c isKindOfClass:[UISegmentedControl class]]) {
((UISegmentedControl *)c).selectedSegmentIndex = (NSInteger)lroundf(value);
} else if ([c isKindOfClass:[UIButton class]] && p.valueStrings.count) {
NSUInteger i = MIN((NSUInteger)lroundf(value), p.valueStrings.count - 1);
[(UIButton *)c setTitle:[NSString stringWithFormat:@"%@ ▾", p.valueStrings[i]] forState:UIControlStateNormal];
}
}
- (void)dealloc {
if (_token && _au) [_au.parameterTree removeParameterObserver:_token];
}
@end