-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_dataset.py
43 lines (33 loc) · 984 Bytes
/
audio_dataset.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
37
38
39
40
41
42
43
import random
from typing import Tuple
from pathlib import Path
import numpy as np
from numpy import ndarray
import torch
from torch import Tensor
from torch.utils.data import Dataset
import pandas as pd
from pandas import DataFrame
import librosa
from sample_logger import SampleLogger, DatasetItem
class AudioDataset(Dataset):
def __init__(self, x: DataFrame, y: ndarray, transform=None):
self.x = x
self.y = y
self.transform = transform
def __len__(self):
return len(self.x)
def __getitem__(self, index):
if torch.is_tensor(index):
index = index.tolist()
filename = self.x.iloc[index]
label = self.y[index]
sample_path = Path(filename)
signal, sampling_rate = librosa.load(sample_path)
if self.transform:
signal = self.transform(signal)
return {
'filename': filename,
'signal': signal,
'label': label
}