Math Music

I don’t recall what I was puzzling over at the time but it was some set of formulas that I was struggling to understand the relationship between.  In my frustration I started to wonder if I were to be able to “hear” the equations would their relationship be easier to grasp, like harmony.  Our brains are amazing signal processors that excel at unraveling and interpreting complex patterns and relationships in an intuitive fashion that our conscious thinking minds seem to struggle with.  Perhaps there is a world of understanding to be had were we to employ our senses in unconventional ways.  This idea quickly grows legs beyond simply internalizing some mathematical formula and is certainly food for future work.

While that may have been the initial concept, it eventually devolved into simply trying to make music with math…

I tinkered down a couple different routes before deciding that the avenue I would pursue would be to write a virtual Midi instrument rather than software that actually produced sound.  What I lose by this decision is that Midi is note based instead of frequency based, additionally you need to track notes to “turn them off” which forced me to write a multithreaded application to get the robustness I wanted.  What I gained though was the ability to leverage sophisticated music software to process and play the midi music.  After finding a decent library I was able to push midi commands without too much trouble.  Additionally I installed a piece of software that emulates a midi device.  This was a life saver as I could push midi notes from code to this emulator which would parlay them as virtual physical device on my pc that I could then latch onto with my preferred music authoring software, FL Studio.  With everything connected I had a working proof of concept that I developed into a rather powerful framework where I can push midi notes and record and play them with the instrument of my choosing in FL Studio.

I decided to start with listening to Perlin Noise, which is a topic truly worthy of its own article.  I have used perlin noise in all kinds of random generators before and since and I understand it’s used commercially for defining organic textures and patterns; you’ve seen it in computer generated effects in movies like smoke and clouds.  For sake of argument, visualize perlin noise like these pictures below where Black = 0, White = 1 and grey is everything in between with high precision.  Here are couple different samples produces by tweaking some of the inputs into the perlin algorithm.

So I started by simply defining two points on these 2d perlin maps and played out the “notes” between them by simply scaling the pitch of the note based on the perlin value.  While it worked, I felt like the perlin route was simply not likely to produce anything with musicality. I began to feel that to produce something musical I would need to use equations with more inherent repetition.  The random nature of the perlin noise just was pretty much that, random noise.

I went back to my answer for everything it seems, waves.

I came up with the following approach to define music mathematically within my software.  I defined the pitch as a trigonometric function where the vertical axis is pitch and the horizontal time. I then went on to define the duration of the note based on second trigonometric function and likewise for velocity.  Lastly I defined a pair of functions where notes exist when they intersect.

A trigonometric function is defined by the equation: f(t) = A * f(Bt+C) + D where f = Any Trig function,  A = Amplitude, B = Period, C = Phase Shift and D = Vertical Shift.

The parameters of each of these functions can be set in the application however I want.  To add further flexibility, in code I created an object oriented trigonometric function class where these parameters (f, A, B, C & D) could themselves be defined by other trigonometric functions… and likewise the parameters of those functions and so on.  With this framework in place my software can easily define very complex wave patterns and (hopefully) by extension very complex musical arrangements.

These are the functions I just happened to have in place the last time I opened the code.  You can see an example of a nested function in the Phase Shift (C ) of the Note Function:

  • Note: F= SIN, A=1, B=Pi, C=[F=SIN, A=1, B=1, C=0, D=0], D=0
  • Note Intersection: F= SIN, A=1, B=2, C=1, D=1
  • Pitch: F= TAN, A=1, B=Pi, C=2, D=1
  • Velocity: F= SIN, A=1, B=1, C=0, D=0
  • Duration: F= SIN, A=1, B=1, C=0, D=0

So with these parameters in place, here is what the screen looks like in my tester application. At the top it actually plots the functions which is pretty sexy:

And here is what that sounds like:

Ok, so it isn’t a masterpiece but recognize that there is a certain musicality with its obvious periodicity yet evolving nuances that you don’t get from simple noise.  Better functions could yield some interesting music I suspect as well as running a couple of these programs in parallel so that instead of a single melody you can have a whole band!

Off-topic: The title of this article is Math Music because that is apparently what I named the program when I first created the project.  Well, every time I read it I hear in my head “Mathman! Mathman! Mathman!”.  Does anyone remember Math Man from the Square One tv show in the 80’s or am I the only one?!

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.