The Sines of Music

Glory Yusuf
3 min readApr 30, 2021
Photo by Anne Nygård on Unsplash

Yes, I know. I apologize for the atrocious pun that is the title of this article. Goes with the territory. On to business now, though. I read a wonderful article on playing music in Python last week, which you can check out here, and naturally, it piqued my curiosity.

For a start, it was based on a topic I like a lot. Wave theory was something I understood very quickly in school and revisiting it seemed like a good idea. Another reason I wanted to try it out was that Jupyter Notebook was recommended. I don’t normally use Jupyter because unlike PyCharm it doesn’t show you as many hints with the code you’re writing. It was a fresh challenge, one I (reluctantly) decided to undertake. After importing the required libraries; NumPy, IPython, and matplotlib, I was good to go.

I wrote the lines of code that show the primary properties of the wave, namely the frequency and the wavelength, and the output I got was a graph showing what the wave would look like with the set parameters.

Nice.

To understand exactly how said properties would affect the wave, I played around with them a bit, producing different waveforms every time.

The code I wrote and the output I got!

Next came playing the actual sounds. For this, I had to pick a base frequency and how long I wanted to play it for. Sadly, I didn’t reduce the volume of my headphones when playing the sound. A very bad idea. Then I just experimented with different frequencies and displayed them on the graph. It was coming together very nicely. Once, I even took the frequencies to the ultrasound range(above human hearing, that is). Hearing nothing but a slight buzzing felt really weird, though.

Last came the pièce de résistance, playing the musical scale. Building on the earlier foundation of picking a baseline (which, in this case, was a particular frequency. 220 Hertz, to be precise), working my way up the scale in incremental steps. To put it simply, it was like a staircase, with the bottom step being 220 Hz, increasing by specific quantities until it came to a peak. It was quite fun playing ‘Mary had a little lamb’ in Python afterward.

As I am no Beethoven or Mozart, I regrettably cannot produce any symphonies with my little program. What I did do, however, was learn. Important skills came into play, skills I am grateful to have learnt. This little project reinforced the lesson I was taught so many months ago:

“The only way to learn programming is to program”

A few days ago, I was talking to my brother about the whole process. He then goes, “That’s nice, but what’s its use?” “Um…” I replied wisely, before thinking about it. I then enthused that if you refined the code a bit, added a few bits and bobs, and with some really good app design, you could make it into an app you could use to see the graphs of sound waves being received by a system or just studying waves in general. Writing code is nice and all, but it needs to have practical use. After all, programs are written to solve real-world problems. It won’t do to spend hours on a program that has no actual use. It’s been an experience, from writing “Hello World” to writing increasingly complex lines of code. It’s not quite the million-dollar idea, but hey, you never know. Thanks for reading!

Reach out to me on Twitter: https://twitter.com/gloryyusuf52

--

--