commit 6df25cafcc5f668b0319b357d89aaaa71ef65ce3
parent f5f0369625d186ad69f968453b7dfcb10f9543de
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Thu, 31 Dec 2020 12:43:48 +0000
drawbar tone
Diffstat:
M | main.cpp | | | 56 | +++++++++++++++++++++++++++++++++++--------------------- |
1 file changed, 35 insertions(+), 21 deletions(-)
diff --git a/main.cpp b/main.cpp
@@ -167,7 +167,7 @@ int cb_sample_rate(jack_nframes_t nframes, void* arg)
static struct voice
{
float envelope, freq, sample;
- unsigned char active : 1, velocity : 7, state : 2;
+ unsigned short active : 1, velocity : 7, state : 3;
} voices[128];
int cb_process(jack_nframes_t nframes, void* arg)
@@ -186,7 +186,7 @@ int cb_process(jack_nframes_t nframes, void* arg)
continue;
case 1: // Note on
voice.active = 1;
- voice.state = 2;
+ voice.state = 4;
voice.velocity = event.buffer[2];
continue;
case 3: // Controller change
@@ -194,10 +194,6 @@ int cb_process(jack_nframes_t nframes, void* arg)
{
case 0x40: // Sustain
sustain = event.buffer[2] > 63;
- if (not sustain)
- for (auto& v : voices)
- if (not v.active and v.state == 1)
- v.state = 0;
continue;
}
/* FALLTHROUGH */
@@ -216,33 +212,51 @@ int cb_process(jack_nframes_t nframes, void* arg)
float accum = 0;
for (auto& v : voices)
{
- accum += std::sin(v.sample * 6.28318530718f / srate) * v.envelope;
+ if (not v.state)
+ continue;
+
+ float pos = v.sample * 6.28318530718f / srate;
+ float sample = std::sin(pos / 2) + std::sin(pos) + std::sin(pos) + std::sin(pos * 2)*0.7f + std::sin(pos * 3)*.7f + std::sin(pos * 4)/2;
v.sample += v.freq;
- if (v.sample >= srate)
- v.sample -= srate;
+ if (v.sample >= srate * 6)
+ v.sample -= srate * 6;
float velocity = v.velocity / 127.f;
- if (v.state == 2)
+ switch (v.state)
{
- v.envelope += velocity / 5000.f;
+ case 4:
+ v.envelope += velocity / 1000.f;
if (v.envelope >= velocity)
{
v.envelope = velocity;
- v.state = 1;
+ v.state = 3;
}
- }
- else if (v.state == 1)
- {
+ break;
+
+ case 3:
v.envelope -= (v.envelope - velocity * 0.2f) * 0.00005f;
if (not (sustain || v.active))
- v.state = 0;
- }
- else
- {
- v.velocity = 0;
+ v.state = 2;
+ break;
+
+ case 2:
+ v.envelope *= 0.9998f;
+ if (v.envelope < 0.1f && sample < 0)
+ v.state = 1;
+ break;
+
+ case 1:
v.envelope *= 0.9998f;
+ if (sample >= 0)
+ {
+ v.envelope = 0;
+ v.sample = 0;
+ v.state = 0;
+ }
+ break;
}
+ accum += sample * v.envelope;
}
audio_buf[i] = 2 / (1 + std::exp(accum)) - 1;
}
@@ -305,7 +319,7 @@ int main()
for (;;)
{
- std::this_thread::sleep_for(5ms);
+ std::this_thread::sleep_for(10ms);
print_buffer_size();
print_sample_rate();
print_midi_events();