-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio.py
36 lines (29 loc) · 891 Bytes
/
audio.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
import librosa
import pygame
import os
class Song(object):
def __init__(self, path):
self.path = os.path.normpath(path)
self.tempo = None
self.beats = None
self.times = None
self.analyze()
def analyze(self):
audio_path = self.path
y, sr = librosa.load(audio_path, sr=None)
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
self.tempo = tempo
self.beats = list(beats)
self.times = list(librosa.frames_to_time(beats, sr=sr))
def getTempo(self):
return self.tempo
def getBeatFrames(self):
return self.beats
def getBeatTimes(self):
return self.times
def getPath(self):
return self.path
class Sound(pygame.mixer.Sound):
def __init__(self, path):
self.path = os.path.normpath(path)
super(Sound, self).__init__(file=self.path)