I'm Learning a New Instrument

c++ · raylib · project · music

I used to carry my saxophone everywhere. My back doesn’t miss it, but I do miss having an instrument with me whenever I feel like playing. These days my laptop is always in my backpack, so I figured I’d try turning it into one.

I’m building a small synthesizer in C++ with Raylib, played entirely on the laptop keyboard. I’m mapping the layout of a chromatic button accordion onto QWERTY. It seems (and I’ve seen someone do it before) like a better fit for the keyboard, compared to mapping a piano.

If you’re not hip to the accordion, check this out. This is one of my favourite players, João Barradas, playing a Chopin étude:

Chopin's Étude in C major, Op. 10 No. 1, played by João Barradas. Watch the full thing.

I believe this accordion uses the same layout logic I tried in my app, but with five rows. I think some notes are repeated.

I remember trying an accordion when I was ten. I don’t think I managed to play even a C major scale. It’s a hard instrument.

Keyboard Layout

I did attempt to map some sort of piano layout, where the hand jumps between two halves of the keyboard. It’s not really the most comfortable, and honestly an accordion layout just sounds way cooler.

A chromatic button accordion forms some sort of a grid. Every button in a column is a minor third above the one before it, and neighbouring rows are one semitone apart.

 Q row:   Q    W    E    R    T    Y    U    I
          A3   C4   Eb4  Gb4  A4   C5   Eb5  Gb5

 A row:    A    S    D    F    G    H    J    K
           Ab3  B3   D4   F4   Ab4  B4   D5   F5

 Z row:      Z    X    C    V    B    N    M    ,
             G3   Bb3  Db4  E4   G4   Bb4  Db5  E5
The synth window on top of the code editor: three rows of circles with note names, three of them black because their keys are held
The keys X, D, T are being pressed, triggering Bb3, D4 and A4.

I get two full octaves. The downside is that it’s not a piano, which has the fingerings I’m familiar with. This process of learning new keybinds to play is starting to give me déjà vu of when I started to learn Vim motions to program. There was definitely a learning curve, but it was worth the effort, and it made my programming practice way more enjoyable and fun. Maybe I’ll get the same amount of enjoyment out of internalizing these keys into my system.

I made a simple interface where I could see the buttons with the note labels on them, and I made them signal when I press the keys.

Getting C major under my fingers.

To the Code and Beyond

Every key plays a note, and every note has a frequency that represents how many times per second its sound wave vibrates. A4 is 440 Hz, and each octave up doubles it.

Digital audio is a long list of numbers, called samples, that tell the speaker how to move. I set the sample rate to be at 44,100. Raylib keeps asking for the next batch of samples, and this function provides them:

void audio_callback(void* buffer, unsigned int frames) {
    float* out = static_cast<float*>(buffer);

    for (unsigned int i = 0; i < frames; ++i) {
        float sample = 0.0f;

        // add up the waves of every key that is held down
        for (Voice& voice : voices) {
            if (!voice.on) continue;

            sample += std::sin(2.0f * PI * voice.phase) * 0.3f;

            // move along the wave, faster for higher notes
            voice.phase += voice.freq / SAMPLE_RATE;
            if (voice.phase >= 1.0f) voice.phase -= 1.0f;
        }

        out[i] = sample;
    }
}

Each key has its own voice, which remembers its note’s frequency and where it is in its wave. A chord is just several waves added together.

Chords that refuse to play

I ran into this strange problem where some three-note chords work perfectly, while others don’t.

Unlike a piano, where each key has its own mechanism, a laptop keyboard uses a grid of shared electrical connections to detect which keys are pressed. It’s a way of reducing the number of wires needed.

The downside is that with some combinations of three keys, the keyboard can’t tell which ones are pressed, so it leaves one out. This is known as ghosting.

You can see it happen in the clip below. The fourth chord, A major (E, A and C#), refuses to sound properly when I press V, T and M together.

Moving major triads in minor 3rds, chord fail

What’s next

I’ll try to get some more sounds, and effects, maybe implement a way to record and layer tracks, I could eventually turn this into a small DAW (digital audio workstation). I don’t know how far I’ll take it, but it sounds like a fun way to keep learning and making music at the same time. I’ll write about it here as I go.

Practicing lines.

Check the code

All posts