0

I'm using SDL2 on macOS but it seems like the SDL_MixAudioFormat isn't working properly. It doesn't crash or provide any errors, but the output is silent.

Here is the function:

static void audio_sdl_play(const uint8_t *buf, size_t len) {
   uint8_t *mix_buf[len];
   SDL_memset(mix_buf, 0, len);
   SDL_MixAudioFormat(mix_buf, buf, AUDIO_S16, len, SDL_MIXER_MAXVOLUME);
   SDL_QueueAudio(dev, mix_buf, len);
}

However, if I do not use the mixer it works perfectly fine:

static void audio_sdl_play(const uint8_t *buf, size_t len) {
   SDL_QueueAudio(dev, buf, len);
}

Am I doing something wrong?

I'm seeing compiler warnings but I'm not sure how to fix them and I'm also using similar code to every example I've seen online.

Here are the warnings:

src/pc/audio/audio_sdl2.c: In function 'audio_sdl_play':
src/pc/audio/audio_sdl2.c:53:28: warning: passing argument 1 of 'SDL_MixAudioFormat' from incompatible pointer type [-Wincompatible-pointer-types]
   53 |         SDL_MixAudioFormat(mix_buf, buf, AUDIO_S16, len, 128);
      |                            ^~~~~~~
      |                            |
      |                            uint8_t ** {aka unsigned char **}
In file included from /opt/homebrew/include/SDL2/SDL.h:36,
                 from src/pc/audio/audio_sdl2.c:4:
/opt/homebrew/include/SDL2/SDL_audio.h:1178:57: note: expected 'Uint8 *' {aka 'unsigned char *'} but argument is of type 'uint8_t **' {aka 'unsigned char **'}
 1178 | extern DECLSPEC void SDLCALL SDL_MixAudioFormat(Uint8 * dst,
      |                                                 ~~~~~~~~^~~
SofaKng
  • 1,063
  • 1
  • 11
  • 32
  • I can’t test this right now but I suspect your problem is that you’re using an array of pointers to uint8_t. SDL_MixAudioFormat expects a pointer to uint8_t as it’s first argument and you’re providing an invalid parameter. It works the second time because you don’t try to include your mix_buf variable. – SafelyFast Apr 04 '23 at 12:40

0 Answers0