playmus.c
author Ozkan Sezer
Wed, 18 Dec 2019 18:55:50 +0300
changeset 1097 b8a083ebef5f
parent 1090 543b77a3c0eb
child 1121 71510d620652
permissions -rw-r--r--
timidity/readmidi.c: tidy-up the dumpstring thing for text events.

avoids a warning too, as a side effect.
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;
slouken@0
   121
slouken@617
   122
    /* Initialize variables */
admin@1043
   123
    audio_rate = MIX_DEFAULT_FREQUENCY;
admin@1043
   124
    audio_format = MIX_DEFAULT_FORMAT;
admin@1043
   125
    audio_channels = MIX_DEFAULT_CHANNELS;
slouken@617
   126
    audio_buffers = 4096;
slouken@0
   127
slouken@617
   128
    /* Check command line usage */
slouken@777
   129
    for (i=1; argv[i] && (*argv[i] == '-'); ++i) {
slouken@777
   130
        if ((strcmp(argv[i], "-r") == 0) && argv[i+1]) {
slouken@617
   131
            ++i;
slouken@617
   132
            audio_rate = atoi(argv[i]);
slouken@617
   133
        } else
slouken@777
   134
        if (strcmp(argv[i], "-m") == 0) {
slouken@617
   135
            audio_channels = 1;
slouken@617
   136
        } else
slouken@777
   137
        if ((strcmp(argv[i], "-c") == 0) && argv[i+1]) {
slouken@617
   138
            ++i;
slouken@617
   139
            audio_channels = atoi(argv[i]);
slouken@617
   140
        } else
slouken@777
   141
        if ((strcmp(argv[i], "-b") == 0) && argv[i+1]) {
slouken@617
   142
            ++i;
slouken@617
   143
            audio_buffers = atoi(argv[i]);
slouken@617
   144
        } else
slouken@777
   145
        if ((strcmp(argv[i], "-v") == 0) && argv[i+1]) {
slouken@617
   146
            ++i;
slouken@617
   147
            audio_volume = atoi(argv[i]);
slouken@617
   148
        } else
slouken@777
   149
        if (strcmp(argv[i], "-l") == 0) {
slouken@617
   150
            looping = -1;
slouken@617
   151
        } else
slouken@777
   152
        if (strcmp(argv[i], "-i") == 0) {
slouken@617
   153
            interactive = 1;
slouken@617
   154
        } else
slouken@777
   155
        if (strcmp(argv[i], "-8") == 0) {
slouken@617
   156
            audio_format = AUDIO_U8;
slouken@617
   157
        } else
slouken@802
   158
        if (strcmp(argv[i], "-f32") == 0) {
slouken@802
   159
            audio_format = AUDIO_F32;
slouken@802
   160
        } else
slouken@777
   161
        if (strcmp(argv[i], "-rwops") == 0) {
slouken@617
   162
            rwops = 1;
slouken@617
   163
        } else {
slouken@617
   164
            Usage(argv[0]);
slouken@617
   165
            return(1);
slouken@617
   166
        }
slouken@617
   167
    }
slouken@777
   168
    if (! argv[i]) {
slouken@617
   169
        Usage(argv[0]);
slouken@617
   170
        return(1);
slouken@617
   171
    }
slouken@0
   172
slouken@617
   173
    /* Initialize the SDL library */
slouken@777
   174
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
slouken@777
   175
        SDL_Log("Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@617
   176
        return(255);
slouken@617
   177
    }
slouken@245
   178
slouken@488
   179
#ifdef HAVE_SIGNAL_H
slouken@617
   180
    signal(SIGINT, IntHandler);
slouken@617
   181
    signal(SIGTERM, CleanUp);
slouken@488
   182
#endif
slouken@0
   183
slouken@617
   184
    /* Open the audio device */
slouken@617
   185
    if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
slouken@777
   186
        SDL_Log("Couldn't open audio: %s\n", SDL_GetError());
slouken@617
   187
        return(2);
slouken@617
   188
    } else {
slouken@617
   189
        Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
slouken@802
   190
        SDL_Log("Opened audio at %d Hz %d bit%s %s %d bytes audio buffer\n", audio_rate,
slouken@617
   191
            (audio_format&0xFF),
slouken@802
   192
            (SDL_AUDIO_ISFLOAT(audio_format) ? " (float)" : ""),
slouken@617
   193
            (audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono",
slouken@777
   194
            audio_buffers);
slouken@617
   195
    }
slouken@617
   196
    audio_open = 1;
slouken@0
   197
slouken@617
   198
    /* Set the music volume */
slouken@617
   199
    Mix_VolumeMusic(audio_volume);
slouken@214
   200
slouken@617
   201
    /* Set the external music player, if any */
slouken@617
   202
    Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"));
slouken@617
   203
slouken@617
   204
    while (argv[i]) {
slouken@617
   205
        next_track = 0;
slouken@0
   206
slouken@617
   207
        /* Load the requested music file */
slouken@777
   208
        if (rwops) {
slouken@625
   209
            music = Mix_LoadMUS_RW(SDL_RWFromFile(argv[i], "rb"), SDL_TRUE);
slouken@617
   210
        } else {
slouken@617
   211
            music = Mix_LoadMUS(argv[i]);
slouken@617
   212
        }
slouken@777
   213
        if (music == NULL) {
slouken@777
   214
            SDL_Log("Couldn't load %s: %s\n",
slouken@617
   215
                argv[i], SDL_GetError());
slouken@617
   216
            CleanUp(2);
slouken@617
   217
        }
slouken@0
   218
admin@1044
   219
        switch (Mix_GetMusicType(music)) {
admin@1044
   220
        case MUS_CMD:
admin@1044
   221
            typ = "CMD";
admin@1044
   222
            break;
admin@1044
   223
        case MUS_WAV:
admin@1044
   224
            typ = "WAV";
admin@1044
   225
            break;
admin@1044
   226
        case MUS_MOD:
admin@1044
   227
        case MUS_MODPLUG_UNUSED:
admin@1044
   228
            typ = "MOD";
admin@1044
   229
            break;
admin@1044
   230
        case MUS_FLAC:
admin@1044
   231
            typ = "FLAC";
admin@1044
   232
            break;
admin@1044
   233
        case MUS_MID:
admin@1044
   234
            typ = "MIDI";
admin@1044
   235
            break;
admin@1044
   236
        case MUS_OGG:
admin@1044
   237
            typ = "OGG Vorbis";
admin@1044
   238
            break;
admin@1044
   239
        case MUS_MP3:
admin@1044
   240
        case MUS_MP3_MAD_UNUSED:
admin@1044
   241
            typ = "MP3";
admin@1044
   242
            break;
admin@1044
   243
        case MUS_OPUS:
admin@1044
   244
            typ = "OPUS";
admin@1044
   245
            break;
admin@1044
   246
        case MUS_NONE:
admin@1044
   247
        default:
admin@1044
   248
            typ = "NONE";
admin@1044
   249
            break;
admin@1044
   250
        }
admin@1044
   251
        SDL_Log("Detected music type: %s", typ);
admin@1044
   252
slouken@617
   253
        /* Play and then exit */
uso@1090
   254
        SDL_Log("Playing %s, duration %f\n", argv[i], Mix_MusicDuration(music));
slouken@617
   255
        Mix_FadeInMusic(music,looping,2000);
slouken@777
   256
        while (!next_track && (Mix_PlayingMusic() || Mix_PausedMusic())) {
slouken@617
   257
            if(interactive)
slouken@617
   258
                Menu();
slouken@617
   259
            else
slouken@617
   260
                SDL_Delay(100);
slouken@617
   261
        }
slouken@617
   262
        Mix_FreeMusic(music);
slouken@617
   263
        music = NULL;
slouken@321
   264
slouken@617
   265
        /* If the user presses Ctrl-C more than once, exit. */
slouken@617
   266
        SDL_Delay(500);
slouken@777
   267
        if (next_track > 1) break;
slouken@617
   268
slouken@617
   269
        i++;
slouken@617
   270
    }
slouken@617
   271
    CleanUp(0);
slouken@617
   272
slouken@617
   273
    /* Not reached, but fixes compiler warnings */
slouken@617
   274
    return 0;
slouken@0
   275
}
slouken@777
   276
slouken@777
   277
/* vi: set ts=4 sw=4 expandtab: */