1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/* HBAudio.m $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#import "HBAudio.h"
#import "HBJob.h"
#import "HBJob+HBJobConversion.h"
#import "HBTitle.h"
#import "HBAudioTrack.h"
#import "HBAudioTrackPreset.h"
#import "HBAudioDefaults.h"
#import "HBCodingUtilities.h"
#import "HBJob+Private.h"
#include "hb.h"
#define NONE_TRACK_INDEX 0
NSString *HBAudioChangedNotification = @"HBAudioChangedNotification";
@interface HBAudio () <HBAudioTrackDataSource, HBAudioTrackDelegate>
@property (nonatomic, readwrite, weak) HBJob *job;
@property (nonatomic, readwrite) int container;
@end
@implementation HBAudio
- (instancetype)initWithJob:(HBJob *)job
{
self = [super init];
if (self)
{
_job = job;
_container = HB_MUX_MP4;
_tracks = [[NSMutableArray alloc] init];
_defaults = [[HBAudioDefaults alloc] init];
// Add the none and foreign track to the source array
NSMutableArray *sourceTracks = [job.title.audioTracks mutableCopy];
NSDictionary *none = @{keyAudioTrackName: NSLocalizedString(@"None", nil)};
[sourceTracks insertObject:none atIndex:0];
_sourceTracks = [sourceTracks copy];
}
return self;
}
#pragma mark - Data Source
- (NSDictionary<NSString *, id> *)sourceTrackAtIndex:(NSUInteger)idx;
{
return self.sourceTracks[idx];
}
- (NSArray<NSString *> *)sourceTracksArray
{
NSMutableArray *sourceNames = [NSMutableArray array];
for (NSDictionary *track in self.sourceTracks)
{
[sourceNames addObject:track[keyAudioTrackName]];
}
return sourceNames;
}
#pragma mark - Delegate
- (void)track:(HBAudioTrack *)track didChangeSourceFrom:(NSUInteger)oldSourceIdx;
{
// If the source was changed to None, remove the track
if (track.sourceTrackIdx == NONE_TRACK_INDEX)
{
NSUInteger idx = [self.tracks indexOfObject:track];
[self removeObjectFromTracksAtIndex:idx];
}
// Else add a new None track
else if (oldSourceIdx == NONE_TRACK_INDEX)
{
[self addNoneTrack];
}
}
- (void)addNoneTrack
{
HBAudioTrack *track = [self trackFromSourceTrackIndex:NONE_TRACK_INDEX];
[self addTrack:track];
}
#pragma mark - Public methods
- (void)addAllTracks
{
[self removeTracksAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.tracks.count)]];
// Add the remainings tracks
for (NSUInteger idx = 1; idx < self.sourceTracksArray.count; idx++) {
[self addTrack:[self trackFromSourceTrackIndex:idx]];
}
[self addNoneTrack];
}
- (void)removeAll
{
[self removeTracksAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.tracks.count)]];
[self addNoneTrack];
}
- (void)reloadDefaults
{
[self addDefaultTracksFromJobSettings:self.job.jobDict];
}
- (void)setContainer:(int)container
{
_container = container;
if (!(self.undo.isUndoing || self.undo.isRedoing))
{
for (HBAudioTrack *track in self.tracks)
{
track.container = container;
}
// Update the Auto Passthru Fallback Codec Popup
// lets get the tag of the currently selected item first so we might reset it later
[self.defaults validateEncoderFallbackForVideoContainer:container];
}
}
- (void)setUndo:(NSUndoManager *)undo
{
_undo = undo;
for (HBAudioTrack *track in self.tracks)
{
track.undo = undo;
}
self.defaults.undo = undo;
}
- (void)setDefaults:(HBAudioDefaults *)defaults
{
if (defaults != _defaults)
{
[[self.undo prepareWithInvocationTarget:self] setDefaults:_defaults];
}
_defaults = defaults;
_defaults.undo = self.undo;
}
/**
* Convenience method to add a track to tracks array.
*
* @param track the track to add.
*/
- (void)addTrack:(HBAudioTrack *)newTrack
{
[self insertObject:newTrack inTracksAtIndex:[self countOfTracks]];
}
/**
* Creates a new track dictionary from a source track.
*
* @param index the index of the source track in the sourceTracks array
*/
- (HBAudioTrack *)trackFromSourceTrackIndex:(NSInteger)index
{
HBAudioTrack *track = [[HBAudioTrack alloc] initWithTrackIdx:index container:self.container dataSource:self delegate:self];
track.undo = self.undo;
return track;
}
#pragma mark - Defaults
- (void)addDefaultTracksFromJobSettings:(NSDictionary *)settings
{
NSMutableArray<HBAudioTrack *> *tracks = [NSMutableArray array];
NSArray<NSDictionary<NSString *, id> *> *settingsTracks = settings[@"Audio"][@"AudioList"];
// Reinitialize the configured list of audio tracks
[self removeTracksAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.tracks.count)]];
// Add the tracks
for (NSDictionary *trackDict in settingsTracks)
{
HBAudioTrack *track = [self trackFromSourceTrackIndex:[trackDict[@"Track"] unsignedIntegerValue] + 1];
track.drc = [trackDict[@"DRC"] doubleValue];
track.gain = [trackDict[@"Gain"] doubleValue];
track.mixdown = hb_mixdown_get_from_name([trackDict[@"Mixdown"] UTF8String]);
if ([trackDict[@"Samplerate"] isKindOfClass:[NSString class]])
{
int sampleRate = hb_audio_samplerate_get_from_name([trackDict[@"Samplerate"] UTF8String]);
track.sampleRate = sampleRate == -1 ? 0 : sampleRate;
}
else
{
track.sampleRate = [trackDict[@"Samplerate"] intValue];
}
track.bitRate = [trackDict[@"Bitrate"] intValue];
track.encoder = hb_audio_encoder_get_from_name([trackDict[@"Encoder"] UTF8String]);
[tracks addObject:track];
}
[self insertTracks:tracks atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, tracks.count)]];
// Add an None item
[self addNoneTrack];
}
- (BOOL)anyCodecMatches:(int)codec
{
for (HBAudioTrack *track in self.tracks)
{
if (track.isEnabled && codec == track.encoder)
{
return YES;
}
}
return NO;
}
#pragma mark -
#pragma mark Notification Handling
- (void)encoderChanged
{
[[NSNotificationCenter defaultCenter] postNotificationName:HBAudioChangedNotification object:self];
}
#pragma mark - NSCopying
- (instancetype)copyWithZone:(NSZone *)zone
{
HBAudio *copy = [[[self class] alloc] init];
if (copy)
{
copy->_container = _container;
copy->_sourceTracks = [_sourceTracks copy];
copy->_tracks = [[NSMutableArray alloc] init];
for (HBAudioTrack *track in _tracks)
{
HBAudioTrack *trackCopy = [track copy];
[copy->_tracks addObject:trackCopy];
trackCopy.dataSource = copy;
trackCopy.delegate = copy;
}
copy->_defaults = [_defaults copy];
}
return copy;
}
#pragma mark - NSCoding
+ (BOOL)supportsSecureCoding
{
return YES;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeInt:2 forKey:@"HBAudioVersion"];
encodeInt(_container);
encodeObject(_sourceTracks);
encodeObject(_tracks);
encodeObject(_defaults);
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super init];
decodeInt(_container);
decodeCollectionOfObjects(_sourceTracks, NSArray, NSDictionary);
decodeCollectionOfObjects(_tracks, NSMutableArray, HBAudioTrack);
for (HBAudioTrack *track in _tracks)
{
track.dataSource = self;
track.delegate = self;
}
decodeObject(_defaults, HBAudioDefaults);
return self;
}
#pragma mark - Presets
- (void)writeToPreset:(HBMutablePreset *)preset
{
[self.defaults writeToPreset:preset];
}
- (void)applyPreset:(HBPreset *)preset jobSettings:(NSDictionary *)settings
{
[self.defaults applyPreset:preset jobSettings:settings];
[self addDefaultTracksFromJobSettings:settings];
}
#pragma mark -
#pragma mark KVC
- (NSUInteger)countOfTracks
{
return self.tracks.count;
}
- (HBAudioTrack *)objectInTracksAtIndex:(NSUInteger)index
{
return self.tracks[index];
}
- (void)insertObject:(HBAudioTrack *)track inTracksAtIndex:(NSUInteger)index;
{
[[self.undo prepareWithInvocationTarget:self] removeObjectFromTracksAtIndex:index];
[self.tracks insertObject:track atIndex:index];
}
- (void)insertTracks:(NSArray<HBAudioTrack *> *)array atIndexes:(NSIndexSet *)indexes
{
[[self.undo prepareWithInvocationTarget:self] removeTracksAtIndexes:indexes];
[self.tracks insertObjects:array atIndexes:indexes];
}
- (void)removeObjectFromTracksAtIndex:(NSUInteger)index
{
HBAudioTrack *track = self.tracks[index];
[[self.undo prepareWithInvocationTarget:self] insertObject:track inTracksAtIndex:index];
[self.tracks removeObjectAtIndex:index];
}
- (void)removeTracksAtIndexes:(NSIndexSet *)indexes
{
NSArray<HBAudioTrack *> *tracks = [self.tracks objectsAtIndexes:indexes];
[[self.undo prepareWithInvocationTarget:self] insertTracks:tracks atIndexes:indexes];
[self.tracks removeObjectsAtIndexes:indexes];
}
@end
|