-
Notifications
You must be signed in to change notification settings - Fork 13
/
main_elements.py
537 lines (428 loc) · 20.3 KB
/
main_elements.py
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
from attr import attrs, attrib, Factory, validate
from attr.validators import and_, gt, instance_of, optional
from enum import Enum
from fractions import Fraction
from six import string_types
from .geom import PositionOffset, InteractionRange, PositionInteractionRange
from ..exceptions import AdmError
from ....common import (
CartesianScreen,
PolarScreen,
default_screen,
finite_float,
list_of,
)
def _lookup_elements(adm, idRefs):
"""Lookup multiple ID references"""
return [adm.lookup_element(key) for key in idRefs]
def _link_track_stream_format(audioTrackFormat, audioStreamFormat):
"""Establish a link between an audioTrackFormat and an audioStreamFormat"""
if (audioTrackFormat.audioStreamFormat is not None and
audioTrackFormat.audioStreamFormat is not audioStreamFormat):
raise AdmError("audioTrackFormat {audioTrackFormat.id} is linked "
"to more than one audioStreamFormat".format(audioTrackFormat=audioTrackFormat))
audioTrackFormat.audioStreamFormat = audioStreamFormat
class TypeDefinition(Enum):
"""ADM type definitions, representing ``typeDefintion`` and ``typeLabel``
attributes.
"""
DirectSpeakers = 1
Matrix = 2
Objects = 3
HOA = 4
Binaural = 5
class FormatDefinition(Enum):
"""ADM format definitions, representing ``formatDefintion`` and
``formatLabel`` attributes.
"""
PCM = 1
@attrs(slots=True)
class LoudnessMetadata(object):
"""ADM loudnessMetadata
Attributes:
loudnessMethod (Optional[str])
loudnessRecType (Optional[str])
loudnessCorrectionType (Optional[str])
integratedLoudness (Optional[float])
loudnessRange (Optional[float])
maxTruePeak (Optional[float])
maxMomentary (Optional[float])
maxShortTerm (Optional[float])
dialogueLoudness (Optional[float])
"""
loudnessMethod = attrib(default=None, validator=optional(instance_of(string_types)))
loudnessRecType = attrib(
default=None, validator=optional(instance_of(string_types))
)
loudnessCorrectionType = attrib(
default=None, validator=optional(instance_of(string_types))
)
integratedLoudness = attrib(default=None, validator=optional(finite_float()))
loudnessRange = attrib(default=None, validator=optional(finite_float()))
maxTruePeak = attrib(default=None, validator=optional(finite_float()))
maxMomentary = attrib(default=None, validator=optional(finite_float()))
maxShortTerm = attrib(default=None, validator=optional(finite_float()))
dialogueLoudness = attrib(default=None, validator=optional(finite_float()))
@attrs(slots=True)
class AudioObjectInteraction(object):
"""ADM audioObjectInteraction element
Attributes:
onOffInteract (bool)
gainInteract (Optional[bool])
positionInteract (Optional[bool])
gainInteractionRange (Optional[InteractionRange])
positionInteractionRange (Optional[PositionInteractionRange])
"""
onOffInteract = attrib(validator=instance_of(bool), default=False)
gainInteract = attrib(validator=optional(instance_of(bool)), default=None)
positionInteract = attrib(validator=optional(instance_of(bool)), default=None)
gainInteractionRange = attrib(
validator=optional(instance_of(InteractionRange)), default=None
)
positionInteractionRange = attrib(
validator=optional(instance_of(PositionInteractionRange)), default=None
)
@attrs(slots=True)
class AlternativeValueSet(object):
"""ADM alternativeValueSet
used in audioObjects and referenced in audioProgramme and audioContents
Attributes:
id (Optional[str]): ADM ID attribute
gain (Optional[float])
mute (Optional[bool])
positionOffset (Optional[PositionOffset])
audioObjectInteraction (Optional[AudioObjectInteraction])
"""
id = attrib(default=None)
gain = attrib(validator=optional(finite_float()), default=None)
mute = attrib(validator=optional(instance_of(bool)), default=None)
positionOffset = attrib(
validator=optional(instance_of(PositionOffset)), default=None
)
audioObjectInteraction = attrib(
validator=optional(instance_of(AudioObjectInteraction)), default=None
)
@attrs(slots=True)
class ADMElement(object):
"""base class for top-level ADM elements
Attributes:
id (str): ADM ID attribute (e.g. audioProgrammeID)
is_common_definition (bool): was this read from the common definitions file?
"""
id = attrib(default=None)
is_common_definition = attrib(default=False, validator=instance_of(bool))
@property
def element_type(self):
return type(self).__name__
def validate(self, adm=None):
validate(self)
@attrs(slots=True)
class AudioProgramme(ADMElement):
"""ADM audioProgramme
Attributes:
audioProgrammeName (str)
audioProgrammeLanguage (Optional[str])
start (Optional[fractions.Fraction])
end (Optional[fractions.Fraction])
maxDuckingDepth (Optional[float])
audioContents (list[AudioContent]): audioContent elements referenced
via audioContentIDRef
referenceScreen (Optional[Union[CartesianScreen, PolarScreen]])
loudnessMetadata (list[LoudnessMetadata])
alternativeValueSets (list[AlternativeValueSet]): referenced alternativeValueSets
"""
audioProgrammeName = attrib(default=None, validator=instance_of(string_types))
audioProgrammeLanguage = attrib(default=None)
start = attrib(default=None)
end = attrib(default=None)
maxDuckingDepth = attrib(default=None)
audioContents = attrib(default=Factory(list), repr=False)
audioContentIDRef = attrib(default=Factory(list))
referenceScreen = attrib(validator=optional(instance_of((CartesianScreen, PolarScreen))),
default=default_screen)
loudnessMetadata = attrib(default=Factory(list), validator=list_of(LoudnessMetadata))
alternativeValueSets = attrib(
validator=list_of(AlternativeValueSet), default=Factory(list)
)
alternativeValueSetIDRef = attrib(default=None)
def lazy_lookup_references(self, adm):
if self.audioContentIDRef is not None:
self.audioContents = _lookup_elements(adm, self.audioContentIDRef)
self.audioContentIDRef = None
@attrs(slots=True)
class AudioContent(ADMElement):
"""ADM audioContent
Attributes:
audioContentName (str)
audioContentLanguage (Optional[str])
loudnessMetadata (list[LoudnessMetadata])
dialogue (Optional[int])
audioObjects (list[AudioObject])
alternativeValueSets (list[AlternativeValueSet]): referenced alternativeValueSets
"""
audioContentName = attrib(default=None, validator=instance_of(string_types))
audioContentLanguage = attrib(default=None)
loudnessMetadata = attrib(default=Factory(list), validator=list_of(LoudnessMetadata))
dialogue = attrib(default=None)
audioObjects = attrib(default=Factory(list), repr=False)
audioObjectIDRef = attrib(default=None)
alternativeValueSets = attrib(
validator=list_of(AlternativeValueSet), default=Factory(list)
)
alternativeValueSetIDRef = attrib(default=None)
def lazy_lookup_references(self, adm):
if self.audioObjectIDRef is not None:
self.audioObjects = _lookup_elements(adm, self.audioObjectIDRef)
self.audioObjectIDRef = None
@attrs(slots=True)
class AudioObject(ADMElement):
"""ADM audioObject
Attributes:
audioObjectName (str)
start (Optional[fractions.Fraction])
duration (Optional[fractions.Fraction])
importance (Optional[int])
interact (Optional[bool])
disableDucking (Optional[bool])
dialogue (Optional[int])
audioPackFormats (list[AudioPackFormat])
audioTrackUIDs (list[AudioTrackUID])
audioObjects (list[AudioObject])
audioComplementaryObjects (list[AudioObject])
gain (float)
mute (bool)
positionOffset (Optional[PositionOffset])
alternativeValueSets (list[AlternativeValueSet])
audioObjectInteraction (Optional[AudioObjectInteraction])
"""
audioObjectName = attrib(default=None, validator=instance_of(string_types))
start = attrib(validator=optional(instance_of(Fraction)), default=None)
duration = attrib(validator=optional(instance_of(Fraction)), default=None)
importance = attrib(default=None, validator=optional(instance_of(int)))
interact = attrib(default=None, validator=optional(instance_of(bool)))
disableDucking = attrib(default=None, validator=optional(instance_of(bool)))
dialogue = attrib(default=None, validator=optional(instance_of(int)))
audioPackFormats = attrib(default=Factory(list), repr=False)
audioTrackUIDs = attrib(default=Factory(list), repr=False)
audioObjects = attrib(default=Factory(list), repr=False)
audioComplementaryObjects = attrib(default=Factory(list), repr=False)
gain = attrib(validator=finite_float(), default=1.0)
mute = attrib(validator=instance_of(bool), default=False)
positionOffset = attrib(
validator=optional(instance_of(PositionOffset)), default=None
)
alternativeValueSets = attrib(
validator=list_of(AlternativeValueSet), default=Factory(list)
)
audioObjectInteraction = attrib(
validator=optional(instance_of(AudioObjectInteraction)), default=None
)
audioPackFormatIDRef = attrib(default=None)
audioTrackUIDRef = attrib(default=None)
audioObjectIDRef = attrib(default=None)
audioComplementaryObjectIDRef = attrib(default=None)
def lazy_lookup_references(self, adm):
if self.audioPackFormatIDRef is not None:
self.audioPackFormats = _lookup_elements(adm, self.audioPackFormatIDRef)
self.audioPackFormatIDRef = None
if self.audioTrackUIDRef is not None:
self.audioTrackUIDs = [adm[ref] if ref is not None else None
for ref in self.audioTrackUIDRef]
self.audioTrackUIDRef = None
if self.audioObjectIDRef is not None:
self.audioObjects = _lookup_elements(adm, self.audioObjectIDRef)
self.audioObjectIDRef = None
if self.audioComplementaryObjectIDRef is not None:
self.audioComplementaryObjects = _lookup_elements(adm, self.audioComplementaryObjectIDRef)
self.audioComplementaryObjectIDRef = None
@attrs(slots=True)
class AudioPackFormat(ADMElement):
"""ADM audioPackformat
Attributes:
audioPackFormatName (str)
type (TypeDefinition): ``typeDefintion`` and/or ``typeLabel``
absoluteDistance (Optional[float])
audioChannelFormats (list[AudioChannelFormat])
audioPackFormats (list[AudioPackFormat])
importance (Optional[int])
encodePackFormats (list[AudioPackFormat]): Only for type==Matrix.
Encode and decode pack references are a single binary many-many
relationship; we only store one side.
inputPackFormat (Optional[AudioPackFormat]): Only for type==Matrix.
outputPackFormat (Optional[AudioPackFormat]): Only for type==Matrix.
normalization (Optional[str]): Only for type==HOA.
nfcRefDist (Optional[float]): Only for type==HOA.
screenRef (Optional[bool]): Only for type==HOA.
"""
audioPackFormatName = attrib(default=None, validator=instance_of(string_types))
type = attrib(default=None, validator=instance_of(TypeDefinition))
absoluteDistance = attrib(default=None)
audioChannelFormats = attrib(default=Factory(list), repr=False)
audioPackFormats = attrib(default=Factory(list), repr=False)
importance = attrib(default=None, validator=optional(instance_of(int)))
# attributes for type==Matrix
# encode and decode pack references are a single binary many-many
# relationship; only store one side
encodePackFormats = attrib(default=Factory(list))
inputPackFormat = attrib(default=None)
outputPackFormat = attrib(default=None)
# attributes for type==HOA
normalization = attrib(default=None, validator=optional(instance_of(str)))
nfcRefDist = attrib(default=None, validator=optional(finite_float()))
screenRef = attrib(default=None, validator=optional(instance_of(bool)))
audioChannelFormatIDRef = attrib(default=None)
audioPackFormatIDRef = attrib(default=None)
encodePackFormatIDRef = attrib(default=None)
decodePackFormatIDRef = attrib(default=None)
inputPackFormatIDRef = attrib(default=None)
outputPackFormatIDRef = attrib(default=None)
def lazy_lookup_references(self, adm):
if self.audioChannelFormatIDRef is not None:
self.audioChannelFormats = _lookup_elements(adm, self.audioChannelFormatIDRef)
self.audioChannelFormatIDRef = None
if self.audioPackFormatIDRef is not None:
self.audioPackFormats = _lookup_elements(adm, self.audioPackFormatIDRef)
self.audioPackFormatIDRef = None
def add_encodePackFormat(decode_pack, new_encode_pack):
if not any(encode_pack is new_encode_pack
for encode_pack in decode_pack.encodePackFormats):
decode_pack.encodePackFormats.append(new_encode_pack)
if self.decodePackFormatIDRef is not None:
for decode_pack in _lookup_elements(adm, self.decodePackFormatIDRef):
add_encodePackFormat(decode_pack, self)
self.decodePackFormatIDRef = None
if self.encodePackFormatIDRef is not None:
for encode_pack in _lookup_elements(adm, self.encodePackFormatIDRef):
add_encodePackFormat(self, encode_pack)
self.encodePackFormatIDRef = None
if self.inputPackFormatIDRef is not None:
self.inputPackFormat = adm.lookup_element(self.inputPackFormatIDRef)
self.inputPackFormatIDRef = None
if self.outputPackFormatIDRef is not None:
self.outputPackFormat = adm.lookup_element(self.outputPackFormatIDRef)
self.outputPackFormatIDRef = None
@attrs(slots=True)
class Frequency(object):
"""ADM frequency element
Attributes:
lowPass (Optional[float])
highPass (Optional[float])
"""
lowPass = attrib(default=None, validator=optional(finite_float()))
highPass = attrib(default=None, validator=optional(finite_float()))
@attrs(slots=True)
class AudioChannelFormat(ADMElement):
"""ADM audioChannelFormat
Attributes:
audioChannelFormatName (str)
type (TypeDefinition): ``typeDefintion`` and/or ``typeLabel``
audioBlockFormats (list[AudioBlockFormat])
frequency (Frequency)
"""
audioChannelFormatName = attrib(default=None, validator=instance_of(string_types))
type = attrib(default=None, validator=instance_of(TypeDefinition))
audioBlockFormats = attrib(default=Factory(list))
frequency = attrib(default=Factory(Frequency), validator=instance_of(Frequency))
def lazy_lookup_references(self, adm):
for block in self.audioBlockFormats:
block.lazy_lookup_references(adm)
@audioBlockFormats.validator
def _validate_audioBlockFormats(self, attr, value):
from . import block_formats # can't import at top level without making a loop
block_type = block_formats.by_type_definition[self.type]
list_of(block_type)(self, attr, value)
def validate(self, adm=None):
super(AudioChannelFormat, self).validate(adm=adm)
for block in self.audioBlockFormats:
block.validate(adm=adm, audioChannelFormat=self)
@attrs(slots=True)
class AudioStreamFormat(ADMElement):
"""ADM audioStreamFormat
Attributes:
audioStreamFormatName (str)
format (FormatDefinition): ``formatDefintion`` and/or ``formatLabel``
audioChannelFormat (Optional[AudioStreamFormat])
audioPackFormat (Optional[AudioPackFormat])
"""
audioStreamFormatName = attrib(default=None, validator=instance_of(string_types))
format = attrib(default=None, validator=instance_of(FormatDefinition))
audioChannelFormat = attrib(default=None, repr=False)
audioPackFormat = attrib(default=None, repr=False)
audioTrackFormatIDRef = attrib(default=None)
audioChannelFormatIDRef = attrib(default=None)
audioPackFormatIDRef = attrib(default=None)
def lazy_lookup_references(self, adm):
if self.audioChannelFormatIDRef is not None:
self.audioChannelFormat = adm.lookup_element(self.audioChannelFormatIDRef)
self.audioChannelFormatIDRef = None
if self.audioPackFormatIDRef is not None:
self.audioPackFormat = adm.lookup_element(self.audioPackFormatIDRef)
self.audioPackFormatIDRef = None
if self.audioTrackFormatIDRef is not None:
for ref in self.audioTrackFormatIDRef:
track_format = adm.lookup_element(ref)
_link_track_stream_format(track_format, self)
self.audioTrackFormatIDRef = None
def validate(self, adm=None):
super(AudioStreamFormat, self).validate(adm=adm)
if self.audioPackFormat is not None and self.audioChannelFormat is not None:
raise AdmError("audioStreamFormat {self.id} has a reference to both an "
"audioPackFormat and an audioChannelFormat".format(self=self))
if self.audioPackFormat is None and self.audioChannelFormat is None:
raise AdmError("audioStreamFormat {self.id} has no reference to an "
"audioPackFormat or audioChannelFormat".format(self=self))
@attrs(slots=True)
class AudioTrackFormat(ADMElement):
"""ADM audioTrackFormat
Attributes:
audioTrackFormatName (str)
format (FormatDefinition): ``formatDefintion`` and/or ``formatLabel``
audioStreamFormat (Optional[AudioStreamFormat])
"""
audioTrackFormatName = attrib(default=None, validator=instance_of(string_types))
format = attrib(default=None, validator=instance_of(FormatDefinition))
audioStreamFormat = attrib(default=None, validator=optional(instance_of(AudioStreamFormat)))
audioStreamFormatIDRef = attrib(default=None)
def lazy_lookup_references(self, adm):
if self.audioStreamFormatIDRef is not None:
stream_format = adm.lookup_element(self.audioStreamFormatIDRef)
_link_track_stream_format(self, stream_format)
self.audioStreamFormatIDRef = None
def validate(self, adm=None):
super(AudioTrackFormat, self).validate(adm=None)
if self.audioStreamFormat is None:
raise AdmError("audioTrackFormat {self.id} is not linked "
"to an audioStreamFormat".format(self=self))
@attrs(slots=True)
class AudioTrackUID(ADMElement):
"""ADM audioTrackUID
Attributes:
trackIndex (Optional[int]): 1-based track index from CHNA chunk
sampleRate (Optional[int])
bitDepth (Optional[int])
audioTrackFormat (Optional[AudioTrackFormat])
audioChannelFormat (Optional[AudioChannelFormat])
audioPackFormat (Optional[AudioPackFormat])
"""
trackIndex = attrib(default=None, validator=optional(and_(instance_of(int), gt(0))))
sampleRate = attrib(default=None)
bitDepth = attrib(default=None)
audioTrackFormat = attrib(default=None, repr=False,
validator=optional(instance_of(AudioTrackFormat)))
audioChannelFormat = attrib(default=None, repr=False,
validator=optional(instance_of(AudioChannelFormat)))
audioPackFormat = attrib(default=None, repr=False,
validator=optional(instance_of(AudioPackFormat)))
audioTrackFormatIDRef = attrib(default=None)
audioChannelFormatIDRef = attrib(default=None)
audioPackFormatIDRef = attrib(default=None)
def lazy_lookup_references(self, adm):
if self.audioTrackFormatIDRef is not None:
self.audioTrackFormat = adm.lookup_element(self.audioTrackFormatIDRef)
self.audioTrackFormatIDRef = None
if self.audioChannelFormatIDRef is not None:
self.audioChannelFormat = adm.lookup_element(self.audioChannelFormatIDRef)
self.audioChannelFormatIDRef = None
if self.audioPackFormatIDRef is not None:
self.audioPackFormat = adm.lookup_element(self.audioPackFormatIDRef)
self.audioPackFormatIDRef = None