Adding Music To My Raylib App

minute read

2025-03-13 Back to posts

raylib and C logo

Adding Music To My Raylib App

For some reason, the musician in me awoke today.

I decided to use my Novation LaunchKey Mini [MK3] to compose and write a simple tune for an app I made last year that has garnered interest from the public on Github.

The app in question is a snowfall simulation using raylib, compiled on Linux Mint with GCC.

In this blog post, I’m going to be breaking down the code and the music composition!

Let’s dive in.

Programming

I decided to tackle the programming first, I’m more comfortable in VSCode compared to Ableton.

Adding the button

I added a button under the other buttons already on the screen, it simply toggles the music on and off. More on the logic later.

Vector2 playMusicPos = {padding + 10, padding + 170};
DrawRectangle(playMusicPos.x, playMusicPos.y, buttonWidth, buttonHeight, GRAY);
DrawText("Toggle Music", playMusicPos.x + buttonWidth / 2 - MeasureText("Toggle Music", 10) / 2, 
                         playMusicPos.y + buttonHeight / 2 - 5, 11, WHITE);

We make use of various functions/classes built into raylib here, including DrawRectangle();, Vector2, and DrawText();.

These built in functions/classes allow us to build UIs in C easily!

The button (red arrow)

Photo of the button

Adding the logic to play music

Adding the control flow to play the music when the button is clicked was simple.

Rectangle playMusicButton = {10, 170, 75, 30};
    if (CheckCollisionPointRec(mousePos, playMusicButton))
    {
        if (soundPlaying) 
        {
            soundPlaying = false;
            StopSound(sound);
        } 
        else
        {
            soundPlaying = true;
            PlaySound(sound);
        }
    }

and in our header file:

bool soundPlaying = false;

Within our main function, we check if the left mouse button is being pressed and grab the mouse position at the time its clicked.

From there, we can simply stop the sound, or play it!

That was basically all the code I had to write (besides a small refactor) in order to play the music.

Now onto the music.

Music Composition

For music production, I use Ableton Live 12 Lite and a Launchkey Mini MK3.

The LaunchKey Mini MK3

  1. Creating a Soft Pad: I started by creating a soft, ambient pad for the background. This pad will give the track a dreamy atmosphere that blends well with the falling snow.

  2. Adding Chord Progression: For a relaxing vibe, I laid down a repetitive chord progression. Something soft & organic, like C - G - A - F, played on the pad.

  3. Adding Drums and Bassline: To keep it chill but moving, I added a slow 808 drum pattern with a simple kick, snare, and hi-hat pattern.

  4. Fine-Tuning Effects: I added some reverb and delay to the pad to give it a spacious, ambient feel.

You can listen to the song I made below 👇

Conclusion

Adding music to my raylib app wasn’t as difficult as I thought it would be.

Thanks to the simplicity of raylib, I was able to integrate it quickly with just a few lines of code.

The music itself was a fun challenge, especially with my Launchkey Mini guiding me through the composition process in Ableton.

If you’re looking to add music to your own project, I hope this breakdown gives you inspiration!

View the full code on Github!

Written By Nick Stambaugh

Nick Stambaugh

Nick Stambaugh

Full Stack Engineer

Full Stack Enterprise Application Engineer

Recent Posts

Adding Music To My Raylib App

Adding audio to a C application is easy with raylib!

2025-03-13

Read more →

Why I Love Houston As A Michigander

Houston is an amazing city to explore with a wide variety of interesting activities, culture, and food.

2025-02-22

Read more →

What I've Learned about LINQ and MVC

An example of why I'm starting to love C# more than Go.

2025-1-19

Read more →

[PART 1] Creating A SQL-Like Language in C#

Part 1 of creating a SQL-like language in C# for fun.

2025-1-3

Read more →

A Simple Bar-Chart Console App in TypeScript

Creating an ASCII bar chart generator in the console with TypeScript.

2024-12-27

Read more →

A New (Unfinished) React Subscribe Component

A new React component for easy email subscriptions on your website.

2024-12-14

Read more →