I've a bit of experience with the SDL lib, but for my new project I need to play some sound in my program so I decided to use SDL-mixer. At first glance it works fine when I'm using Mix_Music pointer, but it isn't working with Mix_Chunk. Though compilation is running fine, the program is crashing right after launching. I have first coded the thing of my own following a bit of doc, and because of the segfault began using a piece of code from a tutorial, and still got the same segfault with this code. Here's the code:
#include <SDL/SDL.h>#include <SDL/SDL_mixer.h>#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){ int continuer = 1; int o = 0; int p = 0; SDL_Init(SDL_INIT_VIDEO); SDL_Surface *ecran = NULL; ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); SDL_Event event; SDL_WM_SetCaption("SDL_Mixer", NULL); SDL_Flip(ecran); if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1) //Initialisation de l'API Mixer { printf("%s", Mix_GetError()); } Mix_AllocateChannels(32); Mix_Volume(1, MIX_MAX_VOLUME/2); Mix_Chunk *kick = Mix_LoadWAV("kick.wav"); Mix_Chunk *snare = Mix_LoadWAV("snare.wav"); Mix_VolumeChunk(kick, MIX_MAX_VOLUME/2); Mix_VolumeChunk(snare, MIX_MAX_VOLUME); while(continuer) { SDL_WaitEvent(&event); switch(event.type) { case SDL_QUIT: continuer = 0; break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_a: Mix_PlayChannel(1, kick, 0); break; case SDLK_z: Mix_PlayChannel(2, snare, 0); break; } break; } } Mix_FreeChunk(kick); Mix_FreeChunk(snare); Mix_CloseAudio(); SDL_Quit(); return EXIT_SUCCESS;}
First to try see where the error was coming from I tried to put some printf to see how far the program would go, and it's not going further the "Mix_Volume(1, MIX_MAX_VOLUME);" function. So I commented those function call to see if the program runs fine without and it did, unless I got no sound because the volume wasn't defined.
To be sure the problem is coming from this function I ran a gdb test and got this:
Thread 1 "main" received signal SIGSEGV, Segmentation fault.0x00007ffff78f7d82 in Mix_VolumeChunk ()
I don't know if it is usefull but I compiled using this command :
gcc main.c sdl-config --cflags -lSDL -lSDL_mixer -o main sdl-config --libs
I'm using Linux Mint 18.2 with gcc 4.8.5. Hope someone here will find out what is going on.
I've ran valgrind, some dubinging flags on gcc and backtrace on gdb and doesn't have any idea where the segfault is coming from. Either it's coming from the Mix_Chunl pointer either it's coming from the .wav file that for some reason the program isn't reaching, I will try with other soudfile to see how it's running.
EDI: The problem was that the file I wanted to play was mono file. I replaced it by a stereo one and now it's working fine.