Skip to main content

A python package for transposing chords from one key to another and changing between DO-RE-MI and A-B-C notations.

Project description

PyTransposer

Build Status Downloads

Python module for transposing chords and entire songs from one key to another and changing between DO-RE-MI and A-B-C notations.

Features

  • Transpose chords and whole songs
  • Change chords and entire songs between DO-RE-MI and A-B-C notations
  • Output chords/song following a specific target key
  • Change target key part-way through a song

Usage

Transposing Single Chords

To transpose single chords, use pytransposer like this:

>>> import pytransposer.transposer as tr
>>> tr.transpose_chord('Fb', 1, 'Db')
'F'
>>> tr.transpose_chord('F##', 1, 'C')
'Ab'
>>> tr.transpose_chord('F', 2, 'D', chord_style_out='doremi')
'SOL'

To translate chords between notations, use pytransposer like this:

>>> from pytransposer.common import chord_doremi_to_abc
>>> chord_doremi_to_abc('MIb')
'Eb'
>>> chord_doremi_to_abc('FA##')
'F##'

and:

>>> from pytransposer.common import chord_abc_to_doremi
>>> chord_abc_to_doremi('Eb')
'MIb'
>>> chord_abc_to_doremi('F##')
'FA##'

You can also use the method pytransposer.express_chord_in_key to express a general chord in a given key with musical correctness:

>>> express_chord_in_key('DO#', 'D#')
'C#'

>>> express_chord_in_key('DO#', 'F', chord_style_out='doremi')
'REb'

Transposing Songs

Use the function pytransposer.transpose_song to transpose a whole song a number of half tones. You can set a target key through the to_key parameter so that the chords are expressed with musical correctness in that key:

>>> transpose_song('Exa\[DO#/RE]mple so\[Bb4]ng', 3, to_key='F')
'Exa\[E/F]mple so\[Db4]ng'

If to_key is set to 'auto', the target key is determined automatically from the first chord of the song.

>>> transpose_song('Exa\[RE]mple so\[Bb4]ng', 3, to_key='auto')
'Exa\[F]mple so\[Db4]ng'

If it is left to its default value (None), no specific key is targeted. Instead, the chords are expressed in their 'reference' (simplest) form.

>>> transpose_song('Exa\[DO#/RE]mple so\[Bb4]ng', 3)
'Exa\[E/F]mple so\[C#4]ng'

You can also set the output notation style:

>>> transpose_song('Exa\[DO#/RE]mple so\[Bb4]ng', 3, to_key='F', chord_style_out='doremi')
'Exa\[MI/FA]mple so\[REb4]ng'

And you can pass custom pre_chord and post_chord regex patterns to specify how you are identifying your chords:

>>> transpose_song('Exa<<DO#/RE>>mple so<<Bb4>>ng', 3, to_key='F', pre_chord=r'<<', post_chord=r'>>', chord_style_out='doremi')
'Exa<<MI/FA>>mple so<<REb4>>ng'

The target to_key can also be changed at any point in the song by adding \key{<to_key>} whenever it should be changed (for example, \key{DO} or \key{D#}) or by adding \key{<half_tones>} (for example, \key{+2} or \key{-5}).

>>> transpose_song('Thi\[F#]s is \key{Eb}an e\[A]xample \[F#]song')
	'Thi\[F#]s is an e\[A]xample \[Gb]song'
>>> transpose_song('Thi\[F#]s is \key{-3}an e\[A]xample \[F#]song')
	'Thi\[F#]s is an e\[A]xample \[Gb]song'

You can change pre_key and post_key to change the way that the key changes are indicated:

>>> transpose_song('Thi\[F#]s is \|Eb|an e\[A]xample \[F#]song', 7, pre_key=r'\\|', post_key=r'\|')
'Thi\[C#]s is an e\[E]xample \[Db]song'
	

By default, the function removes the key change signalling strings. You can avoid this behaviour by setting clean_key_change_signals to False.

>>> transpose_song('Thi\[F#]s is \key{Eb}an e\[A]xample \[F#]song', 7, clean_key_change_signals=False)
'Thi\[C#]s is \key{Bb}an e\[E]xample \[Db]song'

Settings

If you use different symbols to represent sharps and flats, you can set them in the module's configuration like this:

>>> from pytransposer.config import TransposerConfig
>>> from pytransposer.transposer import transpose_song
>>> transpose_song('Exa\[DO#/RE]mple so\[Bb4]ng', 3, 'F')
'Exa\[E/F]mple so\[Db4]ng'
>>> TransposerConfig.sharp = 's'
>>> TransposerConfig.flat = '♭'
>>> transpose_song('Exa\[DOs/RE]mple so\[B♭4]ng', 3, 'F')
'Exa\[E/F]mple so\[D♭4]ng'

However, be aware that not all symbols have been tested, and setting sharps and flats to some specific characters may lead to unexpected side effects. In general, any character that is easily distinguishable from the chords should be fine.

Example

You can see an example of the module's usage here.

Testing

Run unit tests using Python's doctest, first clone the repo:

git clone https://github.com/bfrangi/transposer.git

Then, open a terminal at the root directory of the repo and run:

python3 -m src.pytransposer.transposer -v  

and

This will run the tests for the main transposer sub-module. For the rest of the submodules, use:

python3 -m src.pytransposer.common -v
python3 -m src.pytransposer.config -v

More info

View on the Python Package Index (PyPI) here.

View on GitHub here.

View the change log here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytransposer-1.3.2.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

pytransposer-1.3.2-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file pytransposer-1.3.2.tar.gz.

File metadata

  • Download URL: pytransposer-1.3.2.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for pytransposer-1.3.2.tar.gz
Algorithm Hash digest
SHA256 76bc8eb4097f580d01eadb7ffe91fc9cc8bb81a502838c085d64f41eb57773f4
MD5 3b14c7004edb2237e6a42a72d59a522c
BLAKE2b-256 9ee104703d45fd87572b6fea107b0fb5c14c291a2a1b45cf4c3bfc441caf8273

See more details on using hashes here.

File details

Details for the file pytransposer-1.3.2-py3-none-any.whl.

File metadata

  • Download URL: pytransposer-1.3.2-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for pytransposer-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 16bfc1b4230c433a4a40c23276518a3f38be53aa390a7e8467b338da95417be4
MD5 c40677dd3d55bafe4c15c2e2faa5f23b
BLAKE2b-256 cccad51db6f6fdeb3c180e86508192df50a22bc4ff99b38318885fd18049186b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page