Skip to content

kernpy: Symbolic Music Notation in Python

What is kernpy?

kernpy is a comprehensive Python toolkit for working with Humdrum **kern format—a powerful, text-based system for representing symbolic music notation digitally. Whether you're a music researcher analyzing scores, a developer building music analysis tools, or an educator working with music data, kernpy gives you the tools to parse, analyze, transform, and export symbolic music notation programmatically.

Born from the research community, kernpy makes it easy to:

  • Load and parse **kern files from disk or strings
  • Navigate and analyze musical scores programmatically
  • Transform documents through transposition, filtering, and selective export
  • Build automated workflows for batch processing music collections
  • Access detailed information about pitches, durations, clefs, key signatures, and more

Why Use kernpy?

The kern format has been the standard in music research for decades—used by musicologists, composers, and music technologists worldwide. But working with kern files requires careful parsing of a text-based format. kernpy removes that burden.

Problems kernpy Solves

Working with existing tools Many music software packages support kern, but they often require manual conversion, don't expose the full data structure, or lack batch processing capabilities. kernpy gives you direct, programmatic access to all kern data.

Bridging research and implementation Music researchers need to analyze scores computationally—extracting pitches, comparing durations, studying harmonic progressions, or searching for patterns. kernpy exposes everything you need without forcing you to parse the format yourself.

Building custom workflows Standard tools can't always do what you need. kernpy lets you write Python code to combine operations (load + filter + transpose + export) in exactly the way your project requires.

Handling scale Processing thousands of files manually is impossible. kernpy's batch processing capabilities let you apply the same transformations to entire score collections.

Preserving data quality When converting between formats or tools, data can be lost. kernpy preserves the full richness of **kern notation, maintaining all information through the processing pipeline.

Who Uses kernpy?

Music Researchers and Musicologists

Extract collections of scores for corpus studies, analyze harmonic or melodic patterns, build datasets for machine learning, or study performance practice across historical periods.

Example: Analyze key signatures across all Bach chorales to understand his compositional patterns.

Developers Building Music Tools

Build music analysis platforms, composition assistants, music education software, or analysis plugins that need to read and process **kern files.

Example: Create a web-based score editor that preserves all **kern metadata while providing visual notation.

Music Educators

Prepare teaching materials, build interactive music analysis tools for students, or create assignments that involve programmatic score manipulation.

Example: Automatically transpose a collection of folk songs to different keys for ensemble instruction.

Composers and Music Technologists

Process your own scores or collections, experiment with algorithmic composition, or build tools specialized to your compositional needs.

Example: Generate variations of your own compositions through systematic transposition and spine manipulation.

Getting Started in Minutes

1. Installation

pip install kernpy

Or install from the repository for the latest development version:

pip install git+https://github.com/OMR-PRAIG-UA-ES/kernpy.git

2. Your First Script

import kernpy as kp

# Load a **kern file
document, errors = kp.load('score.krn')

# Inspect what you loaded
print(f"Loaded score with {document.measures_count()} measures")

# Export to a new file
kp.dump(document, 'output.krn')

That's it. You've loaded, inspected, and exported a **kern file.

3. Next Steps

Once you've run your first script:

Learning Paths

Choose your path based on your goals:

Analyze and Extract Data

Parse files, inspect structure, search for patterns, and export subsets of information.

Transform and Export

Filter spines, change encodings, transpose pitches, select measures, and export customized versions of scores.

Build Workflows

Process collections, combine files, automate repetitive tasks, and integrate kernpy into larger systems.

Command-Line Tools

Use kernpy's utilities from the terminal for format conversion and dataset processing.

Explore the Documentation

Core Concepts

Understand the foundations:

Practical Guides

Learn by doing:

API Reference

Full technical documentation:

Advanced Topics

Deep dives into specialized areas:

Help and Resources

  • FAQ — Common questions and troubleshooting
  • Contributing — How to contribute to kernpy
  • Citation — How to cite kernpy in academic work
  • About — The kernpy project and team

Quick Reference

Most Common Operations

import kernpy as kp

# Load a file
doc, errors = kp.load('file.krn')

# Load from a string
doc, errors = kp.loads('**kern\n*M4/4\n4c\n4d\n*-')

# Export to a file
kp.dump(doc, 'output.krn')

# Export to a string
content = kp.dumps(doc)

# Filter to specific spines
kp.dump(doc, 'filtered.krn', spine_types=['**kern'])

# Transpose
transposed_doc = doc.to_transposed('P4', 'up')
kp.dump(transposed_doc, 'transposed.krn')

# Select measures
kp.dump(doc, 'excerpt.krn', from_measure=5, to_measure=10)

Installation and Setup

Get started in three commands:

# Install kernpy
pip install kernpy

# Verify installation
python -c "import kernpy as kp; print('kernpy ready!')"

# Run a quick test
python << 'EOF'
import kernpy as kp
doc, _ = kp.loads('**kern\n*M4/4\n4c 4e 4g\n')
print(f"Loaded chord with {len(doc.get_spines())} spine(s)")
EOF

What's New

kernpy 1.6.0 focuses on:

  • Modern Python API with load, loads, dump, dumps functions
  • Comprehensive support for kern and mens notation
  • Robust measure signature validation
  • Flexible token filtering through the category system
  • Support for multiple export formats (kern, ekern, agnostic kern, etc.)

Note: Some older functions (read, create, export, store) are deprecated. See reference.md for migration guidance.

Next Steps

  1. Install kernpy — Follow the installation instructions above
  2. Read Getting Started — The Getting Started guide covers installation and first steps
  3. Run a quick example — Follow the Quick Start for a 5-minute introduction
  4. Pick a learning path — Choose based on your goals (analyze, transform, or build) in the Learning Paths section

Support and Contributing

License

kernpy is open-source software licensed under the AGPL-3.0 license. See the repository for details.


Acknowledgments

kernpy is developed by the Pattern Recognition and Artificial Intelligence Group at the University of Alicante. Learn more about our research at PRAIG.