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
Synth Provides audio synthesis suitable for direct analog output that can run in the background while other operations take place. Functions Synth Instantiate the synthesiser. add_node Adds a note to the next available position in the current piece of music. play Starts the music (previously constructed via the add_note and rest methods) playing. pause Stops the music playing but retains the current position in the piece. stop Stops the music playing and resets the piece to the beginning. set_repeat Sets whether or not the music should repeat once the end of the piece has been reached (false by default). set_instrument Sets which instrument should be used for synthesising the music. Provides audio synthesis suitable for direct analog output that can run in the background while other operations take place.
class Synth Instantiate the synthesiser.
Synth( PinName aout ) Starts the music (previously constructed via the add_note and rest methods) playing.
void play() Stops the music playing but retains the current position in the piece.
void pause() Stops the music playing and resets the piece to the beginning.
void stop() Sets whether or not the music should repeat once the end of the piece has been reached (false by default).
void set_repeat( bool repeat ) Sets which instrument should be used for synthesising the music.
void set_instrument( PluckedGuitar * instrument )
Instrument Provides an interface for basing instruments on. Functions Instrument Instantiates an instrument object. note Plays a single note, called by Synth. set_samplerate Sets the sample rate for this instrument. set_bpm Sets the speed of playback for this instrument in beats per minute. proc_sample Processes a sample, called periodically by the Synth class to output audio. Provides an interface for basing instruments on.
class Instrument Instantiates an instrument object.
Instrument() Plays a single note, called by Synth.
void note( Note note ) Sets the sample rate for this instrument.
void set_samplerate( int samplerate ) Sets the speed of playback for this instrument in beats per minute.
void set_bpm( int bpm ) Processes a sample, called periodically by the Synth class to output audio.
float proc_sample()
PluckedGuitar Provides a sound similar to a plucked guitar, implementing the Karplus-Strong algorithm. Functions PluckedGuitar Instantiates the plucked guitar instrument. note Plays a single note, called by Synth. proc_sample Process a sample, called periodically by the Synth class to output audio. Provides a sound similar to a plucked guitar, implementing the Karplus-Strong algorithm.
class PluckedGuitar : public Instrument Instantiates the plucked guitar instrument.
PluckedGuitar() Plays a single note, called by Synth.
void note( Note note ) Process a sample, called periodically by the Synth class to output audio.
float proc_sample()
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.
#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); } }
