A MIDI piano synthesizer that implements the Karplus Strong physical modeling algorithm.
Dependencies: mbed USBDevice PinDetect
Audio/KarplusStrong.h@22:b800e1766647, 2016-04-25 (annotated)
- Committer:
- asuszek
- Date:
- Mon Apr 25 23:58:15 2016 +0000
- Revision:
- 22:b800e1766647
- Parent:
- 19:894c31ee9ad4
Fixed LED bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
asuszek | 13:bb0ec927e458 | 1 | |
asuszek | 13:bb0ec927e458 | 2 | /** |
asuszek | 13:bb0ec927e458 | 3 | * The processor used for teh implementation of Karplus strong. |
asuszek | 13:bb0ec927e458 | 4 | * |
asuszek | 13:bb0ec927e458 | 5 | * @author Austin Suszek |
asuszek | 13:bb0ec927e458 | 6 | */ |
asuszek | 13:bb0ec927e458 | 7 | |
asuszek | 13:bb0ec927e458 | 8 | #ifndef AS_KARPLUS_STRONG_H |
asuszek | 13:bb0ec927e458 | 9 | #define AS_KARPLUS_STRONG_H |
asuszek | 13:bb0ec927e458 | 10 | |
asuszek | 13:bb0ec927e458 | 11 | #include <math.h> |
asuszek | 13:bb0ec927e458 | 12 | #include <stdlib.h> |
asuszek | 13:bb0ec927e458 | 13 | #include <time.h> |
asuszek | 13:bb0ec927e458 | 14 | |
asuszek | 13:bb0ec927e458 | 15 | #include "../Constants.h" |
asuszek | 13:bb0ec927e458 | 16 | |
asuszek | 13:bb0ec927e458 | 17 | class KarplusStrong { |
asuszek | 13:bb0ec927e458 | 18 | public: |
asuszek | 13:bb0ec927e458 | 19 | |
asuszek | 13:bb0ec927e458 | 20 | /** |
asuszek | 13:bb0ec927e458 | 21 | * Constructor |
asuszek | 13:bb0ec927e458 | 22 | */ |
asuszek | 13:bb0ec927e458 | 23 | KarplusStrong(); |
asuszek | 13:bb0ec927e458 | 24 | |
asuszek | 13:bb0ec927e458 | 25 | /** |
asuszek | 13:bb0ec927e458 | 26 | * Signal the start of a new note. |
asuszek | 13:bb0ec927e458 | 27 | * |
asuszek | 13:bb0ec927e458 | 28 | * @param key The integer representation of the note |
asuszek | 13:bb0ec927e458 | 29 | * @param velocity The float representation of the velocity (NOT int!) |
asuszek | 13:bb0ec927e458 | 30 | */ |
asuszek | 13:bb0ec927e458 | 31 | void midiNoteOn(int key, float velocity); |
asuszek | 13:bb0ec927e458 | 32 | |
asuszek | 13:bb0ec927e458 | 33 | /** |
asuszek | 13:bb0ec927e458 | 34 | * Create the initial buffer sample for the specific index. |
asuszek | 13:bb0ec927e458 | 35 | * |
asuszek | 13:bb0ec927e458 | 36 | * @param bufferIndex The index to process |
asuszek | 13:bb0ec927e458 | 37 | * @return The new sample to save in the buffer |
asuszek | 13:bb0ec927e458 | 38 | */ |
asuszek | 13:bb0ec927e458 | 39 | float fillBuffer(const int bufferIndex); |
asuszek | 13:bb0ec927e458 | 40 | |
asuszek | 13:bb0ec927e458 | 41 | /** |
asuszek | 13:bb0ec927e458 | 42 | * Process the existing buffer for the specific index. |
asuszek | 13:bb0ec927e458 | 43 | * |
asuszek | 13:bb0ec927e458 | 44 | * @param inputSample The sample to process from the buffer |
asuszek | 13:bb0ec927e458 | 45 | * @return The modified sample to save in the buffer |
asuszek | 13:bb0ec927e458 | 46 | */ |
asuszek | 13:bb0ec927e458 | 47 | float processBuffer(const float inputSample); |
asuszek | 13:bb0ec927e458 | 48 | |
asuszek | 13:bb0ec927e458 | 49 | private: |
asuszek | 13:bb0ec927e458 | 50 | |
asuszek | 13:bb0ec927e458 | 51 | static void initializeNoiseSeed(); |
asuszek | 13:bb0ec927e458 | 52 | static float getRand() { |
asuszek | 13:bb0ec927e458 | 53 | return float(rand()) / float(RAND_MAX); |
asuszek | 13:bb0ec927e458 | 54 | }; |
asuszek | 15:aa5a4c350251 | 55 | static float getRandSample() { |
asuszek | 15:aa5a4c350251 | 56 | return 1.0 - 2.0 * getRand(); |
asuszek | 15:aa5a4c350251 | 57 | }; |
asuszek | 13:bb0ec927e458 | 58 | |
asuszek | 13:bb0ec927e458 | 59 | float lastOutput; |
asuszek | 13:bb0ec927e458 | 60 | float filterCoefficient; |
asuszek | 13:bb0ec927e458 | 61 | float nextFilterCoefficient; |
asuszek | 13:bb0ec927e458 | 62 | |
asuszek | 16:b25ca34a705f | 63 | static const float pluckDampingVariationMin; |
asuszek | 16:b25ca34a705f | 64 | static const float pluckDampingVariationDifference; |
asuszek | 16:b25ca34a705f | 65 | float pluckDampingCoefficient; |
asuszek | 16:b25ca34a705f | 66 | |
asuszek | 13:bb0ec927e458 | 67 | float lowpassFilter(const float output, const float input, const float smoothingFactor); |
asuszek | 13:bb0ec927e458 | 68 | |
asuszek | 13:bb0ec927e458 | 69 | }; |
asuszek | 13:bb0ec927e458 | 70 | |
asuszek | 13:bb0ec927e458 | 71 | #endif |