playmus.c 6.1 KB

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