def _compute_loudness(ex):
    'Compute loudness and add to example.'
    ex = ex.copy()
    beam.metrics.Metrics.counter(split, 'compute-loudness').inc()
    librosa = tfds.core.lazy_imports.librosa
    n_fft = 2048
    amin = 1e-15
    top_db = 200.0
    stft = librosa.stft(ex['audio'], n_fft=n_fft, hop_length=int((_AUDIO_RATE // _F0_AND_LOUDNESS_RATE)))
    loudness_db = librosa.perceptual_weighting((np.abs(stft) ** 2), librosa.fft_frequencies(_AUDIO_RATE, n_fft=n_fft), amin=amin, top_db=top_db)
    mean_loudness_amp = np.mean(librosa.db_to_amplitude(loudness_db), axis=0)
    mean_loudness_db = librosa.amplitude_to_db(mean_loudness_amp, amin=amin, top_db=top_db)
    ex['loudness'] = {'db': mean_loudness_db.astype(np.float32)}
    return ex
Compute loudness and add to example.
def _compute_loudness(ex):
    
    ex = ex.copy()
    beam.metrics.Metrics.counter(split, 'compute-loudness').inc()
    librosa = tfds.core.lazy_imports.librosa
    n_fft = 2048
    amin = 1e-15
    top_db = 200.0
    stft = librosa.stft(ex['audio'], n_fft=n_fft, hop_length=int((_AUDIO_RATE // _F0_AND_LOUDNESS_RATE)))
    loudness_db = librosa.perceptual_weighting((np.abs(stft) ** 2), librosa.fft_frequencies(_AUDIO_RATE, n_fft=n_fft), amin=amin, top_db=top_db)
    mean_loudness_amp = np.mean(librosa.db_to_amplitude(loudness_db), axis=0)
    mean_loudness_db = librosa.amplitude_to_db(mean_loudness_amp, amin=amin, top_db=top_db)
    ex['loudness'] = {'db': mean_loudness_db.astype(np.float32)}
    return ex