Exemplo n.º 1
0
/**
 *  Treiberinitialisierungsfunktion.
 *
 *  @return @p true bei Erfolg, @p false bei Fehler
 *
 *  @author FloSoft
 */
bool AudioSDL::Initialize(void)
{
	if( SDL_InitSubSystem( SDL_INIT_AUDIO ) < 0 )
	{
		fprintf(stderr, "%s\n", SDL_GetError());
		initialized = false;
		return false;
	}

	// open 44.1KHz, signed 16bit, system byte order,
	// stereo audio, using 1024 byte chunks
	if(Mix_OpenAudio(44100, AUDIO_S16LSB, 2, 4096) < 0)
	{
		fprintf(stderr, "%s\n", Mix_GetError());
		initialized = false;
		return false;
	}

	Mix_AllocateChannels(CHANNEL_COUNT);
	Mix_SetMusicCMD(NULL);
	Mix_HookMusicFinished(AudioSDL::MusicFinished);

	initialized = true;

	return initialized;
}
Exemplo n.º 2
0
AudioSDLMixer::AudioSDLMixer(AudioConfig &sc)
  : Audio(sc), audio_open(0), music(NULL), oldPlaying(false)
{
  if ( SDL_InitSubSystem(SDL_INIT_AUDIO) < 0 ) 
    throw std::runtime_error(std::string("Couldn't initialize SDL: ")+SDL_GetError());
    
  /* Open the audio device */
  int audio_rate = m_sc.srate;
  Uint16 audio_format=AUDIO_S8;
  if (m_sc.sbits==16)
    audio_format=AUDIO_S16;
  int audio_channels=1;
  if (m_sc.stereo) audio_channels=2;
  int audio_buffers = m_sc.sbuffers;

  if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
    throw std::runtime_error(std::string("Couldn't open audio: ")+SDL_GetError());
  } else {
    Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
    /*    printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
	   (audio_format&0xFF),
	   (audio_channels > 1) ? "stereo" : "mono", 
	   audio_buffers ); */
  }
  audio_open = 1;
  
  /* Set the external music player, if any */
  // TODO: perhaps we should not use this for security reasons
  Mix_SetMusicCMD(getenv("MUSIC_CMD"));
}
Exemplo n.º 3
0
/* Uninitialize the music players */
void close_music(void)
{
	Mix_HaltMusic();
#ifdef CMD_MUSIC
	Mix_SetMusicCMD(NULL);
#endif
#ifdef MODPLUG_MUSIC
	modplug_exit();
#endif
#ifdef MOD_MUSIC
	MOD_exit();
#endif
#ifdef MID_MUSIC
# ifdef USE_TIMIDITY_MIDI
	Timidity_Close();
# endif
#endif

	/* rcg06042009 report available decoders at runtime. */
	SDL_free(music_decoders);
	music_decoders = NULL;
	num_decoders = 0;

	ms_per_step = 0;
}
Exemplo n.º 4
0
static mrb_value
mrb_sdl2_mixer_set_music_cmd(mrb_state *mrb, mrb_value self)
{
  mrb_value str;
  mrb_get_args(mrb, "S", &str);
  return mrb_fixnum_value(Mix_SetMusicCMD(RSTRING_PTR(str)));
}
Exemplo n.º 5
0
// Initialize music subsystem
static boolean I_SDL_InitMusic(void)
{
    // If SDL_mixer is not initialized, we have to initialize it
    // and have the responsibility to shut it down later on.

    if (SDLIsInitialized())
    {
        music_initialized = true;
    }
    else
    {
        if (SDL_Init(SDL_INIT_AUDIO) < 0)
        {
            fprintf(stderr, "Unable to set up sound.\n");
        }
        else if (Mix_OpenAudio(snd_samplerate, AUDIO_S16SYS, 2, 1024) < 0)
        {
            fprintf(stderr, "Error initializing SDL_mixer: %s\n",
                    Mix_GetError());
            SDL_QuitSubSystem(SDL_INIT_AUDIO);
        }
        else
        {
            SDL_PauseAudio(0);

            sdl_was_initialized = true;
            music_initialized = true;
        }
    }

#if defined(SDL_MIXER_VERSION_ATLEAST)
#if SDL_MIXER_VERSION_ATLEAST(2,0,2)
    // Initialize SDL_Mixer for MIDI music playback
    Mix_Init(MIX_INIT_MID | MIX_INIT_FLAC | MIX_INIT_OGG | MIX_INIT_MP3); // [crispy] initialize some more audio formats
#endif
#endif

    // Once initialization is complete, the temporary Timidity config
    // file can be removed.

    RemoveTimidityConfig();

    // If snd_musiccmd is set, we need to call Mix_SetMusicCMD to
    // configure an external music playback program.

    if (strlen(snd_musiccmd) > 0)
    {
        Mix_SetMusicCMD(snd_musiccmd);
    }

#if defined(_WIN32)
    // [AM] Start up midiproc to handle playing MIDI music.
    I_MidiPipe_InitServer();
#endif

    return music_initialized;
}
Exemplo n.º 6
0
int32_t MUSIC_Shutdown(void)
{
    // TODO - make sure this is being called from the menu -- SA
#if !defined FORK_EXEC_MIDI
    if (external_midi)
        Mix_SetMusicCMD(NULL);
#endif

    MUSIC_StopSong();
    music_context = 0;
    music_initialized = 0;
    music_loopflag = MUSIC_PlayOnce;

    return(MUSIC_Ok);
} // MUSIC_Shutdown
int ONScripter::playMIDI(bool loop_flag)
{
    Mix_SetMusicCMD(midi_cmd);
    
    char midi_filename[256];
    sprintf(midi_filename, "%s%s", archive_path, TMP_MUSIC_FILE);
    if ((midi_info = Mix_LoadMUS(midi_filename)) == NULL) return -1;

    int midi_looping = loop_flag ? -1 : 0;

#if defined(LINUX)
    signal(SIGCHLD, midiCallback);
    if (midi_cmd) midi_looping = 0;
#endif
    
    Mix_VolumeMusic(music_volume);
    Mix_PlayMusic(midi_info, midi_looping);
    current_cd_track = -2; 
    
    return 0;
}
Exemplo n.º 8
0
int  MP3_Load (const char *name)
{
	/* Initialize variables */
	audio_rate = 22050;
	audio_format = AUDIO_S16;
	audio_channels = 2;
	audio_buffers = 4096;
	
	/* Open the audio device */
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
		return(2);
	} else {
		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
		/*printf("Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n", audio_rate,
			(audio_format&0xFF),
			(audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono", 
			(audio_format&0x1000) ? "BE" : "LE",
			audio_buffers );*/
	}
	audio_open = 1;

	/* Set the music volume */
	Mix_VolumeMusic(audio_volume);

	/* Set the external music player, if any */
	Mix_SetMusicCMD(getenv("MUSIC_CMD"));

	music = Mix_LoadMUS(name);
	if ( music == NULL ) {
		fprintf(stderr, "Couldn't load %s,%s\n",name, SDL_GetError());
		CleanUp();
		exit(0);
	}
	return 1;
}
Exemplo n.º 9
0
int I_RegisterSong(void* data, size_t len)
{
  FILE *midfile;

  midfile = fopen(MIDI_TMPFILE, "wb");
  if ( midfile == NULL ) {
    printf("Couldn't write MIDI to %s\n", MIDI_TMPFILE);
    return 0;
  }
  /* Convert MUS chunk to MIDI? */
  if ( memcmp(data, "MUS", 3) == 0 ) {
    qmus2mid(data, len, midfile, 1, 0, 0, 0);
  } else {
    fwrite(data, len, 1, midfile);
  }
  fclose(midfile);

  Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"));
  music[0] = Mix_LoadMUS(MIDI_TMPFILE);
  if ( music[0] == NULL ) {
    printf("Couldn't load %s because: %s\n", MIDI_TMPFILE, Mix_GetError());
  }
  return (0);
}
Exemplo n.º 10
0
VALUE MSPhysics::Music::fade_in(VALUE self, VALUE v_address, VALUE v_repeat, VALUE v_time) {
	Mix_Music* music = c_value_to_music(v_address);
	Mix_SetMusicCMD(NULL);
	return Mix_FadeInMusic(music, Util::value_to_int(v_repeat), Util::value_to_int(v_time)) == 0 ? Qtrue : Qfalse;
}
Exemplo n.º 11
0
VALUE MSPhysics::Music::play(VALUE self, VALUE v_address, VALUE v_repeat) {
	Mix_Music* music = c_value_to_music(v_address);
	Mix_HaltMusic();
	Mix_SetMusicCMD(NULL);
	return Mix_PlayMusic(music, Util::value_to_int(v_repeat)) == 0 ? Qtrue : Qfalse;
}
Exemplo n.º 12
0
int32_t MUSIC_Init(int32_t SoundCard, int32_t Address)
{
#ifdef __ANDROID__
	music_initialized = 1;
	return(MUSIC_Ok);
#endif
    // Use an external MIDI player if the user has specified to do so
    char *command = getenv("EDUKE32_MUSIC_CMD");
    const SDL_version *linked = Mix_Linked_Version();

    UNREFERENCED_PARAMETER(SoundCard);
    UNREFERENCED_PARAMETER(Address);

    if (music_initialized)
    {
        setErrorMessage("Music system is already initialized.");
        return(MUSIC_Error);
    } // if

    if (SDL_VERSIONNUM(linked->major,linked->minor,linked->patch) < MIX_REQUIREDVERSION)
    {
        // reject running with SDL_Mixer versions older than what is stated in sdl_inc.h
        initprintf("You need at least v%d.%d.%d of SDL_mixer for music\n",SDL_MIXER_MIN_X,SDL_MIXER_MIN_Y,SDL_MIXER_MIN_Z);
        return(MUSIC_Error);
    }

    external_midi = (command != NULL && command[0] != 0);

    if (external_midi)
    {
#if defined FORK_EXEC_MIDI
        int32_t ws=1, numargs=0, pagesize=sysconf(_SC_PAGE_SIZE);
        char *c, *cmd;
        size_t sz;
#endif

        initprintf("Setting music command to \"%s\".\n", command);

#if !defined FORK_EXEC_MIDI
        if (Mix_SetMusicCMD(command)==-1)
        {
            perror("Mix_SetMusicCMD");
            goto fallback;
        }
#else

        if (pagesize==-1)
            goto fallback;

        for (c=command; *c; c++)
        {
            if (isspace(*c))
                ws = 1;
            else if (ws)
            {
                ws = 0;
                numargs++;
            }
        }

        if (numargs==0)
            goto fallback;

        sz = (numargs+2)*sizeof(char *) + (c-command+1);
        sz = ((sz+pagesize-1)/pagesize)*pagesize;
#if defined(__APPLE__) || defined(__ANDROID__)
        external_midi_argv = Xcalloc(1,sz+pagesize);
        external_midi_argv = (char **)((intptr_t)external_midi_argv + (pagesize-(((intptr_t)external_midi_argv)&(pagesize-1))));
#else
        if (posix_memalign((void **)&external_midi_argv, pagesize, sz))
            goto fallback;
#endif
        cmd = (char *)external_midi_argv + (numargs+2)*sizeof(char *);
        Bmemcpy(cmd, command, c-command+1);

        ws = 1;
        numargs = 0;
        for (c=cmd; *c; c++)
        {
            if (isspace(*c))
            {
                ws = 1;
                *c = 0;
            }
            else if (ws)
            {
                ws = 0;
                external_midi_argv[numargs++] = c;
            }
        }
        external_midi_argv[numargs] = external_midi_tempfn;
        external_midi_argv[numargs+1] = NULL;

        if (mprotect(external_midi_argv, sz, PROT_READ)==-1)  // make argv and command string read-only
        {
            perror("MUSIC_Init: mprotect");
            goto fallback;
        }
# if 0
        {
            int i;
            initprintf("----Music argv:\n");
            for (i=0; i<numargs+1; i++)
                initprintf("  %s\n", external_midi_argv[i]);
            initprintf("----\n");
        }
# endif
#endif
        music_initialized = 1;
        return(MUSIC_Ok);

fallback:
        initprintf("Error setting music command, falling back to timidity.\n");
    }

    {
        static const char *s[] = { "/etc/timidity.cfg", "/etc/timidity/timidity.cfg", "/etc/timidity/freepats.cfg" };
        FILE *fp;
        int32_t i;

        for (i = ARRAY_SIZE(s)-1; i>=0; i--)
        {
            fp = Bfopen(s[i], "r");
            if (fp == NULL)
            {
                if (i == 0)
                {
                    initprintf("Error: couldn't open any of the following files:\n");
                    for (i = ARRAY_SIZE(s)-1; i>=0; i--)
                        initprintf("%s\n",s[i]);
                    return(MUSIC_Error);
                }
                continue;
            }
            else break;
        }
        Bfclose(fp);
    }

    music_initialized = 1;
    return(MUSIC_Ok);
} // MUSIC_Init
Exemplo n.º 13
0
int main(int argc, char *argv[])
{
	int audio_rate;
	Uint16 audio_format;
	int audio_channels;
	int audio_buffers;
	int audio_volume = MIX_MAX_VOLUME;
	int looping = 0;
	int interactive = 0;
	int i;

	/* Initialize variables */
	audio_rate = 22050;
	audio_format = AUDIO_S16;
	audio_channels = 2;
	audio_buffers = 4096;

	/* Check command line usage */
	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
			++i;
			audio_rate = atoi(argv[i]);
		} else
		if ( strcmp(argv[i], "-m") == 0 ) {
			audio_channels = 1;
		} else
		if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
			++i;
			audio_channels = atoi(argv[i]);
		} else
		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
			++i;
			audio_buffers = atoi(argv[i]);
		} else
		if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
			++i;
			audio_volume = atoi(argv[i]);
		} else
		if ( strcmp(argv[i], "-l") == 0 ) {
			looping = -1;
		} else
		if ( strcmp(argv[i], "-i") == 0 ) {
			interactive = 1;
		} else
		if ( strcmp(argv[i], "-8") == 0 ) {
			audio_format = AUDIO_U8;
		} else {
			Usage(argv[0]);
			return(1);
		}
	}
	if ( ! argv[i] ) {
		Usage(argv[0]);
		return(1);
	}

	/* Initialize the SDL library */
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		return(255);
	}

	atexit(CleanUp);
	signal(SIGINT, IntHandler);
	signal(SIGTERM, exit);

	/* Open the audio device */
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
		return(2);
	} else {
		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
		printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
			(audio_format&0xFF),
			(audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono", 
			audio_buffers );
	}
	audio_open = 1;

	/* Set the music volume */
	Mix_VolumeMusic(audio_volume);

	/* Set the external music player, if any */
	Mix_SetMusicCMD(getenv("MUSIC_CMD"));

	while (argv[i]) {
		next_track = 0;
		
		/* Load the requested music file */
		music = Mix_LoadMUS(argv[i]);
		if ( music == NULL ) {
			fprintf(stderr, "Couldn't load %s: %s\n",
				argv[i], SDL_GetError());
			return(2);
		}
		
		/* Play and then exit */
		printf("Playing %s\n", argv[i]);
		Mix_FadeInMusic(music,looping,2000);
		while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
			if(interactive)
				Menu();
			else
				SDL_Delay(100);
		}
		Mix_FreeMusic(music);
		music = NULL;

		/* If the user presses Ctrl-C more than once, exit. */
		SDL_Delay(500);
		if ( next_track > 1 ) break;
		
		i++;
	}
	return(0);
}