Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttachAudioMixedProcessor Utils for easy callback #215

Conversation

nickyMcDonald
Copy link
Contributor

@nickyMcDonald nickyMcDonald commented Jan 4, 2024

Utilizing delegates. Attaching a callback to the audio is made much more simple.

using Raylib_cs;
using System;
using System.Numerics;

static class Synthesizer
{
    const int width = 1280;
    const int height = 960;

    static int shape = 0;       // The shape of the wave
    static float position = 0f; // The sin wave position
    static float volume = .5f;  // The sin wave amplitude
    static float pitch = .5f;   // The sin wave frequency

    static void Synth(Span<float> buffer)
    {
        for (int i = 0; i < buffer.Length; i += 2)
        {
            position += pitch / 10f;                   // Move the wave position
            position = Raymath.Wrap(position, 0f, 2f); // Wrap around
            float wave = float.SinPi(position);        // The sin wave
            if (shape == 1)
            {
                wave = float.AsinPi(wave) * 2f;        // Triangle wave
            }
            else if (shape == 2)
            {
                wave = float.Sign(wave);               // Square wave
            }
            buffer[i + 0] = wave * volume;             // Left ear
            buffer[i + 1] = wave * volume;             // Right ear
        }
    }

    static void Main()
    {
        Raylib.InitWindow(width, height, $"Raylib-cs Synthesizer");
        Raylib.SetTargetFPS(60);
        Raylib.InitAudioDevice();

        Raylib.AttachAudioMixedProcessor(Synth); // Attach the synth

        while (!Raylib.WindowShouldClose())
        {
            float frameTime = Raylib.GetFrameTime();

            if (Raylib.IsKeyDown(KeyboardKey.KEY_UP))       // Volume up
            {
                volume = Raymath.Lerp(volume, 1f, frameTime);
            }
            if (Raylib.IsKeyDown(KeyboardKey.KEY_DOWN))     // Volume down
            {
                volume = Raymath.Lerp(volume, 0f, frameTime);
            }
            if (Raylib.IsKeyDown(KeyboardKey.KEY_RIGHT))    // Higher tone
            {
                pitch = Raymath.Lerp(pitch, 1f, frameTime);
            }
            if (Raylib.IsKeyDown(KeyboardKey.KEY_LEFT))     // Lower tone
            {
                pitch = Raymath.Lerp(pitch, 0f, frameTime);
            }
            if (Raylib.IsKeyPressed(KeyboardKey.KEY_SPACE)) // Toggle shape
            {
                shape = (int)Raymath.Wrap(shape + 1, 0, 3);
            }

            Raylib.BeginDrawing();
            Raylib.ClearBackground(Color.BLACK);
            Vector2 start = new(0f, height / 2);

            for (int x = 1; x < width; x++)
            {
                float wave = float.SinPi(x * pitch / 10f);
                if (shape == 1)
                {
                    wave = float.AsinPi(wave) * 2f;
                }
                else if (shape == 2)
                {
                    wave = float.Sign(wave);
                }
                Vector2 end = new(x, (wave * volume + 1) * height / 2);
                Raylib.DrawLineEx(start, end, 4f, Color.VIOLET);
                start = end;
            }
            Raylib.EndDrawing();
        }

        Raylib.DetachAudioMixedProcessor(Synth); // Detach the synth

        Raylib.CloseAudioDevice();
        Raylib.CloseWindow();
    }
}
247164f2-50f6-4be6-b22e-5d085724efbf.mp4

@MrScautHD
Copy link
Contributor

looks very well!

@chrisdill chrisdill merged commit b049b4f into chrisdill:master Feb 24, 2024
@chrisdill
Copy link
Owner

Thanks, merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants