accompy.util#
Utility functions for chord parsing and normalization.
Contains helpers for chord symbol normalization, chord string parsing, and iReal URL parsing.
Module Attributes
each |
|
Symbols |
Functions
|
Normalize chord symbols to a standard format. |
|
Parse a chord string into measures, each containing chord symbols. |
|
Extract a Score from an iReal Pro HTML export file. |
|
Parse an iReal Pro URL ( |
|
Parse time signature string like '4/4' into tuple. |
|
Create a |
|
Transpose a chord symbol by semitones. |
|
Transpose a single note name by semitones. |
|
Transpose a |
- accompy.util.CHORD_SYMBOL_REPLACEMENTS = (('maj7', '^7'), ('Maj7', '^7'), ('M7', '^7'), ('maj9', '^9'), ('Maj9', '^9'), ('M9', '^9'), ('maj', '^'), ('Maj', '^'), ('min7', '-7'), ('mi7', '-7'), ('mi9', '-9'), ('min', '-'), ('mi', '-'), ('m7', '-7'), ('m9', '-9'), ('m', '-'), ('dim7', 'o7'), ('dim', 'o'), ('°7', 'o7'), ('°', 'o'), ('m7b5', 'h7'), ('ø7', 'h7'), ('ø', 'h'), ('sus2', 'sus2'), ('add9', 'add9'), ('add2', 'add2'))#
each
oldbecomesnewat the end of the symbol or of a part before a slash (longestoldwins).- Type:
The suffix rewrites
normalize_chord_symbol()applies
- accompy.util.UNNORMALIZED_SYMBOLS = ('', 'n', 'N.C.', 'NC', '%', 'x')#
Symbols
normalize_chord_symbol()returns unchanged (no chord, or a repeat sign).
- accompy.util.normalize_chord_symbol(symbol)[source]#
Normalize chord symbols to a standard format.
Handles conversions like:
Cm -> C-
Cmaj7 -> C^7
Cdim, C° -> Co
CM7, CMaj7 -> C^7; Cmi7 -> C-7
Cm7b5 -> Ch7 (half-diminished)
Each
/-separated part of the symbol has its longest matching suffix fromCHORD_SYMBOL_REPLACEMENTSrewritten, once (sodimis never read as a word ending inm).Example
>>> normalize_chord_symbol("Cmaj7") 'C^7' >>> normalize_chord_symbol("Dm7b5") 'Dh7' >>> normalize_chord_symbol("Cdim7"), normalize_chord_symbol("Bbm7/F") ('Co7', 'Bb-7/F')
- Return type:
- accompy.util.parse_chord_string(chord_string)[source]#
Parse a chord string into measures, each containing chord symbols.
Example
>>> parse_chord_string("| C | Am | F | G |") [['C'], ['A-'], ['F'], ['G']] >>> parse_chord_string("| C Am | F G |") [['C', 'A-'], ['F', 'G']]
- accompy.util.parse_ireal_html(html_path)[source]#
Extract a Score from an iReal Pro HTML export file.
iReal Pro can export songs as HTML files containing an
irealb://link. This function reads the file, extracts that link, and parses it into aScore.- Parameters:
html_path (
str) – Path to the HTML file exported from iReal Pro.- Return type:
Score
- Returns:
A Score with measures, title, key, and time signature.
- Raises:
FileNotFoundError – If html_path does not exist.
ValueError – If no iReal URL is found in the HTML.
Example:
>>> score = parse_ireal_html("/path/to/song.html") >>> score.title 'Autumn Leaves'
- accompy.util.parse_ireal_url(url)[source]#
Parse an iReal Pro URL (
irealb://orirealbook://) into a Score.Decoding is
accompy.ireal.decode_ireal_url(); the Score is the first tune of the URL that has chords, each bar the list of its chords as written (iReal chord notation). RaisesValueErrorwhen no tune has chords.>>> score = parse_ireal_url("irealbook://Blues=Me=Swing=F=n=[T34F7 |Bb7 Bo7 |C7 Z") >>> score.measures, score.time_signature ([['F7'], ['Bb7', 'Bo7'], ['C7']], (3, 4))
- accompy.util.parse_time_sig(ts_str)[source]#
Parse time signature string like ‘4/4’ into tuple.
Example
>>> parse_time_sig("3/4") (3, 4) >>> parse_time_sig("6/8") (6, 8) >>> parse_time_sig(None) (4, 4)
- accompy.util.score_from_chord_specs(chords, *, title='Untitled', key='C', time_signature=(4, 4))[source]#
Create a
Scorefrom(chord, beats)pairs.Durations that are multiples of the bar length map cleanly to repeated bars. Other durations are approximated by grouping chords into bars.
- accompy.util.transpose_chord(chord, semitones, *, use_flat=None)[source]#
Transpose a chord symbol by semitones.
Handles slash chords (e.g.
"C6/E").- Parameters:
chord (
str) – Chord symbol like"Am7","C6/E","G#o".semitones (
int) – Number of semitones.use_flat (
Optional[bool]) – Force flat/sharp spelling (seetranspose_note()).
- Return type:
Example
>>> transpose_chord("Am7", 2) 'Bm7' >>> transpose_chord("C6/E", 5) 'F6/A' >>> transpose_chord("G#o", -2, use_flat=True) 'Gbo'
- accompy.util.transpose_note(name, semitones, *, use_flat=None)[source]#
Transpose a single note name by semitones.
- Parameters:
- Return type:
Example
>>> transpose_note("C", 5) 'F' >>> transpose_note("A", -2) 'G' >>> transpose_note("C", 1, use_flat=True) 'Db'
- accompy.util.transpose_score(score, target_key)[source]#
Transpose a
Scoreto a new key.The spelling (sharp vs flat) is chosen automatically based on the target_key.
- Parameters:
score – A
Scoreinstance.target_key (
str) – Target key, e.g."Eb","G","F#".
- Return type:
Score
- Returns:
A new Score in the target key with an updated title.
Example
>>> from accompy import Score >>> s = Score.from_string("| C | Am | F | G |", key="C") >>> t = transpose_score(s, "G") >>> [m[0] for m in t.measures] ['G', 'E-', 'C', 'D']