VOOZH about

URL: https://towardsdatascience.com/music-in-python-part-2-4f115be3c781/

⇱ Music in Python Part 2 | Towards Data Science


Music in Python Part 2

Consonance and Dissonance Visualized

5 min read

Hands-on Tutorials

👁 Image by author
Image by author

In honor of the World Piano Day (88th day of the year, since there are 88 keys on a standard piano), this is a short follow-up to my previous post on audio signal processing and analysis in Python. This is a bit more music theory heavy with emphasis on visuals and audio rather than coding.

In this article, I will explore why we associate certain sounds with sweetness and lightness while others make us feel tense. I will also add the sustain pedal to my synthetic piano from the previous post to make it sound more realistic.


Consonance and Dissonance

In the previous post, I talked about the overtones having frequencies that are of whole number multiples of the fundamental note. For instance, the concert A has a frequency of 440Hz, and the second overtone has frequency of 880Hz (the A one octave above). Most people would probably find the sound of two A’s being played simultaneously pleasing. However, that’s not necessary the case if we just press any two keys at the same time. Turns out this has to do with the frequency of the notes that we are combining.

Perfect Consonance

The two A’s or any notes one octave apart (called "octave" for short) is an example of perfect consonance in music. We can take a look at what the combined C4 (middle C) and C5 wave looks like using Matplotlib.

👁 Image

You can see that it takes C4 (almost) exactly twice the amount of time to finish one cycle (from peak to peak) than C5. This also makes the peaks of the two notes line up quite close to one another, making a new peak around the same place. And you can tell that it’s a C from the output audio, though it sounds a bit higher than the middle C.

Imperfect Consonance

When we add some other notes together, the sound waves might not line up as nicely as the octave. What if instead of whole number multiples, we add notes that are fractional multiples of each other together?

The plot below shows the combination of C4 (261.63Hz) and E4 (329.62Hz), which have a ratio of about 4:5. In other words, when C4 finishes 4 cycles, E4 finished 5 in the same amount of time. In music, this particular combination is an example of major thirds, which in turns has imperfect consonance. We can see that peaks don’t always line up but every so often they do.

👁 Image

The output doesn’t quite sound like a C or E anymore, but we still find it pleasing or bright as a coherent note.

Dissonance

What if we have two notes that just don’t seem to have their cycles coincide with one another? The peaks don’t line up and they end up cancelling each other out and creating the strange looking wave below.

👁 Image

Although there are places where C and C sharp (c4, or D flat) almost line up with other another, it takes much longer to do so than the major thirds example above. Here the ratio between C (261.63Hz) and C sharp (277.18 Hz) is not close to a nice fraction like the major thirds. We often associate the resulting sound with tension and harshness as we can hear the different frequencies.


The Sustain Pedal

In my opinion, the sustain pedal adds magic to the piano. It connects the notes while your fingers are moving and blends the sound together, which then allows for consonance (or dissonance) among successive notes.

When pressed, the sustain pedal holds on all the keys that are played until you release it. Adding to the piano we built in the last post, we will "press" the pedal at the beginning of each bar and "release" it the end so that all notes within a measure blends together.

We will also modify our function that gets song data to use the pedal.

Of course, it’s not a rule to have to press the pedal at the beginning of a measure or to use it a only once within a measure. I coded it this way so that we can still get the wonderful echoey effect without having to specify where exactly we need to use the pedal. Below is a comparison of C major scale with and without the pedal.

Applying sustain to our Twinkle Twinkle example:


Conclusion

What’s considered consonance and dissonance in music changes through time, context, and genre. Nonetheless, when used effectively, they add unique characteristics and build narrative. The sustain pedal on a piano allows blending of notes which amplifies the relationship among them and let the pianist guide their audience through the piece. Thank you for reading thus far and hope you are enjoying music!

All code on Github.

Bonus Examples

An excerpt of Innocent from Castle in the Sky, by Joe Hisaishi. Some examples of consonance here (among the notes that makes up the chords, and the sequence of notes in the melody).

Excerpt of Requiem from Nausicaä of the Valley of the Wind, once again by the wonderful Joe Hisaishi. There are both consonance and dissonance here, where the use of dissonance makes piece sound more mysterious.


Written By

Katie He

Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.

Write for TDS

Related Articles