Austin Suszek / Mbed 2 deprecated MIDISynthesizer

Dependencies:   mbed USBDevice PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Synthesizer.h Source File

Synthesizer.h

00001 /**
00002  * The class representing a single monophonic voice.
00003  * This class holds and processes its own buffer and play state.
00004  *
00005  * Note: This class also holds implementations for each of the different instruments.
00006  * In a perfect OOP world this would be extracted out into an interface with a
00007  *   concrete class for each instrument, but in order to keep overhead low, I
00008  *   went for an array of member function pointers instead.
00009  *
00010  * @author Austin Suszek
00011  */
00012 
00013 #ifndef AS_SYNTHESIZER_H
00014 #define AS_SYNTHESIZER_H
00015 
00016 #include "../Constants.h"
00017 #include "../mbed.h"
00018 #include "KarplusStrong.h"
00019 
00020 /**
00021  * An enum representing the possible states of the synthesizer.
00022  */
00023 enum SynthState {
00024     OFF,
00025     FILL,
00026     SUSTAIN,
00027     RELEASE  
00028 };
00029 
00030 class Synthesizer {
00031 public:
00032     
00033     /**
00034      * Constructor
00035      */
00036     Synthesizer();
00037     
00038     /**
00039      * Signal the beginning of a note being pressed.
00040      *
00041      * @param key The integer representation of the note
00042      * @param velocity (optional) The note velocity in the range of 0-127
00043      */
00044     void midiNoteOn(int key, int velocity);
00045     
00046     /**
00047      * Signal the end of a note being pressed.
00048      */
00049     void midiNoteOff();
00050     
00051     /**
00052      * Get the next sample to output, and process the buffer for the next period.
00053      */
00054     float getSample();
00055     
00056     /**
00057      * Returns false for SynthState OFF and true for all other states.
00058      */
00059     bool isPlaying();
00060     
00061     /**
00062      * Getter for the voiceIndex variable.
00063      */
00064      int64_t getVoiceIndex(); 
00065     
00066     /**
00067      * Switch to a new synth instrument.
00068      *
00069      * @param direction 1 for next, -1 for previous.
00070      * @return The new index being played.
00071      */
00072     int nextSynth(int direction);
00073     
00074     /**
00075      * A getter for the current key.
00076      * It is always overwritten to the most recent key.
00077      */
00078     int getCurrentKey();
00079     
00080     /**
00081      * Processor functions. These are responsible for all procesing of the buffer based on their instrument.
00082      */
00083     void processKarplusStrong();
00084     void processSine();
00085     void processTriangle();
00086     void processSquare();
00087     void processSaw();
00088 
00089 private:
00090 
00091     float buffer[(C::SAMPLE_RATE / C::MIN_FREQUENCY) + 1];
00092     int bufferIndex;
00093 
00094     SynthState currentState;
00095     KarplusStrong karplusStrong;
00096     
00097     int currentKey;
00098     int bufferSize;
00099     float velocity;
00100     
00101     int nextBufferSize;
00102     float nextVelocity;
00103     
00104     int64_t voiceIndex;
00105     static int64_t nextVoiceIndex;
00106     bool fromDisabled;
00107     
00108     // A process that just outputs input.
00109     void identityProcess();
00110     void checkFillBounds();
00111     
00112     volatile int processorIndex;
00113     
00114 };
00115 
00116 #endif