Cog/Audio/Visualization/VisualizationController.m
Christopher Snowhill 4f5e4eca36 Fix visualization for variable audio block sizes
IN A.D. 2101, WAR WAS BEGINNING. *boom*

Yeah, this was a dumb bug, I didn't realize that AUAudioUnit would just
arbitrarily ignore my configured block size and request a different one.

The AirPods Pro will just request 480 instead of the 512 I ask for, so
let's instead support variable block sizes, and only take up to the last
4096 samples of the chunk fed to the output device.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
2022-02-16 21:54:55 -08:00

69 lines
1.3 KiB
Objective-C

//
// VisualizationController.m
// CogAudio Framework
//
// Created by Christopher Snowhill on 2/12/22.
//
#import "VisualizationController.h"
#import <Accelerate/Accelerate.h>
#import "fft.h"
@implementation VisualizationController
static VisualizationController *_sharedController = nil;
+ (VisualizationController *)sharedController {
@synchronized(self) {
if(!_sharedController) {
_sharedController = [[VisualizationController alloc] init];
}
}
return _sharedController;
}
- (id)init {
self = [super init];
if(self) {
vDSP_vclr(visAudio, 1, 4096);
}
return self;
}
- (void)dealloc {
fft_free();
}
- (void)postSampleRate:(double)sampleRate {
@synchronized(self) {
self->sampleRate = sampleRate;
}
}
- (void)postVisPCM:(const float *)inPCM amount:(int)amount {
int skipAmount = 0;
if(amount > 4096) {
skipAmount = amount - 4096;
amount = 4096;
}
@synchronized(self) {
cblas_scopy(4096 - amount, visAudio + amount, 1, visAudio, 1);
cblas_scopy(amount, inPCM + skipAmount, 1, visAudio + 4096 - amount, 1);
}
}
- (double)readSampleRate {
@synchronized(self) {
return sampleRate;
}
}
- (void)copyVisPCM:(float *)outPCM visFFT:(float *)outFFT {
@synchronized(self) {
cblas_scopy(4096, visAudio, 1, outPCM, 1);
fft_calculate(visAudio, outFFT, 2048);
}
}
@end