Puttering with Poetry in Mathematica

Puttering, playing, having fun: who could argue that these aren’t important aspects of learning?

This activity is intended for anyone who wants to learn in a playful manner about some of the ways that Mathematica can interact with our world. Knowing anything about mathematics is not a prerequisite for this exercise, but it may spark someone’s curiosity about the ways that this software can facilitate mathematical play. In this exercise, you will learn about the data of music and what can be done with it using Mathematica.

Obtaining Royalty-Free Audio Files

If you think you might want to publish the results of your efforts and wish to avoid legal entanglements, then you may want to seek out royalty-free sound files. One example of a website offering a collection of such content is the Free Music Archive.

There are countless ways of going about the process of finding audio files; that said, we will start by looking for an audio file of someone reading their own poetry aloud as well as for some instrumental tracks to act as background music.

By first selecting the poetry subpage, we can see that it contains a number of cleverly composed poems read by their authors. The poems cover a wide variety of subjects.

While not every poem will be to everyone’s taste, there’s a great deal of content to explore. For example, this poem, “Fight the Bedroom Tax,” is a satirical poem with a political slant. While I don’t fully understand the meaning of the poem, the poet’s voice is hypnotic. We’ll use this poem as our sample file going forward.

Download your audio file, rename it something simple (for example, “bedroomtax.mp3”) and place it in a folder that’s easy to find. Keep in mind that, if you’re using a Mac, you’ll want to save outside of your Documents folder, as the Import function we’ll be using may not find files saved there, being outside of $Path.

Once you’ve selected your poem, look for an instrumental piece that you feel could serve as a backing track for the chosen words. For example, if you’d like to add some pizzazz, then “no obstante… sin más” by cátodo dúo might be your choice. If that’s a bit too wild, then “Damaged Soundmachine” by Kielokaz might be a more palatable option. You can’t go wrong with a flute-guitar combo!

Download your instrumental track and put it in the same location as your poetry MP3. For example, I’ve renamed the backing track “damaged.mp3” and moved it into the same folder as “bedroomtax.mp3”.

Importing the Audio Files into Mathematica

Once we have collected the sound files we want to use, we are ready to begin exploring the audio. One caveat: the following commands are suitable for Mathematica 11. That said, variations of this process work in Mathematica 10 and possibly earlier versions.

To import the audio files into Mathematica, type the following:

As mentioned, the Import function will only pull up files under $Path by default, which doesn’t include your Documents folder on a Mac. More specifically, Import will not find files saved under $UserDocumentsDirectory, which is outside of $Path. If you save elsewhere on your computer, regardless of operating system, you shouldn’t have an issue.

Upon pressing Shift + Enter (both at the same time) to execute the Mathematica cell, two buttons will appear below the output. These buttons will allow you to play the audio.

It is important to view what the data looks like before making alterations to it. In order to see this, extract the first five terms of the audio data by typing the following:

To save time, type this command in a new cell below the first and press Shift + Enter.

The length of the array c is equal to 2, which means that the audio data consists of two arrays of numbers. Incidentally, this result shows that the audio was recorded in stereo, or through a two-channel recording.

After we get this value, we can extract the data from each channel through Take. Type in the following:

The code Take[c[[1]], 5] extracts the first five numbers in the first list, while Take[c[[1]], 5] extracts the first five numbers of the second list.

Now that we are familiar with the kind of data we have in these audio files, we are ready to combine them into one file. This part is surprisingly easy. We simply add the numbers from the poetry reading to the numbers from the flute-guitar track.

But not so fast! There’s an error message.

The error message shown in the image tells us that the poetry and instrumental piece do not have the same length. If we were to try to add the arrays {1,2} and {3,4,5}, we would have a problem. That’s why we must make the tracks compatible.

To do this, we first need to know their lengths. We’ll use the Length command again.

We have lengths 4148352, 4148352, 11908224 and 11908224, respectively. Basically, the flute-guitar track is much longer than the poetry. To fix this, we can simply chop off and discard the last 7759872 numbers in the instrumental piece by typing the following:

Now we can add the two tracks together without drama.

It takes some time to perform this sum, but it is well worth the wait. Once the tracks are added, it’s time to listen to the fruits of your labor.

The 2000 that appears in this function is the playback frequency. If we now play this new audio unaltered, we get an eerie noise, like something from the film 2001: A Space Odyssey.

This eerie sound is okay at first because it’s important for getting to know how things work. All the same, since we are not HAL, we need to make the composition fit for human ears, so we must increase the playback frequency.

If you go back and look at the time length of the original poem — in my case, the “Fight the Bedroom Tax” poem — it is 1 minute and 34 seconds, which equals 94 seconds. We need our new track to be 94 seconds in length to match this. Some trial and error shows that we must increase the frequency to about 44000 Hz. Given that 44,100 Hz is a typical sample rate for audio, this number makes sense.

With all this in mind, we should type the following:

If you’d like to make the background music louder, you can multiply d by 1.2 and repeat the previously shown steps, taking care to pay attention and not use headphones (loud sounds can damage eardrums and speakers). Multiplying by a number greater than 1 amplifies volume, so that ability must not be abused.

Finally, I’ll type in the following:

This code will export the finished product, which can then be found in the same default folder where the other two files were originally placed. Once you’ve created your new MP3, you can put the file on your iPod or smartphone and head out for a jog.

Taking Things Further

Much more can be done with this exercise. Have you ever wanted to play a piece of music backward? Mathematica allows you to do just that. The built-in function Reverse will reverse the order of the numbers in an array. You can even incorporate your own musical compositions and use Mathematica to become the next Mozart.

If you’d like to see me break down this process in more detail, you can watch this video on my YouTube channel Maths with Sam, where I explore how mathematics intersects with the world through video learning.

About the blogger:

Samuel Hambleton

Sam wants to earn an honest living working with mathematics. Having been excluded from his university studies in Queensland, Australia, early on due to poor grades, he returned home to California to work for his family maintaining logging roads and building bridges around the Sequoia and Yosemite parks. Sam learned about the merits of hard work and resumed his studies in Queensland, receiving a first-class honors degree in mathematics.

Despite being initially knocked back, he persisted, starting a Master of Philosophy degree in Number Theory while working for a $2 shop. He spent nearly a month preparing an oral presentation for his degree, but after it was well-received, he was subsequently awarded an internal Ph.D. Scholarship by the University of Queensland and offered teaching work in Numerical Analysis. Because of his fears of public speaking, Sam took this teaching work very seriously and eventually learned to be an effective communicator. He learned to appreciate the difficulties and challenges that students face in learning math and feels that the fees that students pay should not be taken for granted.

Sam discovered Mathematica in his first year of university studies, where it helped him maintain his interest in mathematics and overcome his limitations. Sam wants to share the power of Mathematica with as many people as he can reach. He has started a YouTube channel devoted to teaching mathematics courses together with Mathematica in order to contribute to students’ learning and to eventually build a consistent income. He has ten years of lecturing experience and three consecutive years of full-time teaching experience and has jointly authored a 500-page book with H. C. Williams called Cubic Fields with Geometry. Although not currently lecturing, Sam hopes to persist with his first love, mathematical research enhanced by Wolfram technologies. He can be found on LinkedIn and reached via e-mail.

--

--

Tech-Based Teaching Editor
Tech-Based Teaching: Computational Thinking in the Classroom

Tech-Based Teaching is all about computational thinking, edtech, and the ways that tech enriches learning. Want to contribute? Reach out to edutech@wolfram.com.