PyHarmonize: Adding harmony lines to recorded melodies in Python

Overview

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
You might also like...
Karate Club: An API Oriented Open-source Python Framework for Unsupervised Learning on Graphs (CIKM 2020)
Karate Club: An API Oriented Open-source Python Framework for Unsupervised Learning on Graphs (CIKM 2020)

Karate Club is an unsupervised machine learning extension library for NetworkX. Please look at the Documentation, relevant Paper, Promo Video, and Ext

Open source time series library for Python

PyFlux PyFlux is an open source time series library for Python. The library has a good array of modern time series models, as well as a flexible array

A Python Automated Machine Learning tool that optimizes machine learning pipelines using genetic programming.
A Python Automated Machine Learning tool that optimizes machine learning pipelines using genetic programming.

Master status: Development status: Package information: TPOT stands for Tree-based Pipeline Optimization Tool. Consider TPOT your Data Science Assista

MLBox is a powerful Automated Machine Learning python library.
MLBox is a powerful Automated Machine Learning python library.

MLBox is a powerful Automated Machine Learning python library. It provides the following features: Fast reading and distributed data preprocessing/cle

Python package for stacking (machine learning technique)
Python package for stacking (machine learning technique)

vecstack Python package for stacking (stacked generalization) featuring lightweight functional API and fully compatible scikit-learn API Convenient wa

A Python Package to Tackle the Curse of Imbalanced Datasets in Machine Learning

imbalanced-learn imbalanced-learn is a python package offering a number of re-sampling techniques commonly used in datasets showing strong between-cla

Python-based implementations of algorithms for learning on imbalanced data.

ND DIAL: Imbalanced Algorithms Minimalist Python-based implementations of algorithms for imbalanced learning. Includes deep and representational learn

Extreme Learning Machine implementation in Python

Python-ELM v0.3 --- ARCHIVED March 2021 --- This is an implementation of the Extreme Learning Machine [1][2] in Python, based on scikit-learn. From

Python Extreme Learning Machine (ELM) is a machine learning technique used for classification/regression tasks.

Python Extreme Learning Machine (ELM) Python Extreme Learning Machine (ELM) is a machine learning technique used for classification/regression tasks.

Owner
Julian Kappler
Julian Kappler
Educational python for Neural Networks, written in pure Python/NumPy.

Educational python for Neural Networks, written in pure Python/NumPy.

null 127 Oct 27, 2022
learn python in 100 days, a simple step could be follow from beginner to master of every aspect of python programming and project also include side project which you can use as demo project for your personal portfolio

learn python in 100 days, a simple step could be follow from beginner to master of every aspect of python programming and project also include side project which you can use as demo project for your personal portfolio

BDFD 6 Nov 5, 2022
High performance, easy-to-use, and scalable machine learning (ML) package, including linear model (LR), factorization machines (FM), and field-aware factorization machines (FFM) for Python and CLI interface.

What is xLearn? xLearn is a high performance, easy-to-use, and scalable machine learning package that contains linear model (LR), factorization machin

Chao Ma 3k Jan 8, 2023
A modular active learning framework for Python

Modular Active Learning framework for Python3 Page contents Introduction Active learning from bird's-eye view modAL in action From zero to one in a fe

modAL 1.9k Dec 31, 2022
A library of extension and helper modules for Python's data analysis and machine learning libraries.

Mlxtend (machine learning extensions) is a Python library of useful tools for the day-to-day data science tasks. Sebastian Raschka 2014-2021 Links Doc

Sebastian Raschka 4.2k Dec 29, 2022
Sequence learning toolkit for Python

seqlearn seqlearn is a sequence classification toolkit for Python. It is designed to extend scikit-learn and offer as similar as possible an API. Comp

Lars 653 Dec 27, 2022
Simple structured learning framework for python

PyStruct PyStruct aims at being an easy-to-use structured learning and prediction library. Currently it implements only max-margin methods and a perce

pystruct 666 Jan 3, 2023
Python implementation of the rulefit algorithm

RuleFit Implementation of a rule based prediction algorithm based on the rulefit algorithm from Friedman and Popescu (PDF) The algorithm can be used f

Christoph Molnar 326 Jan 2, 2023
Metric learning algorithms in Python

metric-learn: Metric Learning in Python metric-learn contains efficient Python implementations of several popular supervised and weakly-supervised met

null 1.3k Dec 28, 2022
[HELP REQUESTED] Generalized Additive Models in Python

pyGAM Generalized Additive Models in Python. Documentation Official pyGAM Documentation: Read the Docs Building interpretable models with Generalized

daniel servén 747 Jan 5, 2023