Skip to content

Instantly share code, notes, and snippets.

@typeswitch-dev
Created June 26, 2019 23:14
Show Gist options
  • Select an option

  • Save typeswitch-dev/a567b9608d68b55287c1a59f36f6206e to your computer and use it in GitHub Desktop.

Select an option

Save typeswitch-dev/a567b9608d68b55287c1a59f36f6206e to your computer and use it in GitHub Desktop.

Revisions

  1. typeswitch-dev created this gist Jun 26, 2019.
    40 changes: 40 additions & 0 deletions beep-boop.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    #include "SDL.h"

    #define SAMPLE_RATE 48000
    #define CHANNELS 2
    #define SAMPLES (SAMPLE_RATE*2)

    int main (int argc, char** argv)
    {
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_AudioSpec want, got;
    want.freq = SAMPLE_RATE;
    want.format = AUDIO_F32;
    want.channels = CHANNELS;
    want.samples = 4096;
    want.callback = NULL;

    float* buffer = calloc(SAMPLES * CHANNELS, sizeof *buffer);
    for (int i = 0; i < SAMPLE_RATE; i++) {
    float t = (float)i / (float)SAMPLE_RATE;
    float f = sin(t*648*3.14159f) * t*(1-t);
    float g = sin(t*432*3.14159f) * t*(1-t);
    buffer[i*2] = f;
    buffer[i*2+1] = f;
    buffer[SAMPLE_RATE*2 + i*2] = g;
    buffer[SAMPLE_RATE*2 + i*2 + 1] = g;
    }

    SDL_AudioDeviceID dev = SDL_OpenAudioDevice(NULL, 0, &want, &got, 0);
    if (dev == 0) {
    SDL_Log("Failed to open audio: %s", SDL_GetError());
    } else {
    SDL_QueueAudio(dev, buffer, SAMPLES * CHANNELS * sizeof *buffer);
    SDL_PauseAudioDevice(dev, 0);
    SDL_Delay(2200);
    SDL_CloseAudioDevice(dev);
    }
    SDL_Quit();
    return 0;
    }