playmus.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 01 Jun 2013 19:52:15 -0700
changeset 625 1d489d8ec2e0
parent 617 87116a42526e
child 642 dae7bb0a66b1
permissions -rw-r--r--
SDL_LoadMUS_RW() now takes an argument telling whether or not the data source should be freed when done.
Fixes crashes cleaning up the data source for music when loading fails.
slouken@0
     1
/*
slouken@518
     2
  PLAYMUS:  A test application for the SDL mixer library.
slouken@601
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@0
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@0
    21
slouken@140
    22
/* $Id$ */
slouken@138
    23
slouken@0
    24
#include <stdlib.h>
slouken@0
    25
#include <stdio.h>
slouken@0
    26
#include <string.h>
slouken@488
    27
slouken@47
    28
#ifdef unix
slouken@0
    29
#include <unistd.h>
slouken@0
    30
#endif
slouken@0
    31
slouken@24
    32
#include "SDL.h"
slouken@34
    33
#include "SDL_mixer.h"
slouken@0
    34
slouken@503
    35
#ifdef HAVE_SIGNAL_H
slouken@503
    36
#include <signal.h>
slouken@503
    37
#endif
slouken@503
    38
slouken@0
    39
slouken@0
    40
static int audio_open = 0;
slouken@0
    41
static Mix_Music *music = NULL;
slouken@75
    42
static int next_track = 0;
slouken@0
    43
slouken@321
    44
void CleanUp(int exitcode)
slouken@0
    45
{
slouken@617
    46
    if( Mix_PlayingMusic() ) {
slouken@617
    47
        Mix_FadeOutMusic(1500);
slouken@617
    48
        SDL_Delay(1500);
slouken@617
    49
    }
slouken@617
    50
    if ( music ) {
slouken@617
    51
        Mix_FreeMusic(music);
slouken@617
    52
        music = NULL;
slouken@617
    53
    }
slouken@617
    54
    if ( audio_open ) {
slouken@617
    55
        Mix_CloseAudio();
slouken@617
    56
        audio_open = 0;
slouken@617
    57
    }
slouken@617
    58
    SDL_Quit();
slouken@617
    59
    exit(exitcode);
slouken@0
    60
}
slouken@0
    61
slouken@0
    62
void Usage(char *argv0)
slouken@0
    63
{
slouken@617
    64
    fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] <musicfile>\n", argv0);
slouken@0
    65
}
megastep@13
    66
megastep@13
    67
void Menu(void)
megastep@13
    68
{
slouken@617
    69
    char buf[10];
megastep@13
    70
slouken@617
    71
    printf("Available commands: (p)ause (r)esume (h)alt volume(v#) > ");
slouken@617
    72
    fflush(stdin);
slouken@617
    73
    if (scanf("%s",buf) == 1) {
slouken@617
    74
        switch(buf[0]){
slouken@617
    75
        case 'p': case 'P':
slouken@617
    76
            Mix_PauseMusic();
slouken@617
    77
            break;
slouken@617
    78
        case 'r': case 'R':
slouken@617
    79
            Mix_ResumeMusic();
slouken@617
    80
            break;
slouken@617
    81
        case 'h': case 'H':
slouken@617
    82
            Mix_HaltMusic();
slouken@617
    83
            break;
slouken@617
    84
        case 'v': case 'V':
slouken@617
    85
            Mix_VolumeMusic(atoi(buf+1));
slouken@617
    86
            break;
slouken@617
    87
        }
slouken@617
    88
    }
slouken@617
    89
    printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no",
slouken@617
    90
           Mix_PausedMusic() ? "yes" : "no");
megastep@13
    91
}
megastep@13
    92
slouken@488
    93
#ifdef HAVE_SIGNAL_H
slouken@488
    94
slouken@75
    95
void IntHandler(int sig)
slouken@75
    96
{
slouken@617
    97
    switch (sig) {
slouken@617
    98
            case SIGINT:
slouken@617
    99
            next_track++;
slouken@617
   100
            break;
slouken@617
   101
    }
slouken@75
   102
}
slouken@75
   103
slouken@488
   104
#endif
slouken@488
   105
slouken@150
   106
int main(int argc, char *argv[])
slouken@0
   107
{
slouken@617
   108
    int audio_rate;
slouken@617
   109
    Uint16 audio_format;
slouken@617
   110
    int audio_channels;
slouken@617
   111
    int audio_buffers;
slouken@617
   112
    int audio_volume = MIX_MAX_VOLUME;
slouken@617
   113
    int looping = 0;
slouken@617
   114
    int interactive = 0;
slouken@617
   115
    int rwops = 0;
slouken@617
   116
    int i;
slouken@0
   117
slouken@617
   118
    /* Initialize variables */
slouken@617
   119
    audio_rate = 22050;
slouken@617
   120
    audio_format = AUDIO_S16;
slouken@617
   121
    audio_channels = 2;
slouken@617
   122
    audio_buffers = 4096;
slouken@0
   123
slouken@617
   124
    /* Check command line usage */
slouken@617
   125
    for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
slouken@617
   126
        if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
slouken@617
   127
            ++i;
slouken@617
   128
            audio_rate = atoi(argv[i]);
slouken@617
   129
        } else
slouken@617
   130
        if ( strcmp(argv[i], "-m") == 0 ) {
slouken@617
   131
            audio_channels = 1;
slouken@617
   132
        } else
slouken@617
   133
        if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
slouken@617
   134
            ++i;
slouken@617
   135
            audio_channels = atoi(argv[i]);
slouken@617
   136
        } else
slouken@617
   137
        if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
slouken@617
   138
            ++i;
slouken@617
   139
            audio_buffers = atoi(argv[i]);
slouken@617
   140
        } else
slouken@617
   141
        if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
slouken@617
   142
            ++i;
slouken@617
   143
            audio_volume = atoi(argv[i]);
slouken@617
   144
        } else
slouken@617
   145
        if ( strcmp(argv[i], "-l") == 0 ) {
slouken@617
   146
            looping = -1;
slouken@617
   147
        } else
slouken@617
   148
        if ( strcmp(argv[i], "-i") == 0 ) {
slouken@617
   149
            interactive = 1;
slouken@617
   150
        } else
slouken@617
   151
        if ( strcmp(argv[i], "-8") == 0 ) {
slouken@617
   152
            audio_format = AUDIO_U8;
slouken@617
   153
        } else
slouken@617
   154
        if ( strcmp(argv[i], "-rwops") == 0 ) {
slouken@617
   155
            rwops = 1;
slouken@617
   156
        } else {
slouken@617
   157
            Usage(argv[0]);
slouken@617
   158
            return(1);
slouken@617
   159
        }
slouken@617
   160
    }
slouken@617
   161
    if ( ! argv[i] ) {
slouken@617
   162
        Usage(argv[0]);
slouken@617
   163
        return(1);
slouken@617
   164
    }
slouken@0
   165
slouken@617
   166
    /* Initialize the SDL library */
slouken@617
   167
    if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@617
   168
        fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@617
   169
        return(255);
slouken@617
   170
    }
slouken@245
   171
slouken@488
   172
#ifdef HAVE_SIGNAL_H
slouken@617
   173
    signal(SIGINT, IntHandler);
slouken@617
   174
    signal(SIGTERM, CleanUp);
slouken@488
   175
#endif
slouken@0
   176
slouken@617
   177
    /* Open the audio device */
slouken@617
   178
    if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
slouken@617
   179
        fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@617
   180
        return(2);
slouken@617
   181
    } else {
slouken@617
   182
        Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
slouken@617
   183
        printf("Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n", audio_rate,
slouken@617
   184
            (audio_format&0xFF),
slouken@617
   185
            (audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono",
slouken@617
   186
            (audio_format&0x1000) ? "BE" : "LE",
slouken@617
   187
            audio_buffers );
slouken@617
   188
    }
slouken@617
   189
    audio_open = 1;
slouken@0
   190
slouken@617
   191
    /* Set the music volume */
slouken@617
   192
    Mix_VolumeMusic(audio_volume);
slouken@214
   193
slouken@617
   194
    /* Set the external music player, if any */
slouken@617
   195
    Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"));
slouken@0
   196
slouken@617
   197
    while (argv[i]) {
slouken@617
   198
        next_track = 0;
slouken@0
   199
slouken@617
   200
        /* Load the requested music file */
slouken@617
   201
        if ( rwops ) {
slouken@625
   202
            music = Mix_LoadMUS_RW(SDL_RWFromFile(argv[i], "rb"), SDL_TRUE);
slouken@617
   203
        } else {
slouken@617
   204
            music = Mix_LoadMUS(argv[i]);
slouken@617
   205
        }
slouken@617
   206
        if ( music == NULL ) {
slouken@617
   207
            fprintf(stderr, "Couldn't load %s: %s\n",
slouken@617
   208
                argv[i], SDL_GetError());
slouken@617
   209
            CleanUp(2);
slouken@617
   210
        }
slouken@321
   211
slouken@617
   212
        /* Play and then exit */
slouken@617
   213
        printf("Playing %s\n", argv[i]);
slouken@617
   214
        Mix_FadeInMusic(music,looping,2000);
slouken@617
   215
        while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
slouken@617
   216
            if(interactive)
slouken@617
   217
                Menu();
slouken@617
   218
            else
slouken@617
   219
                SDL_Delay(100);
slouken@617
   220
        }
slouken@617
   221
        Mix_FreeMusic(music);
slouken@617
   222
        music = NULL;
slouken@617
   223
slouken@617
   224
        /* If the user presses Ctrl-C more than once, exit. */
slouken@617
   225
        SDL_Delay(500);
slouken@617
   226
        if ( next_track > 1 ) break;
slouken@617
   227
slouken@617
   228
        i++;
slouken@617
   229
    }
slouken@617
   230
    CleanUp(0);
slouken@617
   231
slouken@617
   232
    /* Not reached, but fixes compiler warnings */
slouken@617
   233
    return 0;
slouken@0
   234
}