From a92973c6aa99f48ad02970d9adc38001fc8bc56f Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Wed, 23 Apr 2025 21:43:51 -0700 Subject: [PATCH] HDCD: Make HDCD extension processing optional And disabled by default, at that. I can't actually hear the difference of Peak Extension in the Rock track I have that claims to use it. And Low Level Range Extension is more trouble than it's worth on tracks that use it by mistake, or maliciously, if the case may be. I may add track tag level control in the future. Signed-off-by: Christopher Snowhill --- Application/PlaybackController.m | 1 + Audio/Chain/ChunkList.h | 1 + Audio/Chain/ChunkList.m | 24 ++++++++++---- .../Preferences/Base.lproj/Preferences.xib | 33 ++++++++++++------- .../Preferences/en.lproj/Preferences.strings | 3 ++ .../Preferences/es.lproj/Preferences.strings | 3 ++ 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Application/PlaybackController.m b/Application/PlaybackController.m index feff2f911..b7dd135a4 100644 --- a/Application/PlaybackController.m +++ b/Application/PlaybackController.m @@ -129,6 +129,7 @@ NSString *CogPlaybackDidStopNotificiation = @"CogPlaybackDidStopNotificiation"; @"volumeLimit": @(YES), @"enableHrtf": @(NO), @"enableHeadTracking": @(NO), + @"enableHDCD": @(NO), /*@"rubberbandEngine": @"faster",*/ @"rubberbandTransients": @"crisp", @"rubberbandDetector": @"compound", diff --git a/Audio/Chain/ChunkList.h b/Audio/Chain/ChunkList.h index 3f1a5646c..2dc0c5038 100644 --- a/Audio/Chain/ChunkList.h +++ b/Audio/Chain/ChunkList.h @@ -42,6 +42,7 @@ NS_ASSUME_NONNULL_BEGIN BOOL observersRegistered; BOOL halveDSDVolume; + BOOL enableHDCD; void *hdcd_decoder; BOOL formatRead; diff --git a/Audio/Chain/ChunkList.m b/Audio/Chain/ChunkList.m index 1854c0acc..b2321f3e0 100644 --- a/Audio/Chain/ChunkList.m +++ b/Audio/Chain/ChunkList.m @@ -408,8 +408,10 @@ static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes - (void)addObservers { if(!observersRegistered) { halveDSDVolume = NO; + enableHDCD = NO; [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.halveDSDVolume" options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew) context:kChunkListContext]; + [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.enableHDCD" options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew) context:kChunkListContext]; observersRegistered = YES; } @@ -418,6 +420,7 @@ static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes - (void)removeObservers { if(observersRegistered) { [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.halveDSDVolume" context:kChunkListContext]; + [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.enableHDCD" context:kChunkListContext]; observersRegistered = NO; } @@ -456,6 +459,8 @@ static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes if([keyPath isEqualToString:@"values.halveDSDVolume"]) { halveDSDVolume = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] boolForKey:@"halveDSDVolume"]; + } else if([keyPath isEqualToString:@"values.enableHDCD"]) { + enableHDCD = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] boolForKey:@"enableHDCD"]; } } @@ -693,6 +698,7 @@ static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes inputFormat.mChannelsPerFrame == 2 && inputFormat.mSampleRate == 44100) { // possibly HDCD, run through decoder + [self addObservers]; if(hdcd_decoder) { free(hdcd_decoder); hdcd_decoder = NULL; @@ -839,13 +845,19 @@ static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes ((hdcd_state_stereo_t *)hdcd_decoder)->channel[1].sustain) { hdcdSustained = YES; } - gain = 2.0; - bitsPerSample = 32; - bytesReadFromInput = samplesRead * 4; - isUnsigned = NO; - inputBuffer = &tempData[buffer_adder]; - inputChanged = YES; + if(enableHDCD) { + gain = 2.0; + bitsPerSample = 32; + bytesReadFromInput = samplesRead * 4; + isUnsigned = NO; + inputBuffer = &tempData[buffer_adder]; + inputChanged = YES; + } else { + // Discard the output of the decoder and process again + goto process16bit; + } } else if(bitsPerSample <= 16) { + process16bit: samplesRead = bytesReadFromInput / 2; const size_t buffer_adder = (inputBuffer == &tempData[0]) ? buffer_adder_base : 0; if(isUnsigned) { diff --git a/Preferences/Preferences/Base.lproj/Preferences.xib b/Preferences/Preferences/Base.lproj/Preferences.xib index 2f9a5b1c7..1d94c2bdb 100644 --- a/Preferences/Preferences/Base.lproj/Preferences.xib +++ b/Preferences/Preferences/Base.lproj/Preferences.xib @@ -314,11 +314,11 @@ - + - + + @@ -413,8 +424,8 @@ - - + + @@ -423,7 +434,7 @@ - + diff --git a/Preferences/Preferences/en.lproj/Preferences.strings b/Preferences/Preferences/en.lproj/Preferences.strings index d2053a484..a85037c25 100644 --- a/Preferences/Preferences/en.lproj/Preferences.strings +++ b/Preferences/Preferences/en.lproj/Preferences.strings @@ -248,6 +248,9 @@ /* Class = "NSButtonCell"; title = "Recenter"; ObjectID = "aQP-kn-fMB"; */ "aQP-kn-fMB.title" = "Recenter"; +/* Class = "NSButtonCell"; title = "Enable HDCD Peak and Low Level Range Extend"; ObjectID = "IU9-uF-gU9"; */ +"IU9-uF-gU9.title" = "Enable HDCD Peak and Low Level Range Extend"; + /* Class = "NSButtonCell"; title = "Enable FreeSurround stereo to surround upmixing"; ObjectID = "F0i-UK-6Nu"; */ "F0i-UK-6Nu.title" = "Enable FreeSurround stereo to surround upmixing"; diff --git a/Preferences/Preferences/es.lproj/Preferences.strings b/Preferences/Preferences/es.lproj/Preferences.strings index 6511c4539..8c501a4f2 100644 --- a/Preferences/Preferences/es.lproj/Preferences.strings +++ b/Preferences/Preferences/es.lproj/Preferences.strings @@ -244,6 +244,9 @@ /* Class = "NSButtonCell"; title = "Recenter"; ObjectID = "aQP-kn-fMB"; */ "aQP-kn-fMB.title" = "Recalibrar"; +/* Class = "NSButtonCell"; title = "Enable HDCD Peak and Low Level Range Extend"; ObjectID = "IU9-uF-gU9"; */ +"IU9-uF-gU9.title" = "Activar extensiones de HDCD"; + /* Class = "NSButtonCell"; title = "Automatically check for updates on startup"; ObjectID = "207"; */ "207.title" = "Buscar actualizaciones al abrir";