File size: 1,347 Bytes
ff7b988
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pickle

import librosa
import numpy as np

from ..utils import decode_audio
from .base import Representation


class OAFMelSpec(Representation):
    # NOTE: This configuration is from Onsets & Frames (Hawthorne et al. 17).
    # https://github.com/magenta/magenta/blob/9885adef56d134763a89de5584f7aa18ca7d53b6/magenta/models/onsets_frames_transcription/constants.py
    # https://github.com/magenta/magenta/blob/9885adef56d134763a89de5584f7aa18ca7d53b6/magenta/models/onsets_frames_transcription/data.py#L89
    _SR = 16000
    _NFFT = 2048
    _HOP_SIZE = 512
    _FMIN = 30.0
    _NMELS = 229
    _HTK = False
    _LOG = True

    def __call__(self, audio_path, offset=0.0, duration=None):
        sr, audio = decode_audio(
            audio_path,
            sr=self._SR,
            offset=offset,
            duration=duration,
            mono=True,
            normalize=False,
        )
        features = librosa.feature.melspectrogram(
            y=audio[:, 0],
            sr=self._SR,
            n_fft=self._NFFT,
            hop_length=self._HOP_SIZE,
            fmin=self._FMIN,
            n_mels=self._NMELS,
            htk=self._HTK,
        ).T
        features = features.astype(np.float32)
        if self._LOG:
            features = librosa.power_to_db(features)
        return self._SR / self._HOP_SIZE, features