playmus.c
author Ozkan Sezer
Thu, 26 Dec 2019 17:25:50 +0300
changeset 1123 d40dacdd4ab1
parent 1121 71510d620652
child 1127 d2686bcfcba2
permissions -rw-r--r--
mp3utils.c: check for false positives if id3v1 isn't at file's end.
     1 /*
     2   PLAYMUS:  A test application for the SDL mixer library.
     3   Copyright (C) 1997-2019 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     SDL_Log("Usage: %s [-i] [-l] [-8] [-f32] [-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     const char *typ;
   121     const char *tag_title = NULL;
   122     const char *tag_artist = NULL;
   123     const char *tag_album = NULL;
   124     const char *tag_copyright = NULL;
   125     double loop_start, loop_end, loop_length, current_position;
   126 
   127     (void) argc;
   128 
   129     /* Initialize variables */
   130     audio_rate = MIX_DEFAULT_FREQUENCY;
   131     audio_format = MIX_DEFAULT_FORMAT;
   132     audio_channels = MIX_DEFAULT_CHANNELS;
   133     audio_buffers = 4096;
   134 
   135     /* Check command line usage */
   136     for (i=1; argv[i] && (*argv[i] == '-'); ++i) {
   137         if ((strcmp(argv[i], "-r") == 0) && argv[i+1]) {
   138             ++i;
   139             audio_rate = atoi(argv[i]);
   140         } else
   141         if (strcmp(argv[i], "-m") == 0) {
   142             audio_channels = 1;
   143         } else
   144         if ((strcmp(argv[i], "-c") == 0) && argv[i+1]) {
   145             ++i;
   146             audio_channels = atoi(argv[i]);
   147         } else
   148         if ((strcmp(argv[i], "-b") == 0) && argv[i+1]) {
   149             ++i;
   150             audio_buffers = atoi(argv[i]);
   151         } else
   152         if ((strcmp(argv[i], "-v") == 0) && argv[i+1]) {
   153             ++i;
   154             audio_volume = atoi(argv[i]);
   155         } else
   156         if (strcmp(argv[i], "-l") == 0) {
   157             looping = -1;
   158         } else
   159         if (strcmp(argv[i], "-i") == 0) {
   160             interactive = 1;
   161         } else
   162         if (strcmp(argv[i], "-8") == 0) {
   163             audio_format = AUDIO_U8;
   164         } else
   165         if (strcmp(argv[i], "-f32") == 0) {
   166             audio_format = AUDIO_F32;
   167         } else
   168         if (strcmp(argv[i], "-rwops") == 0) {
   169             rwops = 1;
   170         } else {
   171             Usage(argv[0]);
   172             return(1);
   173         }
   174     }
   175     if (! argv[i]) {
   176         Usage(argv[0]);
   177         return(1);
   178     }
   179 
   180     /* Initialize the SDL library */
   181     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
   182         SDL_Log("Couldn't initialize SDL: %s\n",SDL_GetError());
   183         return(255);
   184     }
   185 
   186 #ifdef HAVE_SIGNAL_H
   187     signal(SIGINT, IntHandler);
   188     signal(SIGTERM, CleanUp);
   189 #endif
   190 
   191     /* Open the audio device */
   192     if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
   193         SDL_Log("Couldn't open audio: %s\n", SDL_GetError());
   194         return(2);
   195     } else {
   196         Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   197         SDL_Log("Opened audio at %d Hz %d bit%s %s %d bytes audio buffer\n", audio_rate,
   198             (audio_format&0xFF),
   199             (SDL_AUDIO_ISFLOAT(audio_format) ? " (float)" : ""),
   200             (audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono",
   201             audio_buffers);
   202     }
   203     audio_open = 1;
   204 
   205     /* Set the music volume */
   206     Mix_VolumeMusic(audio_volume);
   207 
   208     /* Set the external music player, if any */
   209     Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"));
   210 
   211     while (argv[i]) {
   212         next_track = 0;
   213 
   214         /* Load the requested music file */
   215         if (rwops) {
   216             music = Mix_LoadMUS_RW(SDL_RWFromFile(argv[i], "rb"), SDL_TRUE);
   217         } else {
   218             music = Mix_LoadMUS(argv[i]);
   219         }
   220         if (music == NULL) {
   221             SDL_Log("Couldn't load %s: %s\n",
   222                 argv[i], SDL_GetError());
   223             CleanUp(2);
   224         }
   225 
   226         switch (Mix_GetMusicType(music)) {
   227         case MUS_CMD:
   228             typ = "CMD";
   229             break;
   230         case MUS_WAV:
   231             typ = "WAV";
   232             break;
   233         case MUS_MOD:
   234         case MUS_MODPLUG_UNUSED:
   235             typ = "MOD";
   236             break;
   237         case MUS_FLAC:
   238             typ = "FLAC";
   239             break;
   240         case MUS_MID:
   241             typ = "MIDI";
   242             break;
   243         case MUS_OGG:
   244             typ = "OGG Vorbis";
   245             break;
   246         case MUS_MP3:
   247         case MUS_MP3_MAD_UNUSED:
   248             typ = "MP3";
   249             break;
   250         case MUS_OPUS:
   251             typ = "OPUS";
   252             break;
   253         case MUS_NONE:
   254         default:
   255             typ = "NONE";
   256             break;
   257         }
   258         SDL_Log("Detected music type: %s", typ);
   259 
   260         tag_title = Mix_GetMusicTitleTag(music);
   261         if (tag_title && SDL_strlen(tag_title) > 0) {
   262             SDL_Log("Title: %s", tag_title);
   263         }
   264 
   265         tag_artist = Mix_GetMusicArtistTag(music);
   266         if (tag_artist && SDL_strlen(tag_artist) > 0) {
   267             SDL_Log("Artist: %s", tag_artist);
   268         }
   269 
   270         tag_album = Mix_GetMusicAlbumTag(music);
   271         if (tag_album && SDL_strlen(tag_album) > 0) {
   272             SDL_Log("Album: %s", tag_album);
   273         }
   274 
   275         tag_copyright = Mix_GetMusicCopyrightTag(music);
   276         if (tag_copyright && SDL_strlen(tag_copyright) > 0) {
   277             SDL_Log("Copyright: %s", tag_copyright);
   278         }
   279 
   280         loop_start = Mix_GetMusicLoopStartTime(music);
   281         loop_end = Mix_GetMusicLoopEndTime(music);
   282         loop_length = Mix_GetMusicLoopLengthTime(music);
   283 
   284         /* Play and then exit */
   285         SDL_Log("Playing %s, duration %f\n", argv[i], Mix_MusicDuration(music));
   286         if (loop_start > 0.0 && loop_end > 0.0 && loop_length > 0.0) {
   287             SDL_Log("Loop points: start %g s, end %g s, length %g s\n", loop_start, loop_end, loop_length);
   288         }
   289         Mix_FadeInMusic(music,looping,2000);
   290         while (!next_track && (Mix_PlayingMusic() || Mix_PausedMusic())) {
   291             if(interactive)
   292                 Menu();
   293             else {
   294                 current_position = Mix_GetMusicPosition(music);
   295                 if (current_position >= 0.0) {
   296                     printf("Position: %g seconds             \r", current_position);
   297                     fflush(stdout);
   298                 }
   299                 SDL_Delay(100);
   300             }
   301         }
   302         Mix_FreeMusic(music);
   303         music = NULL;
   304 
   305         /* If the user presses Ctrl-C more than once, exit. */
   306         SDL_Delay(500);
   307         if (next_track > 1) break;
   308 
   309         i++;
   310     }
   311     CleanUp(0);
   312 
   313     /* Not reached, but fixes compiler warnings */
   314     return 0;
   315 }
   316 
   317 /* vi: set ts=4 sw=4 expandtab: */