Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: AverageMCP3008 VoltageMonitor mbed-rtos mbed mcp3008
Fork of KIK01_Proto07 by
main.cpp
- Committer:
- ryood
- Date:
- 2017-06-04
- Revision:
- 3:f89b400cfe57
- Parent:
- 2:8dff77a1ee4d
- Child:
- 4:9f53a82fc1b6
File content as of revision 3:f89b400cfe57:
/*
* KIK01
* Kick Machine
*
* 2017.06.04 created.
*
*/
#include "mbed.h"
#include "rtos.h"
#define TITLE_STR1 ("KIK01 Kick Machine")
#define TITLE_STR2 ("20170604")
#define PI_F (3.1415926f)
#define SAMPLING_RATE (96000)
#define SAMPLING_PERIOD (1.0f/SAMPLING_RATE)
#define FREQUENCY_ATTACK (5)
#define FREQUENCY_RELEASE (300)
#define AMPLITUDE_ATTACK (50)
#define AMPLITUDE_RELEASE (200)
AnalogOut Dac1(PA_5);
AnalogIn AinBpm(PA_0);
class EnvelopeAR {
public:
EnvelopeAR(int _attack, int _release, float _v0, float _v1, float _v2, float _attackTauRatio=0.36f, float _releaseTauRatio=0.36f) :
amplitude(_v0),
v0(_v0),
v1(_v1),
v2(_v2),
vLast(_v0),
attackTauRatio(_attackTauRatio),
releaseTauRatio(_releaseTauRatio)
{
setAttack(_attack);
setRelease(_release);
}
~EnvelopeAR() {}
void setAttack(int _attack) {
attack = _attack;
tau0 = attack * attackTauRatio;
}
int getAttack() { return attack; }
void setRelease(int _release) {
release = _release;
tau1 = release * releaseTauRatio;
}
int getRelease() { return release; }
void setAttackTauRatio(float _attackTauRatio) { attackTauRatio = _attackTauRatio; }
float getAttackTauRatio() { return attackTauRatio; }
void setReleaseTauRatio(float _releaseTauRatio) { releaseTauRatio = _releaseTauRatio; }
float getReleaseTauRatio() { return releaseTauRatio; }
void setV0(float _v0) { v0 = _v0; }
float getV0() { return v0; }
void setV1(float _v1) { v1 = _v1; }
float getV1() { return v1; }
void setV2(float _v2) { v2 = _v2; }
float getV2() { return v2; }
float getAmplitude() { return amplitude; }
float getAmplitude(int tick) {
if (tick < attack) {
// attackの処理
amplitude = v0 + (v1 - v0) * (1 - expf(-(float)tick / tau0));
vLast = amplitude;
}
else {
// releaseの処理
amplitude = (vLast - v2) * (expf(-(float)(tick - attack) / tau1)) + v2;
}
return amplitude;
}
private:
int attack;
int release;
float amplitude;
float v0;
float v1;
float v2;
float vLast;
float tau0;
float tau1;
float attackTauRatio;
float releaseTauRatio;
};
EnvelopeAR envelopeFrequency(
FREQUENCY_ATTACK, FREQUENCY_RELEASE, 880.0f, 120.0f, 40.0f, 0.36f, 0.1f);
EnvelopeAR envelopeAmplitude(AMPLITUDE_ATTACK, AMPLITUDE_RELEASE, 0.9f, 1.0f, 0.0f);
volatile int ticks;
volatile int envelopeTicks;
volatile float frequency;
volatile float phi;
volatile float phiDelta;
volatile float amplitude;
float bpm;
int envelopeLength;
void generateWave()
{
phi += phiDelta;
if (phi >= 1.0f) {
phi -= 2.0f;
}
float level = sinf(PI_F * phi) * amplitude;
Dac1.write((level * 0.7f + 1.0f) / 2.0f);
}
void generateEnvelope()
{
// Frequency Envelope
frequency = envelopeFrequency.getAmplitude(envelopeTicks);
phiDelta = 2.0f * frequency / SAMPLING_RATE;
// Amplitude Envelope
amplitude = envelopeAmplitude.getAmplitude(envelopeTicks);
envelopeTicks++;
if (envelopeTicks == envelopeLength) {
envelopeTicks = 0;
}
}
void update()
{
ticks++;
if (ticks == SAMPLING_RATE / 1000) {
ticks = 0;
generateEnvelope();
}
generateWave();
}
int main()
{
printf("%s %s\r\n", TITLE_STR1, TITLE_STR2);
frequency = 1000.0f;
phiDelta = 2.0f * frequency / SAMPLING_RATE;
amplitude = 1.0f;
ticks = 0;
envelopeTicks = 0;
Ticker samplingTicker;
samplingTicker.attach(&update, SAMPLING_PERIOD);
bpm = 120.0f;
for (;;) {
bpm = AinBpm.read() * 120.0f + 60.0f;
envelopeLength = 60 * 1000 / bpm;
printf("%f\t%d\r\n", bpm, envelopeLength);
Thread::wait(100);
}
}
