I have been doing a lot of work on my web application Virtuoso, and if there is one thing I know, it is that midi is a pain in the butt! However, there are its pluses. Such as being an extremely compact file format which means quicker parsing/load times. The next update is going to be a major overhaul of the current engine, and I wanted to share something interesting with you before I released it.
Now, I am sure that most of you are familiar with basic sound concepts. Just as a refresher, sound is best described as a vibration or wave with some frequency that travels through a medium (e.g. air, water, etc.). We hear these waves when they make contact with our eardrums.
Of course, there are lots and lots of different kinds of sounds and that is due to the fact that sounds are not “pure.” A sound can be considered pure when there is only one wave that makes it up. A sine wave is an example of a pure sound, and so is a square wave. If you were to combine multiple you would get a sound that is a different, but is no longer pure. Even if the sounds are the same type, but have different frequencies, you are making an entirely different sound.
So without further ado, let’s start looking at the formulae for these simple waveforms!
Now when coding to generate usable raw sound data, there are two things you need. One is the frequency. In the equations that I will be showing, frequency will be represented by the variable f.
Two, is sample rate. This is what defines how many samples are in one second. This will be represented by the variable s
Also, when generating the waveform you need something to represent time. For that I will be using x
By far the most simplest waveform is the sine wave. Even if you are not a sound engineer, the concept of a sine wave should be fairly familiar.
So what does it sound like? It is very soft, and seems as though it is almost muffled.
In order to generate a sine wave with the correct frequency, however, we need to multiply by 2PI. This will give us a frequency of 1 * f / s, as you will see in the following formula.
This will give generate a sign wave with the specified frequency and sample rate
This is one of the most recognizable of all waveforms. Those of you who have played NES games will definitely know what I mean. Every time I think of this sound the word “Doot” pops into my head. A square wave is comparatively louder than all the other waveforms, even when they are at the same amplitude.
There is not much to be said about this waveform in terms of its formula. All you really need to do is take a sine wave and use the sgn function to get +1, 0, or -1.
I will use brackets”” to represent sign for this equation.
So yeah, it’s the exact same formula, but now has sgn to lock it into having only three unique values.
Saw Tooth Wave
Another simple wave form. The saw tooth wave sounds kind of like if a bug was buzzing around your ear! Although, that doesn’t make it any less of an interesting waveform. All you really need in order to generate this is the modulus (remainder) operator, and some subtraction, and you’ve got yourself a saw tooth wave.
This was by far the most difficult for me to figure out. Eventually, I gave up and decided to just look up how to create a generic triangle wave. Now that I think about this one is not much more complicated than the saw tooth wave. Just a couple of extra operations.
Triangle waves kind of sound like a mix between a sine and square wave. So the sound is not to harsh, but it still has some interesting qualities.
Well, I hope you enjoyed learning about these basic waveforms. With these, you can easilly make some very interesting sounds, as you will see in the future updates of Virtuoso.