SDL_Mixer, printf output

Hello sdl,

Can someone please tell what’s wrong with this small program:-----------

#include “SDL.h”
#include “SDL_mixer.h”
#include <stdio.h>
int main( int argc, char* argv[] )

{

    SDL_Init (SDL_INIT_AUDIO);
    
    if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)==-1) {
            char* err=Mix_GetError();
            return 0;
    }
    
    Mix_AllocateChannels(2);
    Mix_Chunk *sample;
    sample=Mix_LoadWAV("Bes2.wav");
    if(!sample) {
            char* err=Mix_GetError();
    }else{
            
            Mix_VolumeChunk(sample, MIX_MAX_VOLUME);
            
            if(Mix_PlayChannel(-1, sample, 1)==-1) {
                    char* err=Mix_GetError();
                    // may be critical error, or maybe just no channels were free.
                    // you could allocated another channel in that case...
            }
            Mix_FreeChunk(sample);
    }
    
    Mix_CloseAudio();
    
    SDL_Quit();

}


It compiles well, no errors appears while executing, but Mix_PlayChannel()
simply do not play wanted wav file (but does not return an error?). What’s wrong
here?

Also, I’m not very familiar with MS VS environment - when I use printf where
that output goes? I don’t see any window/console opens.


Best regards,
Milan mailto:milan_g at eunet.yu

There may be 2 versions of sdlmain.lib, with/without stdio redirection.
Just use the normail one, and a “stdout.txt” would be found in the directory
same as your program. That’s your standard output. :slight_smile:

“Milan Golubovic” <milan_g at EUnet.yu> ???:655912950.20041130105647 at eunet.yu…> It compiles well, no errors appears while executing, but

Mix_PlayChannel()
simply do not play wanted wav file (but does not return an error?). What’s
wrong
here?

Also, I’m not very familiar with MS VS environment - when I use printf
where
that output goes? I don’t see any window/console opens.

    }else{
            
            Mix_VolumeChunk(sample, MIX_MAX_VOLUME);
            
            if(Mix_PlayChannel(-1, sample, 1)==-1) {
                    char* err=Mix_GetError();
                    // may be critical error, or maybe just no channels were free.
                    // you could allocated another channel in that case...
            }
            Mix_FreeChunk(sample);
    }
    
    Mix_CloseAudio();

You are freeing the chunk while it’s still playing. You’re lucky the
program isn’t just crashing, but since you close down the mixer
immediately afterwards, you are probably beating the clock on that one.

Mix_PlayChannel() returns immediately; it doesn’t block until the sound
is finished playing. You can either set up a callback with
Mix_ChannelFinished() to notify you when the sound is done playback, or
do something like this instead:

int chan = Mix_PlayChannel(-1, sample, 0);
// make sure chan isn’t an error state here…
while (Mix_Playing(chan))
{
SDL_Delay(30); // do nothing…sound is playing in another thread.
}
// now it is safe to free the chunk and close the mixer.

The while loop above assumes you don’t have anything else you want to do
in the meantime.

Also, I’m not very familiar with MS VS environment - when I use printf where
that output goes? I don’t see any window/console opens.

Roughly speaking, it goes to /dev/null. Use freopen() and point
stderr/stdout at files, then you can read them after running the
program. If you need realtime output, use the Win32 API call
"OutputDebugString()" … maybe with a short call to SDL_Delay() right
afterwards so that the debugger has time to receive and display the string.

When run under VS.NET’s debugger, OutputDebugString() will output text
to a debugger window in the IDE. Outside the debugger, this API call
more or less goes off the ether, but there are other programs that trap
this output, so it’s best not to leave it in a final program.

–ryan.

Hello Ryan,

RCG> You are freeing the chunk while it’s still playing. You’re lucky the
RCG> program isn’t just crashing, but since you close down the mixer
RCG> immediately afterwards, you are probably beating the clock on that one.

Yep, that’s right. It works now. Thank you.

Also while debugging I noticed that sound actually start playing only when
program reach SDL_Delay() and keep playing only while SDL_Delay() is kept
calling?!? Is that means that sound mixing routine is not in interrupt or some
kind of separate thread?? Normally I want my game to have frame rate higher as
possible. That means I wont call SDL_Delay() to slow me down. How will then
may samples be played? And generally, do I have to call some delay/wait function
to give other applications running in multitasking some CPU time?
Or maybe it acts like this only while debugging (execution freezes)?

RCG> When run under VS.NET’s debugger, OutputDebugString() will output text
RCG> to a debugger window in the IDE. Outside the debugger, this API call
RCG> more or less goes off the ether, but there are other programs that trap
RCG> this output, so it’s best not to leave it in a final program.

Speaking of .net - I had some problems compiling programs that are developed
with VS6. What is situation with SDL. Should there be some problems switching
between VS 6 and 7? Which is better for SDL developing?–
Best regards,
Milan mailto:milan_g at eunet.yu