PyHarmonize: Adding harmony lines to recorded melodies in Python
About
To use this module, the user provides
- a wav file containing a melody,
- the key in which the melody is, and
- the scale degree(s) of the desired harmony.
The module then outputs a wav file which contains the original melody, together with the added harmony line(s).
We first give some examples, the installation instructions are further below.
Examples (with audio files)
We here provide three audio examples together with the code used to generate them. See the folder examples/ for more detailed example notebooks.
Note that the embedded mp4 video files that contain the audio in the following are by default muted.
Example 1: Added third on a distorted electric guitar
In this example we add a harmony line a third above the input melody, which is played on a distorted electric guitar. Here are the input signal used, as well as the final result:
guitar_distorted_E_major_ex1.mp4
guitar_distorted_E_major_ex1_added_3.mp4
And here is the code used to generate this output:
import PyHarmonize
# Create dictionary with parameters
parameters = {'input_filename':'./guitar_distorted_E_major_ex1.wav', # input audio is in the key of E major
'output_filename':'./guitar_distorted_E_major_ex1_with_harmony.wav',
'key':'E',
'mode':'major'}
# Generate instance of the class harmony_generator
harmony_generator = PyHarmonize.harmony_generator(parameters=parameters)
# Add harmony
# Note that scale_degrees = [3] means we add one melody line,
# which is always three notes higher within the scale. Depending on the note
# played, "three notes higher within the scale" is either 3 or 4 semitones up.
output_dictionary = harmony_generator.add_harmonies(scale_degrees = [3])
Example 2: Added third and fifth on a distorted electric guitar
In this example we add two harmony lines to an input signal. Here are the input signal and the result:
guitar_distorted_E_major_ex2.mp4
guitar_distorted_E_major_ex2_added_3_5.mp4
The code for this example is essentially the same as in the first example, except that now the list scale_degrees contains more than one element:
import PyHarmonize
# Create dictionary with parameters
parameters = {'input_filename':'./guitar_distorted_E_major_ex2.wav', # input audio is in the key of E major
'output_filename':'./guitar_distorted_E_major_ex2_with_harmony.wav',
'key':'E',
'mode':'major'}
# Generate instance of the class harmony_generator
harmony_generator = PyHarmonize.harmony_generator(parameters=parameters)
# Add harmony
output_dictionary = harmony_generator.add_harmonies(scale_degrees = [3, 5]) # add third and fifth
If we add some more octaves and thirds, we can generate a more synthesizer-like sound. Here is an example for that:
guitar_distorted_E_major_ex2_added_3_5_octaves.mp4
To generate this output, we pass scale_degrees = [-8, -6, 3, 5, 8, 10], which adds pitch shifted signals an octave lower (-8), the third one octave lower (-6), a third up (3), a fifth up (5), an octave up (8), and a third an octave higher (10).
Example 3: Added third, fifth, and octave on a clean electric guitar
In this example we add thirds, fifths, and octaves to a melody in A major, which is played on a clean electric guitar. Here are input and output files:
guitar_clean_A_major.mp4
guitar_clean_A_major_added_3_5_8.mp4
The code for generating this harmony is:
import PyHarmonize
# Create dictionary with parameters
parameters = {'input_filename':'./guitar_clean_A_major.wav', # input audio is in the key of A major
'output_filename':'./guitar_clean_A_major_with_harmony.wav',
'key':'A',
'mode':'major'}
# Generate instance of the class harmony_generator
harmony_generator = PyHarmonize.harmony_generator(parameters=parameters)
# Add harmony
output_dictionary = harmony_generator.add_harmonies(scale_degrees = [3,5,8])
# The list
# scale_degrees = [3, 5, 8]
# means that we add four melody lines:
# 1. a third up
# 2. a fifth up
# 3. one octave up
Installation
To install the module PyHarmonize, as well as its requirements (NumPy, SciPy, librosa, and SoundFile), clone this repository and run the installation script:
>> git clone https://github.com/juliankappler/PyHarmonize.git
>> cd PyHarmonize
>> pip install -r requirements.txt
>> python setup.py install