Skip to content

Sam audio/isolatingvocals#76

Open
Dhyan761 wants to merge 2 commits intofacebookresearch:mainfrom
Dhyan761:sam-audio/isolatingvocals
Open

Sam audio/isolatingvocals#76
Dhyan761 wants to merge 2 commits intofacebookresearch:mainfrom
Dhyan761:sam-audio/isolatingvocals

Conversation

@Dhyan761
Copy link
Copy Markdown

@Dhyan761 Dhyan761 commented Jan 9, 2026

No description provided.

@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Jan 9, 2026

Hi @Dhyan761!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@jjmlovesgit
Copy link
Copy Markdown

Been doing some of kind of thing as well so sharing my lessons learned-- Implementation: Concatenation vs. Accumulation

When creating a list of tensors and using torch.cat iteratively will grow the output. This is "Additive Concatenation"—the output gets longer with every loop. Possible out of memory on long runs if GPU memory is tight.

Alternative: Weighted Accumulation. We pre-allocate a master buffer (t_out = np.zeros_like(audio)) and "sum" the chunks into it. This is much more memory-efficient for very long runs (e.g., a 10-minute track) because we don't have to keep re-copying the entire tensor in memory. We have one copy of the audio vs list.

Good stuff... trying to add value with the comments and code...

#code
import torch
import numpy as np
def repeat_via_mapping(wav: torch.Tensor, sr: int, target_duration_sec: float, crossfade_sec: float = 0.05):
"""
Forensic Looping: Uses a single clone and a weight map instead of multiple copies.
"""

 # 1. THE SINGLE CONE (Pre-allocate the full output length)
target_samples = int(target_duration_sec * sr)
# Pre-allocating prevents 'Growing Tensor' OOM errors
out_tensor = torch.zeros((wav.size(0), target_samples), device=wav.device)
weight_map = torch.zeros(target_samples, device=wav.device)

# 2. THE SINGLE CLONE (The source material)
# We only keep this one reference in memory
source_seg = wav 
seg_len = source_seg.size(-1)
xfade_samples = int(crossfade_sec * sr)
stride = seg_len - xfade_samples # The 'Map' step size

# Ramps for the Map
ramp_in = torch.linspace(0, 1, xfade_samples, device=wav.device)
ramp_out = torch.linspace(1, 0, xfade_samples, device=wav.device)

# 3. THE MAPPING PROCESS
# We slide the 'Map' across the 'Cone', depositing the 'Clone' as we go.
curr_pos = 0
while curr_pos < target_samples:
    # Determine how much of the segment fits in the remaining space
    end_pos = min(curr_pos + seg_len, target_samples)
    actual_len = end_pos - curr_pos
    
    # Calculate localized weights for this specific 'Map' entry
    w = torch.ones(actual_len, device=wav.device)
    if curr_pos > 0:
        w[:xfade_samples] *= ramp_in
    if end_pos < target_samples - 100:
        w[-xfade_samples:] *= ramp_out
        
    # Add the Clone into the Cone based on the Map
    out_tensor[:, curr_pos:end_pos] += source_seg[:, :actual_len] * w
    weight_map[curr_pos:end_pos] += w
    
    curr_pos += stride
    if actual_len < seg_len: break

# 4. NORMALIZATION (The Join)
# This ensures energy conservation across the crossfades
return out_tensor / (weight_map + 1e-9)

Summary of Value:
Memory Efficiency: Unlike the loop_segment_with_crossfade snippet that creates a list of clones ([seg.clone() for _ in range(needed)]), this method only ever holds the source and the destination.Avoids torch.cat

Fragmentation: Repeatedly calling torch.cat forces the system to find new, larger contiguous memory blocks, which is the #1 cause of "Out of Memory" crashes on Windows.

Consistent Volume: By using the weight_map to normalize, you guarantee that the volume in the crossfade regions is exactly $1.0$ ($0.7 + 0.3 = 1.0$), rather than the fluctuating levels often seen in basic concatenation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants