My SDL program won't sound!

This is a discussion on My SDL program won't sound! within the C++ Programming forums, part of the General Programming Boards category; I can't understand why my program doesn't sound anything. I think it should work because I've almost copied the code ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    9

    My SDL program won't sound!

    I can't understand why my program doesn't sound anything. I think it should work because I've almost copied the code from here. As you can se I use SDL Mixer.
    Code:
    #include "SDL.h"
    #include "SDL_mixer.h"
    
    bool init();
    bool load_files();
    void clean_up();
    
    Mix_Music   *music  = NULL;
    SDL_Surface *screen = NULL;
    SDL_Event event;
    
    int main(int argc, char *argv[])
    {
        bool quit = false;
        
        if (init() == false)
            return 1;
        if (load_files() == false)
            return 1;
        
        while (quit == false)
        {
            while (SDL_PollEvent(&event))
            {
                if (event.type == SDL_KEYDOWN) {
                    if (event.key.keysym.sym == SDLK_SPACE) {
                        if (Mix_PlayingMusic == 0) {
                            if (Mix_PlayMusic(music, -1) == -1)
                                return 1;
                        } else {
                            if (Mix_PausedMusic() == 1)
                                Mix_ResumeMusic();
                            else
                                Mix_PauseMusic();
                        }
                    } else if (event.key.keysym.sym == SDLK_ESCAPE) {
                        Mix_HaltMusic();
                    }
                } else if (event.type == SDL_QUIT) {
                    quit = true;
                }
            }
        }
        
        clean_up();
        return 0;
    }
    
    bool init()
    {
        if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) == -1)
            return false;
            
        screen = SDL_SetVideoMode(320, 240, 0, 0);
        
        if (screen == NULL)
            return false;
            
        if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
            return false;
            
        SDL_WM_SetCaption("Musicplayer", NULL);
        
        return true;
    }
    
    bool load_files()
    {
        music = Mix_LoadMUS("test.mp3");
        
        if (music == NULL)
            return false;
            
        return true;
    }
    
    void clean_up()
    {
        Mix_FreeMusic(music);
        Mix_CloseAudio();
        SDL_Quit();
    }

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I don't know anything about this particular library.... but this stuff is tricky!

    Do they include any diagnostic code-samples? It's kind of hard to narrow-down the problem if the program simply doesn't seem to do anything...

    Have you tried it with a WAV file? Are you decoding the MP3? (I didn't notice a reference to a codec in your code, but I might have missed it.)

    You need to make sure you've "made a connection" with your soundcard/soundship's driver too. If you have a soundcard and a motherboard-soundchip, you might be sending the sound to the wrong one.

    If you're running Windows, you might consider using DirectSound. It's a lot easier to get help with the more-popular libraries. It's a lot easier to find reference material too... not easy... just easier.
    Last edited by DougDbug; 04-14-2006 at 03:34 PM.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    I've tested with mp3, ogg vorbis and wave files. When I use mp3 and ogg the result is the same; nothing happens, and when I test with a wave file the Mix_LoadMUS() function returns NULL (which leads to that the load_files() function returns false and the program shuts down).

    I'm running Windows.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    OK, one more guess... Maybe it can't find the file... It looks a little weird that you didn't tell the program what folder it's in.

    Try moving the file to the root directory. Then, specify the full path like this:

    music = Mix_LoadMUS("C:\\test.wav"); \\ I think you need 2 backslashes because it's an escape character.

    A WAV file should be the eaisest, because it's not encoded. A wave file is essentially an array of individual samples. You can simply "connect the dots" to re-construct the waveform. I assume the SDL library will automatically read the WAV file-header to get the sample rate and bit depth, and then put the sample-data into an array or buffer.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    Still the same. Mix_LoadMUS() returns NULL.

    Edit: With Mix_GetError() I get Module format not recognized when trying to load a mp3 file and Unable to load WAV file when trying to load a wav.

    Edit #2: I think I've solved it now. The reason it wouldn't play no matter which format is that I forgot parenthesis on line 30.
    Now it plays Wave and Ogg vorbis, but unfortunately not mp3.
    Last edited by @nders; 04-14-2006 at 05:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a makefile to compile a program using SDL
    By Noerbo in forum Game Programming
    Replies: 9
    Last Post: 04-30-2006, 09:11 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. Can't compile an SDL program under Linux
    By dwks in forum Game Programming
    Replies: 2
    Last Post: 01-16-2006, 07:51 PM
  4. Help me modify my Sound Server
    By zdude in forum C Programming
    Replies: 1
    Last Post: 05-14-2003, 05:15 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21