music_timidity.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 17 Oct 2017 02:33:47 -0700
changeset 777 92882ef2ab81
child 782 e7d3a8f73e88
permissions -rw-r--r--
Rewrote music.c to support any number of decode libraries using a compiled-in plugin interface
Mix_LoadWAV_RW() can now load sound formats that were previously available only as music.

This is still work in progress. Testing and project updates need to happen on other platforms.
slouken@777
     1
/*
slouken@777
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@777
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@777
     4
slouken@777
     5
  This software is provided 'as-is', without any express or implied
slouken@777
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@777
     7
  arising from the use of this software.
slouken@777
     8
slouken@777
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@777
    10
  including commercial applications, and to alter it and redistribute it
slouken@777
    11
  freely, subject to the following restrictions:
slouken@777
    12
slouken@777
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@777
    14
     claim that you wrote the original software. If you use this software
slouken@777
    15
     in a product, an acknowledgment in the product documentation would be
slouken@777
    16
     appreciated but is not required.
slouken@777
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@777
    18
     misrepresented as being the original software.
slouken@777
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@777
    20
*/
slouken@777
    21
slouken@777
    22
/* This file supports playing MIDI files with timidity */
slouken@777
    23
slouken@777
    24
#ifdef MUSIC_MID_TIMIDITY
slouken@777
    25
slouken@777
    26
#include "music_timidity.h"
slouken@777
    27
slouken@777
    28
#include "timidity/timidity.h"
slouken@777
    29
slouken@777
    30
slouken@777
    31
static int samplesize;
slouken@777
    32
slouken@777
    33
static int TIMIDITY_Open(const SDL_AudioSpec *spec)
slouken@777
    34
{
slouken@777
    35
    samplesize = spec->size / spec->samples;
slouken@777
    36
    if (Timidity_Init(spec->freq, spec->format, spec->channels, spec->samples) != 0) {
slouken@777
    37
        Mix_SetError("%s", Timidity_Error());
slouken@777
    38
        return -1;
slouken@777
    39
    }
slouken@777
    40
    return 0;
slouken@777
    41
}
slouken@777
    42
slouken@777
    43
static void TIMIDITY_Close(void)
slouken@777
    44
{
slouken@777
    45
    Timidity_Close();
slouken@777
    46
}
slouken@777
    47
slouken@777
    48
void *TIMIDITY_CreateFromRW(SDL_RWops *src, int freesrc)
slouken@777
    49
{
slouken@777
    50
    MidiSong *music = Timidity_LoadSong_RW(src, freesrc);
slouken@777
    51
    if (!music) {
slouken@777
    52
        Mix_SetError("%s", Timidity_Error());
slouken@777
    53
    }
slouken@777
    54
    return music;
slouken@777
    55
}
slouken@777
    56
slouken@777
    57
static void TIMIDITY_SetVolume(void *context, int volume)
slouken@777
    58
{
slouken@777
    59
    Timidity_SetVolume(volume);
slouken@777
    60
}
slouken@777
    61
slouken@777
    62
static int TIMIDITY_Play(void *context)
slouken@777
    63
{
slouken@777
    64
    MidiSong *music = (MidiSong *)context;
slouken@777
    65
    Timidity_Start(music);
slouken@777
    66
    return 0;
slouken@777
    67
}
slouken@777
    68
slouken@777
    69
static SDL_bool TIMIDITY_IsPlaying(void *context)
slouken@777
    70
{
slouken@777
    71
    return Timidity_Active() ? SDL_TRUE : SDL_FALSE;
slouken@777
    72
}
slouken@777
    73
slouken@777
    74
static int TIMIDITY_GetAudio(void *context, void *data, int bytes)
slouken@777
    75
{
slouken@777
    76
    int samples = (bytes / samplesize);
slouken@777
    77
    Timidity_PlaySome(data, samples);
slouken@777
    78
    return 0;
slouken@777
    79
}
slouken@777
    80
slouken@777
    81
static void TIMIDITY_Stop(void *context)
slouken@777
    82
{
slouken@777
    83
    Timidity_Stop();
slouken@777
    84
}
slouken@777
    85
slouken@777
    86
static void TIMIDITY_Delete(void *context)
slouken@777
    87
{
slouken@777
    88
    MidiSong *music = (MidiSong *)context;
slouken@777
    89
    Timidity_FreeSong(music);
slouken@777
    90
}
slouken@777
    91
slouken@777
    92
Mix_MusicInterface Mix_MusicInterface_TIMIDITY =
slouken@777
    93
{
slouken@777
    94
    "TIMIDITY",
slouken@777
    95
    MIX_MUSIC_TIMIDITY,
slouken@777
    96
    MUS_MID,
slouken@777
    97
    SDL_FALSE,
slouken@777
    98
    SDL_FALSE,
slouken@777
    99
slouken@777
   100
    NULL,   /* Load */
slouken@777
   101
    TIMIDITY_Open,
slouken@777
   102
    TIMIDITY_CreateFromRW,
slouken@777
   103
    NULL,   /* CreateFromFile */
slouken@777
   104
    TIMIDITY_SetVolume,
slouken@777
   105
    TIMIDITY_Play,
slouken@777
   106
    TIMIDITY_IsPlaying,
slouken@777
   107
    TIMIDITY_GetAudio,
slouken@777
   108
    NULL,   /* Seek */
slouken@777
   109
    NULL,   /* Pause */
slouken@777
   110
    NULL,   /* Resume */
slouken@777
   111
    NULL,   /* Stop */
slouken@777
   112
    TIMIDITY_Delete,
slouken@777
   113
    TIMIDITY_Close,
slouken@777
   114
    NULL,   /* Unload */
slouken@777
   115
};
slouken@777
   116
slouken@777
   117
#endif /* MUSIC_MID_TIMIDITY */
slouken@777
   118
slouken@777
   119
/* vi: set ts=4 sw=4 expandtab: */