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