Converter Node: Change volume scale observer

This should fix an exception being thrown because the observer wasn't
registered, or known to be registered. Only register it when it will be
used, and only unregister it if it was registered.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
This commit is contained in:
Christopher Snowhill 2025-05-03 01:23:19 -07:00
parent 5753c48245
commit 048bc7c30d
2 changed files with 18 additions and 3 deletions

View file

@ -45,6 +45,8 @@
double sampleRatio; double sampleRatio;
BOOL observersAdded;
float volumeScale; float volumeScale;
void *floatBuffer; void *floatBuffer;

View file

@ -66,8 +66,6 @@ static void *kConverterNodeContext = &kConverterNodeContext;
extrapolateBuffer = NULL; extrapolateBuffer = NULL;
extrapolateBufferSize = 0; extrapolateBufferSize = 0;
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.volumeScaling" options:0 context:kConverterNodeContext];
#ifdef LOG_CHAINS #ifdef LOG_CHAINS
[self initLogFiles]; [self initLogFiles];
#endif #endif
@ -76,6 +74,20 @@ static void *kConverterNodeContext = &kConverterNodeContext;
return self; return self;
} }
- (void)addObservers {
if(!observersAdded) {
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.volumeScaling" options:(NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew) context:kConverterNodeContext];
observersAdded = YES;
}
}
- (void)removeObservers {
if(observersAdded) {
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.volumeScaling" context:kConverterNodeContext];
observersAdded = NO;
}
}
void scale_by_volume(float *buffer, size_t count, float volume) { void scale_by_volume(float *buffer, size_t count, float volume) {
if(volume != 1.0) { if(volume != 1.0) {
size_t unaligned = (uintptr_t)buffer & 15; size_t unaligned = (uintptr_t)buffer & 15;
@ -334,6 +346,7 @@ void scale_by_volume(float *buffer, size_t count, float volume) {
if(nodeChannelConfig) { if(nodeChannelConfig) {
[chunk setChannelConfig:nodeChannelConfig]; [chunk setChannelConfig:nodeChannelConfig];
} }
[self addObservers];
scale_by_volume(floatBuffer, ioNumberPackets / sizeof(float), volumeScale); scale_by_volume(floatBuffer, ioNumberPackets / sizeof(float), volumeScale);
[chunk setStreamTimestamp:streamTimestamp]; [chunk setStreamTimestamp:streamTimestamp];
[chunk setStreamTimeRatio:streamTimeRatio]; [chunk setStreamTimeRatio:streamTimeRatio];
@ -488,7 +501,7 @@ static float db_to_scale(float db) {
- (void)dealloc { - (void)dealloc {
DLog(@"Converter dealloc"); DLog(@"Converter dealloc");
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.volumeScaling" context:kConverterNodeContext]; [self removeObservers];
paused = NO; paused = NO;
[self cleanUp]; [self cleanUp];