music_timidity.c
changeset 777 92882ef2ab81
child 782 e7d3a8f73e88
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/music_timidity.c	Tue Oct 17 02:33:47 2017 -0700
     1.3 @@ -0,0 +1,119 @@
     1.4 +/*
     1.5 +  SDL_mixer:  An audio mixer library based on the SDL library
     1.6 +  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +/* This file supports playing MIDI files with timidity */
    1.26 +
    1.27 +#ifdef MUSIC_MID_TIMIDITY
    1.28 +
    1.29 +#include "music_timidity.h"
    1.30 +
    1.31 +#include "timidity/timidity.h"
    1.32 +
    1.33 +
    1.34 +static int samplesize;
    1.35 +
    1.36 +static int TIMIDITY_Open(const SDL_AudioSpec *spec)
    1.37 +{
    1.38 +    samplesize = spec->size / spec->samples;
    1.39 +    if (Timidity_Init(spec->freq, spec->format, spec->channels, spec->samples) != 0) {
    1.40 +        Mix_SetError("%s", Timidity_Error());
    1.41 +        return -1;
    1.42 +    }
    1.43 +    return 0;
    1.44 +}
    1.45 +
    1.46 +static void TIMIDITY_Close(void)
    1.47 +{
    1.48 +    Timidity_Close();
    1.49 +}
    1.50 +
    1.51 +void *TIMIDITY_CreateFromRW(SDL_RWops *src, int freesrc)
    1.52 +{
    1.53 +    MidiSong *music = Timidity_LoadSong_RW(src, freesrc);
    1.54 +    if (!music) {
    1.55 +        Mix_SetError("%s", Timidity_Error());
    1.56 +    }
    1.57 +    return music;
    1.58 +}
    1.59 +
    1.60 +static void TIMIDITY_SetVolume(void *context, int volume)
    1.61 +{
    1.62 +    Timidity_SetVolume(volume);
    1.63 +}
    1.64 +
    1.65 +static int TIMIDITY_Play(void *context)
    1.66 +{
    1.67 +    MidiSong *music = (MidiSong *)context;
    1.68 +    Timidity_Start(music);
    1.69 +    return 0;
    1.70 +}
    1.71 +
    1.72 +static SDL_bool TIMIDITY_IsPlaying(void *context)
    1.73 +{
    1.74 +    return Timidity_Active() ? SDL_TRUE : SDL_FALSE;
    1.75 +}
    1.76 +
    1.77 +static int TIMIDITY_GetAudio(void *context, void *data, int bytes)
    1.78 +{
    1.79 +    int samples = (bytes / samplesize);
    1.80 +    Timidity_PlaySome(data, samples);
    1.81 +    return 0;
    1.82 +}
    1.83 +
    1.84 +static void TIMIDITY_Stop(void *context)
    1.85 +{
    1.86 +    Timidity_Stop();
    1.87 +}
    1.88 +
    1.89 +static void TIMIDITY_Delete(void *context)
    1.90 +{
    1.91 +    MidiSong *music = (MidiSong *)context;
    1.92 +    Timidity_FreeSong(music);
    1.93 +}
    1.94 +
    1.95 +Mix_MusicInterface Mix_MusicInterface_TIMIDITY =
    1.96 +{
    1.97 +    "TIMIDITY",
    1.98 +    MIX_MUSIC_TIMIDITY,
    1.99 +    MUS_MID,
   1.100 +    SDL_FALSE,
   1.101 +    SDL_FALSE,
   1.102 +
   1.103 +    NULL,   /* Load */
   1.104 +    TIMIDITY_Open,
   1.105 +    TIMIDITY_CreateFromRW,
   1.106 +    NULL,   /* CreateFromFile */
   1.107 +    TIMIDITY_SetVolume,
   1.108 +    TIMIDITY_Play,
   1.109 +    TIMIDITY_IsPlaying,
   1.110 +    TIMIDITY_GetAudio,
   1.111 +    NULL,   /* Seek */
   1.112 +    NULL,   /* Pause */
   1.113 +    NULL,   /* Resume */
   1.114 +    NULL,   /* Stop */
   1.115 +    TIMIDITY_Delete,
   1.116 +    TIMIDITY_Close,
   1.117 +    NULL,   /* Unload */
   1.118 +};
   1.119 +
   1.120 +#endif /* MUSIC_MID_TIMIDITY */
   1.121 +
   1.122 +/* vi: set ts=4 sw=4 expandtab: */