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