Modelli IA preaddestrati

Usare CLAP, ricostruzione con RAVE e generazione audio.

In questa sezione

  • prepare_clap_embeddings — Compute and save the CLAP embeddings of the demo sounds and phrases.
  • download_model — Download the model .ts file from Hugging Face, when not already there.
  • mean_mfcc — Mean MFCC vector of a signal, used as a compact timbre descriptor.
  • reconstruction_metrics — Compare two signals, taking into account the causal delay of the model.
  • generate_materials — Regenerate the materials (reconstructions, morphing, out of domain, metadata).
  • load_materials — Read the precomputed materials of the lab.
  • generate — Generate the three clips with AudioLDM-S and write them to folder.
  • load_samples — Read the precomputed clips with their provenance.

Modulo embeddings

prepare_clap_embeddings

al.prepare_clap_embeddings(output_dir=None)

Compute and save the CLAP embeddings of the demo sounds and phrases.

Writes embedding.npz (keys “audio” and “testo”) and provenienza.json in the output folder.

Parametri

Parametro Tipo Valore predefinito Descrizione
output_dir str or pathlib.Path or None None Destination folder (default: lab-2026/materiali/05-clap).

Risultato

  • pathlib.Path: The folder written.

Esempio

al.prepare_clap_embeddings()

Modulo vae

download_model

al.download_model(destination=None)

Download the model .ts file from Hugging Face, when not already there.

Returns the local path. Uses the commit pinned in REVISION.

Parametri

Parametro Tipo Valore predefinito Descrizione
destination str or pathlib.Path or None None Where to save the file (default: MODEL_DIR / MODEL_FILE).

Risultato

  • pathlib.Path: The local path of the model file.

Esempio

al.download_model()

mean_mfcc

al.mean_mfcc(y, sr=48000)

Mean MFCC vector of a signal, used as a compact timbre descriptor.

Parametri

Parametro Tipo Valore predefinito Descrizione
y numpy.ndarray Obbligatorio Mono audio samples.
sr int 48000 Sampling rate in Hz (48000 for the RAVE materials).

Risultato

  • numpy.ndarray: Vector of 13 MFCCs, averaged over time with a hop of 512 samples.

Esempio

y, sr = al.load_audio(al.AUDIO_DIR / "speech1.wav", sr=48000)
descriptor = al.mean_mfcc(y, sr)
descriptor.shape
# (13,)

reconstruction_metrics

al.reconstruction_metrics(reference, reconstruction, sr=48000)

Compare two signals, taking into account the causal delay of the model.

The signals are truncated to the shorter length, centred and aligned using the lag at the largest absolute cross-correlation.

Parametri

Parametro Tipo Valore predefinito Descrizione
reference numpy.ndarray Obbligatorio Original mono signal.
reconstruction numpy.ndarray Obbligatorio Reconstructed mono signal, at the same sampling rate.
sr int 48000 Sampling rate of both signals in Hz (48000 for the RAVE materials).

Risultato

  • dict: “ritardo”: estimated lag in samples (positive when the reconstruction is delayed); “inviluppo”: correlation of RMS envelopes in blocks of 512 samples after alignment; “mel”: cosine similarity between centred log-Mel spectrograms with 80 bands after alignment.

Note

Use signals long enough to compare several RMS blocks. Envelope correlation can be NaN for constant envelopes or too few blocks.

Esempio

metrics = al.reconstruction_metrics(originale, ricostruita, sr=48000)
print(metrics["ritardo"], metrics["inviluppo"], metrics["mel"])

generate_materials

al.generate_materials(folder=None)

Regenerate the materials (reconstructions, morphing, out of domain, metadata).

Requires torch. Writes to lab-2026/materiali/05-vae unless told otherwise; downloads the model only when missing. Returns the folder written.

Parametri

Parametro Tipo Valore predefinito Descrizione
folder str or pathlib.Path or None None Destination folder.

Risultato

  • pathlib.Path: The folder written.

Esempio

al.vae.generate_materials()

load_materials

al.load_materials(folder=None)

Read the precomputed materials of the lab.

Parametri

Parametro Tipo Valore predefinito Descrizione
folder str or pathlib.Path or None None The folder written by generate_materials().

Risultato

  • dict: With “voce1”, “voce2”, “ricostruite” (list), “morph” (list of (alpha, signal) pairs), “latenti” (dict of arrays), “fuori_dominio” (dict with “originale” and “ricostruito”), “sr”, “alpha” and “meta”.

Esempio

materials = al.vae.load_materials()
len(materials["morph"])
# 9

Modulo generation

generate

al.generate(folder, device='cpu', steps=25)

Generate the three clips with AudioLDM-S and write them to folder.

A new folder is required, so previous results are never overwritten. Each clip comes with a JSON file recording model revision, parameters and package versions.

Parametri

Parametro Tipo Valore predefinito Descrizione
folder str or pathlib.Path Obbligatorio Destination folder (must not exist).
device str 'cpu' “cpu”, “cuda” or “mps”.
steps int 25 Number of inference steps.

Risultato

  • pathlib.Path: The folder written.

Esempio

al.generate("tmp/generazione-01", device="cpu", steps=25)

load_samples

al.load_samples(folder=al.generation.OFFLINE_DIR)

Read the precomputed clips with their provenance.

Parametri

Parametro Tipo Valore predefinito Descrizione
folder str or pathlib.Path al.generation.OFFLINE_DIR The folder written by generate().

Risultato

  • list of (str, numpy.ndarray, int, dict): For each clip: name, signal, sampling rate and metadata.

Esempio

for name, y, sr, meta in al.load_samples():
    print(name, round(len(y) / sr, 2), "s")