mingw32-SDL_mixer cannot open audio device

Christopher Thielen chris at epiar.net
Wed Jul 27 03:29:13 UTC 2011


Hi fedora-mingw,

I've tried the following on Windows XP and WINE, using multiple machines
as well as trying the mingw32-SDL_mixer RPM versus compiling SDL_mixer
myself using fedora-mingw's gcc - no matter what I do, I cannot get this
SDL_mixer demo to work.

It should open a 320x200 window and play 'music.ogg' from the current
directory when you press 'm'. Press again to stop.

This demo works fine as a native Linux app. Cross-compiling with MinGW
causes the audio device to simply not open.

Anybody have any clue what's going on?

- Christopher Thielen

-------------- next part --------------
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include "SDL.h"
#include "SDL_mixer.h"

/* Mix_Music actually holds the music information.  */
Mix_Music *music = NULL;

void handleKey(SDL_KeyboardEvent key);
void musicDone();

int main(int argc, char **argv) {
  SDL_Surface *screen;
  SDL_Event event;
  int done = 0;

  /* We're going to be requesting certain things from our audio
     device, so we set them up beforehand */
  int audio_rate = 44100;
  Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
  int audio_channels = 2;
  int audio_buffers = 4096;

  SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);

  /* This is where we open up our audio device.  Mix_OpenAudio takes
     as its parameters the audio format we'd /like/ to have. */
  if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
    printf("Unable to open audio! Reason: %s\n", Mix_GetError());
    exit(1);
  }

  /* If we actually care about what we got, we can ask here.  In this
     program we don't, but I'm showing the function call here anyway
     in case we'd want to know later. */
  Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);

  /* We're going to be using a window onscreen to register keypresses
     in.  We don't really care what it has in it, since we're not
     doing graphics, so we'll just throw something up there. */
  screen = SDL_SetVideoMode(320, 240, 0, 0);

  while(!done) {
    while(SDL_PollEvent(&event)) {
      switch(event.type) {
      case SDL_QUIT:
	done = 1;
	break;
      case SDL_KEYDOWN:
      case SDL_KEYUP:
	handleKey(event.key);
	break;
      }
    }

    /* So we don't hog the CPU */
    SDL_Delay(50);

  }

  /* This is the cleaning up part */
  Mix_CloseAudio();
  SDL_Quit();

}

void handleKey(SDL_KeyboardEvent key) {
  switch(key.keysym.sym) {
  case SDLK_m:
    if(key.state == SDL_PRESSED) {

      /* Here we're going to have the 'm' key toggle the music on and
	 off.  When it's on, it'll be loaded and 'music' will point to
	 something valid.  If it's off, music will be NULL. */

      if(music == NULL) {
	
	/* Actually loads up the music */
	music = Mix_LoadMUS("music.ogg");

	/* This begins playing the music - the first argument is a
	   pointer to Mix_Music structure, and the second is how many
	   times you want it to loop (use -1 for infinite, and 0 to
	   have it just play once) */
	Mix_PlayMusic(music, 0);

	/* We want to know when our music has stopped playing so we
	   can free it up and set 'music' back to NULL.  SDL_Mixer
	   provides us with a callback routine we can use to do
	   exactly that */
	Mix_HookMusicFinished(musicDone);
	
      } else {
	/* Stop the music from playing */
	Mix_HaltMusic();

	/* Unload the music from memory, since we don't need it
	   anymore */
	Mix_FreeMusic(music);
	
	music = NULL;
      }
      break;
    }
  }
}

/* This is the function that we told SDL_Mixer to call when the music
   was finished. In our case, we're going to simply unload the music
   as though the player wanted it stopped.  In other applications, a
   different music file might be loaded and played. */
void musicDone() {
  Mix_HaltMusic();
  Mix_FreeMusic(music);
  music = NULL;
}

-------------- next part --------------
default:
	gcc -g -pipe -Wall -o music_test music_test.c -lSDL_mixer -lSDL -I/usr/include/SDL

mingw:
	i686-pc-mingw32-gcc -g -pipe -Wall -I/usr/i686-pc-mingw32/sys-root/mingw/include -I/usr/i686-pc-mingw32/sys-root/mingw/include/freetype2   -I/usr/i686-pc-mingw32/sys-root/mingw/include/libxml2   -I/usr/i686-pc-mingw32/sys-root/mingw/include/SDL -Dmain=SDL_main -Dmain=SDL_main -I/usr/i686-pc-mingw32/sys-root/mingw/include/SDL -I/usr/i686-pc-mingw32/sys-root/mingw/include/SDL -o music_test.exe music_test.c -L/usr/i686-pc-mingw32/sys-root/mingw/lib -L/usr/i686-pc-mingw32/sys-root/mingw/lib -mwindows -L/usr/i686-pc-mingw32/sys-root/mingw/lib -L/usr/i686-pc-mingw32/sys-root/mingw/lib -lSDL_mixer -lmingw32 -lSDLmain -lSDL

clean:
	rm -f music_test.exe



More information about the mingw mailing list