from Signal import Signal from SignalContext import SignalContext from SignalRegistry import register from custom_types import Hz, FrameRange, Frames, Seconds from mixins.buffers import TruncatingMixin from mixins.domains import TemporalDomainHelper class ConstantSignal(TruncatingMixin, TemporalDomainHelper, Signal): def __init__(self, context: SignalContext): Signal.__init__(self, context) TemporalDomainHelper.__init__(self) self.value = float(self.data.data['value']) self.dur = Seconds(self.data.data['dur']) self.fs = int(self.data.data['fs']) def get_temporal_checked(self, start: Frames, end: Frames): return np.full(end - start, self.value) def get_range(self) -> FrameRange: return 0, self.dur * self.fs def get_fs(self) -> Frames: return self.fs register( name="constant", ctor=ConstantSignal, )
class OffsetSignal(TemporalDomainHelper, Signal): def __init__(self, context: SignalContext): Signal.__init__(self, context) self.child = self.data.resolved_refs['child'] self.offset: Seconds = Seconds(self.data.data['offset']) def get_fs(self): return self.child.get_fs() def get_offset_frames(self) -> Partial: return self.get_fs() * self.offset def get_range(self) -> FrameRange: child_lower, child_upper = self.child.get_range() offset_frames = self.get_offset_frames() return child_lower+offset_frames, child_upper+offset_frames def get_temporal(self, start: Frames, end: Frames): offset_frames = to_frames(self.get_offset_frames()) return self.child.get_temporal(start-offset_frames, end-offset_frames) register( name="offset", ctor=OffsetSignal, )
n_frames = w.getnframes() self.fs = w.getframerate() self.buffer = np.frombuffer(w.readframes(n_frames), dtype=np.int32) self.buffer = self.buffer * 1.0 / np.max(np.abs(self.buffer)) def get_source_buffer(self): if self.buffer is None: self.load_source() return self.buffer def get_temporal_checked(self, start: Frames, end: Frames): if self.buffer is None: self.load_source() return self.buffer[start:end] def get_fs(self): if self.buffer is None: self.load_source() return self.fs def get_range(self) -> FrameRange: if self.buffer is None: self.load_source() return 0, len(self.buffer) register( name="wav", ctor=WavSignal, )
from Signal import Signal from SignalContext import SignalContext from SignalRegistry import register from custom_types import Frames, FrameRange, Hz from mixins.domains import TemporalDomainHelper class DilatedSignal(TemporalDomainHelper, Signal): def __init__(self, context: SignalContext): Signal.__init__(self, context) self.child = context.resolved_refs['child'] self.factor = float(context.data['factor']) def get_temporal(self, fs: Hz, start: Frames, end: Frames): return self.child.get_temporal(fs * self.factor, start, end) def get_range(self, fs: Hz) -> FrameRange: return self.child.get_range(fs * self.factor) register( name="dilated", ctor=DilatedSignal, )
from mixins.buffers import TilingMixin, TruncatingMixin from mixins.domains import TemporalDomainHelper class BufferSignal(TruncatingMixin, TemporalDomainHelper, Signal): def __init__(self, context: SignalContext): Signal.__init__(self, context) TruncatingMixin.__init__(self) TilingMixin.__init__(self) self.buffer = np.array(self.data.data['buffer'], dtype=float) self.fs = int(self.data.data['fs']) def get_temporal_checked(self, start: Frames, end: Frames): return self.buffer[start:end] def get_range(self) -> FrameRange: return 0, len(self.buffer) def get_source_buffer(self): return self.buffer def get_fs(self) -> Partial: return self.fs register( name="buffer", ctor=BufferSignal, )
# arrangement: # ... | offset_min | child range | offset_max | ... offset_seconds = self.offset.get_temporal( child_lower + offset_min, child_upper + offset_max, ) offset_partials = offset_seconds * fs position_frames = np.arange(len(offset_partials)) sample_frames = np_to_frames(position_frames - offset_partials) in_range = np.logical_and( sample_frames >= -offset_min, sample_frames < child_upper - offset_min, ) lower_idx = np.argmax(in_range) upper_idx = len(in_range) - np.argmax(in_range[::-1]) - 1 lower = min(child_lower + offset_min + lower_idx, child_lower) upper = max(child_lower + offset_min + upper_idx + 1, child_upper) return lower, upper register( name="var_offset", ctor=VariableOffsetSignal, )
from Signal import Signal from SignalContext import SignalContext from SignalRegistry import register from custom_types import Frames, Hz, FrameRange, Seconds from mixins.buffers import TruncatingMixin from mixins.domains import TemporalDomainHelper class SineSignal(TruncatingMixin, TemporalDomainHelper, Signal): def __init__(self, context: SignalContext): Signal.__init__(self, context) self.freq = Hz(self.data.data['freq']) self.dur = Seconds(self.data.data['dur']) self.fs = int(self.data.data['fs']) def get_temporal_checked(self, start: Frames, end: Frames): return np.sin(np.arange(start, end) * 2 * self.freq * np.pi / self.fs) def get_range(self) -> FrameRange: return 0, self.dur * self.get_fs() def get_fs(self) -> Frames: return self.fs register( name="sine", ctor=SineSignal, )
from Signal import Signal from SignalContext import SignalContext from SignalRegistry import register from custom_types import FrameRange, Hz, Partial, Seconds from mixins.buffers import TilingMixin from mixins.domains import TemporalDomainHelper from util.buffer import get_centered_sample from util.frames import to_frames class TiledSignal(TilingMixin, TemporalDomainHelper, Signal): def __init__(self, context: SignalContext): Signal.__init__(self, context) self.child = self.data.resolved_refs['child'] self.lower = Seconds(self.data.data['lower']) self.upper = Seconds(self.data.data['upper']) def get_range(self, fs: Hz) -> FrameRange: return to_frames(self.lower*fs), to_frames(self.upper*fs) def get_buffer(self, fs: Hz): return get_centered_sample(self.child, fs) register( name="tiled", ctor=TiledSignal, )
import numpy as np from SignalContext import SignalContext from SignalRegistry import register from custom_types import Frames, Hz from mixins.domains import TemporalDomainHelper from signals.DerivedSignal import DerivedSignal class ScaledSignal(TemporalDomainHelper, DerivedSignal): def __init__(self, context: SignalContext): TemporalDomainHelper.__init__(self) DerivedSignal.__init__(self, context, 'child') self.factor = self.data.resolved_refs['factor'] if self.factor.get_fs() != self.child.get_fs(): raise ValueError(f"factor fs={self.factor.get_fs()} " f"does not match child fs={self.child.get_fs()}") def get_temporal(self, start: Frames, end: Frames): return np.multiply( self.child.get_temporal(start, end), self.factor.get_temporal(start, end), ) register( name="scaled", ctor=ScaledSignal, )