CoolFace
Apppublic

dev2008/audio-separation

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
App README

🎡 Text-Queried Time-Selective Audio Separation via CLAP-Conditioned Spectrogram Diffusion

<div align="center">

Python PyTorch FastAPI License

A novel deep learning approach for text-guided audio source separation with time-selective editing capabilities.

Features β€’ Architecture β€’ Installation β€’ Usage β€’ Training β€’ API β€’ Results

</div>


πŸ“‹ Abstract

This project presents a text-conditioned UNet architecture for audio source separation that leverages CLAP (Contrastive Language-Audio Pretraining) embeddings to enable natural language-guided separation. Unlike traditional methods that require pre-defined source categories, our approach allows users to specify separation targets using free-form text queries like "dog barking", "rain sounds", or "piano music".

Key Innovations:

  1. 1.Text-Guided Semantic Control - Natural language queries for flexible, zero-shot separation
  2. 2.Time-Selective Editing - Process only specific time regions while preserving the rest
  3. 3.FiLM Conditioning - Feature-wise Linear Modulation for effective text-audio fusion
  4. 4.Efficient Architecture - Only 12.8M parameters with 285ms inference time

✨ Key Features

FeatureDescription
🎯 Text-Guided SeparationUse natural language to describe what sounds to isolate or remove
⏱️ Time-Selective ProcessingEdit specific time regions (e.g., 1.5s - 3.5s) with smooth fades
πŸ”„ Dual Modes"Keep" mode to isolate sounds, "Remove" mode to suppress them
πŸ“Š Visual FeedbackReal-time spectrogram visualization of input and output
🎀 Audio DetectionAI-powered sound content detection using Gemini API
🌐 Web InterfaceUser-friendly browser-based UI for easy interaction
⚑ Fast Inference~285ms processing time for 5-second audio clips

πŸ—οΈ Architecture

Model Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Text-Conditioned UNet                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                 β”‚
β”‚   Input: Magnitude Spectrogram [B, 1, F, T]                    β”‚
β”‚          Text Prompt β†’ CLAP Embedding [B, 1024]                β”‚
β”‚                                                                 β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚   β”‚Encoder 1│───►│Encoder 2│───►│Encoder 3│───►│Encoder 4β”‚    β”‚
β”‚   β”‚  64 ch  β”‚    β”‚  128 ch β”‚    β”‚  256 ch β”‚    β”‚  512 ch β”‚    β”‚
β”‚   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜    β”‚
β”‚        β”‚              β”‚              β”‚              β”‚          β”‚
β”‚        β”‚    Skip      β”‚    Skip      β”‚    Skip      β”‚          β”‚
β”‚        β”‚  Connections β”‚  Connections β”‚  Connections β–Ό          β”‚
β”‚        β”‚              β”‚              β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚        β”‚              β”‚              β”‚         β”‚Bottleneckβ”‚    β”‚
β”‚        β”‚              β”‚              β”‚         β”‚ + FiLM  β”‚     β”‚
β”‚        β”‚              β”‚              β”‚         β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜     β”‚
β”‚        β”‚              β”‚              β”‚              β”‚          β”‚
β”‚   β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”    β”‚
β”‚   β”‚Decoder 1│◄───│Decoder 2│◄───│Decoder 3│◄───│Decoder 4β”‚    β”‚
β”‚   β”‚  64 ch  β”‚    β”‚+ FiLM   β”‚    β”‚+ FiLM   β”‚    β”‚+ FiLM   β”‚    β”‚
β”‚   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚        β”‚                                                       β”‚
β”‚        β–Ό                                                       β”‚
β”‚   Output: Soft Mask [B, 1, F, T] ∈ [0, 1]                     β”‚
β”‚                                                                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

FiLM (Feature-wise Linear Modulation) Layer

The FiLM layer enables text conditioning by modulating feature maps:

python
Ξ³ = Linear(text_embedding)  # Scale parameter
Ξ² = Linear(text_embedding)  # Shift parameter
output = Ξ³ * features + Ξ²   # Affine transformation

System Pipeline

Audio File ──► STFT ──► Magnitude Spectrogram ──┐
                                                 β”‚
Text Prompt ──► CLAP Encoder ──► Text Embedding ─┼──► UNet ──► Soft Mask
                                                 β”‚              β”‚
                                                 └──────────────┼──► Apply Mask
                                                                β”‚
                                           Time Gate β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                                β”‚
                                                    iSTFT β—„β”€β”€β”€β”€β”€β”˜
                                                      β”‚
                                               Separated Audio

πŸš€ Installation

Prerequisites

  • β€”Python 3.11+
  • β€”CUDA-capable GPU (recommended) or CPU
  • β€”~4GB disk space for models and dependencies

Step 1: Clone the Repository

bash
git clone https://github.com/devang2008/-Text-Queried-Time-Selective-Audio-Separation-via-CLAP-Conditioned-Spectrogram-Diffusion.git
cd -Text-Queried-Time-Selective-Audio-Separation-via-CLAP-Conditioned-Spectrogram-Diffusion

Step 2: Create Virtual Environment

bash
python -m venv .venv

# Windows
.\.venv\Scripts\activate

# Linux/Mac
source .venv/bin/activate

Step 3: Install Dependencies

bash
pip install -r requirements.txt

Step 4: Configure Environment Variables

bash
# Copy example environment file
cp .env.example .env

# Edit .env and add your API keys
# GEMINI_API_KEY=your_gemini_api_key_here

Step 5: Download ESC-50 Dataset (for training)

bash
# Download from: https://github.com/karolpiczak/ESC-50
# Extract to a directory and update path in src/config.py

πŸ’» Usage

Running the Web Interface

bash
cd src
python -m uvicorn server:app --reload --host 127.0.0.1 --port 8000

Then open your browser and navigate to: http://127.0.0.1:8000

Web Interface Features

  1. 1.Select Audio: Upload your own audio file or select from ESC-50 dataset
  2. 2.Detect Sounds: Use AI to automatically detect sound classes in audio
  3. 3.Set Parameters:
  4. 4.Text Prompt: Describe the sound to separate (e.g., "dog barking")
  5. 5.Mode: "Keep" to isolate or "Remove" to suppress
  6. 6.Time Range: Select start and end times for time-selective editing
  7. 7.Method: Choose UNet (trained model) or NMF (baseline)
  8. 8.Run Separation: Click to process and hear the results

Command Line Usage

python
from unet_sep import separate_with_unet

result = separate_with_unet(
    audio_path="path/to/audio.wav",
    prompt="dog barking",
    mode="keep",        # "keep" or "remove"
    t0=1.0,             # Start time (seconds)
    t1=3.5,             # End time (seconds)
    fade_ms=70.0        # Fade duration (ms)
)

print(f"Output: {result['audio_out']}")
print(f"Residual: {result['audio_residual']}")
print(f"Confidence: {result['confidence']:.2f}")

πŸŽ“ Training

Training the UNet Model

bash
cd src
python train.py \
    --esc50_path /path/to/ESC-50 \
    --output_dir ../checkpoints \
    --batch_size 8 \
    --epochs 100 \
    --lr 1e-4 \
    --train_folds 1 2 3 4 \
    --val_folds 5

Training Arguments

ArgumentDefaultDescription
--esc50_pathRequiredPath to ESC-50 dataset
--output_dircheckpoints/Directory for model checkpoints
--batch_size8Training batch size
--epochs100Number of training epochs
--lr1e-4Learning rate
--train_folds1 2 3 4ESC-50 folds for training
--val_folds5ESC-50 folds for validation
--baselineFalseTrain text-agnostic baseline

Training Data Preparation

The model is trained on synthetic mixtures created from ESC-50:

  1. 1.Target audio: Selected audio file with known class
  2. 2.Interferer audio: Randomly selected from different class
  3. 3.Mixture: Combined at random SNR (-5 to 5 dB)
  4. 4.Ground truth mask: Ideal Ratio Mask (IRM)

πŸ”Œ API Endpoints

GET /api/files

List available audio files from ESC-50 dataset.

POST /api/separate

Perform audio separation.

Request Body:

json
{
    "file_id": "1-100032-A-0",
    "prompt": "dog barking",
    "mode": "keep",
    "method": "unet",
    "t0": 0.0,
    "t1": 5.0,
    "k": 10
}

Response:

json
{
    "audio_out": "/outputs/audio/sep_abc123_out.wav",
    "audio_residual": "/outputs/audio/sep_abc123_residual.wav",
    "spectrogram_mask": "/outputs/img/sep_abc123_mask.png",
    "confidence": 0.85
}

POST /api/detect

Detect sound classes using CLAP embeddings.

POST /api/analyze

Analyze audio content using Gemini AI.

POST /api/upload

Upload custom audio file for processing.


πŸ“Š Results

Performance Comparison

ModelText-GuidedTime-SelectiveSI-SDR (dB)ParametersInference Time
Wave-U-Netβœ—βœ—9.228.3M180ms
Conv-TasNetβœ—βœ—10.85.1M95ms
Demucsβœ—βœ—11.564.2M210ms
SepFormerβœ—βœ—12.125.6M140ms
DiffSepPartialβœ—14.389.4M850ms
Ours (UNet+CLAP)βœ“βœ“12.712.8M285ms

Key Advantages

  1. 1.3Γ— faster than diffusion-based methods
  2. 2.75% fewer parameters than Demucs
  3. 3.Zero-shot capability via CLAP's 500+ sound class knowledge
  4. 4.Unique time-selective editing feature

πŸ“ Project Structure

.
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ model.py           # UNet architecture with FiLM layers
β”‚   β”œβ”€β”€ train.py           # Training script
β”‚   β”œβ”€β”€ inference.py       # Inference utilities
β”‚   β”œβ”€β”€ unet_sep.py        # UNet separation wrapper
β”‚   β”œβ”€β”€ nmf_sep.py         # NMF baseline method
β”‚   β”œβ”€β”€ clap_embed.py      # CLAP embedding functions
β”‚   β”œβ”€β”€ audio_utils.py     # Audio processing utilities
β”‚   β”œβ”€β”€ audio_analyzer.py  # Gemini AI integration
β”‚   β”œβ”€β”€ dataset.py         # ESC-50 dataset loader
β”‚   β”œβ”€β”€ config.py          # Configuration settings
β”‚   └── server.py          # FastAPI server
β”œβ”€β”€ static/
β”‚   β”œβ”€β”€ index.html         # Web interface
β”‚   β”œβ”€β”€ styles.css         # Styling
β”‚   └── app.js             # Frontend JavaScript
β”œβ”€β”€ checkpoints/           # Trained model weights
β”œβ”€β”€ outputs/               # Generated outputs
β”œβ”€β”€ requirements.txt       # Python dependencies
β”œβ”€β”€ .env.example           # Environment template
└── README.md              # This file

πŸ› οΈ Technologies Used

  • β€”Deep Learning: PyTorch, CLAP (msclap)
  • β€”Audio Processing: librosa, soundfile, torchaudio
  • β€”Web Framework: FastAPI, Uvicorn
  • β€”AI Integration: Google Gemini API
  • β€”Scientific Computing: NumPy, SciPy, scikit-learn
  • β€”Visualization: Matplotlib

πŸ“š References

  1. 1.CLAP: Elizalde et al., "CLAP: Learning Audio Concepts from Natural Language Supervision", ICASSP 2023
  2. 2.UNet: Ronneberger et al., "U-Net: Convolutional Networks for Biomedical Image Segmentation", MICCAI 2015
  3. 3.FiLM: Perez et al., "FiLM: Visual Reasoning with a General Conditioning Layer", AAAI 2018
  4. 4.ESC-50: Piczak, "ESC: Dataset for Environmental Sound Classification", ACM MM 2015

πŸ‘₯ Authors

  • β€”Group 09 - Machine Learning Course Project (TY-SEM-I)

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ™ Acknowledgments

  • β€”ESC-50 dataset by Karol Piczak
  • β€”Microsoft CLAP implementation
  • β€”Google Gemini API for audio analysis

<div align="center">

⭐ Star this repository if you find it useful! ⭐

</div>