KIK 01 Prototype 05

Dependencies:   AverageMCP3008 mbed-rtos mbed mcp3008

Fork of KIK01_Proto03 by Ryo Od

Committer:
ryood
Date:
Sun Jun 04 07:49:50 2017 +0000
Revision:
4:9f53a82fc1b6
Parent:
3:f89b400cfe57
Child:
5:846772a77d33
Attack, Tone Test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ryood 3:f89b400cfe57 1 /*
ryood 3:f89b400cfe57 2 * KIK01
ryood 3:f89b400cfe57 3 * Kick Machine
ryood 3:f89b400cfe57 4 *
ryood 3:f89b400cfe57 5 * 2017.06.04 created.
ryood 3:f89b400cfe57 6 *
ryood 3:f89b400cfe57 7 */
ryood 3:f89b400cfe57 8
ryood 0:2dcec10e9199 9 #include "mbed.h"
ryood 0:2dcec10e9199 10 #include "rtos.h"
ryood 0:2dcec10e9199 11
ryood 0:2dcec10e9199 12 #define TITLE_STR1 ("KIK01 Kick Machine")
ryood 0:2dcec10e9199 13 #define TITLE_STR2 ("20170604")
ryood 0:2dcec10e9199 14
ryood 0:2dcec10e9199 15 #define PI_F (3.1415926f)
ryood 2:8dff77a1ee4d 16 #define SAMPLING_RATE (96000)
ryood 0:2dcec10e9199 17 #define SAMPLING_PERIOD (1.0f/SAMPLING_RATE)
ryood 0:2dcec10e9199 18
ryood 0:2dcec10e9199 19 #define FREQUENCY_ATTACK (5)
ryood 0:2dcec10e9199 20 #define FREQUENCY_RELEASE (300)
ryood 4:9f53a82fc1b6 21 //#define AMPLITUDE_ATTACK (50)
ryood 0:2dcec10e9199 22 #define AMPLITUDE_RELEASE (200)
ryood 0:2dcec10e9199 23
ryood 3:f89b400cfe57 24 AnalogOut Dac1(PA_5);
ryood 3:f89b400cfe57 25
ryood 3:f89b400cfe57 26 AnalogIn AinBpm(PA_0);
ryood 4:9f53a82fc1b6 27 AnalogIn AinAmplitudeAttack(PA_1);
ryood 4:9f53a82fc1b6 28 AnalogIn AinFrequencyF0(PA_4);
ryood 0:2dcec10e9199 29
ryood 0:2dcec10e9199 30 class EnvelopeAR {
ryood 0:2dcec10e9199 31 public:
ryood 0:2dcec10e9199 32 EnvelopeAR(int _attack, int _release, float _v0, float _v1, float _v2, float _attackTauRatio=0.36f, float _releaseTauRatio=0.36f) :
ryood 0:2dcec10e9199 33 amplitude(_v0),
ryood 0:2dcec10e9199 34 v0(_v0),
ryood 0:2dcec10e9199 35 v1(_v1),
ryood 0:2dcec10e9199 36 v2(_v2),
ryood 0:2dcec10e9199 37 vLast(_v0),
ryood 0:2dcec10e9199 38 attackTauRatio(_attackTauRatio),
ryood 0:2dcec10e9199 39 releaseTauRatio(_releaseTauRatio)
ryood 0:2dcec10e9199 40 {
ryood 0:2dcec10e9199 41 setAttack(_attack);
ryood 0:2dcec10e9199 42 setRelease(_release);
ryood 0:2dcec10e9199 43 }
ryood 0:2dcec10e9199 44
ryood 0:2dcec10e9199 45 ~EnvelopeAR() {}
ryood 0:2dcec10e9199 46
ryood 0:2dcec10e9199 47 void setAttack(int _attack) {
ryood 0:2dcec10e9199 48 attack = _attack;
ryood 0:2dcec10e9199 49 tau0 = attack * attackTauRatio;
ryood 0:2dcec10e9199 50 }
ryood 0:2dcec10e9199 51 int getAttack() { return attack; }
ryood 0:2dcec10e9199 52
ryood 0:2dcec10e9199 53 void setRelease(int _release) {
ryood 0:2dcec10e9199 54 release = _release;
ryood 0:2dcec10e9199 55 tau1 = release * releaseTauRatio;
ryood 0:2dcec10e9199 56 }
ryood 0:2dcec10e9199 57 int getRelease() { return release; }
ryood 0:2dcec10e9199 58
ryood 0:2dcec10e9199 59 void setAttackTauRatio(float _attackTauRatio) { attackTauRatio = _attackTauRatio; }
ryood 0:2dcec10e9199 60 float getAttackTauRatio() { return attackTauRatio; }
ryood 0:2dcec10e9199 61
ryood 0:2dcec10e9199 62 void setReleaseTauRatio(float _releaseTauRatio) { releaseTauRatio = _releaseTauRatio; }
ryood 0:2dcec10e9199 63 float getReleaseTauRatio() { return releaseTauRatio; }
ryood 0:2dcec10e9199 64
ryood 0:2dcec10e9199 65 void setV0(float _v0) { v0 = _v0; }
ryood 0:2dcec10e9199 66 float getV0() { return v0; }
ryood 0:2dcec10e9199 67 void setV1(float _v1) { v1 = _v1; }
ryood 0:2dcec10e9199 68 float getV1() { return v1; }
ryood 0:2dcec10e9199 69 void setV2(float _v2) { v2 = _v2; }
ryood 0:2dcec10e9199 70 float getV2() { return v2; }
ryood 0:2dcec10e9199 71
ryood 0:2dcec10e9199 72 float getAmplitude() { return amplitude; }
ryood 0:2dcec10e9199 73 float getAmplitude(int tick) {
ryood 0:2dcec10e9199 74 if (tick < attack) {
ryood 0:2dcec10e9199 75 // attackの処理
ryood 0:2dcec10e9199 76 amplitude = v0 + (v1 - v0) * (1 - expf(-(float)tick / tau0));
ryood 0:2dcec10e9199 77 vLast = amplitude;
ryood 0:2dcec10e9199 78 }
ryood 0:2dcec10e9199 79 else {
ryood 0:2dcec10e9199 80 // releaseの処理
ryood 0:2dcec10e9199 81 amplitude = (vLast - v2) * (expf(-(float)(tick - attack) / tau1)) + v2;
ryood 0:2dcec10e9199 82 }
ryood 0:2dcec10e9199 83 return amplitude;
ryood 0:2dcec10e9199 84 }
ryood 0:2dcec10e9199 85
ryood 0:2dcec10e9199 86 private:
ryood 0:2dcec10e9199 87 int attack;
ryood 0:2dcec10e9199 88 int release;
ryood 0:2dcec10e9199 89 float amplitude;
ryood 0:2dcec10e9199 90 float v0;
ryood 0:2dcec10e9199 91 float v1;
ryood 0:2dcec10e9199 92 float v2;
ryood 0:2dcec10e9199 93 float vLast;
ryood 0:2dcec10e9199 94 float tau0;
ryood 0:2dcec10e9199 95 float tau1;
ryood 0:2dcec10e9199 96 float attackTauRatio;
ryood 0:2dcec10e9199 97 float releaseTauRatio;
ryood 0:2dcec10e9199 98 };
ryood 0:2dcec10e9199 99
ryood 0:2dcec10e9199 100 EnvelopeAR envelopeFrequency(
ryood 0:2dcec10e9199 101 FREQUENCY_ATTACK, FREQUENCY_RELEASE, 880.0f, 120.0f, 40.0f, 0.36f, 0.1f);
ryood 4:9f53a82fc1b6 102 EnvelopeAR envelopeAmplitude(50, AMPLITUDE_RELEASE, 0.95f, 1.0f, 0.0f);
ryood 0:2dcec10e9199 103
ryood 3:f89b400cfe57 104 volatile int ticks;
ryood 3:f89b400cfe57 105 volatile int envelopeTicks;
ryood 3:f89b400cfe57 106 volatile float frequency;
ryood 3:f89b400cfe57 107 volatile float phi;
ryood 3:f89b400cfe57 108 volatile float phiDelta;
ryood 3:f89b400cfe57 109 volatile float amplitude;
ryood 3:f89b400cfe57 110
ryood 3:f89b400cfe57 111 float bpm;
ryood 3:f89b400cfe57 112 int envelopeLength;
ryood 4:9f53a82fc1b6 113 int amplitudeAttack;
ryood 4:9f53a82fc1b6 114 float frequencyF0;
ryood 0:2dcec10e9199 115
ryood 0:2dcec10e9199 116 void generateWave()
ryood 0:2dcec10e9199 117 {
ryood 0:2dcec10e9199 118 phi += phiDelta;
ryood 0:2dcec10e9199 119 if (phi >= 1.0f) {
ryood 0:2dcec10e9199 120 phi -= 2.0f;
ryood 0:2dcec10e9199 121 }
ryood 0:2dcec10e9199 122 float level = sinf(PI_F * phi) * amplitude;
ryood 1:f9b967ae26e4 123
ryood 3:f89b400cfe57 124 Dac1.write((level * 0.7f + 1.0f) / 2.0f);
ryood 0:2dcec10e9199 125 }
ryood 0:2dcec10e9199 126
ryood 0:2dcec10e9199 127 void generateEnvelope()
ryood 0:2dcec10e9199 128 {
ryood 0:2dcec10e9199 129 // Frequency Envelope
ryood 0:2dcec10e9199 130 frequency = envelopeFrequency.getAmplitude(envelopeTicks);
ryood 0:2dcec10e9199 131 phiDelta = 2.0f * frequency / SAMPLING_RATE;
ryood 0:2dcec10e9199 132
ryood 0:2dcec10e9199 133 // Amplitude Envelope
ryood 0:2dcec10e9199 134 amplitude = envelopeAmplitude.getAmplitude(envelopeTicks);
ryood 0:2dcec10e9199 135
ryood 0:2dcec10e9199 136 envelopeTicks++;
ryood 3:f89b400cfe57 137 if (envelopeTicks == envelopeLength) {
ryood 0:2dcec10e9199 138 envelopeTicks = 0;
ryood 0:2dcec10e9199 139 }
ryood 0:2dcec10e9199 140 }
ryood 0:2dcec10e9199 141
ryood 2:8dff77a1ee4d 142 void update()
ryood 2:8dff77a1ee4d 143 {
ryood 3:f89b400cfe57 144 ticks++;
ryood 3:f89b400cfe57 145 if (ticks == SAMPLING_RATE / 1000) {
ryood 3:f89b400cfe57 146 ticks = 0;
ryood 2:8dff77a1ee4d 147 generateEnvelope();
ryood 2:8dff77a1ee4d 148 }
ryood 2:8dff77a1ee4d 149 generateWave();
ryood 2:8dff77a1ee4d 150 }
ryood 2:8dff77a1ee4d 151
ryood 0:2dcec10e9199 152 int main()
ryood 0:2dcec10e9199 153 {
ryood 0:2dcec10e9199 154 printf("%s %s\r\n", TITLE_STR1, TITLE_STR2);
ryood 0:2dcec10e9199 155
ryood 0:2dcec10e9199 156 frequency = 1000.0f;
ryood 0:2dcec10e9199 157 phiDelta = 2.0f * frequency / SAMPLING_RATE;
ryood 0:2dcec10e9199 158 amplitude = 1.0f;
ryood 0:2dcec10e9199 159
ryood 3:f89b400cfe57 160 ticks = 0;
ryood 2:8dff77a1ee4d 161 envelopeTicks = 0;
ryood 1:f9b967ae26e4 162
ryood 2:8dff77a1ee4d 163 Ticker samplingTicker;
ryood 2:8dff77a1ee4d 164 samplingTicker.attach(&update, SAMPLING_PERIOD);
ryood 2:8dff77a1ee4d 165
ryood 3:f89b400cfe57 166 bpm = 120.0f;
ryood 3:f89b400cfe57 167 for (;;) {
ryood 4:9f53a82fc1b6 168 bpm = AinBpm.read() * 180.0f + 60.0f;
ryood 3:f89b400cfe57 169 envelopeLength = 60 * 1000 / bpm;
ryood 4:9f53a82fc1b6 170 amplitudeAttack = AinAmplitudeAttack.read() * envelopeLength * 0.7f;
ryood 4:9f53a82fc1b6 171 envelopeAmplitude.setAttack(amplitudeAttack);
ryood 4:9f53a82fc1b6 172 frequencyF0 = AinFrequencyF0.read() * 2000.0f + 50.0f;
ryood 4:9f53a82fc1b6 173 envelopeFrequency.setV0(frequencyF0);
ryood 4:9f53a82fc1b6 174
ryood 4:9f53a82fc1b6 175 printf("%f\t%d\t%d\t%f\r\n", bpm, envelopeLength, amplitudeAttack, frequencyF0);
ryood 4:9f53a82fc1b6 176
ryood 4:9f53a82fc1b6 177 Thread::wait(500);
ryood 3:f89b400cfe57 178 }
ryood 0:2dcec10e9199 179 }