KIK 01 Prototype 05
Dependencies: AverageMCP3008 mbed-rtos mbed mcp3008
Fork of KIK01_Proto03 by
main.cpp@2:8dff77a1ee4d, 2017-06-04 (annotated)
- Committer:
- ryood
- Date:
- Sun Jun 04 05:53:01 2017 +0000
- Revision:
- 2:8dff77a1ee4d
- Parent:
- 1:f9b967ae26e4
- Child:
- 3:f89b400cfe57
Ticker wave and envelope generation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 0:2dcec10e9199 | 1 | #include "mbed.h" |
ryood | 0:2dcec10e9199 | 2 | #include "rtos.h" |
ryood | 0:2dcec10e9199 | 3 | |
ryood | 0:2dcec10e9199 | 4 | #define TITLE_STR1 ("KIK01 Kick Machine") |
ryood | 0:2dcec10e9199 | 5 | #define TITLE_STR2 ("20170604") |
ryood | 0:2dcec10e9199 | 6 | |
ryood | 0:2dcec10e9199 | 7 | #define PI_F (3.1415926f) |
ryood | 2:8dff77a1ee4d | 8 | #define SAMPLING_RATE (96000) |
ryood | 0:2dcec10e9199 | 9 | #define SAMPLING_PERIOD (1.0f/SAMPLING_RATE) |
ryood | 0:2dcec10e9199 | 10 | |
ryood | 0:2dcec10e9199 | 11 | #define ENVELOPE_PERIOD (500) |
ryood | 0:2dcec10e9199 | 12 | #define FREQUENCY_ATTACK (5) |
ryood | 0:2dcec10e9199 | 13 | #define FREQUENCY_RELEASE (300) |
ryood | 0:2dcec10e9199 | 14 | #define AMPLITUDE_ATTACK (50) |
ryood | 0:2dcec10e9199 | 15 | #define AMPLITUDE_RELEASE (200) |
ryood | 0:2dcec10e9199 | 16 | |
ryood | 0:2dcec10e9199 | 17 | AnalogOut Dac1(PA_4); |
ryood | 0:2dcec10e9199 | 18 | |
ryood | 0:2dcec10e9199 | 19 | class EnvelopeAR { |
ryood | 0:2dcec10e9199 | 20 | public: |
ryood | 0:2dcec10e9199 | 21 | EnvelopeAR(int _attack, int _release, float _v0, float _v1, float _v2, float _attackTauRatio=0.36f, float _releaseTauRatio=0.36f) : |
ryood | 0:2dcec10e9199 | 22 | amplitude(_v0), |
ryood | 0:2dcec10e9199 | 23 | v0(_v0), |
ryood | 0:2dcec10e9199 | 24 | v1(_v1), |
ryood | 0:2dcec10e9199 | 25 | v2(_v2), |
ryood | 0:2dcec10e9199 | 26 | vLast(_v0), |
ryood | 0:2dcec10e9199 | 27 | attackTauRatio(_attackTauRatio), |
ryood | 0:2dcec10e9199 | 28 | releaseTauRatio(_releaseTauRatio) |
ryood | 0:2dcec10e9199 | 29 | { |
ryood | 0:2dcec10e9199 | 30 | setAttack(_attack); |
ryood | 0:2dcec10e9199 | 31 | setRelease(_release); |
ryood | 0:2dcec10e9199 | 32 | } |
ryood | 0:2dcec10e9199 | 33 | |
ryood | 0:2dcec10e9199 | 34 | ~EnvelopeAR() {} |
ryood | 0:2dcec10e9199 | 35 | |
ryood | 0:2dcec10e9199 | 36 | void setAttack(int _attack) { |
ryood | 0:2dcec10e9199 | 37 | attack = _attack; |
ryood | 0:2dcec10e9199 | 38 | tau0 = attack * attackTauRatio; |
ryood | 0:2dcec10e9199 | 39 | } |
ryood | 0:2dcec10e9199 | 40 | int getAttack() { return attack; } |
ryood | 0:2dcec10e9199 | 41 | |
ryood | 0:2dcec10e9199 | 42 | void setRelease(int _release) { |
ryood | 0:2dcec10e9199 | 43 | release = _release; |
ryood | 0:2dcec10e9199 | 44 | tau1 = release * releaseTauRatio; |
ryood | 0:2dcec10e9199 | 45 | } |
ryood | 0:2dcec10e9199 | 46 | int getRelease() { return release; } |
ryood | 0:2dcec10e9199 | 47 | |
ryood | 0:2dcec10e9199 | 48 | void setAttackTauRatio(float _attackTauRatio) { attackTauRatio = _attackTauRatio; } |
ryood | 0:2dcec10e9199 | 49 | float getAttackTauRatio() { return attackTauRatio; } |
ryood | 0:2dcec10e9199 | 50 | |
ryood | 0:2dcec10e9199 | 51 | void setReleaseTauRatio(float _releaseTauRatio) { releaseTauRatio = _releaseTauRatio; } |
ryood | 0:2dcec10e9199 | 52 | float getReleaseTauRatio() { return releaseTauRatio; } |
ryood | 0:2dcec10e9199 | 53 | |
ryood | 0:2dcec10e9199 | 54 | void setV0(float _v0) { v0 = _v0; } |
ryood | 0:2dcec10e9199 | 55 | float getV0() { return v0; } |
ryood | 0:2dcec10e9199 | 56 | void setV1(float _v1) { v1 = _v1; } |
ryood | 0:2dcec10e9199 | 57 | float getV1() { return v1; } |
ryood | 0:2dcec10e9199 | 58 | void setV2(float _v2) { v2 = _v2; } |
ryood | 0:2dcec10e9199 | 59 | float getV2() { return v2; } |
ryood | 0:2dcec10e9199 | 60 | |
ryood | 0:2dcec10e9199 | 61 | float getAmplitude() { return amplitude; } |
ryood | 0:2dcec10e9199 | 62 | float getAmplitude(int tick) { |
ryood | 0:2dcec10e9199 | 63 | if (tick < attack) { |
ryood | 0:2dcec10e9199 | 64 | // attackの処理 |
ryood | 0:2dcec10e9199 | 65 | amplitude = v0 + (v1 - v0) * (1 - expf(-(float)tick / tau0)); |
ryood | 0:2dcec10e9199 | 66 | vLast = amplitude; |
ryood | 0:2dcec10e9199 | 67 | } |
ryood | 0:2dcec10e9199 | 68 | else { |
ryood | 0:2dcec10e9199 | 69 | // releaseの処理 |
ryood | 0:2dcec10e9199 | 70 | amplitude = (vLast - v2) * (expf(-(float)(tick - attack) / tau1)) + v2; |
ryood | 0:2dcec10e9199 | 71 | } |
ryood | 0:2dcec10e9199 | 72 | return amplitude; |
ryood | 0:2dcec10e9199 | 73 | } |
ryood | 0:2dcec10e9199 | 74 | |
ryood | 0:2dcec10e9199 | 75 | private: |
ryood | 0:2dcec10e9199 | 76 | int attack; |
ryood | 0:2dcec10e9199 | 77 | int release; |
ryood | 0:2dcec10e9199 | 78 | float amplitude; |
ryood | 0:2dcec10e9199 | 79 | float v0; |
ryood | 0:2dcec10e9199 | 80 | float v1; |
ryood | 0:2dcec10e9199 | 81 | float v2; |
ryood | 0:2dcec10e9199 | 82 | float vLast; |
ryood | 0:2dcec10e9199 | 83 | float tau0; |
ryood | 0:2dcec10e9199 | 84 | float tau1; |
ryood | 0:2dcec10e9199 | 85 | float attackTauRatio; |
ryood | 0:2dcec10e9199 | 86 | float releaseTauRatio; |
ryood | 0:2dcec10e9199 | 87 | }; |
ryood | 0:2dcec10e9199 | 88 | |
ryood | 0:2dcec10e9199 | 89 | EnvelopeAR envelopeFrequency( |
ryood | 0:2dcec10e9199 | 90 | FREQUENCY_ATTACK, FREQUENCY_RELEASE, 880.0f, 120.0f, 40.0f, 0.36f, 0.1f); |
ryood | 0:2dcec10e9199 | 91 | EnvelopeAR envelopeAmplitude(AMPLITUDE_ATTACK, AMPLITUDE_RELEASE, 0.9f, 1.0f, 0.0f); |
ryood | 0:2dcec10e9199 | 92 | |
ryood | 2:8dff77a1ee4d | 93 | int ticks; |
ryood | 2:8dff77a1ee4d | 94 | int envelopeTicks; |
ryood | 0:2dcec10e9199 | 95 | float frequency; |
ryood | 0:2dcec10e9199 | 96 | float phi; |
ryood | 0:2dcec10e9199 | 97 | float phiDelta; |
ryood | 0:2dcec10e9199 | 98 | float amplitude; |
ryood | 0:2dcec10e9199 | 99 | |
ryood | 0:2dcec10e9199 | 100 | void generateWave() |
ryood | 0:2dcec10e9199 | 101 | { |
ryood | 0:2dcec10e9199 | 102 | phi += phiDelta; |
ryood | 0:2dcec10e9199 | 103 | if (phi >= 1.0f) { |
ryood | 0:2dcec10e9199 | 104 | phi -= 2.0f; |
ryood | 0:2dcec10e9199 | 105 | } |
ryood | 0:2dcec10e9199 | 106 | float level = sinf(PI_F * phi) * amplitude; |
ryood | 1:f9b967ae26e4 | 107 | |
ryood | 0:2dcec10e9199 | 108 | Dac1.write((level + 1.0f) / 2.0f); |
ryood | 0:2dcec10e9199 | 109 | } |
ryood | 0:2dcec10e9199 | 110 | |
ryood | 0:2dcec10e9199 | 111 | void generateEnvelope() |
ryood | 0:2dcec10e9199 | 112 | { |
ryood | 0:2dcec10e9199 | 113 | // Frequency Envelope |
ryood | 0:2dcec10e9199 | 114 | frequency = envelopeFrequency.getAmplitude(envelopeTicks); |
ryood | 0:2dcec10e9199 | 115 | phiDelta = 2.0f * frequency / SAMPLING_RATE; |
ryood | 0:2dcec10e9199 | 116 | |
ryood | 0:2dcec10e9199 | 117 | // Amplitude Envelope |
ryood | 0:2dcec10e9199 | 118 | amplitude = envelopeAmplitude.getAmplitude(envelopeTicks); |
ryood | 0:2dcec10e9199 | 119 | |
ryood | 0:2dcec10e9199 | 120 | envelopeTicks++; |
ryood | 0:2dcec10e9199 | 121 | if (envelopeTicks == ENVELOPE_PERIOD) { |
ryood | 0:2dcec10e9199 | 122 | envelopeTicks = 0; |
ryood | 0:2dcec10e9199 | 123 | } |
ryood | 0:2dcec10e9199 | 124 | } |
ryood | 0:2dcec10e9199 | 125 | |
ryood | 2:8dff77a1ee4d | 126 | void update() |
ryood | 2:8dff77a1ee4d | 127 | { |
ryood | 2:8dff77a1ee4d | 128 | ticks--; |
ryood | 2:8dff77a1ee4d | 129 | if (ticks == 0) { |
ryood | 2:8dff77a1ee4d | 130 | ticks = SAMPLING_RATE / 1000; |
ryood | 2:8dff77a1ee4d | 131 | generateEnvelope(); |
ryood | 2:8dff77a1ee4d | 132 | } |
ryood | 2:8dff77a1ee4d | 133 | generateWave(); |
ryood | 2:8dff77a1ee4d | 134 | } |
ryood | 2:8dff77a1ee4d | 135 | |
ryood | 0:2dcec10e9199 | 136 | int main() |
ryood | 0:2dcec10e9199 | 137 | { |
ryood | 0:2dcec10e9199 | 138 | printf("%s %s\r\n", TITLE_STR1, TITLE_STR2); |
ryood | 0:2dcec10e9199 | 139 | |
ryood | 0:2dcec10e9199 | 140 | frequency = 1000.0f; |
ryood | 0:2dcec10e9199 | 141 | phiDelta = 2.0f * frequency / SAMPLING_RATE; |
ryood | 0:2dcec10e9199 | 142 | amplitude = 1.0f; |
ryood | 0:2dcec10e9199 | 143 | |
ryood | 2:8dff77a1ee4d | 144 | ticks = SAMPLING_RATE / 1000; |
ryood | 2:8dff77a1ee4d | 145 | envelopeTicks = 0; |
ryood | 1:f9b967ae26e4 | 146 | |
ryood | 2:8dff77a1ee4d | 147 | Ticker samplingTicker; |
ryood | 2:8dff77a1ee4d | 148 | samplingTicker.attach(&update, SAMPLING_PERIOD); |
ryood | 2:8dff77a1ee4d | 149 | |
ryood | 1:f9b967ae26e4 | 150 | for (;;) {} |
ryood | 0:2dcec10e9199 | 151 | } |