"""Custom methods for validation of audio metadata.""" import trafaret as t from src.constants import errors from src.constants import validation_const """For any changes that are made to audio metadata requirements, please update: https://github.com/theorchard/frontend-distribution/blob/b0f7ea92b0a2df8df145777abdb1b04b2c59c547/src/selectors/track-audio-validation-errors.js """ # noqa: E501 def sample_rate_validation(audio_meta): """Validate sample_rate value for default schema. Args: audio_meta (dict): input audio metadata. { 'sample_rate': 44100, 'bits_per_sample': 16 } Returns: dict: sample_rate value or trafaret.DataError exception. """ bits_per_sample = audio_meta.get('bits_per_sample') sample_rate = audio_meta.get('sample_rate') if not bits_per_sample or not sample_rate: return {'sample_rate': sample_rate} if bits_per_sample not in ( validation_const.BITS_PER_SAMPLE_TO_ALLOWED_SAMPLE_RATES ): return {'sample_rate': sample_rate} available_sample_rates = ( validation_const.BITS_PER_SAMPLE_TO_ALLOWED_SAMPLE_RATES[ bits_per_sample ] ) if sample_rate not in available_sample_rates: error_message = errors.INVALID_INPUT_VALUE_ERROR_MESSAGE.format( name='sample_rate', expected=available_sample_rates, value=sample_rate ) return {'sample_rate': t.DataError(error_message)} return {'sample_rate': sample_rate}