music_modplug.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Jan 2016 22:12:31 -0800
changeset 712 7e59d684b070
parent 711 f40c5ac95b12
child 725 bdf7b8d20566
permissions -rw-r--r--
Updated to version 2.0.1
     1 /*
     2   SDL_mixer:  An audio mixer library based on the SDL library
     3   Copyright (C) 1997-2016 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 #ifdef MODPLUG_MUSIC
    23 
    24 #include "SDL_mixer.h"
    25 #include "dynamic_modplug.h"
    26 #include "music_modplug.h"
    27 
    28 static int current_output_channels=0;
    29 static int music_swap8=0;
    30 static int music_swap16=0;
    31 static ModPlug_Settings settings;
    32 
    33 int modplug_init(SDL_AudioSpec *spec)
    34 {
    35     if ( !Mix_Init(MIX_INIT_MODPLUG) ) {
    36         return -1;
    37     }
    38 
    39     modplug.ModPlug_GetSettings(&settings);
    40     settings.mFlags=MODPLUG_ENABLE_OVERSAMPLING;
    41     current_output_channels=spec->channels;
    42     settings.mChannels=spec->channels>1?2:1;
    43     settings.mBits=spec->format&0xFF;
    44 
    45     music_swap8 = 0;
    46     music_swap16 = 0;
    47 
    48     switch(spec->format)
    49     {
    50         case AUDIO_U8:
    51         case AUDIO_S8: {
    52             if ( spec->format == AUDIO_S8 ) {
    53                 music_swap8 = 1;
    54             }
    55             settings.mBits=8;
    56         }
    57         break;
    58 
    59         case AUDIO_S16LSB:
    60         case AUDIO_S16MSB: {
    61             /* See if we need to correct MikMod mixing */
    62 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
    63             if ( spec->format == AUDIO_S16MSB ) {
    64 #else
    65             if ( spec->format == AUDIO_S16LSB ) {
    66 #endif
    67                 music_swap16 = 1;
    68             }
    69             settings.mBits=16;
    70         }
    71         break;
    72 
    73         default: {
    74             Mix_SetError("Unknown hardware audio format");
    75             return -1;
    76         }
    77 
    78     }
    79 
    80     settings.mFrequency=spec->freq; /*TODO: limit to 11025, 22050, or 44100 ? */
    81     settings.mResamplingMode=MODPLUG_RESAMPLE_FIR;
    82     settings.mReverbDepth=0;
    83     settings.mReverbDelay=100;
    84     settings.mBassAmount=0;
    85     settings.mBassRange=50;
    86     settings.mSurroundDepth=0;
    87     settings.mSurroundDelay=10;
    88     settings.mLoopCount=0;
    89     modplug.ModPlug_SetSettings(&settings);
    90     return 0;
    91 }
    92 
    93 /* Uninitialize the music players */
    94 void modplug_exit()
    95 {
    96 }
    97 
    98 /* Set the volume for a modplug stream */
    99 void modplug_setvolume(modplug_data *music, int volume)
   100 {
   101     modplug.ModPlug_SetMasterVolume(music->file, volume*4);
   102 }
   103 
   104 /* Load a modplug stream from an SDL_RWops object */
   105 modplug_data *modplug_new_RW(SDL_RWops *src, int freesrc)
   106 {
   107     modplug_data *music = NULL;
   108     Sint64 offset;
   109     size_t sz;
   110     char *buf;
   111 
   112     /* Make sure the modplug library is loaded */
   113     if ( !Mix_Init(MIX_INIT_MODPLUG) ) {
   114         return NULL;
   115     }
   116 
   117     offset = SDL_RWtell(src);
   118     SDL_RWseek(src, 0, RW_SEEK_END);
   119     sz = (size_t)(SDL_RWtell(src) - offset);
   120     SDL_RWseek(src, offset, RW_SEEK_SET);
   121     buf = (char*)SDL_malloc(sz);
   122     if (buf) {
   123         if (SDL_RWread(src, buf, sz, 1) == 1) {
   124             music = (modplug_data*)SDL_malloc(sizeof(modplug_data));
   125             if (music) {
   126                 music->playing = 0;
   127                 music->file = modplug.ModPlug_Load(buf, sz);
   128                 if (!music->file) {
   129                     SDL_free(music);
   130                     music = NULL;
   131                 }
   132             } else {
   133                 SDL_OutOfMemory();
   134             }
   135         }
   136         SDL_free(buf);
   137     } else {
   138         SDL_OutOfMemory();
   139     }
   140     if (music && freesrc) {
   141         SDL_RWclose(src);
   142     }
   143     return music;
   144 }
   145 
   146 /* Start playback of a given modplug stream */
   147 void modplug_play(modplug_data *music)
   148 {
   149     modplug.ModPlug_Seek(music->file,0);
   150     music->playing=1;
   151 }
   152 
   153 /* Return non-zero if a stream is currently playing */
   154 int modplug_playing(modplug_data *music)
   155 {
   156     return music && music->playing;
   157 }
   158 
   159 /* Play some of a stream previously started with modplug_play() */
   160 int modplug_playAudio(modplug_data *music, Uint8 *stream, int len)
   161 {
   162     if (current_output_channels > 2) {
   163         int small_len = 2 * len / current_output_channels;
   164         int i;
   165         Uint8 *src, *dst;
   166 
   167         i=modplug.ModPlug_Read(music->file, stream, small_len);
   168         if(i<small_len)
   169         {
   170             SDL_memset(stream+i,0,small_len-i);
   171             music->playing=0;
   172         }
   173         /* and extend to len by copying channels */
   174         src = stream + small_len;
   175         dst = stream + len;
   176 
   177         switch (settings.mBits) {
   178             case 8:
   179                 for ( i=small_len/2; i; --i ) {
   180                     src -= 2;
   181                     dst -= current_output_channels;
   182                     dst[0] = src[0];
   183                     dst[1] = src[1];
   184                     dst[2] = src[0];
   185                     dst[3] = src[1];
   186                     if (current_output_channels == 6) {
   187                         dst[4] = src[0];
   188                         dst[5] = src[1];
   189                     }
   190                 }
   191                 break;
   192             case 16:
   193                 for ( i=small_len/4; i; --i ) {
   194                     src -= 4;
   195                     dst -= 2 * current_output_channels;
   196                     dst[0] = src[0];
   197                     dst[1] = src[1];
   198                     dst[2] = src[2];
   199                     dst[3] = src[3];
   200                     dst[4] = src[0];
   201                     dst[5] = src[1];
   202                     dst[6] = src[2];
   203                     dst[7] = src[3];
   204                     if (current_output_channels == 6) {
   205                         dst[8] = src[0];
   206                         dst[9] = src[1];
   207                         dst[10] = src[2];
   208                         dst[11] = src[3];
   209                     }
   210                 }
   211                 break;
   212         }
   213     } else {
   214         int i=modplug.ModPlug_Read(music->file, stream, len);
   215         if(i<len)
   216         {
   217             SDL_memset(stream+i,0,len-i);
   218             music->playing=0;
   219         }
   220     }
   221     if ( music_swap8 ) {
   222         Uint8 *dst;
   223         int i;
   224 
   225         dst = stream;
   226         for ( i=len; i; --i ) {
   227             *dst++ ^= 0x80;
   228         }
   229     } else
   230     if ( music_swap16 ) {
   231         Uint8 *dst, tmp;
   232         int i;
   233 
   234         dst = stream;
   235         for ( i=(len/2); i; --i ) {
   236             tmp = dst[0];
   237             dst[0] = dst[1];
   238             dst[1] = tmp;
   239             dst += 2;
   240         }
   241     }
   242     return 0;
   243 }
   244 
   245 /* Stop playback of a stream previously started with modplug_play() */
   246 void modplug_stop(modplug_data *music)
   247 {
   248     music->playing=0;
   249 }
   250 
   251 /* Close the given modplug stream */
   252 void modplug_delete(modplug_data *music)
   253 {
   254     modplug.ModPlug_Unload(music->file);
   255     SDL_free(music);
   256 }
   257 
   258 /* Jump (seek) to a given position (time is in seconds) */
   259 void modplug_jump_to_time(modplug_data *music, double time)
   260 {
   261     modplug.ModPlug_Seek(music->file,(int)(time*1000));
   262 }
   263 
   264 #endif /* MODPLUG_MUSIC */