From faeba9157d45934526bbfc2aed1ec6cbc4cf64a3 Mon Sep 17 00:00:00 2001 From: dynaflash Date: Mon, 20 Sep 2010 16:15:23 +0000 Subject: MacGui: Audio Fixes and Enhancements ... patch by circleone. Fixes issue where changing to custom from a preset then changing titles will cause an empty audio tab. Also: - Added the ability for HandBrake's language preference to be honored when it occurs in the title's tracks. - Ensured the limit (currently 24) is not passed when adding audios using presets. - If no preset is chosen, when a new title loads, one track is added (based on the language preference or the first if that is not met). - Reinitializing the audio array is now KVO-friendly, making the UI refresh more pleasant. - The "Add All Tracks" button has been made mini in size. - The audio table view no longer attempts to keep the selection. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@3549 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- macosx/HBAudioController.m | 130 ++++++++++++++++++++++++++++++--------------- 1 file changed, 86 insertions(+), 44 deletions(-) (limited to 'macosx/HBAudioController.m') diff --git a/macosx/HBAudioController.m b/macosx/HBAudioController.m index ccd6363c7..da58cfbc1 100644 --- a/macosx/HBAudioController.m +++ b/macosx/HBAudioController.m @@ -32,6 +32,7 @@ NSString *HBMixdownChangedNotification = @"HBMixdownChangedNotification"; { if (self = [super init]) { [self setVideoContainerTag: [NSNumber numberWithInt: HB_MUX_MP4]]; + audioArray = [[NSMutableArray alloc] init]; } return self; } @@ -60,6 +61,15 @@ NSString *HBMixdownChangedNotification = @"HBMixdownChangedNotification"; return; } +- (void) _clearAudioArray + +{ + while (0 < [self countOfAudioArray]) { + [self removeObjectFromAudioArrayAtIndex: 0]; + } + return; +} + #pragma mark - #pragma mark HBController Support @@ -165,8 +175,7 @@ NSString *HBMixdownChangedNotification = @"HBMixdownChangedNotification"; int maximumNumberOfAllowedAudioTracks = [HBController maximumNumberOfAllowedAudioTracks]; // Reinitialize the configured list of audio tracks - [audioArray release]; - audioArray = [[NSMutableArray alloc] init]; + [self _clearAudioArray]; // The following is the pattern to follow, but with Audio%dTrack being the key to seek... // Can we assume that there will be no skip in the data? @@ -228,68 +237,95 @@ NSString *HBMixdownChangedNotification = @"HBMixdownChangedNotification"; NSEnumerator *enumerator = [templateAudioArray objectEnumerator]; NSDictionary *dict; NSString *key; + int maximumNumberOfAllowedAudioTracks = [HBController maximumNumberOfAllowedAudioTracks]; while (nil != (dict = [enumerator nextObject])) { - HBAudio *newAudio = [[HBAudio alloc] init]; - [newAudio setController: self]; - [self insertObject: newAudio inAudioArrayAtIndex: [self countOfAudioArray]]; - [newAudio setVideoContainerTag: [self videoContainerTag]]; - [newAudio setTrackFromIndex: trackIndex]; - key = [dict objectForKey: @"AudioEncoder"]; - if (0 == aType && - YES == [[NSUserDefaults standardUserDefaults] boolForKey: @"UseCoreAudio"] && - YES == [key isEqualToString: @"AAC (faac)"] - ) { - key = @"AAC (CoreAudio)"; - } - // If our preset wants us to support a codec that the track does not support, instead - // of changing the codec we remove the audio instead. - if (YES == [newAudio setCodecFromName: key]) { - [newAudio setMixdownFromName: [dict objectForKey: @"AudioMixdown"]]; - [newAudio setSampleRateFromName: [dict objectForKey: @"AudioSamplerate"]]; - [newAudio setBitRateFromName: [dict objectForKey: @"AudioBitrate"]]; - [newAudio setDrc: [dict objectForKey: @"AudioTrackDRCSlider"]]; - } - else { - [self removeObjectFromAudioArrayAtIndex: [self countOfAudioArray] - 1]; + if ([self countOfAudioArray] < maximumNumberOfAllowedAudioTracks) { + HBAudio *newAudio = [[HBAudio alloc] init]; + [newAudio setController: self]; + [self insertObject: newAudio inAudioArrayAtIndex: [self countOfAudioArray]]; + [newAudio setVideoContainerTag: [self videoContainerTag]]; + [newAudio setTrackFromIndex: trackIndex]; + key = [dict objectForKey: @"AudioEncoder"]; + if (0 == aType && + YES == [[NSUserDefaults standardUserDefaults] boolForKey: @"UseCoreAudio"] && + YES == [key isEqualToString: @"AAC (faac)"] + ) { + key = @"AAC (CoreAudio)"; + } + // If our preset wants us to support a codec that the track does not support, instead + // of changing the codec we remove the audio instead. + if (YES == [newAudio setCodecFromName: key]) { + [newAudio setMixdownFromName: [dict objectForKey: @"AudioMixdown"]]; + [newAudio setSampleRateFromName: [dict objectForKey: @"AudioSamplerate"]]; + [newAudio setBitRateFromName: [dict objectForKey: @"AudioBitrate"]]; + [newAudio setDrc: [dict objectForKey: @"AudioTrackDRCSlider"]]; + } + else { + [self removeObjectFromAudioArrayAtIndex: [self countOfAudioArray] - 1]; + } + [newAudio release]; } - [newAudio release]; } return; } -- (void) addTracksFromPreset: (NSMutableDictionary *) aPreset +// This matches the FIRST track with the specified prefix, otherwise it uses the defaultIfNotFound value +- (unsigned int) _trackWithTitlePrefix: (NSString *) prefix defaultIfNotFound: (unsigned int) defaultIfNotFound + +{ + unsigned int retval = defaultIfNotFound; + int count = [masterTrackArray count]; + NSString *languageTitle; + BOOL found = NO; + + // We search for the prefix noting that our titles have the format %d: %s where the %s is the prefix + for (unsigned int i = 1; i < count && NO == found; i++) { // Note that we skip the "None" track + languageTitle = [[masterTrackArray objectAtIndex: i] objectForKey: keyAudioTrackName]; + if (YES == [[languageTitle substringFromIndex: [languageTitle rangeOfString: @" "].location + 1] hasPrefix: prefix]) { + retval = i; + found = YES; + } + } + return retval; +} + +- (void) addTracksFromPreset: (NSMutableDictionary *) aPreset allTracks: (BOOL) allTracks { id whatToUse = [self _presetAudioArrayFromPreset: aPreset]; + NSString *preferredLanguageName = [[NSUserDefaults standardUserDefaults] stringForKey: @"DefaultLanguage"]; + int preferredLanguage = [self _trackWithTitlePrefix: preferredLanguageName defaultIfNotFound: 1]; // Reinitialize the configured list of audio tracks - [audioArray release]; - audioArray = [[NSMutableArray alloc] init]; + [self _clearAudioArray]; - [self _processPresetAudioArray: whatToUse forTrack: 1 andType: [[aPreset objectForKey: @"Type"] intValue]]; + [self _processPresetAudioArray: whatToUse forTrack: preferredLanguage andType: [[aPreset objectForKey: @"Type"] intValue]]; + if (YES == allTracks) { + unsigned int count = [masterTrackArray count]; + for (unsigned int i = 1; i < count; i++) { + if (i != preferredLanguage) { + [self _processPresetAudioArray: whatToUse forTrack: i andType: [[aPreset objectForKey: @"Type"] intValue]]; + } + } + } [self switchingTrackFromNone: nil]; // see if we need to add one to the list return; } -- (void) addAllTracksFromPreset: (NSMutableDictionary *) aPreset +- (void) addTracksFromPreset: (NSMutableDictionary *) aPreset { - id whatToUse = [self _presetAudioArrayFromPreset: aPreset]; - - // Reinitialize the configured list of audio tracks - [audioArray release]; - audioArray = [[NSMutableArray alloc] init]; - - for (unsigned int i = 1; i < [masterTrackArray count]; i++) { - [self _processPresetAudioArray: whatToUse forTrack: i andType: [[aPreset objectForKey: @"Type"] intValue]]; - } - - [self switchingTrackFromNone: nil]; // see if we need to add one to the list - + [self addTracksFromPreset: aPreset allTracks: NO]; return; +} + +- (void) addAllTracksFromPreset: (NSMutableDictionary *) aPreset + +{ + [self addTracksFromPreset: aPreset allTracks: YES]; return; } @@ -419,9 +455,15 @@ NSString *HBMixdownChangedNotification = @"HBMixdownChangedNotification"; } // Reinitialize the configured list of audio tracks - [audioArray release]; - audioArray = [[NSMutableArray alloc] init]; + [self _clearAudioArray]; + if (NO == [myController hasValidPresetSelected]) { + NSString *preferredLanguageName = [[NSUserDefaults standardUserDefaults] stringForKey: @"DefaultLanguage"]; + int preferredLanguage = [self _trackWithTitlePrefix: preferredLanguageName defaultIfNotFound: 1]; + [self addNewAudioTrack]; + HBAudio *anAudio = [self objectInAudioArrayAtIndex: 0]; + [anAudio setTrackFromIndex: preferredLanguage]; + } return; } -- cgit v1.2.3