| 1 | import librosa |
| 2 | |
| 3 | class AudioFile: |
| 4 | |
| 5 | def __init__(self, path, decimal_places=8): |
| 6 | self.path = path |
| 7 | self.decimal_places = decimal_places |
| 8 | |
| 9 | def duration(self): |
| 10 | duration = self.__get_duration() |
| 11 | return round(duration, self.decimal_places) |
| 12 | |
| 13 | def duration_pretty(self): |
| 14 | duration = self.__get_duration() |
| 15 | m, s = divmod(duration, 60) |
| 16 | return str(round(m, self.decimal_places)) + ':' + str(round(s, self.decimal_places)) |
| 17 | |
| 18 | def samplerate(self): |
| 19 | y, sr = librosa.load(self.path) |
| 20 | return sr |
| 21 | |
| 22 | def onset_detect(self): |
| 23 | y, sr = librosa.load(self.path) |
| 24 | onsets = librosa.onset.onset_detect(y=y, sr=sr, units='time') |
| 25 | return onsets |
| 26 | |
| 27 | # Private methods |
| 28 | def __get_duration(self): |
| 29 | y, sr = librosa.load(self.path) |
| 30 | duration = librosa.get_duration(y=y, sr=sr) |
| 31 | return duration |