dynamic_mp3.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Jun 2013 00:35:46 -0700
changeset 629 87c536d57d92
parent 625 1d489d8ec2e0
child 711 f40c5ac95b12
permissions -rw-r--r--
Switched Mac OS X framework to use weak linking on external libraries.
This allows someone to just remove the smpeg2.framework if they don't want MP3 support, for example.
The weak import attribute is necessary to prevent the optimizer from eliminating the symbol NULL check.
slouken@314
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@601
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@314
     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@314
     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@314
    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@314
    20
*/
slouken@314
    21
slouken@314
    22
#ifdef MP3_MUSIC
slouken@314
    23
slouken@314
    24
#include "SDL_loadso.h"
slouken@629
    25
#include "SDL_mixer.h"
slouken@314
    26
#include "dynamic_mp3.h"
slouken@314
    27
slouken@314
    28
smpeg_loader smpeg = {
slouken@617
    29
    0, NULL
slouken@314
    30
};
slouken@314
    31
slouken@314
    32
#ifdef MP3_DYNAMIC
slouken@314
    33
int Mix_InitMP3()
slouken@314
    34
{
slouken@617
    35
    if ( smpeg.loaded == 0 ) {
slouken@617
    36
        smpeg.handle = SDL_LoadObject(MP3_DYNAMIC);
slouken@617
    37
        if ( smpeg.handle == NULL ) {
slouken@617
    38
            return -1;
slouken@617
    39
        }
slouken@617
    40
        smpeg.SMPEG_actualSpec =
slouken@617
    41
            (void (*)( SMPEG *, SDL_AudioSpec * ))
slouken@617
    42
            SDL_LoadFunction(smpeg.handle, "SMPEG_actualSpec");
slouken@617
    43
        if ( smpeg.SMPEG_actualSpec == NULL ) {
slouken@617
    44
            SDL_UnloadObject(smpeg.handle);
slouken@617
    45
            return -1;
slouken@617
    46
        }
slouken@617
    47
        smpeg.SMPEG_delete =
slouken@617
    48
            (void (*)( SMPEG* ))
slouken@617
    49
            SDL_LoadFunction(smpeg.handle, "SMPEG_delete");
slouken@617
    50
        if ( smpeg.SMPEG_delete == NULL ) {
slouken@617
    51
            SDL_UnloadObject(smpeg.handle);
slouken@617
    52
            return -1;
slouken@617
    53
        }
slouken@617
    54
        smpeg.SMPEG_enableaudio =
slouken@617
    55
            (void (*)( SMPEG*, int ))
slouken@617
    56
            SDL_LoadFunction(smpeg.handle, "SMPEG_enableaudio");
slouken@617
    57
        if ( smpeg.SMPEG_enableaudio == NULL ) {
slouken@617
    58
            SDL_UnloadObject(smpeg.handle);
slouken@617
    59
            return -1;
slouken@617
    60
        }
slouken@617
    61
        smpeg.SMPEG_enablevideo =
slouken@617
    62
            (void (*)( SMPEG*, int ))
slouken@617
    63
            SDL_LoadFunction(smpeg.handle, "SMPEG_enablevideo");
slouken@617
    64
        if ( smpeg.SMPEG_enablevideo == NULL ) {
slouken@617
    65
            SDL_UnloadObject(smpeg.handle);
slouken@617
    66
            return -1;
slouken@617
    67
        }
slouken@617
    68
        smpeg.SMPEG_new_rwops =
slouken@625
    69
            (SMPEG* (*)(SDL_RWops *, SMPEG_Info*, int, int))
slouken@617
    70
            SDL_LoadFunction(smpeg.handle, "SMPEG_new_rwops");
slouken@617
    71
        if ( smpeg.SMPEG_new_rwops == NULL ) {
slouken@617
    72
            SDL_UnloadObject(smpeg.handle);
slouken@617
    73
            return -1;
slouken@617
    74
        }
slouken@617
    75
        smpeg.SMPEG_play =
slouken@617
    76
            (void (*)( SMPEG* ))
slouken@617
    77
            SDL_LoadFunction(smpeg.handle, "SMPEG_play");
slouken@617
    78
        if ( smpeg.SMPEG_play == NULL ) {
slouken@617
    79
            SDL_UnloadObject(smpeg.handle);
slouken@617
    80
            return -1;
slouken@617
    81
        }
slouken@617
    82
        smpeg.SMPEG_playAudio =
slouken@617
    83
            (int (*)( SMPEG *, Uint8 *, int ))
slouken@617
    84
            SDL_LoadFunction(smpeg.handle, "SMPEG_playAudio");
slouken@617
    85
        if ( smpeg.SMPEG_playAudio == NULL ) {
slouken@617
    86
            SDL_UnloadObject(smpeg.handle);
slouken@617
    87
            return -1;
slouken@617
    88
        }
slouken@617
    89
        smpeg.SMPEG_rewind =
slouken@617
    90
            (void (*)( SMPEG* ))
slouken@617
    91
            SDL_LoadFunction(smpeg.handle, "SMPEG_rewind");
slouken@617
    92
        if ( smpeg.SMPEG_rewind == NULL ) {
slouken@617
    93
            SDL_UnloadObject(smpeg.handle);
slouken@617
    94
            return -1;
slouken@617
    95
        }
slouken@617
    96
        smpeg.SMPEG_setvolume =
slouken@617
    97
            (void (*)( SMPEG*, int ))
slouken@617
    98
            SDL_LoadFunction(smpeg.handle, "SMPEG_setvolume");
slouken@617
    99
        if ( smpeg.SMPEG_setvolume == NULL ) {
slouken@617
   100
            SDL_UnloadObject(smpeg.handle);
slouken@617
   101
            return -1;
slouken@617
   102
        }
slouken@617
   103
        smpeg.SMPEG_skip =
slouken@617
   104
            (void (*)( SMPEG*, float ))
slouken@617
   105
            SDL_LoadFunction(smpeg.handle, "SMPEG_skip");
slouken@617
   106
        if ( smpeg.SMPEG_skip == NULL ) {
slouken@617
   107
            SDL_UnloadObject(smpeg.handle);
slouken@617
   108
            return -1;
slouken@617
   109
        }
slouken@617
   110
        smpeg.SMPEG_status =
slouken@617
   111
            (SMPEGstatus (*)( SMPEG* ))
slouken@617
   112
            SDL_LoadFunction(smpeg.handle, "SMPEG_status");
slouken@617
   113
        if ( smpeg.SMPEG_status == NULL ) {
slouken@617
   114
            SDL_UnloadObject(smpeg.handle);
slouken@617
   115
            return -1;
slouken@617
   116
        }
slouken@617
   117
        smpeg.SMPEG_stop =
slouken@617
   118
            (void (*)( SMPEG* ))
slouken@617
   119
            SDL_LoadFunction(smpeg.handle, "SMPEG_stop");
slouken@617
   120
        if ( smpeg.SMPEG_stop == NULL ) {
slouken@617
   121
            SDL_UnloadObject(smpeg.handle);
slouken@617
   122
            return -1;
slouken@617
   123
        }
slouken@617
   124
    }
slouken@617
   125
    ++smpeg.loaded;
slouken@314
   126
slouken@617
   127
    return 0;
slouken@314
   128
}
slouken@314
   129
void Mix_QuitMP3()
slouken@314
   130
{
slouken@617
   131
    if ( smpeg.loaded == 0 ) {
slouken@617
   132
        return;
slouken@617
   133
    }
slouken@617
   134
    if ( smpeg.loaded == 1 ) {
slouken@617
   135
        SDL_UnloadObject(smpeg.handle);
slouken@617
   136
    }
slouken@617
   137
    --smpeg.loaded;
slouken@314
   138
}
slouken@314
   139
#else
slouken@314
   140
int Mix_InitMP3()
slouken@314
   141
{
slouken@617
   142
    if ( smpeg.loaded == 0 ) {
slouken@629
   143
#ifdef __MACOSX__
slouken@629
   144
        extern SMPEG* SMPEG_new_rwops(SDL_RWops*, SMPEG_Info*, int, int) __attribute__((weak_import));
slouken@629
   145
        if ( SMPEG_new_rwops == NULL )
slouken@629
   146
        {
slouken@629
   147
            /* Missing weakly linked framework */
slouken@629
   148
            Mix_SetError("Missing smpeg2.framework");
slouken@629
   149
            return -1;
slouken@629
   150
        }
slouken@629
   151
#endif // __MACOSX__
slouken@629
   152
slouken@617
   153
        smpeg.SMPEG_actualSpec = SMPEG_actualSpec;
slouken@617
   154
        smpeg.SMPEG_delete = SMPEG_delete;
slouken@617
   155
        smpeg.SMPEG_enableaudio = SMPEG_enableaudio;
slouken@617
   156
        smpeg.SMPEG_enablevideo = SMPEG_enablevideo;
slouken@617
   157
        smpeg.SMPEG_new_rwops = SMPEG_new_rwops;
slouken@617
   158
        smpeg.SMPEG_play = SMPEG_play;
slouken@617
   159
        smpeg.SMPEG_playAudio = SMPEG_playAudio;
slouken@617
   160
        smpeg.SMPEG_rewind = SMPEG_rewind;
slouken@617
   161
        smpeg.SMPEG_setvolume = SMPEG_setvolume;
slouken@617
   162
        smpeg.SMPEG_skip = SMPEG_skip;
slouken@617
   163
        smpeg.SMPEG_status = SMPEG_status;
slouken@617
   164
        smpeg.SMPEG_stop = SMPEG_stop;
slouken@617
   165
    }
slouken@617
   166
    ++smpeg.loaded;
slouken@314
   167
slouken@617
   168
    return 0;
slouken@314
   169
}
slouken@314
   170
void Mix_QuitMP3()
slouken@314
   171
{
slouken@617
   172
    if ( smpeg.loaded == 0 ) {
slouken@617
   173
        return;
slouken@617
   174
    }
slouken@617
   175
    if ( smpeg.loaded == 1 ) {
slouken@617
   176
    }
slouken@617
   177
    --smpeg.loaded;
slouken@314
   178
}
slouken@314
   179
#endif /* MP3_DYNAMIC */
slouken@314
   180
slouken@314
   181
#endif /* MP3_MUSIC */