REVIEWED: example: audio_amp_envelope, code formating to follow raylib code conventions #5713

This commit is contained in:
Ray
2026-04-04 11:12:30 +02:00
parent bb7b8d70e9
commit ea6292e27a

View File

@ -20,10 +20,10 @@
#define RAYGUI_IMPLEMENTATION #define RAYGUI_IMPLEMENTATION
#include "raygui.h" #include "raygui.h"
#include <math.h> #include <math.h> // Required for: sinf()
#define BUFFER_SIZE 4096 #define BUFFER_SIZE 4096
#define SAMPLE_RATE 44100 #define SAMPLE_RATE 44100
// Wave state // Wave state
typedef enum { typedef enum {
@ -47,9 +47,9 @@ typedef struct {
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Module Functions Declaration // Module Functions Declaration
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
static void FillAudioBuffer(int i, float* buffer, float envelopeValue, float* audioTime); static void FillAudioBuffer(int i, float *buffer, float envelopeValue, float *audioTime);
static void UpdateEnvelope(Envelope* env); static void UpdateEnvelope(Envelope *env);
static void DrawADSRGraph(const Envelope *env, Rectangle bounds); static void DrawADSRGraph(Envelope *env, Rectangle bounds);
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
@ -93,24 +93,22 @@ int main(void)
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_SPACE)) env.state = ATTACK;
if (IsKeyPressed(KEY_SPACE)) { if (IsKeyReleased(KEY_SPACE) && (env.state != IDLE)) env.state = RELEASE;
env.state = ATTACK;
}
if (IsKeyReleased(KEY_SPACE)) { if (IsAudioStreamProcessed(stream))
if (env.state != IDLE) env.state = RELEASE; {
} if ((env.state != IDLE) || (env.currentValue > 0.0f))
{
if (IsAudioStreamProcessed(stream)) {
if (env.state != IDLE || env.currentValue > 0.0f) {
for (int i = 0; i < BUFFER_SIZE; i++) for (int i = 0; i < BUFFER_SIZE; i++)
{ {
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;
@ -139,9 +137,11 @@ int main(void)
DrawText(TextFormat("Current Gain: %2.2f", env.currentValue), 535, 345 - (env.currentValue * 100), 10, MAROON); DrawText(TextFormat("Current Gain: %2.2f", env.currentValue), 535, 345 - (env.currentValue * 100), 10, MAROON);
DrawText("Press SPACE to PLAY the sound!", 200, 400, 20, LIGHTGRAY); DrawText("Press SPACE to PLAY the sound!", 200, 400, 20, LIGHTGRAY);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadAudioStream(stream); UnloadAudioStream(stream);
@ -156,57 +156,60 @@ int main(void)
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Module Functions Definition // Module Functions Definition
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
static void FillAudioBuffer(int i, float* buffer, float envelopeValue, float* audioTime) static void FillAudioBuffer(int i, float *buffer, float envelopeValue, float *audioTime)
{ {
int frequency = 440; int frequency = 440;
buffer[i] = envelopeValue * sinf(2.0f * PI * frequency * (*audioTime)); buffer[i] = envelopeValue*sinf(2.0f*PI*frequency*(*audioTime));
*audioTime += 1.0f / SAMPLE_RATE; *audioTime += (1.0f/SAMPLE_RATE);
} }
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)
{ {
case ATTACK: case ATTACK:
env->currentValue += (1.0f / env->attackTime) * sampleTime; {
env->currentValue += (1.0f/env->attackTime)*sampleTime;
if (env->currentValue >= 1.0f) if (env->currentValue >= 1.0f)
{ {
env->currentValue = 1.0f; env->currentValue = 1.0f;
env->state = DECAY; env->state = DECAY;
} }
break;
} break;
case DECAY: case DECAY:
env->currentValue -= ((1.0f - env->sustainLevel) / env->decayTime) * sampleTime; {
env->currentValue -= ((1.0f - env->sustainLevel)/env->decayTime)*sampleTime;
if (env->currentValue <= env->sustainLevel) if (env->currentValue <= env->sustainLevel)
{ {
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:
env->currentValue -= (env->sustainLevel / env->releaseTime) * sampleTime; {
env->currentValue -= (env->sustainLevel/env->releaseTime)*sampleTime;
if (env->currentValue <= 0.001f) // Use a small threshold to avoid infinite tail if (env->currentValue <= 0.001f) // Use a small threshold to avoid infinite tail
{ {
env->currentValue = 0.0f; env->currentValue = 0.0f;
env->state = IDLE; env->state = IDLE;
} }
break;
} break;
default: break; default: break;
} }
} }
static void DrawADSRGraph(const Envelope *env, Rectangle bounds) static void DrawADSRGraph(Envelope *env, Rectangle bounds)
{ {
DrawRectangleRec(bounds, Fade(LIGHTGRAY, 0.3f)); DrawRectangleRec(bounds, Fade(LIGHTGRAY, 0.3f));
DrawRectangleLinesEx(bounds, 1, GRAY); DrawRectangleLinesEx(bounds, 1, GRAY);
@ -217,14 +220,14 @@ static void DrawADSRGraph(const 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;
Vector2 start = { bounds.x, bounds.y + bounds.height }; Vector2 start = { bounds.x, bounds.y + bounds.height };
Vector2 peak = { start.x + (env->attackTime * scaleX), bounds.y }; Vector2 peak = { start.x + (env->attackTime*scaleX), bounds.y };
Vector2 sustain = { peak.x + (env->decayTime * scaleX), bounds.y + (1.0f - env->sustainLevel) * scaleY }; Vector2 sustain = { peak.x + (env->decayTime*scaleX), bounds.y + (1.0f - env->sustainLevel)*scaleY };
Vector2 rel = { sustain.x + (sustainWidth * scaleX), sustain.y }; Vector2 rel = { sustain.x + (sustainWidth*scaleX), sustain.y };
Vector2 end = { rel.x + (env->releaseTime * scaleX), bounds.y + bounds.height }; Vector2 end = { rel.x + (env->releaseTime*scaleX), bounds.y + bounds.height };
DrawLineV(start, peak, SKYBLUE); DrawLineV(start, peak, SKYBLUE);
DrawLineV(peak, sustain, BLUE); DrawLineV(peak, sustain, BLUE);
@ -232,4 +235,4 @@ static void DrawADSRGraph(const Envelope *env, Rectangle bounds)
DrawLineV(rel, end, ORANGE); DrawLineV(rel, end, ORANGE);
DrawText("ADSR Visualizer", bounds.x, bounds.y - 20, 10, DARKGRAY); DrawText("ADSR Visualizer", bounds.x, bounds.y - 20, 10, DARKGRAY);
} }