
Nucleo F446 Envelope Generator
Revision 0:c5e94349541f, committed 2018-06-30
- Comitter:
- ryood
- Date:
- Sat Jun 30 08:51:09 2018 +0000
- Commit message:
- first commit
Changed in this revision
diff -r 000000000000 -r c5e94349541f EnvelopeADSR.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EnvelopeADSR.h Sat Jun 30 08:51:09 2018 +0000 @@ -0,0 +1,141 @@ +/* + * ADSR Type Envelope Class + * + * 2018.06.28 + * + */ + +#ifndef _ENVELOPE_ADSR_H_ +#define _ENVELOPE_ADSR_H_ + +class EnvelopeADSR +{ +private: + enum EnvelopeState { + ST_ATTACK, + ST_DECAY, + ST_SUSTAIN, + ST_RELEASE + }; + +public: + EnvelopeADSR() + { + count = 0; + amplitude = 0; + setADSR(0, 0, 0.0f, 0); + } + + void setADSR(int _attack, int _decay, float _sustain, int _release) + { + attack = _attack; + decay = _decay; + sustain = _sustain; + release = _release; + + if (attack != 0) { + attackRatio = 1.0f / attack; + } + else { + attackRatio = 0.0f; + } + if (decay != 0) { + decayRatio = -(1.0f - sustain) / decay; + } + else { + decayRatio = 0.0f; + } + if (release != 0) { + releaseRatio = -sustain / release; + } + else { + releaseRatio = 0.0f; + } + } + + void gateOn() + { + isGateOn = true; + count = 0; + amplitude = 0.0f; + state = ST_ATTACK; + } + + void gateOff() + { + isGateOn = false; + state = ST_RELEASE; + } + + float getAmplitude() + { + return amplitude; + } + + long getCount() + { + return count; + } + + int getState() + { + return state; + } + + float tick() + { + float currentAmplitude = amplitude; + + switch (state) { + case ST_ATTACK: + amplitude += attackRatio; + break; + case ST_DECAY: + amplitude += decayRatio; + break; + case ST_SUSTAIN: + // do nothing + break; + case ST_RELEASE: + amplitude += releaseRatio; + break; + } + + // Limiter + if (amplitude < 0.0f) { + amplitude = 0.0f; + } + else if (amplitude > 1.0f) { + amplitude = 1.0f; + } + + count++; + if (count < attack) { + state = ST_ATTACK; + } else if (count < attack + decay) { + state = ST_DECAY; + } else if (isGateOn) { + state = ST_SUSTAIN; + } + + return currentAmplitude; + } + +private: + int attack; + int decay; + float sustain; + int release; + + float attackRatio; + float decayRatio; + float releaseRatio; + + long count; + bool isGateOn; + EnvelopeState state; + + float amplitude; +}; + +#endif //_ENVELOPE_ADSR_H_
diff -r 000000000000 -r c5e94349541f main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Jun 30 08:51:09 2018 +0000 @@ -0,0 +1,69 @@ +#include "mbed.h" +#include "EnvelopeADSR.h" + +#define TITLE_STR1 ("Nucleo F446 EG") +#define TITLE_STR2 ("2018.06.28") + +// Internal DAC +AnalogOut Aout0(PA_4); + +// ADSR POT +AnalogIn Ain0(PC_2); +AnalogIn Ain1(PC_3); +AnalogIn Ain2(PC_1); +AnalogIn Ain3(PC_0); +AnalogIn Ain4(PB_0); +AnalogIn Ain5(PA_1); +AnalogIn Ain6(PA_0); +AnalogIn Ain7(PC_4); + +// Envelope +EnvelopeADSR envelope; +const int sampleMagnify = 10000; // POTの読み取り値が1.0fの場合のA/D/Rの値 + +// Gate +InterruptIn gateIn(USER_BUTTON); + +void gateInterruptRise() { + envelope.gateOff(); +} + +void gateInterruptFall() { + envelope.gateOn(); +} + +int main() +{ + printf("\r\n%s %s \r\n", TITLE_STR1, TITLE_STR2); + + gateIn.rise(&gateInterruptRise); + gateIn.fall(&gateInterruptFall); + + while(1) { + float a0 = Ain0.read(); + float a1 = Ain1.read(); + float a2 = Ain2.read(); + float a3 = Ain3.read(); + float a4 = Ain4.read(); + float a5 = Ain5.read(); + float a6 = Ain6.read(); + float a7 = Ain7.read(); + + // Envelope 1 + int a = a0 * sampleMagnify; + int d = a1 * sampleMagnify; + float s = a2; + int r = a3 * sampleMagnify; + + envelope.setADSR(a, d, s, r); + float amplitude = envelope.tick(); + + Aout0.write(amplitude); + + /* + printf("%.4f\t%.2f\t%.2f\t%.2f\t%.2f\t\r\n", + amplitude, a0, a1, a2, a3); + */ + + } +}
diff -r 000000000000 -r c5e94349541f mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Jun 30 08:51:09 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/a7c7b631e539 \ No newline at end of file