playmus.c
author Ozkan Sezer <sezeroz@gmail.com>
Wed, 03 Oct 2018 21:55:00 +0300
changeset 863 b81eb791d10d
parent 848 3907db698eb5
child 926 d6c9518fb5ee
permissions -rw-r--r--
readmidi.c (groom_list): avoid integer overflow when recomputing time.

from libtimidity -- see:
https://sourceforge.net/p/libtimidity/libtimidity/ci/11be98a89eac229111420e6a3d521edbfddb0dbc/
slouken@0
     1
/*
slouken@518
     2
  PLAYMUS:  A test application for the SDL mixer library.
slouken@848
     3
  Copyright (C) 1997-2018 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;
slouken@0
   120
slouken@617
   121
    /* Initialize variables */
slouken@617
   122
    audio_rate = 22050;
slouken@617
   123
    audio_format = AUDIO_S16;
slouken@617
   124
    audio_channels = 2;
slouken@617
   125
    audio_buffers = 4096;
slouken@0
   126
slouken@617
   127
    /* Check command line usage */
slouken@777
   128
    for (i=1; argv[i] && (*argv[i] == '-'); ++i) {
slouken@777
   129
        if ((strcmp(argv[i], "-r") == 0) && argv[i+1]) {
slouken@617
   130
            ++i;
slouken@617
   131
            audio_rate = atoi(argv[i]);
slouken@617
   132
        } else
slouken@777
   133
        if (strcmp(argv[i], "-m") == 0) {
slouken@617
   134
            audio_channels = 1;
slouken@617
   135
        } else
slouken@777
   136
        if ((strcmp(argv[i], "-c") == 0) && argv[i+1]) {
slouken@617
   137
            ++i;
slouken@617
   138
            audio_channels = atoi(argv[i]);
slouken@617
   139
        } else
slouken@777
   140
        if ((strcmp(argv[i], "-b") == 0) && argv[i+1]) {
slouken@617
   141
            ++i;
slouken@617
   142
            audio_buffers = atoi(argv[i]);
slouken@617
   143
        } else
slouken@777
   144
        if ((strcmp(argv[i], "-v") == 0) && argv[i+1]) {
slouken@617
   145
            ++i;
slouken@617
   146
            audio_volume = atoi(argv[i]);
slouken@617
   147
        } else
slouken@777
   148
        if (strcmp(argv[i], "-l") == 0) {
slouken@617
   149
            looping = -1;
slouken@617
   150
        } else
slouken@777
   151
        if (strcmp(argv[i], "-i") == 0) {
slouken@617
   152
            interactive = 1;
slouken@617
   153
        } else
slouken@777
   154
        if (strcmp(argv[i], "-8") == 0) {
slouken@617
   155
            audio_format = AUDIO_U8;
slouken@617
   156
        } else
slouken@802
   157
        if (strcmp(argv[i], "-f32") == 0) {
slouken@802
   158
            audio_format = AUDIO_F32;
slouken@802
   159
        } else
slouken@777
   160
        if (strcmp(argv[i], "-rwops") == 0) {
slouken@617
   161
            rwops = 1;
slouken@617
   162
        } else {
slouken@617
   163
            Usage(argv[0]);
slouken@617
   164
            return(1);
slouken@617
   165
        }
slouken@617
   166
    }
slouken@777
   167
    if (! argv[i]) {
slouken@617
   168
        Usage(argv[0]);
slouken@617
   169
        return(1);
slouken@617
   170
    }
slouken@0
   171
slouken@617
   172
    /* Initialize the SDL library */
slouken@777
   173
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
slouken@777
   174
        SDL_Log("Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@617
   175
        return(255);
slouken@617
   176
    }
slouken@245
   177
slouken@488
   178
#ifdef HAVE_SIGNAL_H
slouken@617
   179
    signal(SIGINT, IntHandler);
slouken@617
   180
    signal(SIGTERM, CleanUp);
slouken@488
   181
#endif
slouken@0
   182
slouken@617
   183
    /* Open the audio device */
slouken@617
   184
    if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
slouken@777
   185
        SDL_Log("Couldn't open audio: %s\n", SDL_GetError());
slouken@617
   186
        return(2);
slouken@617
   187
    } else {
slouken@617
   188
        Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
slouken@802
   189
        SDL_Log("Opened audio at %d Hz %d bit%s %s %d bytes audio buffer\n", audio_rate,
slouken@617
   190
            (audio_format&0xFF),
slouken@802
   191
            (SDL_AUDIO_ISFLOAT(audio_format) ? " (float)" : ""),
slouken@617
   192
            (audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono",
slouken@777
   193
            audio_buffers);
slouken@617
   194
    }
slouken@617
   195
    audio_open = 1;
slouken@0
   196
slouken@617
   197
    /* Set the music volume */
slouken@617
   198
    Mix_VolumeMusic(audio_volume);
slouken@214
   199
slouken@617
   200
    /* Set the external music player, if any */
slouken@617
   201
    Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"));
slouken@0
   202
slouken@617
   203
    while (argv[i]) {
slouken@617
   204
        next_track = 0;
slouken@0
   205
slouken@617
   206
        /* Load the requested music file */
slouken@777
   207
        if (rwops) {
slouken@625
   208
            music = Mix_LoadMUS_RW(SDL_RWFromFile(argv[i], "rb"), SDL_TRUE);
slouken@617
   209
        } else {
slouken@617
   210
            music = Mix_LoadMUS(argv[i]);
slouken@617
   211
        }
slouken@777
   212
        if (music == NULL) {
slouken@777
   213
            SDL_Log("Couldn't load %s: %s\n",
slouken@617
   214
                argv[i], SDL_GetError());
slouken@617
   215
            CleanUp(2);
slouken@617
   216
        }
slouken@321
   217
slouken@617
   218
        /* Play and then exit */
slouken@777
   219
        SDL_Log("Playing %s\n", argv[i]);
slouken@617
   220
        Mix_FadeInMusic(music,looping,2000);
slouken@777
   221
        while (!next_track && (Mix_PlayingMusic() || Mix_PausedMusic())) {
slouken@617
   222
            if(interactive)
slouken@617
   223
                Menu();
slouken@617
   224
            else
slouken@617
   225
                SDL_Delay(100);
slouken@617
   226
        }
slouken@617
   227
        Mix_FreeMusic(music);
slouken@617
   228
        music = NULL;
slouken@617
   229
slouken@617
   230
        /* If the user presses Ctrl-C more than once, exit. */
slouken@617
   231
        SDL_Delay(500);
slouken@777
   232
        if (next_track > 1) break;
slouken@617
   233
slouken@617
   234
        i++;
slouken@617
   235
    }
slouken@617
   236
    CleanUp(0);
slouken@617
   237
slouken@617
   238
    /* Not reached, but fixes compiler warnings */
slouken@617
   239
    return 0;
slouken@0
   240
}
slouken@777
   241
slouken@777
   242
/* vi: set ts=4 sw=4 expandtab: */