Overview

An audio synthesis library capable of running alongside other activities, providing direct analog output. Currently the library is monophonic (only one note at a time) and can only synthesize the sound of a plucked guitar.

Library

Library location: http://mbed.org/projects/cookbook/svn/Synth/trunk/

API

SynthProvides audio synthesis suitable for direct analog output that can run in the background while other operations take place.
Functions
SynthInstantiate the synthesiser.
add_nodeAdds a note to the next available position in the current piece of music.
playStarts the music (previously constructed via the add_note and rest methods) playing.
pauseStops the music playing but retains the current position in the piece.
stopStops the music playing and resets the piece to the beginning.
set_repeatSets whether or not the music should repeat once the end of the piece has been reached (false by default).
set_instrumentSets which instrument should be used for synthesising the music.
class Synth
Provides audio synthesis suitable for direct analog output that can run in the background while other operations take place.
Synth(PinName aout)
Instantiate the synthesiser.
void play()
Starts the music (previously constructed via the add_note and rest methods) playing.
void pause()
Stops the music playing but retains the current position in the piece.
void stop()
Stops the music playing and resets the piece to the beginning.
void set_repeat(bool repeat)
Sets whether or not the music should repeat once the end of the piece has been reached (false by default).
void set_instrument(PluckedGuitar *instrument)
Sets which instrument should be used for synthesising the music.

InstrumentProvides an interface for basing instruments on.
Functions
InstrumentInstantiates an instrument object.
notePlays a single note, called by Synth.
set_samplerateSets the sample rate for this instrument.
set_bpmSets the speed of playback for this instrument in beats per minute.
proc_sampleProcesses a sample, called periodically by the Synth class to output audio.
class Instrument
Provides an interface for basing instruments on.
Instrument()
Instantiates an instrument object.
void note(Note note)
Plays a single note, called by Synth.
void set_samplerate(int samplerate)
Sets the sample rate for this instrument.
void set_bpm(int bpm)
Sets the speed of playback for this instrument in beats per minute.
float proc_sample()
Processes a sample, called periodically by the Synth class to output audio.

PluckedGuitarProvides a sound similar to a plucked guitar, implementing the Karplus-Strong algorithm.
Functions
PluckedGuitarInstantiates the plucked guitar instrument.
notePlays a single note, called by Synth.
proc_sampleProcess a sample, called periodically by the Synth class to output audio.
class PluckedGuitar : public Instrument
Provides a sound similar to a plucked guitar, implementing the Karplus-Strong algorithm.
PluckedGuitar()
Instantiates the plucked guitar instrument.
void note(Note note)
Plays a single note, called by Synth.
float proc_sample()
Process a sample, called periodically by the Synth class to output audio.


Example

The following example will play "Fly me to the moon" by Frank Sinatra on a plucked guitar. While the music is playing the foreground task will flash the LEDs.

Listen to an mp3 of this example.

#include "mbed.h"
#include "Synth.h"

Synth s(18);
void fly_me_to_the_moon(Synth *s) {
    //Fly me to the moon
    s->add_note(NOTE_C5, 1.5);
    s->add_note(NOTE_B4, 0.5);
    s->add_note(NOTE_A4, 0.5);
    s->add_note(NOTE_G4, 0.5);
    s->add_note(NOTE_F4, 1.5);
    s->add_note(NOTE_G4, 0.5);
    s->add_note(NOTE_A4, 0.5);
    s->add_note(NOTE_C5, 0.5);
    s->add_note(NOTE_B4, 1.5);
    s->add_note(NOTE_A4, 0.5);
    s->add_note(NOTE_G4, 0.5);
    s->add_note(NOTE_F4, 0.5);
    s->add_note(NOTE_E4, 3);
    s->add_note(NOTE_A4, 1.5);
    s->add_note(NOTE_G4, 0.5);
    s->add_note(NOTE_F4, 0.5);
    s->add_note(NOTE_E4, 0.5);
    s->add_note(NOTE_D4, 1.5);
    s->add_note(NOTE_E4, 0.5);
    s->add_note(NOTE_F4, 0.5);
    s->add_note(NOTE_A4, 0.5);
    s->add_note(NOTE_A_FLAT4, 1.5);
    s->add_note(NOTE_F4, 0.5);
    s->add_note(NOTE_E4, 0.5);
    s->add_note(NOTE_D4, 0.5);
    s->add_note(NOTE_C4, 2);
    s->add_note(NOTE_C_SHARP4, 1);
    s->add_note(NOTE_D4, 0.5);
    s->add_note(NOTE_A4, 0.5);
    s->add_note(NOTE_A4, 3);
    s->add_note(NOTE_C5, 1);
    s->add_note(NOTE_B4, 1);
    s->add_note(NOTE_G4, 5);
    s->add_note(NOTE_B3, 1);
    s->add_note(NOTE_C4, 0.5);
    s->add_note(NOTE_F4, 0.5);
    s->add_note(NOTE_F4, 3);
    s->add_note(NOTE_A4, 1);
    s->add_note(NOTE_G4, 1);
    s->add_note(NOTE_F4, 1);
    s->add_note(NOTE_E4, 5);
    s->add_note(NOTE_C5, 1.5);
    s->add_note(NOTE_B4, 0.5);
    s->add_note(NOTE_A4, 0.5);
    s->add_note(NOTE_G4, 0.5);
    s->add_note(NOTE_F4, 1.5);
    s->add_note(NOTE_G4, 0.5);
    s->add_note(NOTE_A4, 0.5);
    s->add_note(NOTE_C5, 0.5);
    s->add_note(NOTE_B4, 1.5);
    s->add_note(NOTE_A4, 0.5);
    s->add_note(NOTE_G4, 0.5);
    s->add_note(NOTE_F4, 0.5);
    s->add_note(NOTE_E4, 3);
    s->add_note(NOTE_A4, 1.5);
    s->add_note(NOTE_G4, 0.5);
    s->add_note(NOTE_F4, 0.5);
    s->add_note(NOTE_E4, 0.5);
    s->add_note(NOTE_D4, 1.5);
    s->add_note(NOTE_E4, 0.5);
    s->add_note(NOTE_F4, 0.5);
    s->add_note(NOTE_G4, 0.5);
    s->add_note(NOTE_A_FLAT4, 1.5);
    s->add_note(NOTE_F4, 0.5);
    s->add_note(NOTE_E4, 0.5);
    s->add_note(NOTE_D4, 0.5);
    s->add_note(NOTE_C4, 2);
    s->add_note(NOTE_C_SHARP4, 1);
    s->add_note(NOTE_D4, 0.5);
    s->add_note(NOTE_A4, 0.5);
    s->add_note(NOTE_A4, 3);
    s->add_note(NOTE_C5, 1);
    s->add_note(NOTE_B4, 1);
    s->add_note(NOTE_G4, 5);
    s->add_note(NOTE_A_FLAT4, 1);
    s->add_note(NOTE_A4, 0.5);
    s->add_note(NOTE_C4, 0.5);
    s->add_note(NOTE_C4, 3);
    s->add_note(NOTE_C4, 1);
    s->add_note(NOTE_D4, 1);
    s->add_note(NOTE_C4, 3);
}
int main() {
    PluckedGuitar *g = new PluckedGuitar();
    s.set_bpm(100);
    s.set_instrument(g);
    fly_me_to_the_moon(&s);
    s.set_repeat(true);
    s.play();
    DigitalOut led1(LED1);
    DigitalOut led4(LED4);
    led1 = 1;
    while(1) {
        led1 = !led1;
        led4 = !led4;
        wait(1);
    }
}