Lunar Boom Learning

2.2 · Collecting and Cleaning Audio

Section 2.2 of 2.5

Collecting and Cleaning Audio

Turning mixed source files into reliable model inputs

Learn how to validate, resample, level, trim, segment, and deduplicate audio without removing the musical information a model needs.

About 12 minutes

Guiding question

Which properties should be standardised before training?

By the end, you’ll be able to
  • Explain why audio datasets use consistent sample rates, channel layouts, formats, and segment lengths.
  • Identify practical checks for invalid files, clipping, silence, loudness, and poor segment boundaries.
  • Explain how exact duplicates and near-duplicates can create memorisation and evaluation leakage.

Audio collected from different sources rarely arrives in one neat format. One track may be stereo at 44.1 kHz, another mono at 48 kHz, and another stored in a damaged container. Some files begin with long silence. Others are clipped, unusually quiet, repeated under different names, or several hours longer than the model can process at once.

Cleaning turns this mixed collection into reliable training examples. The goal is not to make every recording sound identical. The goal is to make the files technically compatible, remove records that cannot be trusted, and keep useful musical differences intact.

A strong workflow also keeps the original files. Cleaning should create traceable derivatives rather than silently replacing the source material.

A practical audio preparation pipeline

  1. Keep the original

    Store the source file and its provenance before creating any processed version.

  2. Inspect and decode

    Check that the container and audio stream can be read, then record duration, codec, sample rate, channels, and other technical properties.

  3. Standardise the signal

    Convert the audio to the sample rate, channel layout, and numerical format expected by the model.

  4. Measure quality

    Check loudness, true peak, silence, clipping, duration, and unusual values before deciding whether to change or reject the record.

  5. Segment the audio

    Create examples that fit the model's context while keeping captions, lyrics, MIDI, and timestamps aligned.

  6. Find duplicates

    Detect exact and near-identical recordings before assigning records to dataset splits.

  7. Save the processing history

    Record every change so the prepared example can be traced back to the original.

1. Validate before converting

A file extension does not prove that a file contains valid audio. A file named .wav may be incomplete, use an unsupported codec, or contain no usable audio stream.

A validation step should inspect the file before expensive processing begins. Useful checks include:

    1. Can the container be opened?
    2. Is there at least one usable audio stream?
    3. Is the codec supported by the pipeline?
    4. Are sample rate, channel count, and duration present and sensible?
    5. Can the audio be decoded without a fatal error?
    6. Does the decoded signal contain finite values rather than NaN or infinity?

FFprobe can report stream and format information in a machine-readable form. The FFmpeg documentation also provides a stream selector for streams with usable configuration, which requires essential information such as the audio sample rate.

inspect-audio.sh · A simple FFprobe command for inspecting the first audio stream.

Swipe sideways to view code

ffprobe -v error \
  -select_streams a:0 \
  -show_entries stream=codec_name,sample_rate,channels,channel_layout,duration \
  -show_entries format=format_name,duration,size \
  -of json input.wav

2. Resample to the model's expected rate

Models normally expect one sample rate. If the model was designed for 32 kHz audio, feeding it a mixture of 22.05, 44.1, and 48 kHz files would give the same number of samples different meanings across records.

Resampling converts the signal from its original sample rate to the target rate. It does not simply discard samples at regular intervals. A proper resampler filters and interpolates the signal so frequencies above the new Nyquist limit do not fold back into the audible range as aliasing.

PyTorch's official audio tutorial provides bandlimited resampling functions and lets developers control filter width and roll-off. The same target rate should be used consistently during training and inference.

3. Standardise the channel layout

Mono and stereo examples have different shapes. A model must either support both layouts or receive a consistent conversion.

Downmixing stereo to mono reduces the amount of audio data and removes the need to model left-right placement. It also removes stereo width and can change the balance of sounds that differ between the channels. Converting mono to stereo by copying one channel does not create real spatial information.

The choice should match the model. MusicGen's main training setup downmixed audio to mono unless a stereo experiment was being run. Stable Audio Open was built for stereo output. Keep the original channel layout in metadata even when the training derivative uses a different one.

What should be standardised?

Swipe sideways to view the full comparison

PropertyWhy consistency helpsWhat may be lost or changed
Sample rateEach sample has the same time scale and frequency limitVery high frequencies may be removed when downsampling
Channel layoutBatches have a consistent shapeDownmixing can remove stereo placement
Numerical typeThe model receives a predictable value range and precisionPoor conversion can clip or quantize the signal
Segment lengthExamples fit the model context and batchBad boundaries can cut phrases, words, or labels
Loudness policyExtreme level differences can be controlledAggressive normalisation can remove useful level variation

4. Measure loudness before changing it

Amplitude peaks and perceived loudness are not the same thing. ITU-R BS.1770 defines algorithms for measuring programme loudness and true-peak level. Loudness measurement uses frequency weighting, channel weighting, and gating to estimate how loud a programme may feel.

A dataset may use loudness normalisation to stop extremely quiet files from contributing tiny signals and to reduce large level differences between sources. Stability AI's training tools include optional normalisation to a target LUFS value, followed by optional gain variation.

Normalisation should not be automatic in every project. Loudness may carry useful information for dynamics, expressive performance, sound effects, or mastering style. A good pipeline measures first, sets an allowed range, and only changes records that fall outside the policy.

Peak level and loudness are different checks

Swipe sideways to view the full comparison

MeasurementWhat it describesWhy it matters
Sample peakThe largest stored sample valueCan reveal obvious digital clipping risk
True peakAn estimate of peaks that can occur between stored samplesHelps measure remaining headroom more accurately
Integrated loudnessAn estimate of programme loudness across timeSupports more consistent level policies across recordings

5. Treat silence as data, not just empty space

Long silence can waste model capacity, especially when a random crop lands in a quiet section. Librosa provides functions for trimming leading and trailing silence and splitting a signal into non-silent intervals using a threshold relative to a reference level.

Stability AI's dataset pipeline can optionally strip trailing silence, shorten long internal silent sections, skip pure-silence samples, and produce a padding mask showing which frames contain real audio.

Still, silence can be musically meaningful. A pause before a chorus, space between notes, or a quiet introduction is part of the composition. Remove only silence that is clearly unusable for the task. Save thresholds and masks so the decision can be reviewed.

Possible ways to handle silence

Swipe sideways to view the full comparison

ActionWhen it may helpRisk
Keep itPauses and dynamics matter to the taskLong empty crops may waste training steps
Trim the beginning or endThe silence comes from recording or export paddingA quiet entrance or decay may be cut
Shorten long internal gapsThe dataset contains unusable dead airSong timing and structure may be changed
Skip pure-silence segmentsThe crop contains no meaningful signalVery quiet music may be rejected by a poor threshold
Keep a padding maskShort files must be padded to a fixed lengthThe model and loss must use the mask correctly

6. Turn long tracks into model-sized examples

A full track is often longer than the model's training context. Segmentation divides it into shorter examples.

MusicGen trained on random 30-second crops sampled from full tracks. This gave the model a fixed amount of audio per example while exposing it to different parts of each recording across training.

Random cropping is simple, but it is not correct for every task. A caption may describe the whole song but not a specific crop. Lyrics, MIDI, and structural labels must be cut and shifted to match the selected time range. For transcription or section modelling, boundaries based on notes, phrases, bars, or annotated sections may be more useful than arbitrary cuts.

Ways to create segments

Swipe sideways to view the full comparison

StrategyStrengthMain risk
Fixed non-overlapping windowsSimple and reproducibleBoundaries can cut musical events
Overlapping windowsReduces boundary lossCreates highly similar examples
Random cropsProvides variation across epochsA condition may not describe every crop
Silence-based boundariesCan follow natural gapsQuiet music may be split incorrectly
Musical or annotated boundariesCan preserve phrases, bars, lyrics, or sectionsRequires reliable analysis or annotations

7. Find duplicates before splitting the dataset

Duplicates are not limited to files with the same name. The same recording may appear as WAV and MP3, with different loudness, shortened silence, changed metadata, or a slightly different duration.

Use several levels of checking:

  1. A file hash finds byte-for-byte copies.
  2. Basic metadata and duration can narrow the search.
  3. An acoustic fingerprint can identify near-identical audio after re-encoding or small changes.
  4. Similarity models can create a review list for harder cases, but similarity alone should not automatically prove that two tracks are duplicates.

Chromaprint is designed for full-file identification and duplicate audio detection. It aims to identify near-identical audio rather than broad musical similarity.

In practice

Real example: duplicates changed the benchmark

Researchers studying the SoundDesc audio-text retrieval dataset found duplicate recordings across the training and evaluation data. The overlap produced overly optimistic retrieval results. They created revised splits and grouped files that shared similar recording setups to reduce contamination.

In practice

Implementation example: Stable Audio Tools

Stability AI's open training tools document a raw-audio pipeline that loads and resamples audio, optionally normalises loudness, strips trailing silence, removes long internal silence, pads or crops to the model sample size, skips pure-silence samples, and converts the channel layout. It also stores a padding mask and normalised timing metadata. This is a useful example of how separate cleaning decisions become one repeatable pipeline.

processed-audio-record.json · A simplified processing record that keeps the derivative traceable.

Swipe sideways to view code

{
  "sourceId": "track-0042-original",
  "sourcePath": "originals/track-0042.flac",
  "processedPath": "training/track-0042__0030-0060.wav",
  "sourceSampleRateHz": 44100,
  "targetSampleRateHz": 32000,
  "sourceChannels": 2,
  "targetChannels": 1,
  "segmentStartSeconds": 30,
  "segmentDurationSeconds": 30,
  "integratedLoudnessLufs": -18.7,
  "gainAppliedDb": 0,
  "silenceAction": "none",
  "duplicateGroupId": "recording-group-184",
  "processingVersion": "audio-clean-v1"
}

Interactive lesson

Clean a Training Clip

Try it: Begin with validation and inspect the technical report. Apply resampling, channel conversion, silence handling, and segmentation one step at a time. Finish by reviewing the simulated audio fingerprint match and assigning the source group to a dataset split.

A reliable audio pipeline validates and decodes each source, converts it to the model's expected sample rate and channel layout, measures loudness and true peak, handles silence carefully, creates aligned segments, detects duplicates before splitting, and records every processing step.

Check your understanding

Ready for a quick check?

Test whether you can design a reliable audio cleaning pipeline and avoid common dataset leakage problems.

Ready to continue?

Save this section to your account and continue from any device.

Checking your account…
Sources for this lesson (12)
  1. ffprobe Documentation — FFmpeg Project
  2. Audio Resampling — Caroline Chen and Moto Hira
  3. librosa.resample — librosa development team
  4. Recommendation ITU-R BS.1770-5: Algorithms to Measure Audio Programme Loudness and True-Peak Audio Level — ITU Radiocommunication Sector (2023)
  5. librosa.effects.trim — librosa development team
  6. librosa.effects.split — librosa development team
  7. Stable Audio Tools Dataset Documentation — Stability AI
  8. Stable Audio Open — Zach Evans et al. (2024)
  9. Simple and Controllable Music Generation — Jade Copet et al. (2023)
  10. Chromaprint — Lukas Lalinsky and contributors
  11. Using Audio Fingerprinting for Duplicate Detection and Thumbnail Generation — Christopher J. C. Burges, John C. Platt, and Soumya Jana (2005)
  12. Data Leakage in Cross-Modal Retrieval Training: A Case Study — Benno Weck and Xavier Serra (2023)
Browse all contextual sources →