RandyGaul / cute_headers

Collection of cross-platform one-file C/C++ libraries with no dependencies, primarily used for games
4.24k stars 264 forks source link

cute_sound.h segmentation fault with `cs_insert_sound()` #303

Closed benstigsen closed 2 years ago

benstigsen commented 2 years ago

I'm really not sure what's happening. So I downloaded piano2.wav from https://www.kozco.com/tech/soundtests.html

Using this with the tigr library I get an error. This is the full code:

#define CUTE_SOUND_IMPLEMENTATION
#include "cute_sound.h"
#include "tigr.h"

int main(int argc, char *argv[])
{
    Tigr *screen = tigrWindow(320, 240, "Hello", 0);

    cs_context_t* ctx = cs_make_context(screen->handle, 48000, 4092 * 2, 0, NULL);
    cs_loaded_sound_t piano_audio = cs_load_wav("./assets/piano2.wav");
    cs_playing_sound_t piano_instance = cs_make_playing_sound(&piano_audio);
    cs_insert_sound(ctx, &piano_instance);
    while (!tigrClosed(screen))
    {
        tigrClear(screen, tigrRGB(0x80, 0x90, 0xa0));
        tigrPrint(screen, tfont, 120, 110, tigrRGB(0xff, 0xff, 0xff), "Hello, world.");
        tigrUpdate(screen);
    }
    tigrFree(screen);
    cs_free_sound(&piano_audio);
    return 0;
}

If I remove cs_insert_sound(ctx, &piano_instance); then it shows the screen and everything, but doesn't play any audio of course.

My project structure looks like this:

project/
  assets/
    - piano2.wav
  src/
    - cute_sound.h
    - tigr.c
    - tigr.h
    - main.c
  - executable

I am not sure what's happening. The sample rate for the audio is 48k Hz. I have a feeling it may be related to the context window handle? I'm not too experienced with C debugging, so not sure how to go about it. But if someone could verify, that would be awesome.

RandyGaul commented 2 years ago

This is on which platform?

benstigsen commented 2 years ago

Linux, specifically Pop!_OS 22.04

RandyGaul commented 2 years ago

Okay cool and it’s using the Alsa backend right? That backend is buggy and I recommend trying the SDL2 backend instead. Can you confirm which you’re using?

RandyGaul commented 2 years ago

I tried loading up and playing your wav file on Windows without any issues. The only thing I could think of for your case, besides a potential Alsa implementation bug, is that the audio file path you provided isn't quite correct, so the the audio samples are NULL causing a crash.

RandyGaul commented 2 years ago

Going to close this for now, assuming you’ve lost interest. Please re-open or comment here if you’d like to continue!