dev2008/audio-separation
π΅ Text-Queried Time-Selective Audio Separation via CLAP-Conditioned Spectrogram Diffusion
<div align="center">
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:
- Text-Guided Semantic Control - Natural language queries for flexible, zero-shot separation
- Time-Selective Editing - Process only specific time regions while preserving the rest
- FiLM Conditioning - Feature-wise Linear Modulation for effective text-audio fusion
- Efficient Architecture - Only 12.8M parameters with 285ms inference time
β¨ Key Features
ποΈ 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:
Ξ³ = Linear(text_embedding) # Scale parameter
Ξ² = Linear(text_embedding) # Shift parameter
output = Ξ³ * features + Ξ² # Affine transformationSystem 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
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-DiffusionStep 2: Create Virtual Environment
python -m venv .venv
# Windows
.\.venv\Scripts\activate
# Linux/Mac
source .venv/bin/activateStep 3: Install Dependencies
pip install -r requirements.txtStep 4: Configure Environment Variables
# Copy example environment file
cp .env.example .env
# Edit .env and add your API keys
# GEMINI_API_KEY=your_gemini_api_key_hereStep 5: Download ESC-50 Dataset (for training)
# Download from: https://github.com/karolpiczak/ESC-50
# Extract to a directory and update path in src/config.pyπ» Usage
Running the Web Interface
cd src
python -m uvicorn server:app --reload --host 127.0.0.1 --port 8000Then open your browser and navigate to: http://127.0.0.1:8000
Web Interface Features
- Select Audio: Upload your own audio file or select from ESC-50 dataset
- Detect Sounds: Use AI to automatically detect sound classes in audio
- Set Parameters:
- Text Prompt: Describe the sound to separate (e.g., "dog barking")
- Mode: "Keep" to isolate or "Remove" to suppress
- Time Range: Select start and end times for time-selective editing
- Method: Choose UNet (trained model) or NMF (baseline)
- Run Separation: Click to process and hear the results
Command Line Usage
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
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 5Training Arguments
Training Data Preparation
The model is trained on synthetic mixtures created from ESC-50:
- Target audio: Selected audio file with known class
- Interferer audio: Randomly selected from different class
- Mixture: Combined at random SNR (-5 to 5 dB)
- 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:
{
"file_id": "1-100032-A-0",
"prompt": "dog barking",
"mode": "keep",
"method": "unet",
"t0": 0.0,
"t1": 5.0,
"k": 10
}Response:
{
"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
Key Advantages
- 3Γ faster than diffusion-based methods
- 75% fewer parameters than Demucs
- Zero-shot capability via CLAP's 500+ sound class knowledge
- 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
- CLAP: Elizalde et al., "CLAP: Learning Audio Concepts from Natural Language Supervision", ICASSP 2023
- UNet: Ronneberger et al., "U-Net: Convolutional Networks for Biomedical Image Segmentation", MICCAI 2015
- FiLM: Perez et al., "FiLM: Visual Reasoning with a General Conditioning Layer", AAAI 2018
- 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>
