Skip to main content

pymusictheory

pymusictheory is a Python library for performing calculations with musical notes. It provides tools to work with notes, intervals, and chords, both in the context of octaves and without. This library is useful for music theory analysis, algorithmic composition, and other musical applications.

Key features:

  • Correctly returning double accidentals when adding intervals to notes. Example: B# + M3 = D##

Not implemented:

  • Adding intervals to notes with double accidentals. For example, B## + M3 = D###

Overview of Classes

1. NoteLetter

Represents the basic musical note letters (C, D, E, F, G, A, B). It supports operations like addition and subtraction to navigate the musical scale.

  • Example Usage:
    from pymusictheory import NoteLetter
    
    note = NoteLetter.C + 4  # G
    print(note)  # Output: G
    

2. NoteAlteration

Represents alterations to a note (sharp, flat, natural). It defines how many semitones the alteration modifies the note by.

  • Example Usage:
    from pymusictheory import NoteAlteration
    
    alteration = NoteAlteration.SHARP
    print(alteration.semitone_difference)  # Output: 1
    

3. Note

Represents a musical note without a specific octave. It combines a NoteLetter and a NoteAlteration to define a note like C#, Db, or F.

  • Key Features:

    • Convert from string representation (e.g., "C#").
    • Calculate semitone offsets from C.
    • Generate all possible notes for a given semitone offset.
  • Example Usage:

    from pymusictheory import Note
    
    note = Note.from_str("C#")
    print(note.semitone_offset)  # Output: 1
    

4. NoteInOctave

Represents a musical note in a specific octave. It extends Note by adding an octave number and supports operations like calculating absolute semitone offsets and adding intervals.

  • Key Features:

    • Convert from string representation (e.g., "C4").
    • Calculate absolute semitone offsets from C0.
    • Add intervals to notes.
  • Example Usage:

    from pymusictheory import NoteInOctave
    
    note = NoteInOctave.from_str("C4")
    print(note.absolute_semitone_offset)  # Output: 48
    

5. Interval

Represents musical intervals (e.g., major third, perfect fifth). It defines the semitone and letter distances for each interval.

  • Example Usage:
    from pymusictheory import Interval
    
    interval = Interval.MAJOR_THIRD
    print(interval.semitone_distance)  # Output: 4
    

6. Chord

Represents a set of NoteInOctave objects, forming a chord. It allows iteration over the notes in the chord.

  • Example Usage:
    from pymusictheory import NoteInOctave, Chord
    
    chord = Chord({NoteInOctave.from_str("C4"), NoteInOctave.from_str("E4"), NoteInOctave.from_str("G4")})
    for note in chord:
        print(note)
    

Relationships Between Classes

  • NoteLetter and NoteAlteration combine to form a Note.
  • Note and an octave number combine to form a NoteInOctave.
  • Interval can be added to a NoteInOctave to calculate a new note.
  • A Chord is a collection of NoteInOctave objects.

Common Use Cases

1. Calculate Semitone Offsets

from pymusictheory import Note

note = Note.from_str("F#")
print(note.semitone_offset)  # Output: 6

2. Add Intervals to Notes

from pymusictheory import NoteInOctave, Interval

note = NoteInOctave.from_str("C4")
new_note = note + Interval.PERFECT_FIFTH
print(new_note)  # Output: G4

note = NoteInOctave.from_str("B#3")
new_note = note + Interval.MAJOR_THIRD
print(new_note)  # Output: D##4

3. Generate Chords

from pymusictheory import NoteInOctave, Chord

chord = Chord({NoteInOctave.from_str("C4"), NoteInOctave.from_str("E4"), NoteInOctave.from_str("G4")})
print(chord)  # Output: {C4, E4, G4}

Installation

Ensure you have Python 3.13 or higher installed. Install the library using:

pip install pymusictheory

Contributing

Contributions are welcome! Please submit issues or pull requests on the GitHub repository.

License

This project is licensed under the MIT License.

Metadata

Release files for pymusictheory 2.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymusictheory 2.2.3
File Size Uploaded
pymusictheory-2.2.3.tar.gz 13.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pymusictheory 2.2.3
File Interpreter ABI Platform
pymusictheory-2.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 20.7 kB

Release files / pymusictheory-2.2.3.tar.gz

Download URL pymusictheory-2.2.3.tar.gz
Size 13.5 kB
Tags Source
SHA-256 checksum
How to use checksums
056b69f425a4dd8267a47bb7cf4ab11abff17e5878186fca893418ffef978a65
BLAKE2b-256 checksum
How to use checksums
6928aaa22c79bbcb1179432173892f53b6842ff3b8b4dd62ad08477d72e315bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Release files / pymusictheory-2.2.3-py3-none-any.whl

Download URL pymusictheory-2.2.3-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e45bfcc3477a7754899045d674f88ee8f279414a40a000f0933bb39c71fe5ba
BLAKE2b-256 checksum
How to use checksums
4fdd2b762ab00457987681000dc63d20e2e05bdd7a1fd3599994e7e9ff8ad787
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Release history Release notifications | RSS feed

This release

2.2.3 This release

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page