работа над envelope закончена

envelope работает. без щелчков
This commit is contained in:
2026-11-13 02:02:50 +06:00
parent a92de417b5
commit 3a16ac0c0e
7 changed files with 130 additions and 77 deletions
+67 -49
View File
@@ -1,52 +1,70 @@
/*
* Состояние огибающей
*
П родумай enum class с пятью состояниями: *
- Idle
- Attack
- Decay
- Sustain
- Release
2. Параметры
Добавь четыре параметра ADSR:
- attack;
- decay;
- sustain;
- release.
3. Управление
Предусмотри методы:
- note_on() — начать атаку;
- note_off() — начать отпускание;
- process() — вернуть следующий коэффициент огибающей.
process() должен вызываться для каждого аудиосэмпла, а не один раз на весь буфер.
4. Внутреннее состояние
Тебе понадобится хранить:
- текущее состояние;
- текущий уровень амплитуды;
- прогресс текущей фазы.
Несколько важных условий
1. После note_on() огибающая начинает Attack.
2. После Attack автоматически переходит в Decay.
3. После Decay переходит в Sustain.
4. note_off() переводит огибающую в Release.
5. После Release уровень становится 0, состояние — Idle.
Пока реализуй только структуру класса и переходы между состояниями. Математику изменения амплитуды добавим следующим шагом.
И ещё: в дальнейшем нам понадобится аккуратно организовать передачу команд из main() в аудиопоток. Нельзя бездумно изменять обычные переменные из главного потока, если callback одновременно их читает. Это вопрос потокобезопасности, к которому мы отдельно вернёмся
*/
#include "../include/envelope.hpp"
double Envelope::process(){
amplitude = synth_.get_amplitude();
phase = synth_.get_phase();
return 0.0;
void Envelope::note_on(){
condition = Condition::Attack;
// note_is_pressed = true;
if (attack_time <= 0.0){
attack_time = 0.0001;
}
rate = (1.0 - currnet_amplitude)/(attack_time*sample_rate);
}
void Envelope::note_off(){
if (condition != Condition::Idle){
condition = Condition::Release;
if (release_time <= 0.0){
release_time = 0.0001;
}
rate = (0.0 - currnet_amplitude)/(release_time*sample_rate);
}
// note_is_pressed = false;
}
double Envelope::process(){
switch(condition){
case Condition::Attack:{
currnet_amplitude += rate;
if (currnet_amplitude >= 1.0){
currnet_amplitude = 1.0;
if (decay_time <= 0.0){
condition = Condition::Sustain;
}
else{
condition = Condition::Decay;
rate = (sustain_level - 1.0)/(decay_time*sample_rate);
}
}
return currnet_amplitude;
}
case Condition::Decay:{
currnet_amplitude += rate;
if (currnet_amplitude <= sustain_level){
currnet_amplitude = sustain_level;
condition = Condition::Sustain;
}
return currnet_amplitude;
}
case Condition::Sustain:{
return currnet_amplitude;
}
case Condition::Release:{
currnet_amplitude += rate;
if (currnet_amplitude <= 0.0){
currnet_amplitude = 0.0;
condition = Condition::Idle;
}
return currnet_amplitude;
}
case Condition::Idle:{
return 0.0;
}
default: return 0.0;
}
}