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.
slouken@0
     1
/*
slouken@518
     2
  PLAYMUS:  A test application for the SDL mixer library.
slouken@926
     3
  Copyright (C) 1997-2019 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
aschiffler@642
    22
/* Quiet windows compiler warnings */
slouken@777
    23
#define _CRT_SECURE_NO_WARNINGS
aschiffler@642
    24
slouken@140
    25
/* $Id$ */
slouken@138
    26
slouken@0
    27
#include <stdlib.h>
slouken@0
    28
#include <stdio.h>
slouken@0
    29
#include <string.h>
slouken@488
    30
slouken@47
    31
#ifdef unix
slouken@0
    32
#include <unistd.h>
slouken@0
    33
#endif
slouken@0
    34
slouken@24
    35
#include "SDL.h"
slouken@34
    36
#include "SDL_mixer.h"
slouken@0
    37
slouken@503
    38
#ifdef HAVE_SIGNAL_H
slouken@503
    39
#include <signal.h>
slouken@503
    40
#endif
slouken@503
    41
slouken@0
    42
slouken@0
    43
static int audio_open = 0;
slouken@0
    44
static Mix_Music *music = NULL;
slouken@75
    45
static int next_track = 0;
slouken@0
    46
slouken@321
    47
void CleanUp(int exitcode)
slouken@0
    48
{
slouken@777
    49
    if(Mix_PlayingMusic()) {
slouken@617
    50
        Mix_FadeOutMusic(1500);
slouken@617
    51
        SDL_Delay(1500);
slouken@617
    52
    }
slouken@777
    53
    if (music) {
slouken@617
    54
        Mix_FreeMusic(music);
slouken@617
    55
        music = NULL;
slouken@617
    56
    }
slouken@777
    57
    if (audio_open) {
slouken@617
    58
        Mix_CloseAudio();
slouken@617
    59
        audio_open = 0;
slouken@617
    60
    }
slouken@617
    61
    SDL_Quit();
slouken@617
    62
    exit(exitcode);
slouken@0
    63
}
slouken@0
    64
slouken@0
    65
void Usage(char *argv0)
slouken@0
    66
{
slouken@802
    67
    SDL_Log("Usage: %s [-i] [-l] [-8] [-f32] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] <musicfile>\n", argv0);
slouken@0
    68
}
megastep@13
    69
megastep@13
    70
void Menu(void)
megastep@13
    71
{
slouken@617
    72
    char buf[10];
megastep@13
    73
slouken@617
    74
    printf("Available commands: (p)ause (r)esume (h)alt volume(v#) > ");
slouken@617
    75
    fflush(stdin);
slouken@617
    76
    if (scanf("%s",buf) == 1) {
slouken@617
    77
        switch(buf[0]){
slouken@617
    78
        case 'p': case 'P':
slouken@617
    79
            Mix_PauseMusic();
slouken@617
    80
            break;
slouken@617
    81
        case 'r': case 'R':
slouken@617
    82
            Mix_ResumeMusic();
slouken@617
    83
            break;
slouken@617
    84
        case 'h': case 'H':
slouken@617
    85
            Mix_HaltMusic();
slouken@617
    86
            break;
slouken@617
    87
        case 'v': case 'V':
slouken@617
    88
            Mix_VolumeMusic(atoi(buf+1));
slouken@617
    89
            break;
slouken@617
    90
        }
slouken@617
    91
    }
slouken@617
    92
    printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no",
slouken@617
    93
           Mix_PausedMusic() ? "yes" : "no");
megastep@13
    94
}
megastep@13
    95
slouken@488
    96
#ifdef HAVE_SIGNAL_H
slouken@488
    97
slouken@75
    98
void IntHandler(int sig)
slouken@75
    99
{
slouken@617
   100
    switch (sig) {
slouken@617
   101
            case SIGINT:
slouken@617
   102
            next_track++;
slouken@617
   103
            break;
slouken@617
   104
    }
slouken@75
   105
}
slouken@75
   106
slouken@488
   107
#endif
slouken@488
   108
slouken@150
   109
int main(int argc, char *argv[])
slouken@0
   110
{
slouken@617
   111
    int audio_rate;
slouken@617
   112
    Uint16 audio_format;
slouken@617
   113
    int audio_channels;
slouken@617
   114
    int audio_buffers;
slouken@617
   115
    int audio_volume = MIX_MAX_VOLUME;
slouken@617
   116
    int looping = 0;
slouken@617
   117
    int interactive = 0;
slouken@617
   118
    int rwops = 0;
slouken@617
   119
    int i;
admin@1044
   120
    const char *typ;
admin@1121
   121
    const char *tag_title = NULL;
admin@1121
   122
    const char *tag_artist = NULL;
admin@1121
   123
    const char *tag_album = NULL;
admin@1121
   124
    const char *tag_copyright = NULL;
admin@1121
   125
    double loop_start, loop_end, loop_length, current_position;
admin@1121
   126
admin@1121
   127
    (void) argc;
slouken@0
   128
slouken@617
   129
    /* Initialize variables */
admin@1043
   130
    audio_rate = MIX_DEFAULT_FREQUENCY;
admin@1043
   131
    audio_format = MIX_DEFAULT_FORMAT;
admin@1043
   132
    audio_channels = MIX_DEFAULT_CHANNELS;
slouken@617
   133
    audio_buffers = 4096;
slouken@0
   134
slouken@617
   135
    /* Check command line usage */
slouken@777
   136
    for (i=1; argv[i] && (*argv[i] == '-'); ++i) {
slouken@777
   137
        if ((strcmp(argv[i], "-r") == 0) && argv[i+1]) {
slouken@617
   138
            ++i;
slouken@617
   139
            audio_rate = atoi(argv[i]);
slouken@617
   140
        } else
slouken@777
   141
        if (strcmp(argv[i], "-m") == 0) {
slouken@617
   142
            audio_channels = 1;
slouken@617
   143
        } else
slouken@777
   144
        if ((strcmp(argv[i], "-c") == 0) && argv[i+1]) {
slouken@617
   145
            ++i;
slouken@617
   146
            audio_channels = atoi(argv[i]);
slouken@617
   147
        } else
slouken@777
   148
        if ((strcmp(argv[i], "-b") == 0) && argv[i+1]) {
slouken@617
   149
            ++i;
slouken@617
   150
            audio_buffers = atoi(argv[i]);
slouken@617
   151
        } else
slouken@777
   152
        if ((strcmp(argv[i], "-v") == 0) && argv[i+1]) {
slouken@617
   153
            ++i;
slouken@617
   154
            audio_volume = atoi(argv[i]);
slouken@617
   155
        } else
slouken@777
   156
        if (strcmp(argv[i], "-l") == 0) {
slouken@617
   157
            looping = -1;
slouken@617
   158
        } else
slouken@777
   159
        if (strcmp(argv[i], "-i") == 0) {
slouken@617
   160
            interactive = 1;
slouken@617
   161
        } else
slouken@777
   162
        if (strcmp(argv[i], "-8") == 0) {
slouken@617
   163
            audio_format = AUDIO_U8;
slouken@617
   164
        } else
slouken@802
   165
        if (strcmp(argv[i], "-f32") == 0) {
slouken@802
   166
            audio_format = AUDIO_F32;
slouken@802
   167
        } else
slouken@777
   168
        if (strcmp(argv[i], "-rwops") == 0) {
slouken@617
   169
            rwops = 1;
slouken@617
   170
        } else {
slouken@617
   171
            Usage(argv[0]);
slouken@617
   172
            return(1);
slouken@617
   173
        }
slouken@617
   174
    }
slouken@777
   175
    if (! argv[i]) {
slouken@617
   176
        Usage(argv[0]);
slouken@617
   177
        return(1);
slouken@617
   178
    }
slouken@0
   179
slouken@617
   180
    /* Initialize the SDL library */
slouken@777
   181
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
slouken@777
   182
        SDL_Log("Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@617
   183
        return(255);
slouken@617
   184
    }
slouken@245
   185
slouken@488
   186
#ifdef HAVE_SIGNAL_H
slouken@617
   187
    signal(SIGINT, IntHandler);
slouken@617
   188
    signal(SIGTERM, CleanUp);
slouken@488
   189
#endif
slouken@0
   190
slouken@617
   191
    /* Open the audio device */
slouken@617
   192
    if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
slouken@777
   193
        SDL_Log("Couldn't open audio: %s\n", SDL_GetError());
slouken@617
   194
        return(2);
slouken@617
   195
    } else {
slouken@617
   196
        Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
slouken@802
   197
        SDL_Log("Opened audio at %d Hz %d bit%s %s %d bytes audio buffer\n", audio_rate,
slouken@617
   198
            (audio_format&0xFF),
slouken@802
   199
            (SDL_AUDIO_ISFLOAT(audio_format) ? " (float)" : ""),
slouken@617
   200
            (audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono",
slouken@777
   201
            audio_buffers);
slouken@617
   202
    }
slouken@617
   203
    audio_open = 1;
slouken@0
   204
slouken@617
   205
    /* Set the music volume */
slouken@617
   206
    Mix_VolumeMusic(audio_volume);
slouken@214
   207
slouken@617
   208
    /* Set the external music player, if any */
slouken@617
   209
    Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"));
slouken@617
   210
slouken@617
   211
    while (argv[i]) {
slouken@617
   212
        next_track = 0;
slouken@0
   213
slouken@617
   214
        /* Load the requested music file */
slouken@777
   215
        if (rwops) {
slouken@625
   216
            music = Mix_LoadMUS_RW(SDL_RWFromFile(argv[i], "rb"), SDL_TRUE);
slouken@617
   217
        } else {
slouken@617
   218
            music = Mix_LoadMUS(argv[i]);
slouken@617
   219
        }
slouken@777
   220
        if (music == NULL) {
slouken@777
   221
            SDL_Log("Couldn't load %s: %s\n",
slouken@617
   222
                argv[i], SDL_GetError());
slouken@617
   223
            CleanUp(2);
slouken@617
   224
        }
slouken@0
   225
admin@1044
   226
        switch (Mix_GetMusicType(music)) {
admin@1044
   227
        case MUS_CMD:
admin@1044
   228
            typ = "CMD";
admin@1044
   229
            break;
admin@1044
   230
        case MUS_WAV:
admin@1044
   231
            typ = "WAV";
admin@1044
   232
            break;
admin@1044
   233
        case MUS_MOD:
admin@1044
   234
        case MUS_MODPLUG_UNUSED:
admin@1044
   235
            typ = "MOD";
admin@1044
   236
            break;
admin@1044
   237
        case MUS_FLAC:
admin@1044
   238
            typ = "FLAC";
admin@1044
   239
            break;
admin@1044
   240
        case MUS_MID:
admin@1044
   241
            typ = "MIDI";
admin@1044
   242
            break;
admin@1044
   243
        case MUS_OGG:
admin@1044
   244
            typ = "OGG Vorbis";
admin@1044
   245
            break;
admin@1044
   246
        case MUS_MP3:
admin@1044
   247
        case MUS_MP3_MAD_UNUSED:
admin@1044
   248
            typ = "MP3";
admin@1044
   249
            break;
admin@1044
   250
        case MUS_OPUS:
admin@1044
   251
            typ = "OPUS";
admin@1044
   252
            break;
admin@1044
   253
        case MUS_NONE:
admin@1044
   254
        default:
admin@1044
   255
            typ = "NONE";
admin@1044
   256
            break;
admin@1044
   257
        }
admin@1044
   258
        SDL_Log("Detected music type: %s", typ);
admin@1044
   259
admin@1121
   260
        tag_title = Mix_GetMusicTitleTag(music);
admin@1121
   261
        if (tag_title && SDL_strlen(tag_title) > 0) {
admin@1121
   262
            SDL_Log("Title: %s", tag_title);
admin@1121
   263
        }
admin@1121
   264
admin@1121
   265
        tag_artist = Mix_GetMusicArtistTag(music);
admin@1121
   266
        if (tag_artist && SDL_strlen(tag_artist) > 0) {
admin@1121
   267
            SDL_Log("Artist: %s", tag_artist);
admin@1121
   268
        }
admin@1121
   269
admin@1121
   270
        tag_album = Mix_GetMusicAlbumTag(music);
admin@1121
   271
        if (tag_album && SDL_strlen(tag_album) > 0) {
admin@1121
   272
            SDL_Log("Album: %s", tag_album);
admin@1121
   273
        }
admin@1121
   274
admin@1121
   275
        tag_copyright = Mix_GetMusicCopyrightTag(music);
admin@1121
   276
        if (tag_copyright && SDL_strlen(tag_copyright) > 0) {
admin@1121
   277
            SDL_Log("Copyright: %s", tag_copyright);
admin@1121
   278
        }
admin@1121
   279
admin@1121
   280
        loop_start = Mix_GetMusicLoopStartTime(music);
admin@1121
   281
        loop_end = Mix_GetMusicLoopEndTime(music);
admin@1121
   282
        loop_length = Mix_GetMusicLoopLengthTime(music);
admin@1121
   283
slouken@617
   284
        /* Play and then exit */
uso@1090
   285
        SDL_Log("Playing %s, duration %f\n", argv[i], Mix_MusicDuration(music));
admin@1121
   286
        if (loop_start > 0.0 && loop_end > 0.0 && loop_length > 0.0) {
admin@1121
   287
            SDL_Log("Loop points: start %g s, end %g s, length %g s\n", loop_start, loop_end, loop_length);
admin@1121
   288
        }
slouken@617
   289
        Mix_FadeInMusic(music,looping,2000);
slouken@777
   290
        while (!next_track && (Mix_PlayingMusic() || Mix_PausedMusic())) {
slouken@617
   291
            if(interactive)
slouken@617
   292
                Menu();
admin@1121
   293
            else {
admin@1121
   294
                current_position = Mix_GetMusicPosition(music);
admin@1121
   295
                if (current_position >= 0.0) {
admin@1121
   296
                    printf("Position: %g seconds             \r", current_position);
admin@1121
   297
                    fflush(stdout);
admin@1121
   298
                }
slouken@617
   299
                SDL_Delay(100);
admin@1121
   300
            }
slouken@617
   301
        }
slouken@617
   302
        Mix_FreeMusic(music);
slouken@617
   303
        music = NULL;
slouken@321
   304
slouken@617
   305
        /* If the user presses Ctrl-C more than once, exit. */
slouken@617
   306
        SDL_Delay(500);
slouken@777
   307
        if (next_track > 1) break;
slouken@617
   308
slouken@617
   309
        i++;
slouken@617
   310
    }
slouken@617
   311
    CleanUp(0);
slouken@617
   312
slouken@617
   313
    /* Not reached, but fixes compiler warnings */
slouken@617
   314
    return 0;
slouken@0
   315
}
slouken@777
   316
slouken@777
   317
/* vi: set ts=4 sw=4 expandtab: */