|
from typing import Optional |
|
|
|
import torch |
|
from torch import Tensor, tensor |
|
|
|
from torchsynth.config import SynthConfig |
|
from torchsynth.module import ( |
|
VCO, LFO, ModulationMixer, |
|
) |
|
from torchsynth.signal import Signal |
|
from torchsynth.synth import AbstractSynth |
|
|
|
|
|
class SinLFO(LFO): |
|
"""A LFO that generates the sine waveform. |
|
(The implementation of this class is a modification of the code in TorchSynth) """ |
|
|
|
def output(self, mod_signal: Optional[Signal] = None) -> Signal: |
|
|
|
if mod_signal is not None: |
|
assert mod_signal.shape == (self.batch_size, self.control_buffer_size) |
|
|
|
frequency = self.make_control(mod_signal) |
|
argument = torch.cumsum(2 * torch.pi * frequency / self.control_rate, dim=1) |
|
argument = argument + self.p("initial_phase").unsqueeze(1) |
|
|
|
shapes = torch.stack(self.make_lfo_shapes(argument), dim=1).as_subclass(Signal) |
|
|
|
mode = torch.stack([self.p(lfo) for lfo in self.lfo_types], dim=1) |
|
mode[0] = tensor([1.0, 0., 0., 0., 0.]) |
|
mode = torch.pow(mode, self.exponent) |
|
mode = mode / torch.sum(mode, dim=1, keepdim=True) |
|
return torch.matmul(mode.unsqueeze(1), shapes).squeeze(1).as_subclass(Signal) |
|
|
|
|
|
class TriLFO(LFO): |
|
"""A LFO that generates the triangle waveform. |
|
(The implementation of this class is a modification of the code in TorchSynth) """ |
|
|
|
def output(self, mod_signal: Optional[Signal] = None) -> Signal: |
|
|
|
if mod_signal is not None: |
|
assert mod_signal.shape == (self.batch_size, self.control_buffer_size) |
|
|
|
frequency = self.make_control(mod_signal) |
|
argument = torch.cumsum(2 * torch.pi * frequency / self.control_rate, dim=1) |
|
argument = argument + self.p("initial_phase").unsqueeze(1) |
|
|
|
shapes = torch.stack(self.make_lfo_shapes(argument), dim=1).as_subclass(Signal) |
|
|
|
mode = torch.stack([self.p(lfo) for lfo in self.lfo_types], dim=1) |
|
mode[0] = tensor([0.5, 0.5, 0., 0., 0.]) |
|
mode = torch.pow(mode, self.exponent) |
|
mode = mode / torch.sum(mode, dim=1, keepdim=True) |
|
return torch.matmul(mode.unsqueeze(1), shapes).squeeze(1).as_subclass(Signal) |
|
|
|
|
|
class SawLFO(LFO): |
|
"""A LFO that generates the sawtooth waveform. |
|
(The implementation of this class is a modification of the code in TorchSynth) """ |
|
|
|
def output(self, mod_signal: Optional[Signal] = None) -> Signal: |
|
|
|
if mod_signal is not None: |
|
assert mod_signal.shape == (self.batch_size, self.control_buffer_size) |
|
|
|
frequency = self.make_control(mod_signal) |
|
argument = torch.cumsum(2 * torch.pi * frequency / self.control_rate, dim=1) |
|
argument = argument + self.p("initial_phase").unsqueeze(1) |
|
|
|
shapes = torch.stack(self.make_lfo_shapes(argument), dim=1).as_subclass(Signal) |
|
|
|
mode = torch.stack([self.p(lfo) for lfo in self.lfo_types], dim=1) |
|
mode[0] = tensor([0.5, 0., 0.5, 0., 0.]) |
|
mode = torch.pow(mode, self.exponent) |
|
mode = mode / torch.sum(mode, dim=1, keepdim=True) |
|
return torch.matmul(mode.unsqueeze(1), shapes).squeeze(1).as_subclass(Signal) |
|
|
|
|
|
class RSawLFO(LFO): |
|
"""A LFO that generates the sawtooth waveform. |
|
(The implementation of this class is a modification of the code in TorchSynth) """ |
|
|
|
def output(self, mod_signal: Optional[Signal] = None) -> Signal: |
|
|
|
if mod_signal is not None: |
|
assert mod_signal.shape == (self.batch_size, self.control_buffer_size) |
|
|
|
frequency = self.make_control(mod_signal) |
|
argument = torch.cumsum(2 * torch.pi * frequency / self.control_rate, dim=1) |
|
argument = argument + self.p("initial_phase").unsqueeze(1) |
|
|
|
shapes = torch.stack(self.make_lfo_shapes(argument), dim=1).as_subclass(Signal) |
|
|
|
mode = torch.stack([self.p(lfo) for lfo in self.lfo_types], dim=1) |
|
mode[0] = tensor([0.5, 0., 0.0, 0.5, 0.]) |
|
mode = torch.pow(mode, self.exponent) |
|
mode = mode / torch.sum(mode, dim=1, keepdim=True) |
|
return torch.matmul(mode.unsqueeze(1), shapes).squeeze(1).as_subclass(Signal) |
|
|
|
|
|
class SquareLFO(LFO): |
|
"""A LFO that generates the square waveform. |
|
(The implementation of this class is a modification of the code in TorchSynth) """ |
|
|
|
def output(self, mod_signal: Optional[Signal] = None) -> Signal: |
|
|
|
if mod_signal is not None: |
|
assert mod_signal.shape == (self.batch_size, self.control_buffer_size) |
|
|
|
frequency = self.make_control(mod_signal) |
|
argument = torch.cumsum(2 * torch.pi * frequency / self.control_rate, dim=1) |
|
argument = argument + self.p("initial_phase").unsqueeze(1) |
|
|
|
shapes = torch.stack(self.make_lfo_shapes(argument), dim=1).as_subclass(Signal) |
|
|
|
mode = torch.stack([self.p(lfo) for lfo in self.lfo_types], dim=1) |
|
mode[0] = tensor([0.5, 0., 0., 0., 0.5]) |
|
mode = torch.pow(mode, self.exponent) |
|
mode = mode / torch.sum(mode, dim=1, keepdim=True) |
|
return torch.matmul(mode.unsqueeze(1), shapes).squeeze(1).as_subclass(Signal) |