Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Facebookresearch Audiocraft ChromaCosineSimilarity

From Leeroopedia
Knowledge Sources
Domains Audio_Metrics, Music_Generation
Last Updated 2026-02-14 01:00 GMT

Overview

Concrete tool for measuring the cosine similarity between chromagrams extracted from generated and reference audio.

Description

ChromaCosineSimilarityMetric is a torchmetrics-based metric that extracts chromagram features from audio pairs and computes their frame-level cosine similarity. It evaluates how well a melody-conditioned generation preserves the pitch content of the reference.

Usage

Import this metric when evaluating melody-conditioned music generation models (e.g., MusicGen Melody) to assess pitch preservation.

Code Reference

Source Location

Signature

class ChromaCosineSimilarityMetric(torchmetrics.Metric):
    def __init__(self, sample_rate: int, n_chroma: int, radix2_exp: int, argmax: bool, eps: float = 1e-8): ...
    def update(self, preds: torch.Tensor, targets: torch.Tensor, sizes: torch.Tensor, sample_rates: torch.Tensor): ...
    def compute(self) -> float: ...

Import

from audiocraft.metrics.chroma_cosinesim import ChromaCosineSimilarityMetric

I/O Contract

Inputs

Name Type Required Description
preds torch.Tensor Yes Generated audio [B, C, T]
targets torch.Tensor Yes Reference audio [B, C, T]
sizes torch.Tensor Yes Actual lengths per sample [B]
sample_rates torch.Tensor Yes Sample rates [B]

Outputs

Name Type Description
similarity float Mean chroma cosine similarity

Usage Examples

from audiocraft.metrics.chroma_cosinesim import ChromaCosineSimilarityMetric

metric = ChromaCosineSimilarityMetric(sample_rate=32000, n_chroma=12, radix2_exp=14, argmax=True)
metric.update(generated_wav, reference_wav, sizes, sample_rates)
score = metric.compute()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment