1 /*
2   PLAYMUS:  A test application for the SDL mixer library.
3   Copyright (C) 1997-2018 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 
CleanUp(int exitcode)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 
Usage(char * argv0)65 void Usage(char *argv0)
66 {
67     SDL_Log("Usage: %s [-i] [-l] [-8] [-f32] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] <musicfile>\n", argv0);
68 }
69 
Menu(void)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 
IntHandler(int sig)98 void IntHandler(int sig)
99 {
100     switch (sig) {
101             case SIGINT:
102             next_track++;
103             break;
104     }
105 }
106 
107 #endif
108 
main(int argc,char * argv[])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], "-f32") == 0) {
158             audio_format = AUDIO_F32;
159         } else
160         if (strcmp(argv[i], "-rwops") == 0) {
161             rwops = 1;
162         } else {
163             Usage(argv[0]);
164             return(1);
165         }
166     }
167     if (! argv[i]) {
168         Usage(argv[0]);
169         return(1);
170     }
171 
172     /* Initialize the SDL library */
173     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
174         SDL_Log("Couldn't initialize SDL: %s\n",SDL_GetError());
175         return(255);
176     }
177 
178 #ifdef HAVE_SIGNAL_H
179     signal(SIGINT, IntHandler);
180     signal(SIGTERM, CleanUp);
181 #endif
182 
183     /* Open the audio device */
184     if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
185         SDL_Log("Couldn't open audio: %s\n", SDL_GetError());
186         return(2);
187     } else {
188         Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
189         SDL_Log("Opened audio at %d Hz %d bit%s %s %d bytes audio buffer\n", audio_rate,
190             (audio_format&0xFF),
191             (SDL_AUDIO_ISFLOAT(audio_format) ? " (float)" : ""),
192             (audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono",
193             audio_buffers);
194     }
195     audio_open = 1;
196 
197     /* Set the music volume */
198     Mix_VolumeMusic(audio_volume);
199 
200     /* Set the external music player, if any */
201     Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"));
202 
203     while (argv[i]) {
204         next_track = 0;
205 
206         /* Load the requested music file */
207         if (rwops) {
208             music = Mix_LoadMUS_RW(SDL_RWFromFile(argv[i], "rb"), SDL_TRUE);
209         } else {
210             music = Mix_LoadMUS(argv[i]);
211         }
212         if (music == NULL) {
213             SDL_Log("Couldn't load %s: %s\n",
214                 argv[i], SDL_GetError());
215             CleanUp(2);
216         }
217 
218         /* Play and then exit */
219         SDL_Log("Playing %s\n", argv[i]);
220         Mix_FadeInMusic(music,looping,2000);
221         while (!next_track && (Mix_PlayingMusic() || Mix_PausedMusic())) {
222             if(interactive)
223                 Menu();
224             else
225                 SDL_Delay(100);
226         }
227         Mix_FreeMusic(music);
228         music = NULL;
229 
230         /* If the user presses Ctrl-C more than once, exit. */
231         SDL_Delay(500);
232         if (next_track > 1) break;
233 
234         i++;
235     }
236     CleanUp(0);
237 
238     /* Not reached, but fixes compiler warnings */
239     return 0;
240 }
241 
242 /* vi: set ts=4 sw=4 expandtab: */
243