Core
core
Reusable, pure algorithmic components for inference and training.
Classes:
| Name | Description |
|---|---|
Audio |
|
NormalizationStats |
Statistics for normalizing |
NormalizedAudio |
Container for normalized audio and its original stats. |
ModelWaveformToWaveform |
|
LogMelSpect |
Computes the log-mel spectrogram of a waveform. |
SequenceFeatureExtractor |
Protocol for sequence feature extractors. |
IdentitySequenceFeatureExtractor |
|
LogMelSequenceFeatureExtractor |
|
CqtSequenceFeatureExtractor |
|
CQT |
Constant-Q transform layer (complex output) implemented via |
HarmonicCQT |
Harmonic CQT computed by stacking one CQT per harmonic multiplier. |
Functions:
| Name | Description |
|---|---|
normalize_audio |
Preprocess the raw audio in the time domain to have a mean of 0 and a std of 1 |
denormalize_audio |
Take the model output and restore them to their original loudness. |
generate_chunks |
Generates batches of overlapping chunks from an audio tensor. |
stitch_chunks |
Stitches processed audio chunks back together using the overlap-add method. |
aggregate_logits |
Stitches time-series logits (split/aggregate strategy). |
aggregate_sequence_chunks |
Aggregate generic time-major chunk outputs. |
pad_dim |
Pad an arbitrary tensor on a specific dimension. |
split_sequence_tensor |
Split a time-major sequence tensor into overlapping chunks. |
apply_mask |
Applies a complex mask to a spectrogram. |
get_model_floating_dtype |
Infer floating input dtype from the model's first floating parameter. |
to_model_device |
Move tensor to model device while preserving model floating dtype compatibility. |
create_w2w_model |
|
to_log_magnitude |
Convert complex or real spectrogram-like tensors to dB log-magnitude. |
create_cqt_kernels |
Create time-domain CQT kernels using only PyTorch ops. |
create_sequence_feature_extractor |
|
derive_stems |
It is the caller's responsibility to ensure that all tensors are aligned and have the same shape. |
str_to_torch_dtype |
|
Audio
dataclass
Audio(data: _AudioTensorLike, sample_rate: SampleRate)
Bases: Generic[_AudioTensorLike]
Attributes:
| Name | Type | Description |
|---|---|---|
data |
_AudioTensorLike
|
This should either be an raw or a |
sample_rate |
SampleRate
|
|
data
instance-attribute
data: _AudioTensorLike
This should either be an raw or a normalized audio tensor.
NormalizationStats
dataclass
NormalizedAudio
dataclass
NormalizedAudio(
audio: Audio[NormalizedAudioTensor],
stats: NormalizationStats,
)
Container for normalized audio and its original stats.
Attributes:
| Name | Type | Description |
|---|---|---|
audio |
Audio[NormalizedAudioTensor]
|
|
stats |
NormalizationStats
|
|
normalize_audio
normalize_audio(
audio: Audio[RawAudioTensor],
) -> NormalizedAudio
Preprocess the raw audio in the time domain to have a mean of 0 and a std of 1 before passing it to the model.
Operates on the mean of the channels.
Source code in src/splifft/core.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
denormalize_audio
denormalize_audio(
audio_data: NormalizedAudioTensor,
stats: NormalizationStats,
) -> RawAudioTensor
Take the model output and restore them to their original loudness.
Source code in src/splifft/core.py
100 101 102 103 104 | |
generate_chunks
generate_chunks(
audio_data: RawAudioTensor | NormalizedAudioTensor,
chunk_size: ChunkSize,
hop_size: HopSize,
batch_size: BatchSize,
*,
padding_mode: PaddingMode = "reflect",
) -> Iterator[PaddedChunkedAudioTensor]
Generates batches of overlapping chunks from an audio tensor.
Returns:
| Type | Description |
|---|---|
Iterator[PaddedChunkedAudioTensor]
|
An iterator that yields batches of chunks of shape (B, C, chunk_T). |
Source code in src/splifft/core.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
stitch_chunks
stitch_chunks(
processed_chunks: Sequence[SeparatedChunkedTensor],
num_stems: NumModelStems,
chunk_size: ChunkSize,
hop_size: HopSize,
target_num_samples: Samples,
*,
window: WindowTensor,
) -> RawSeparatedTensor
Stitches processed audio chunks back together using the overlap-add method.
Reconstructs the full audio signal from a sequence of overlapping, processed chunks. Ensures that the sum of all overlapping windows is constant at every time step: \(\sum_{m=-\infty}^{\infty} w[n - mH] = C\) where \(H\) is the hop size.
Source code in src/splifft/core.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
aggregate_logits
aggregate_logits(
processed_chunks: Sequence[LogitsTensor],
starts: Sequence[int],
full_size: int,
chunk_size: int,
num_stems: int,
*,
trim_margin: int = 0,
overlap_mode: OverlapMode = "keep_first",
) -> LogitsTensor
Stitches time-series logits (split/aggregate strategy).
This is a 1:1 map of beat_this's aggregation behavior:
- trim trim_margin frames from each chunk side
- write into a full-size buffer
- in keep_first mode, process chunks in reverse so earlier chunks
overwrite later ones
Source code in src/splifft/core.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
aggregate_sequence_chunks
aggregate_sequence_chunks(
processed_chunks: Sequence[Tensor],
starts: Sequence[int],
full_size: int,
chunk_size: int,
*,
trim_margin: int = 0,
overlap_mode: OverlapMode = "keep_first",
) -> Tensor
Aggregate generic time-major chunk outputs.
Each processed_chunks[i] must have shape (chunk_time, ...) where ... can
contain any additional feature dimensions (for example bins for activations).
Source code in src/splifft/core.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
pad_dim
Pad an arbitrary tensor on a specific dimension.
This avoids relying on F.pad's reverse-dimension argument ordering.
Source code in src/splifft/core.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
split_sequence_tensor
split_sequence_tensor(
sequence: Tensor,
chunk_size: int,
*,
trim_margin: int = 0,
avoid_short_end: bool = True,
) -> tuple[list[Tensor], list[int]]
Split a time-major sequence tensor into overlapping chunks.
sequence must be shaped (time, ...), where ... can be any feature tail.
Source code in src/splifft/core.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
apply_mask
apply_mask(
spec_for_masking: ComplexSpectrogram,
mask_batch: ComplexSpectrogram,
mask_add_sub_dtype: dtype | None,
mask_out_dtype: dtype | None,
) -> SeparatedSpectrogramTensor
Applies a complex mask to a spectrogram.
While this can be simply replaced by a complex multiplication and torch.view_as_complex,
CoreML does not support it: https://github.com/apple/coremltools/issues/2003 so we handroll our
own.
Source code in src/splifft/core.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
get_model_floating_dtype
Infer floating input dtype from the model's first floating parameter.
Source code in src/splifft/core.py
415 416 417 418 419 420 | |
to_model_device
to_model_device(
tensor: Tensor,
*,
model_device: device,
model_floating_dtype: dtype | None,
) -> Tensor
Move tensor to model device while preserving model floating dtype compatibility.
Source code in src/splifft/core.py
423 424 425 426 427 428 429 430 431 432 | |
ModelWaveformToWaveform
ModelWaveformToWaveform(
model: Module,
preprocess: PreprocessFn,
postprocess: PostprocessFn,
*,
io_device: device,
model_device: device,
)
Bases: Module
Methods:
| Name | Description |
|---|---|
forward |
|
Attributes:
| Name | Type | Description |
|---|---|---|
model |
|
|
preprocess |
|
|
postprocess |
|
|
io_device |
|
|
model_device |
|
|
model_input_dtype |
|
Source code in src/splifft/core.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
model
instance-attribute
model = model
preprocess
instance-attribute
preprocess = preprocess
postprocess
instance-attribute
postprocess = postprocess
io_device
instance-attribute
io_device = io_device
model_device
instance-attribute
model_device = model_device
forward
forward(
waveform_chunk: RawAudioTensor | NormalizedAudioTensor,
) -> SeparatedChunkedTensor | LogitsTensor
Source code in src/splifft/core.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
create_w2w_model
create_w2w_model(
model: Module,
model_input_type: ModelInputType,
model_output_type: ModelOutputType,
stft_cfg: StftConfig | None,
num_channels: Channels,
chunk_size: ChunkSize,
masking_cfg: MaskingConfig,
*,
io_device: device,
model_device: device,
) -> ModelWaveformToWaveform
Source code in src/splifft/core.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | |
LogMelSpect
LogMelSpect(
sample_rate: int,
n_fft: int,
hop_length: int,
n_mels: int,
f_min: float = 0.0,
f_max: float | None = None,
mel_scale: str = "slaney",
normalized: bool | str = "frame_length",
power: float = 1.0,
log_multiplier: float = 1000.0,
)
Bases: Module
Computes the log-mel spectrogram of a waveform.
Methods:
| Name | Description |
|---|---|
forward |
:param x: Waveform tensor of shape (batch, channels, time) or (batch, time) |
Attributes:
| Name | Type | Description |
|---|---|---|
spect_class |
|
|
log_multiplier |
|
Source code in src/splifft/core.py
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
spect_class
instance-attribute
spect_class = MelSpectrogram(
sample_rate=sample_rate,
n_fft=n_fft,
hop_length=hop_length,
f_min=f_min,
f_max=f_max,
n_mels=n_mels,
mel_scale=mel_scale,
normalized=normalized,
power=power,
)
log_multiplier
instance-attribute
log_multiplier = log_multiplier
forward
forward(x: Tensor) -> LogMelSpectrogram
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Waveform tensor of shape (batch, channels, time) or (batch, time) |
required |
Returns:
| Type | Description |
|---|---|
LogMelSpectrogram
|
Log-Mel spectrogram of shape (batch, channels, n_mels, time) |
Source code in src/splifft/core.py
654 655 656 657 658 659 660 661 662 | |
to_log_magnitude
Convert complex or real spectrogram-like tensors to dB log-magnitude.
Source code in src/splifft/core.py
665 666 667 668 669 670 671 | |
create_cqt_kernels
create_cqt_kernels(
*,
Q: float,
fs: int,
fmin: float,
n_bins: int,
bins_per_octave: int,
norm: int = 1,
window: str = "hann",
fmax: float | None = None,
gamma: float = 0.0,
device: device,
dtype: dtype = float32,
) -> tuple[Tensor, int, Tensor, Tensor]
Create time-domain CQT kernels using only PyTorch ops.
This mirrors the nnAudio-style kernel generation used by PESTO but avoids
SciPy so splifft can keep a minimal dependency surface.
Source code in src/splifft/core.py
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | |
SequenceFeatureExtractor
Bases: Protocol
Protocol for sequence feature extractors.
Required contract:
- input: (B, C, T)
- output: (B, seq_len, feature_dim)
Methods:
| Name | Description |
|---|---|
__call__ |
|
Attributes:
| Name | Type | Description |
|---|---|---|
hop_length_samples |
int
|
|
stage_name |
str
|
|
IdentitySequenceFeatureExtractor
Bases: Module, SequenceFeatureExtractor
Methods:
| Name | Description |
|---|---|
forward |
|
Attributes:
| Name | Type | Description |
|---|---|---|
hop_length_samples |
|
|
stage_name |
|
hop_length_samples
class-attribute
instance-attribute
hop_length_samples = 1
stage_name
class-attribute
instance-attribute
stage_name = 'sequence_features'
forward
Source code in src/splifft/core.py
774 775 776 777 778 779 780 781 | |
LogMelSequenceFeatureExtractor
LogMelSequenceFeatureExtractor(
mel: LogMelSpect, *, hop_length_samples: int
)
Bases: Module, SequenceFeatureExtractor
Methods:
| Name | Description |
|---|---|
forward |
|
Attributes:
| Name | Type | Description |
|---|---|---|
stage_name |
|
|
mel |
|
|
hop_length_samples |
|
Source code in src/splifft/core.py
787 788 789 790 | |
stage_name
class-attribute
instance-attribute
stage_name = 'mel'
mel
instance-attribute
mel = mel
hop_length_samples
instance-attribute
hop_length_samples = hop_length_samples
forward
Source code in src/splifft/core.py
792 793 794 795 796 797 798 799 800 801 802 | |
CqtSequenceFeatureExtractor
CqtSequenceFeatureExtractor(
hcqt: HarmonicCQT,
*,
hop_length_samples: int,
log_epsilon: float,
)
Bases: Module, SequenceFeatureExtractor
Methods:
| Name | Description |
|---|---|
forward |
|
Attributes:
| Name | Type | Description |
|---|---|---|
stage_name |
|
|
hcqt |
|
|
hop_length_samples |
|
|
log_epsilon |
|
Source code in src/splifft/core.py
808 809 810 811 812 813 814 815 816 817 818 | |
stage_name
class-attribute
instance-attribute
stage_name = 'cqt'
hcqt
instance-attribute
hcqt = hcqt
hop_length_samples
instance-attribute
hop_length_samples = hop_length_samples
log_epsilon
instance-attribute
log_epsilon = log_epsilon
forward
Source code in src/splifft/core.py
820 821 822 823 824 825 826 827 828 829 830 831 | |
create_sequence_feature_extractor
create_sequence_feature_extractor(
feature_cfg: FeatureExtractionConfig | None,
*,
sample_rate: SampleRate,
device: device,
) -> SequenceFeatureExtractor
Source code in src/splifft/core.py
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 | |
CQT
CQT(
*,
sr: SampleRate,
hop_length: int,
fmin: float,
fmax: float | None,
n_bins: int,
bins_per_octave: int,
gamma: float,
center: bool,
window: str = "hann",
norm: int = 1,
)
Bases: Module
Constant-Q transform layer (complex output) implemented via Conv1d.
Methods:
| Name | Description |
|---|---|
forward |
|
Attributes:
| Name | Type | Description |
|---|---|---|
n_bins |
|
|
conv |
|
Source code in src/splifft/core.py
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 | |
n_bins
instance-attribute
n_bins = n_bins
conv
instance-attribute
conv = Conv1d(
1,
2 * n_bins,
kernel_size=kernel_width,
stride=hop_length,
padding=padding,
padding_mode="reflect",
bias=False,
)
forward
Source code in src/splifft/core.py
937 938 939 940 941 942 943 944 945 946 947 | |
HarmonicCQT
HarmonicCQT(
*,
harmonics: Sequence[int],
sr: SampleRate,
hop_length: int,
fmin: float,
fmax: float | None,
bins_per_semitone: int,
n_bins: int,
center_bins: bool,
gamma: float,
center: bool,
)
Bases: Module
Harmonic CQT computed by stacking one CQT per harmonic multiplier.
Methods:
| Name | Description |
|---|---|
forward |
|
Attributes:
| Name | Type | Description |
|---|---|---|
cqt_kernels |
|
Source code in src/splifft/core.py
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 | |
cqt_kernels
instance-attribute
cqt_kernels = ModuleList(
[
(
CQT(
sr=sr,
hop_length=hop_length,
fmin=h * fmin,
fmax=fmax,
n_bins=n_bins,
bins_per_octave=12 * bins_per_semitone,
gamma=gamma,
center=center,
)
)
for h in harmonics
]
)
derive_stems
derive_stems(
separated_stems: Mapping[
ModelOutputStemName, RawAudioTensor
],
mixture_input: RawAudioTensor,
stem_rules: DerivedStemsConfig,
) -> dict[StemName, RawAudioTensor]
It is the caller's responsibility to ensure that all tensors are aligned and have the same shape.
Note
Mixture input and separated stems must first be denormalized.
Source code in src/splifft/core.py
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 | |
str_to_torch_dtype
Source code in src/splifft/core.py
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 | |