Spaces:
Sleeping
Sleeping
File size: 1,665 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 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import torch
from typing import Any, Union, Sequence
from src.simulation.component import Component
################################################################################
# Simulate environmental acoustic distortions in sequence
################################################################################
class Effect(Component):
"""
Base class for all acoustic simulation effects units. Adds random parameter
sampling functionality to Component class.
"""
def __init__(self, compute_grad: bool = True):
super().__init__(compute_grad)
def forward(self, x: torch.Tensor):
raise NotImplementedError()
def sample_params(self):
"""
Sample effect parameters to allow for expectation-over-transformation
"""
raise NotImplementedError()
@staticmethod
def parse_range(params: Any, dtype: Any, error_msg: str):
"""
For real-valued parameters, obtain acceptable range of values from which
to sample randomly
"""
# for any sequence, assume endpoints mark range of values
if isinstance(params, Sequence):
min_val, max_val = params[0], params[1]
# if a single value is given, use as both "endpoints"
elif isinstance(params, dtype):
min_val = max_val = params
else:
raise ValueError(error_msg)
try:
assert isinstance(min_val, dtype)
assert isinstance(max_val, dtype)
except AssertionError:
raise ValueError(error_msg)
return min_val, max_val
|