* chg: New build system that uses the officially distributed binaries, bumped version to 8.0.0, simplified git workflow, removed deprecated OpenGL 1.1 functionality. * chg: Modernize CI workflow, enable SourceLink - Bump workflow actions to latest majors (Node 24); drop deprecated softprops/action-gh-release@v1 - Trigger push builds on main instead of master - Create local nuget feed dir before pack (fixes NU1301) - Enable Microsoft.SourceLink.GitHub for debugging symbols (ref PR #340) * fix: centralized version data in Directory.build.props, and fixed various interop details that had incorrect function signatures * chore: updated readme * fix: version the native extract marker and chain download via DependsOnTargets The .extracted marker now includes the raylib package name, so bumping TargetRaylibTag re-extracts the new archive instead of silently keeping (and packing/copying) the previous version's files. _PrepareNativeLibrary and _StageWasmNative now depend directly on _DownloadAndExtractInternal instead of CallTarget-ing it; dependency targets run in the same project instance, so the resolved properties (RaylibPackageName etc.) propagate naturally. * fix: let the binding build for browser-wasm on both net8.0 and net10.0 The net8-era wasm workload (Microsoft.NET.Runtime.WebAssembly.Sdk 8.0.x, auto-imported for RID browser-wasm) treats every browser-wasm project as a wasm app: it forces OutputType=Exe after project evaluation (CS5001 for a classlib) and hooks its app-bundle build after Build, which errors because a library has no assemblies to bundle. Opt Raylib-cs out via DisableAutoWasmBuildApp (props time, before the workload defaults its trigger) and pin OutputType back to Library in Directory.Build.targets (evaluated after the workload props, so the assignment wins). net10's wasm SDK needs neither workaround. * chore: readme updated * chg: simplifying build logic - a simple line in the documentation should save us the code here * fix: Wrong signature of FrameBufferComplete * chore: readme update * feat: samples default to local project reference, and can optionally use the nuget package * feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style * chore: readme, gitignore, and targets backport. * fix: Examples.csproj runs the download task when building locally * feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style * chore: readme, gitignore, and targets backport. * chore: clean up linter warnings * feat: html harness focuses the example and allows quick navigation with J/K instead. * chore: readme mentions the property to use nuget vs. the local project reference * feat: replaced the J/K navigation with good old HTML buttons * chore: run dotnet format scoped default (was previously scoped to just 'style')
273 lines
9.4 KiB
C#
273 lines
9.4 KiB
C#
/*******************************************************************************************
|
|
*
|
|
* raylib [audio] example - amp envelope
|
|
*
|
|
* Example complexity rating: [★☆☆☆] 1/4
|
|
*
|
|
* Example originally created with raylib 6.0, last time updated with raylib 6.0
|
|
*
|
|
* Example contributed by Arbinda Rizki Muhammad (@arbipink) and reviewed by Ramon Santamaria (@raysan5)
|
|
*
|
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
|
* BSD-like license that allows static linking with closed source software
|
|
*
|
|
* Copyright (c) 2026 Arbinda Rizki Muhammad (@arbipink)
|
|
*
|
|
********************************************************************************************/
|
|
|
|
namespace Examples.Audio;
|
|
|
|
public unsafe partial class AmpEnvelope : IExample
|
|
{
|
|
private const int screenWidth = 800;
|
|
private const int screenHeight = 450;
|
|
|
|
private const int BUFFER_SIZE = 4096;
|
|
private const int SAMPLE_RATE = 44100;
|
|
|
|
// Wave state
|
|
private enum ADSRState
|
|
{
|
|
Idle,
|
|
Attack,
|
|
Decay,
|
|
Sustain,
|
|
Release
|
|
}
|
|
|
|
// Grouping all ADSR parameters and state into a struct
|
|
private struct Envelope
|
|
{
|
|
public float AttackTime;
|
|
public float DecayTime;
|
|
public float SustainLevel;
|
|
public float ReleaseTime;
|
|
public float CurrentValue;
|
|
public ADSRState State;
|
|
}
|
|
|
|
public string Name => "Audio / Amp Envelope";
|
|
|
|
public string Title => "raylib [audio] example - amp envelope";
|
|
|
|
private float[] buffer;
|
|
private AudioStream stream;
|
|
private float audioTime;
|
|
private Envelope env;
|
|
|
|
public void Init()
|
|
{
|
|
InitAudioDevice();
|
|
|
|
// Set the number of samples the stream will keep in memory at a time to BUFFER_SIZE
|
|
SetAudioStreamBufferSizeDefault(BUFFER_SIZE);
|
|
buffer = new float[BUFFER_SIZE];
|
|
|
|
// Init raw audio stream (sample rate: 44100, sample size: 32bit-float, channels: 1-mono)
|
|
stream = LoadAudioStream(SAMPLE_RATE, 32, 1);
|
|
|
|
// Init Phase
|
|
audioTime = 0.0f;
|
|
|
|
// Initialize the struct
|
|
env = new Envelope
|
|
{
|
|
AttackTime = 1.0f,
|
|
DecayTime = 1.0f,
|
|
SustainLevel = 0.5f,
|
|
ReleaseTime = 1.0f,
|
|
CurrentValue = 0.0f,
|
|
State = ADSRState.Idle
|
|
};
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
// Update
|
|
//----------------------------------------------------------------------------------
|
|
if (IsKeyPressed(KeyboardKey.Space))
|
|
{
|
|
env.State = ADSRState.Attack;
|
|
}
|
|
|
|
if (IsKeyReleased(KeyboardKey.Space) && (env.State != ADSRState.Idle))
|
|
{
|
|
env.State = ADSRState.Release;
|
|
}
|
|
|
|
if (IsAudioStreamProcessed(stream))
|
|
{
|
|
if ((env.State != ADSRState.Idle) || (env.CurrentValue > 0.0f))
|
|
{
|
|
for (int i = 0; i < BUFFER_SIZE; i++)
|
|
{
|
|
UpdateEnvelope(ref env);
|
|
FillAudioBuffer(i, buffer, env.CurrentValue, ref audioTime);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Clear buffer if silent to avoid looping noise
|
|
for (int i = 0; i < BUFFER_SIZE; i++)
|
|
{
|
|
buffer[i] = 0;
|
|
}
|
|
|
|
audioTime = 0.0f;
|
|
}
|
|
|
|
fixed (float* bufferPtr = buffer)
|
|
{
|
|
UpdateAudioStream(stream, bufferPtr, BUFFER_SIZE);
|
|
}
|
|
}
|
|
|
|
if (!IsAudioStreamPlaying(stream))
|
|
{
|
|
PlayAudioStream(stream);
|
|
}
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Draw
|
|
//----------------------------------------------------------------------------------
|
|
BeginDrawing();
|
|
|
|
ClearBackground(Color.RayWhite);
|
|
|
|
// NOTE: raygui is not bound in raylib-cs, so the sliders below are left as reference.
|
|
// The envelope keeps its default parameters (Attack/Decay/Release = 1.0s, Sustain = 0.5).
|
|
//GuiSliderBar(new Rectangle( 100, 60, 400, 30 ), "Attack (s)", TextFormat("%2.2fs", env.AttackTime), ref env.AttackTime, 0.1f, 3.0f);
|
|
//GuiSliderBar(new Rectangle( 100, 100, 400, 30 ), "Decay (s)", TextFormat("%2.2fs", env.DecayTime), ref env.DecayTime, 0.1f, 3.0f);
|
|
//GuiSliderBar(new Rectangle( 100, 140, 400, 30 ), "Sustain", TextFormat("%2.2f", env.SustainLevel), ref env.SustainLevel, 0.0f, 1.0f);
|
|
//GuiSliderBar(new Rectangle( 100, 180, 400, 30 ), "Release (s)", TextFormat("%2.2fs", env.ReleaseTime), ref env.ReleaseTime, 0.1f, 3.0f);
|
|
|
|
DrawADSRGraph(ref env, new Rectangle(100, 250, 400, 100));
|
|
|
|
DrawCircleV(new Vector2(520, 350 - (env.CurrentValue * 100)), 5, Color.Maroon);
|
|
DrawText($"Current Gain: {env.CurrentValue:F2}", 535, (int)(345 - (env.CurrentValue * 100)), 10, Color.Maroon);
|
|
|
|
DrawText("Press SPACE to PLAY the sound!", 200, 400, 20, Color.LightGray);
|
|
|
|
EndDrawing();
|
|
//----------------------------------------------------------------------------------
|
|
}
|
|
|
|
public void Unload()
|
|
{
|
|
UnloadAudioStream(stream);
|
|
CloseAudioDevice();
|
|
}
|
|
|
|
//------------------------------------------------------------------------------------
|
|
// Module Functions Definition
|
|
//------------------------------------------------------------------------------------
|
|
private static void FillAudioBuffer(int i, float[] buffer, float envelopeValue, ref float audioTime)
|
|
{
|
|
int frequency = 440;
|
|
buffer[i] = envelopeValue * MathF.Sin(2.0f * MathF.PI * frequency * audioTime);
|
|
audioTime += (1.0f / SAMPLE_RATE);
|
|
}
|
|
|
|
private static void UpdateEnvelope(ref Envelope env)
|
|
{
|
|
// Calculate the time delta for ONE sample (1/44100)
|
|
float sampleTime = 1.0f / SAMPLE_RATE;
|
|
|
|
switch (env.State)
|
|
{
|
|
case ADSRState.Attack:
|
|
{
|
|
env.CurrentValue += (1.0f / env.AttackTime) * sampleTime;
|
|
if (env.CurrentValue >= 1.0f)
|
|
{
|
|
env.CurrentValue = 1.0f;
|
|
env.State = ADSRState.Decay;
|
|
}
|
|
}
|
|
break;
|
|
case ADSRState.Decay:
|
|
{
|
|
env.CurrentValue -= ((1.0f - env.SustainLevel) / env.DecayTime) * sampleTime;
|
|
if (env.CurrentValue <= env.SustainLevel)
|
|
{
|
|
env.CurrentValue = env.SustainLevel;
|
|
env.State = ADSRState.Sustain;
|
|
}
|
|
}
|
|
break;
|
|
case ADSRState.Sustain:
|
|
{
|
|
env.CurrentValue = env.SustainLevel;
|
|
}
|
|
break;
|
|
case ADSRState.Release:
|
|
{
|
|
env.CurrentValue -= (env.SustainLevel / env.ReleaseTime) * sampleTime;
|
|
if (env.CurrentValue <= 0.001f) // Use a small threshold to avoid infinite tail
|
|
{
|
|
env.CurrentValue = 0.0f;
|
|
env.State = ADSRState.Idle;
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private static void DrawADSRGraph(ref Envelope env, Rectangle bounds)
|
|
{
|
|
DrawRectangleRec(bounds, Fade(Color.LightGray, 0.3f));
|
|
DrawRectangleLinesEx(bounds, 1, Color.Gray);
|
|
|
|
// Fixed visual width for sustain stage since it's an amplitude not a time value
|
|
float sustainWidth = 1.0f;
|
|
|
|
// Total time to visualize (sum of A, D, R + a padding for Sustain)
|
|
float totalTime = env.AttackTime + env.DecayTime + sustainWidth + env.ReleaseTime;
|
|
|
|
float scaleX = bounds.Width / totalTime;
|
|
float scaleY = bounds.Height;
|
|
|
|
Vector2 start = new Vector2(bounds.X, bounds.Y + bounds.Height);
|
|
Vector2 peak = new Vector2(start.X + (env.AttackTime * scaleX), bounds.Y);
|
|
Vector2 sustain = new Vector2(peak.X + (env.DecayTime * scaleX), bounds.Y + (1.0f - env.SustainLevel) * scaleY);
|
|
Vector2 rel = new Vector2(sustain.X + (sustainWidth * scaleX), sustain.Y);
|
|
Vector2 end = new Vector2(rel.X + (env.ReleaseTime * scaleX), bounds.Y + bounds.Height);
|
|
|
|
DrawLineV(start, peak, Color.SkyBlue);
|
|
DrawLineV(peak, sustain, Color.Blue);
|
|
DrawLineV(sustain, rel, Color.DarkBlue);
|
|
DrawLineV(rel, end, Color.Orange);
|
|
|
|
DrawText("ADSR Visualizer", (int)bounds.X, (int)bounds.Y - 20, 10, Color.DarkGray);
|
|
}
|
|
|
|
public static int Main()
|
|
{
|
|
// Initialization
|
|
//--------------------------------------------------------------------------------------
|
|
InitWindow(screenWidth, screenHeight, "raylib [audio] example - amp envelope");
|
|
|
|
SetTargetFPS(60);
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
var game = new AmpEnvelope();
|
|
game.Init();
|
|
|
|
// Main game loop
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
{
|
|
game.Update();
|
|
}
|
|
|
|
game.Unload();
|
|
|
|
// De-Initialization
|
|
//--------------------------------------------------------------------------------------
|
|
CloseWindow(); // Close window and OpenGL context
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
return 0;
|
|
}
|
|
}
|