File size: 846 Bytes
88b57c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Standard Library
import os

# Explicit Typing
from typing import Tuple
from numpy import ndarray

# Third-party
import librosa
import torch


class Loader:
    """Loading sound files into a usable format for pytorch"""

    def __init__(self, INPUT_FOLDER, OUTPUT_FOLDER):
        self.input = INPUT_FOLDER
        self.output = OUTPUT_FOLDER
    def load_wav(self, name) -> Tuple[ndarray, int]:
        music_array, samplerate = librosa.load(
            os.path.join(self.input, name + ".wav"), mono=False, sr=44100
        )
        return music_array, samplerate

    def prepare_uploaded_file(self, uploaded_file) -> Tuple[torch.Tensor, int]:
        music_array, samplerate = librosa.load(uploaded_file, mono=False, sr=44100)

        music_tensor = torch.tensor(music_array, dtype=torch.float32)

        return music_tensor, samplerate