forked from f90/Wave-U-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample.py
35 lines (29 loc) · 996 Bytes
/
Sample.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
import Metadata
class Sample(object):
'''
Represents a particular audio track - maintains metadata about the audio file for faster audio handling during training
'''
def __init__(self, path, sample_rate, channels, duration):
self.path = path
self.sample_rate = sample_rate
self.channels = channels
self.duration = duration
@classmethod
def from_path(cls, path):
'''
Create new sample object from audio file path by retrieving metadata.
:param path:
:return:
'''
sr, channels, duration = Metadata.get_audio_metadata(path)
return cls(path, sr, channels, duration)
@classmethod
def from_array(cls, path, audio, sr):
'''
Create new sample object from a numpy audio array
:param path:
:return:
'''
channels = audio.shape[1]
duration = float(audio.shape[0]) / float(sr)
return cls(path, sr, channels, duration)