After shaping up the waves and sending them to the DAC, you are thinking that all it can do is to hear a loud smooth beep from the speakers. Of course, you won’t be just generating sine waves alone – it sounds bland. With this “platform” you have built, it has a lot of room for improvement! Why not “shape” the sine waves to sound a little bit richer?

In this example application, you can do some extra tricks on the sine wave calculation. Remember the DDS algorithm in the past tutorial? One of the extra tricks to increase the timbre of the sound is to use the FM synthesis. You will get to know how it works in this application:

FM Synthesis

If you have listened to the music of the 80s, you get to hear those electronic sound effects which sounds very metallic, harsh and/or artificial in nature. These are done by modulating a sine wave with another sine wave:

f(t) = Acsin(2πfct + Amsin(2πfmt))

Ac is amplitude of the carrier.

fc is the frequency of the carrier.

Am is amplitude of the modulator.

fm is the frequency of the modulator.

t is time in seconds.

Basically, you see another sine wave inside the sine wave in the formula. This significantly alters the shape of the sine wave, giving some very unique sounds. Changing the ratios of the frequency of the carrier, fc and the frequency of the modulator, fm, alters the behaviour of the wave. This behaviour is also changed by changing the amplitude of the modulator Am too.

If the Am is zero, the modulator part is cancelled out, leaving this:

f(t) = Acsin(2πfct)

You are then left with the plain sine wave. In order to obtain a more rich sound, the modulator amplitude Am must be more than zero.

In addition, you can watch a bit of the FM synthesis tutorial for your further understanding:

To use this formula with the DDS algorithm is nothing more than just adding a few extra variables for the modulator:

Do note that the FM synthesis requires at least two oscillators (or sine wave generator in this tutorial) to come up with the expected sound quality. That is why you see two tuning words and two accumulators, each for the modulator, and another for the carrier.

The following code generates one (1) FM channel using the variables mentioned earlier:

Afterwards, you have to sum up the outputs and it is written in the buffer for output. You have to average it by 2 (since it has two FM channels calculated here) to keep the output value within -32767 and +32767.

final_output = ( output1 + output2 ) / 2

The activity of adding the other channels (2,3,4,5 and 6) will be an exercise for you.

Attention: If you are using SK1632 with PIC32MX1xx/2xx series, do not use more than 6 channels due to the lower processing power of the processors.

That’s what a music box would sound like. It’s all nice… but it is still missing something? The waves being output would have the constant volume all the time? Naturally, if you have hit a note on a piano (or any other music instrument), the sound comes out loud and then fades away. So to make it even sound more pleasing, the sound being generated by the microcontroller fades away after striking the note.

Envelope generation:

Similarly to the DDS method, a 256-sample table of a decay envelope is used.

To understand the decay envelope, here is a small picture about it:

Source: http://www.integracoustics.com/MUG/MUG/articles/phase/

The yellow portion is the decay envelope, and look how the amplitude of the sine wave slowly decreasing in an exponential manner.

In the following code, there is a little counter (envelope_count1) thats counts up every each sample is generated, and resets to 0 when it has exceeded or equal the ENVELOPE_SPEED value. Afterwards, the envelope_ptr1 (the index number of the envelope table) increases by one each time. If the envelope_ptr1 equals or exceeds the maximum envelope table size (256 samples), it is “saturated” to 255.

Due to the approximation of the formula y = e-xin the form of table, the resulting output of the envelope value will be very small, so the envelope_ptr1 is saturated to 255 every time, giving the envelope output a value very close to zero. This means the sound is loud at the begining, but slowly decays until it is almost inaudible.

With this envelope now, you multiply the sample of the envelope with the sample of the FM channel:

g(t) = Aenvelope*(Acsin(2πfct + Amsin(2πfmt)))

Aha, now this one sounds like it’s hitting a note in a music instrument.

To sum this all up, you add all the outputs of each FM channel (with each of the FM channel being multiplied by the envelopes) and put it into the buffer:

In the temp_output1, the total of the outputs from 6 FM channels are divided by 16 instead of 6 is due to controlling the overall volume so that the output remains in the range between -32767 and 32767. Be reminded that the DAC only takes in 16-bit signed values, so exceeding them would give you nasty noise in the output.

Combining it all together: Music Box

This tutorial is all about playing music on the PIC32 microcontroller. Now you are able to play one! We use Len Shustek’s music parser program (source: https://github.com/LenShustek/arduino-playtune) to process the midi files into a simpler C array for the program in the PIC32 to play the music. The parsing is done in the body of the program – it looks very complicated, but you do not need to fully understand the parsing. The purpose of the tutorial is to show you how to effectively use the I2S and the DMA so that you can deploy your audio applications in an effective manner.

Exercise 1: Using the sample program supplied, change the carrier and the modulator’s ratio. What will you hear from it? Hint: CARRIER_AMPL and MOD_AMPL in the defines!

Exercise 2: Grab a midi file of your favourite song and use Len Shustek’s midi tunes software to convert them to a C array form, and replace the output with the one in the songdata.h.

Exercise 3: Play with the modulator’s amplitude on one of the FM channels! Hint: look out for the “unsigned longamplitudeX_m = _____“, where X is the FM channel’s number.