playmus.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 May 2013 21:21:23 -0700
changeset 617 87116a42526e
parent 601 05123263dab3
child 625 1d489d8ec2e0
permissions -rw-r--r--
Cleaned up whitespace for the 2.0.0 release
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
    SDL_RWops *rwfp = NULL;
slouken@617
   109
    int audio_rate;
slouken@617
   110
    Uint16 audio_format;
slouken@617
   111
    int audio_channels;
slouken@617
   112
    int audio_buffers;
slouken@617
   113
    int audio_volume = MIX_MAX_VOLUME;
slouken@617
   114
    int looping = 0;
slouken@617
   115
    int interactive = 0;
slouken@617
   116
    int rwops = 0;
slouken@617
   117
    int i;
slouken@0
   118
slouken@617
   119
    /* Initialize variables */
slouken@617
   120
    audio_rate = 22050;
slouken@617
   121
    audio_format = AUDIO_S16;
slouken@617
   122
    audio_channels = 2;
slouken@617
   123
    audio_buffers = 4096;
slouken@0
   124
slouken@617
   125
    /* Check command line usage */
slouken@617
   126
    for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
slouken@617
   127
        if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
slouken@617
   128
            ++i;
slouken@617
   129
            audio_rate = atoi(argv[i]);
slouken@617
   130
        } else
slouken@617
   131
        if ( strcmp(argv[i], "-m") == 0 ) {
slouken@617
   132
            audio_channels = 1;
slouken@617
   133
        } else
slouken@617
   134
        if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
slouken@617
   135
            ++i;
slouken@617
   136
            audio_channels = atoi(argv[i]);
slouken@617
   137
        } else
slouken@617
   138
        if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
slouken@617
   139
            ++i;
slouken@617
   140
            audio_buffers = atoi(argv[i]);
slouken@617
   141
        } else
slouken@617
   142
        if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
slouken@617
   143
            ++i;
slouken@617
   144
            audio_volume = atoi(argv[i]);
slouken@617
   145
        } else
slouken@617
   146
        if ( strcmp(argv[i], "-l") == 0 ) {
slouken@617
   147
            looping = -1;
slouken@617
   148
        } else
slouken@617
   149
        if ( strcmp(argv[i], "-i") == 0 ) {
slouken@617
   150
            interactive = 1;
slouken@617
   151
        } else
slouken@617
   152
        if ( strcmp(argv[i], "-8") == 0 ) {
slouken@617
   153
            audio_format = AUDIO_U8;
slouken@617
   154
        } else
slouken@617
   155
        if ( strcmp(argv[i], "-rwops") == 0 ) {
slouken@617
   156
            rwops = 1;
slouken@617
   157
        } else {
slouken@617
   158
            Usage(argv[0]);
slouken@617
   159
            return(1);
slouken@617
   160
        }
slouken@617
   161
    }
slouken@617
   162
    if ( ! argv[i] ) {
slouken@617
   163
        Usage(argv[0]);
slouken@617
   164
        return(1);
slouken@617
   165
    }
slouken@0
   166
slouken@617
   167
    /* Initialize the SDL library */
slouken@617
   168
    if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@617
   169
        fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@617
   170
        return(255);
slouken@617
   171
    }
slouken@245
   172
slouken@488
   173
#ifdef HAVE_SIGNAL_H
slouken@617
   174
    signal(SIGINT, IntHandler);
slouken@617
   175
    signal(SIGTERM, CleanUp);
slouken@488
   176
#endif
slouken@0
   177
slouken@617
   178
    /* Open the audio device */
slouken@617
   179
    if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
slouken@617
   180
        fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@617
   181
        return(2);
slouken@617
   182
    } else {
slouken@617
   183
        Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
slouken@617
   184
        printf("Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n", audio_rate,
slouken@617
   185
            (audio_format&0xFF),
slouken@617
   186
            (audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono",
slouken@617
   187
            (audio_format&0x1000) ? "BE" : "LE",
slouken@617
   188
            audio_buffers );
slouken@617
   189
    }
slouken@617
   190
    audio_open = 1;
slouken@0
   191
slouken@617
   192
    /* Set the music volume */
slouken@617
   193
    Mix_VolumeMusic(audio_volume);
slouken@214
   194
slouken@617
   195
    /* Set the external music player, if any */
slouken@617
   196
    Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"));
slouken@0
   197
slouken@617
   198
    while (argv[i]) {
slouken@617
   199
        next_track = 0;
slouken@0
   200
slouken@617
   201
        /* Load the requested music file */
slouken@617
   202
        if ( rwops ) {
slouken@617
   203
            rwfp = SDL_RWFromFile(argv[i], "rb");
slouken@617
   204
            music = Mix_LoadMUS_RW(rwfp);
slouken@617
   205
        } else {
slouken@617
   206
            music = Mix_LoadMUS(argv[i]);
slouken@617
   207
        }
slouken@617
   208
        if ( music == NULL ) {
slouken@617
   209
            fprintf(stderr, "Couldn't load %s: %s\n",
slouken@617
   210
                argv[i], SDL_GetError());
slouken@617
   211
            CleanUp(2);
slouken@617
   212
        }
slouken@321
   213
slouken@617
   214
        /* Play and then exit */
slouken@617
   215
        printf("Playing %s\n", argv[i]);
slouken@617
   216
        Mix_FadeInMusic(music,looping,2000);
slouken@617
   217
        while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
slouken@617
   218
            if(interactive)
slouken@617
   219
                Menu();
slouken@617
   220
            else
slouken@617
   221
                SDL_Delay(100);
slouken@617
   222
        }
slouken@617
   223
        Mix_FreeMusic(music);
slouken@617
   224
        if ( rwops ) {
slouken@617
   225
            SDL_RWclose(rwfp);
slouken@617
   226
        }
slouken@617
   227
        music = NULL;
slouken@617
   228
slouken@617
   229
        /* If the user presses Ctrl-C more than once, exit. */
slouken@617
   230
        SDL_Delay(500);
slouken@617
   231
        if ( next_track > 1 ) break;
slouken@617
   232
slouken@617
   233
        i++;
slouken@617
   234
    }
slouken@617
   235
    CleanUp(0);
slouken@617
   236
slouken@617
   237
    /* Not reached, but fixes compiler warnings */
slouken@617
   238
    return 0;
slouken@0
   239
}