Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions ios/Elementary.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ - (instancetype)init
_sharedInstance = self;
self.loadedResources = [[NSMutableSet alloc] init];

// Configure session before engine: iOS default (SoloAmbient, ~4096-frame buffers)
// mismatches Elementary's block size and produces garbage audio on launch.
NSError *sessionError;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback
mode:AVAudioSessionModeDefault
options:AVAudioSessionCategoryOptionMixWithOthers |
Comment on lines +25 to +29
AVAudioSessionCategoryOptionAllowBluetoothA2DP
error:&sessionError];
if (sessionError) {
NSLog(@"[Elementary] Failed to set audio session category: %@", sessionError);
}
[session setPreferredIOBufferDuration:(512.0 / 48000.0) error:&sessionError];
if (sessionError) {
NSLog(@"[Elementary] Failed to set preferred IO buffer duration: %@", sessionError);
}
[session setActive:YES error:&sessionError];
if (sessionError) {
NSLog(@"[Elementary] Failed to activate audio session: %@", sessionError);
}
Comment on lines +23 to +42

self.audioEngine = [[AVAudioEngine alloc] init];

AVAudioFormat *outputFormat = [self.audioEngine.outputNode outputFormatForBus:0];
Expand All @@ -30,7 +51,8 @@ - (instancetype)init
const float **inputBuffer = (const float **)calloc(numOutputChannels, sizeof(float *));
float **outputBuffer = (float **)malloc(numOutputChannels * sizeof(float *));

NSLog(@"[Elementary] Init: %d output channels, sampleRate=%.0f", numOutputChannels, outputFormat.sampleRate);
NSLog(@"[Elementary] Init: %d output channels, sampleRate=%.0f, IOBufferDuration=%.4fs",
numOutputChannels, outputFormat.sampleRate, session.IOBufferDuration);

AVAudioSourceNode *sourceNode = [[AVAudioSourceNode alloc] initWithRenderBlock:^OSStatus(
BOOL * _Nonnull isSilence,
Expand Down Expand Up @@ -82,7 +104,10 @@ - (instancetype)init
return nil;
}

int bufferSize = 512;
// Use granted IOBufferDuration — iOS may not honor the preferred value exactly.
int bufferSize = (int)round(outputFormat.sampleRate * session.IOBufferDuration);
if (bufferSize <= 0 || bufferSize > 4096) bufferSize = 512; // safety fallback
NSLog(@"[Elementary] Runtime block size: %d frames", bufferSize);
self.runtime = std::make_shared<elem::Runtime<float>>(outputFormat.sampleRate, bufferSize);
Comment on lines +107 to 111

[[NSNotificationCenter defaultCenter] addObserver:self
Expand Down
Loading