Update audio_amp_envelope.c

This commit is contained in:
Ray
2026-04-04 11:13:29 +02:00
parent ea6292e27a
commit 6ff51d3a2a

View File

@ -93,7 +93,7 @@ int main(void)
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_SPACE)) env.state = ATTACK; if (IsKeyPressed(KEY_SPACE)) env.state = ATTACK;
if (IsKeyReleased(KEY_SPACE) && (env.state != IDLE)) env.state = RELEASE; if (IsKeyReleased(KEY_SPACE) && (env.state != IDLE)) env.state = RELEASE;
@ -106,14 +106,14 @@ int main(void)
UpdateEnvelope(&env); UpdateEnvelope(&env);
FillAudioBuffer(i, buffer, env.currentValue, &audioTime); FillAudioBuffer(i, buffer, env.currentValue, &audioTime);
} }
} }
else else
{ {
// Clear buffer if silent to avoid looping noise // Clear buffer if silent to avoid looping noise
for (int i = 0; i < BUFFER_SIZE; i++) buffer[i] = 0; for (int i = 0; i < BUFFER_SIZE; i++) buffer[i] = 0;
audioTime = 0.0f; audioTime = 0.0f;
} }
UpdateAudioStream(stream, buffer, BUFFER_SIZE); UpdateAudioStream(stream, buffer, BUFFER_SIZE);
} }
@ -166,7 +166,7 @@ static void FillAudioBuffer(int i, float *buffer, float envelopeValue, float *au
static void UpdateEnvelope(Envelope *env) static void UpdateEnvelope(Envelope *env)
{ {
// Calculate the time delta for ONE sample (1/44100) // Calculate the time delta for ONE sample (1/44100)
float sampleTime = 1.0f/SAMPLE_RATE; float sampleTime = 1.0f/SAMPLE_RATE;
switch(env->state) switch(env->state)
{ {
@ -178,7 +178,7 @@ static void UpdateEnvelope(Envelope *env)
env->currentValue = 1.0f; env->currentValue = 1.0f;
env->state = DECAY; env->state = DECAY;
} }
} break; } break;
case DECAY: case DECAY:
{ {
@ -188,12 +188,12 @@ static void UpdateEnvelope(Envelope *env)
env->currentValue = env->sustainLevel; env->currentValue = env->sustainLevel;
env->state = SUSTAIN; env->state = SUSTAIN;
} }
} break; } break;
case SUSTAIN: case SUSTAIN:
{ {
env->currentValue = env->sustainLevel; env->currentValue = env->sustainLevel;
} break; } break;
case RELEASE: case RELEASE:
{ {
@ -203,8 +203,8 @@ static void UpdateEnvelope(Envelope *env)
env->currentValue = 0.0f; env->currentValue = 0.0f;
env->state = IDLE; env->state = IDLE;
} }
} break; } break;
default: break; default: break;
} }
} }
@ -219,7 +219,7 @@ static void DrawADSRGraph(Envelope *env, Rectangle bounds)
// Total time to visualize (sum of A, D, R + a padding for Sustain) // Total time to visualize (sum of A, D, R + a padding for Sustain)
float totalTime = env->attackTime + env->decayTime + sustainWidth + env->releaseTime; float totalTime = env->attackTime + env->decayTime + sustainWidth + env->releaseTime;
float scaleX = bounds.width/totalTime; float scaleX = bounds.width/totalTime;
float scaleY = bounds.height; float scaleY = bounds.height;