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. |
StreamingStitcher |
Incremental overlap-add for chunked model outputs. |
SequenceChunkSplit |
|
ModelWaveformToWaveform |
|
LogMelSpect |
Computes the log-mel spectrogram of a waveform. |
SequenceFeatureExtractor |
Protocol for sequence feature extractors. |
IdentitySequenceFeatureExtractor |
|
LogMelSequenceFeatureExtractor |
|
HcqtSequenceFeatureExtractor |
|
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. |
to_log_power |
Convert complex or real spectrogram-like tensors to dB log-power. |
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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
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
102 103 104 105 106 | |
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.
Note that reflect padding requires pad size to be strictly less than the audio dimension size.
Use constant padding mode otherwise.
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
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 142 143 144 145 146 | |
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.
Warning
This function materializes all chunks in memory simultaneously before overlap-add,
which scales poorly with track length and stem count, often leading to OOM.
Prefer using StreamingStitcher instead,
which performs overlap-add incrementally.
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.
Assumptions:
processed_chunksis non-empty.- Every batch has shape
(batch, num_stems, channels, chunk_size). - Batches are already ordered in the same temporal order used during chunking.
- All batches share the same channel count and dtype.
windowhas lengthchunk_sizeand matches the overlap-add window used during chunking.
Source code in src/splifft/core.py
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
StreamingStitcher
StreamingStitcher(
chunk_size: ChunkSize,
hop_size: HopSize,
target_num_samples: Samples,
window: WindowTensor,
)
Incremental overlap-add for chunked model outputs.
Classes:
| Name | Description |
|---|---|
Buffers |
|
Methods:
| Name | Description |
|---|---|
step |
Accumulate one |
flush |
Emit the trailing valid tail after the final chunk. |
Attributes:
| Name | Type | Description |
|---|---|---|
chunk_size |
|
|
hop_size |
|
|
target_num_samples |
|
|
window |
|
|
tail_size |
|
|
samples_to_skip |
|
|
samples_to_keep |
|
|
buffers |
Buffers | None
|
|
Source code in src/splifft/core.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
Buffers
dataclass
Attributes:
| Name | Type | Description |
|---|---|---|
buffer |
Tensor
|
|
norm_buffer |
Tensor
|
|
shift_buffer |
Tensor
|
|
shift_norm_buffer |
Tensor
|
|
chunk_size
instance-attribute
chunk_size = chunk_size
hop_size
instance-attribute
hop_size = hop_size
target_num_samples
instance-attribute
target_num_samples = target_num_samples
window
instance-attribute
window = window
tail_size
instance-attribute
tail_size = chunk_size - hop_size
samples_to_skip
instance-attribute
samples_to_skip = tail_size
samples_to_keep
instance-attribute
samples_to_keep = target_num_samples
step
Accumulate one (stems, channels, chunk_size) chunk.
Source code in src/splifft/core.py
244 245 246 247 248 249 250 251 252 253 254 255 256 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 | |
flush
flush() -> Tensor | None
Emit the trailing valid tail after the final chunk.
Source code in src/splifft/core.py
296 297 298 299 300 301 302 303 | |
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
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 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 | |
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
379 380 381 382 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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | |
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
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 | |
SequenceChunkSplit
split_sequence_tensor
split_sequence_tensor(
sequence: Tensor,
chunk_size: int,
*,
trim_margin: int = 0,
avoid_short_end: bool = True,
) -> SequenceChunkSplit
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
474 475 476 477 478 479 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 | |
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
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 | |
get_model_floating_dtype
Infer floating input dtype from the model's first floating parameter.
Source code in src/splifft/core.py
542 543 544 545 546 547 | |
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
550 551 552 553 554 555 556 557 558 559 | |
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
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 | |
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
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | |
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
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 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 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 | |
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: Literal["htk", "slaney"] = "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
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 | |
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=cast(Any, 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
786 787 788 789 790 791 792 793 794 | |
to_log_magnitude
Convert complex or real spectrogram-like tensors to dB log-magnitude.
Source code in src/splifft/core.py
797 798 799 800 801 802 803 | |
to_log_power
Convert complex or real spectrogram-like tensors to dB log-power.
Source code in src/splifft/core.py
806 807 808 809 810 811 812 | |
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
843 844 845 846 847 848 849 850 | |
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
856 857 858 859 | |
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
861 862 863 864 865 866 867 868 869 870 | |
HcqtSequenceFeatureExtractor
HcqtSequenceFeatureExtractor(
hcqt: Module,
*,
hop_length_samples: HopSize,
log_epsilon: Gt0[float],
power_epsilon: Ge0[float] | None,
)
Bases: Module, SequenceFeatureExtractor
Methods:
| Name | Description |
|---|---|
forward |
|
Attributes:
| Name | Type | Description |
|---|---|---|
stage_name |
|
|
hcqt |
|
|
hop_length_samples |
|
|
log_epsilon |
|
|
power_epsilon |
|
Source code in src/splifft/core.py
876 877 878 879 880 881 882 883 884 885 886 887 888 | |
stage_name
class-attribute
instance-attribute
stage_name = 'hcqt'
hcqt
instance-attribute
hcqt = hcqt
hop_length_samples
instance-attribute
hop_length_samples = hop_length_samples
log_epsilon
instance-attribute
log_epsilon = log_epsilon
power_epsilon
instance-attribute
power_epsilon = power_epsilon
forward
Source code in src/splifft/core.py
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 | |
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
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 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 | |
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
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 | |
str_to_torch_dtype
Source code in src/splifft/core.py
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 | |