File size: 1,289 Bytes
957e2dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch

from src.simulation.effect import Effect

################################################################################
# Random time-domain offset
################################################################################


class Offset(Effect):

    def __init__(self, compute_grad: bool = True, length: any = None):
        """

        Shift audio and trim/zero-pad to maintain length



        :param compute_grad: if False, use straight-through gradient estimator

        :param length: offset length in seconds; sign indicates direction

        """
        super().__init__(compute_grad)

        self.min_length, self.max_length = self.parse_range(
            length,
            float,
            f'Invalid offset length {length}'
        )

        self.length = None
        self.sample_params()

    def forward(self, x: torch.Tensor):
        shifted = torch.roll(x, shifts=self.length, dims=-1)
        if self.length >= 0:
            shifted[..., :self.length] = 0
        else:
            shifted[..., self.length:] = 0
        return shifted

    def sample_params(self):
        self.length = round(
            random.uniform(self.min_length, self.max_length) * self.sample_rate
        )