DuetYourself: A Real-time Counterpoint Generator

Sarah Smith, Fall 2011 – Final Project for Music 256A


As the name suggests, DuetYourself accepts an input audio stream and outputs and outputs up to three voices in counterpoint. In addition to passing the input audio stream back to the speakers, the system outputs a transformation of this stream shifted in pitch according to user specified parameters. This transformation is a combination of a user specified transposition interval (in half steps) and optional melodic inversion. Finally, the system generates a bass line in good counterpoint to the input audio. In creating this line, the system generally follows the rules of tonal harmony and voice leading to produce a consonant interval. This bass line then sonified with a Glass Harmonica model from the stk instrument classes. Finally, the program visualizes the output lines in an intuitive way, so that the user can better understand the transformations as they happen.


Possible Use:

In addition to being a fun tool to play around with and sing into, DuetYourself is also potentially useful for exploring the different ways in which a melody counterpoints itself. For example, since the interval of transposition can be changed with a single press of a keyboard key, the user can experiment with different levels of transposition and see how the melody and inversion sound together.


Melodic Transformation:

For each audio buffer, the input frequency is determined using an autocorrelation and basic checking for octave errors. This works sung input, but has not been tested with other instruments. This input pitch is then used to calculate the appropriate shift for the output transformation. This resulting shift amount is based on the user specified transposition interval, and, for inversion the distance of the current pitch from the tonic (currently hard coded to C). The output sound is then generated by applying a pitch shifter to the input audio.


Bass Line Generation:

In order to generate a bass line for the input audio, the system first calculates a MIDI pitch corresponding to the input frequency and determines whether or not this constitutes a change in note from the previous state. When a note change is detected, the system uses the previous and current input pitches as well as the previous bass line pitch in order to determine where to move next. In doing this, it attempts to follow the rules of tonal voice leading a much as possible moving (generally) as described below. Note that this counterpoint is calculated only with respect to the input stream, and is not guaranteed to be consonant with the transformation generated above.

1.     If the bass line can remain on the same pitch and form a consonant interval with the input, it will do so.

2.     If the bass line must move, and is currently on the dominant or leading tone notes, it will resolve to tonic.

3.     If the bass line must move, it will move one scale degree in contrary motion to the melody if this results in a consonance.

4.     In all other cases, the bass line will attempt to move a small interval in contrary motion, or move in similar motion, careful to avoid parallel fifths and octaves.


User Controls:

The program is set to update only when a sufficiently loud sound is detected at the microphone. This prevents the visualization and sonfiication of things like breaths and other unintentional short pauses. A list of the keyboard controls included in the current version is listed below.

s                              Toggle Bass line on/off

i                              Toggle inversion on/off

“0-7”                          Upward transposition by the according number of half steps (0 resets to unison transposition)

“Shift” + “1-7”        Downward transposition by the according number of half steps

“8”                              Add an octave to the current transposition level

“Shift” + “8”            Subtract an octave from the current transposition level


Additional Libraries Used

DuetYourself makes use of RtAudio by Gary Scavone and Stk by Perry Cook and Gary Scavone.



Coming Soon!