Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Authored by Souvik Mandal, Ph.D.

Project Leader & Instructor, Computational Behavioral Sciences, LS100, FAS, Harvard University | Linkedin ID: souvik-mandal-phd


Last updated: 2026-07-02

Open in Colab Open in GitHub Codespaces

Notebook overview

This LS100 notebook performs batch feature extraction from digital audio files and builds student-friendly metadata tables you can use for interpretation, clustering, or later machine-learning exercises.

Learning objectives

By the end of this notebook, you should be able to:

  1. Explain what tempo, meter, chords, and melody features represent in plain language.

  2. Run an end-to-end audio analysis pipeline over multiple files.

  3. Read and interpret exported metadata in JSON/CSV format.

  4. Identify when an output is likely unreliable and troubleshoot common issues.

Prerequisites

  • You have a folder of audio files (.wav, .mp3, .flac, .ogg, or .m4a).

  • You can edit the AUDIO_FOLDER path in the configuration cell.

  • Optional: install crepe if you want melody extraction.

Expected runtime

  • Small folder (5-20 short tracks): usually a few minutes.

  • Larger folders or melody extraction enabled: can take longer.

Workflow (what this notebook does)

  1. Configure paths and analysis options.

  2. Check dependencies and validate your input folder.

  3. Extract tempo/meter, chord summary, and optional melody information per track.

  4. Aggregate outputs across all tracks into JSON + CSV files.

  5. Create a compact summary table and quick plots for interpretation.

Mini glossary (LS100 quick reference)

  • Waveform: the amplitude of sound over time.

  • Sample rate (sr): how many audio samples are stored per second.

  • Beat: a regular pulse inferred from the signal.

  • Meter: grouping pattern of beats (for example, 4/4-like or 6/8-like).

  • Chroma: pitch-class energy (C, C#, D, ... B) independent of octave.

  • Triad chord template: simplified major/minor chord pattern used for matching.

  • Melody (f0): estimated fundamental frequency contour over time.

  • Confidence: model certainty for an estimated value.

Imports and configuration

librosa version: 0.11.0
Audio folder: /Users/souvikmandal/Documents/06_Teaching_Mentoring/LS100_comp_etho/2025/media/audio/music_tracks/Audio_Clips

File listing and audio loading

What this stage does: finds audio files recursively and loads each track as mono for consistent downstream analysis.

Why this matters: consistent input format reduces feature drift caused by different channel layouts or inconsistent loading assumptions.

Input: AUDIO_FOLDER (path to your track directory) Output: list of audio file paths and waveform arrays (y) with sample rate (sr)

Tip: if this stage finds 0 files, fix the path first before running the rest of the notebook.

Found 13 audio files in /Users/souvikmandal/Documents/06_Teaching_Mentoring/LS100_comp_etho/2025/media/audio/music_tracks/Audio_Clips

Tempo and meter analysis

What this stage does: estimates global tempo (BPM), beat count, and a simple meter label (for example, approximately 4/4 or approximately 6/8).

How it works (simplified):

  1. Detect beats from onset patterns.

  2. Estimate BPM from beat timing.

  3. Compare accent periodicity against candidate bar lengths to infer meter.

Interpretation notes:

  • Higher meter_confidence means stronger periodic accent evidence.

  • Meter labels here are approximate global summaries, not strict symbolic transcription.

Chord analysis (triad summary)

What this stage does: computes beat-synchronous chroma features and summarizes the most-used major/minor triads by total duration.

How it works (simplified):

  1. Convert audio to chroma (pitch-class energy over time).

  2. Align chroma frames to beat locations.

  3. Compare each beat slice with major/minor chord templates.

  4. Merge adjacent repeated labels and summarize durations.

Interpretation notes:

  • top_chords gives a harmonic footprint, not a full music-theory analysis.

  • Fast harmonic changes can reduce summary stability.

Melody analysis with CREPE (optional)

What this stage does: estimates frame-level pitch (f0) and summarizes melodic range and note usage.

How it works (simplified):

  1. Optionally resample audio to CREPE target rate.

  2. Predict pitch and confidence per frame.

  3. Keep only confident frames and convert Hz to note names.

  4. Summarize range, median note, and most frequent notes.

Interpretation notes:

  • Voice-leading trends are better interpreted from many frames, not single-frame values.

  • If melody_available is false, CREPE may be missing or confidence may be too low for this track.

Full-track wrapper

What this stage does: combines all enabled analyses into one consistent per-track metadata record.

Why this helps learning: you can inspect one dictionary object and see rhythm, harmony, melody, and error fields together.

Common troubleshooting (likely causes):

  • error field filled: file may be corrupted or codec may be unsupported.

  • Very low beat/chord stability: track has weak transients or atypical rhythm.

  • Melody missing: crepe not installed, confidence threshold too strict, or highly noisy signal.

Batch loop and output files

What this stage does: runs analysis on every file and exports JSON + CSV outputs for class use.

Output files:

  • track_metadata.json: rich nested metadata per track.

  • track_metadata.csv: flattened, spreadsheet-friendly summary.

Expected value ranges (rule-of-thumb, not strict):

  • global_tempo_bpm: often ~60 to ~200 for many popular tracks.

  • meter_confidence: 0 to 1, where higher means clearer periodic accents.

  • num_chord_segments: larger values suggest more harmonic change.

If output looks odd:

  • Recheck audio quality and file format.

  • Verify AUDIO_FOLDER path and file count.

  • Disable optional melody first, then re-enable after baseline works.


▶ Analyzing: Mediu Zhiga.wav
294/294 ━━━━━━━━━━━━━━━━━━━━ 6s 21ms/step

▶ Analyzing: Ra Bacheeza.wav
294/294 ━━━━━━━━━━━━━━━━━━━━ 6s 21ms/step

▶ Analyzing: [SP] Alfonso Ortiz Tirado - TE QUIERO DIJISTE.mp3
311/311 ━━━━━━━━━━━━━━━━━━━━ 7s 21ms/step

▶ Analyzing: [SP] Alvaro Carrillo - Pinotepa Nacional.mp3
292/292 ━━━━━━━━━━━━━━━━━━━━ 6s 21ms/step

▶ Analyzing: [SP] Lagrimas Negras.mp3
126/126 ━━━━━━━━━━━━━━━━━━━━ 3s 21ms/step

▶ Analyzing: [SP] Los Panchos - Contigo.mp3
258/258 ━━━━━━━━━━━━━━━━━━━━ 6s 25ms/step

▶ Analyzing: [SP] Los Panchos - Jamas Jamas Jamas.mp3
283/283 ━━━━━━━━━━━━━━━━━━━━ 6s 22ms/step

▶ Analyzing: [SP] Los Panchos - Te Quiero Dijiste.mp3
229/229 ━━━━━━━━━━━━━━━━━━━━ 5s 21ms/step

▶ Analyzing: [SP] Soledad y el Mar - Natalia Lafourcade.mp3
338/338 ━━━━━━━━━━━━━━━━━━━━ 7s 21ms/step

▶ Analyzing: [ZAP] Binni Gula_za - Ni_bixi Dxi Zina.mp3
277/277 ━━━━━━━━━━━━━━━━━━━━ 6s 21ms/step

▶ Analyzing: [ZAP] Mediu Zhiga.mp3
294/294 ━━━━━━━━━━━━━━━━━━━━ 6s 21ms/step

▶ Analyzing: [ZAP] Ra Bacheeza.mp3
294/294 ━━━━━━━━━━━━━━━━━━━━ 6s 21ms/step

▶ Analyzing: [ZAP] Sabor a Mi - Trio Galenos Y Mario Carrillo.mp3
88/88 ━━━━━━━━━━━━━━━━━━━━ 2s 21ms/step

✅ Done. Analyzed 13 tracks.
JSON metadata saved to: /Users/souvikmandal/Documents/06_Teaching_Mentoring/LS100_comp_etho/2025/media/audio/music_tracks/Audio_Clips/track_metadata.json
CSV metadata saved to : /Users/souvikmandal/Documents/06_Teaching_Mentoring/LS100_comp_etho/2025/media/audio/music_tracks/Audio_Clips/track_metadata.csv

Interpretation prompts and what to submit

Use these prompts after running the batch cell:

  1. Which track appears rhythmically most stable, and what output supports that claim?

  2. Do tracks with similar BPM always share similar harmonic summaries? Why or why not?

  3. Which track shows the widest melodic range, and is the confidence level convincing?

Suggested LS100 submission checklist

  • Screenshot of successful batch completion output.

  • The generated track_metadata.csv file.

  • A short paragraph (4-6 lines) interpreting at least two tracks using tempo, meter, chords, and melody fields.