music_mod.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 03 Oct 2009 20:43:33 +0000
changeset 419 e27fe0bfe470
parent 411 cade1e3a4cb8
child 420 2594a266fc8b
permissions -rw-r--r--
Sam Lantinga - Sat Oct 3 13:33:36 PDT 2009
* MOD support uses libmikmod and is dynamically loaded by default
slouken@411
     1
/*
slouken@411
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@411
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@411
     4
slouken@411
     5
    This library is free software; you can redistribute it and/or
slouken@411
     6
    modify it under the terms of the GNU Library General Public
slouken@411
     7
    License as published by the Free Software Foundation; either
slouken@411
     8
    version 2 of the License, or (at your option) any later version.
slouken@411
     9
slouken@411
    10
    This library is distributed in the hope that it will be useful,
slouken@411
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@411
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@411
    13
    Library General Public License for more details.
slouken@411
    14
slouken@411
    15
    You should have received a copy of the GNU Library General Public
slouken@411
    16
    License along with this library; if not, write to the Free
slouken@411
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@411
    18
slouken@411
    19
    Sam Lantinga
slouken@411
    20
    slouken@libsdl.org
slouken@411
    21
*/
slouken@411
    22
slouken@411
    23
/* $Id: music_mod.c 4211 2008-12-08 00:27:32Z slouken $ */
slouken@411
    24
slouken@411
    25
#ifdef MOD_MUSIC
slouken@411
    26
slouken@411
    27
/* This file supports MOD tracker music streams */
slouken@411
    28
slouken@411
    29
#include "SDL_mixer.h"
slouken@419
    30
#include "dynamic_mod.h"
slouken@411
    31
#include "music_mod.h"
slouken@411
    32
slouken@411
    33
#include "mikmod.h"
slouken@411
    34
slouken@411
    35
#define SDL_SURROUND
slouken@411
    36
#ifdef SDL_SURROUND
slouken@411
    37
#define MAX_OUTPUT_CHANNELS 6
slouken@411
    38
#else
slouken@411
    39
#define MAX_OUTPUT_CHANNELS 2
slouken@411
    40
#endif
slouken@411
    41
slouken@411
    42
/* Reference for converting mikmod output to 4/6 channels */
slouken@411
    43
static int current_output_channels;
slouken@411
    44
static Uint16 current_output_format;
slouken@411
    45
slouken@411
    46
static int music_swap8;
slouken@411
    47
static int music_swap16;
slouken@411
    48
slouken@419
    49
/* Initialize the MOD player, with the given mixer settings
slouken@411
    50
   This function returns 0, or -1 if there was an error.
slouken@411
    51
 */
slouken@411
    52
int MOD_init(SDL_AudioSpec *mixerfmt)
slouken@411
    53
{
slouken@411
    54
	CHAR *list;
slouken@419
    55
slouken@419
    56
	if ( Mix_InitMOD() < 0 ) {
slouken@419
    57
		return NULL;
slouken@419
    58
	}
slouken@411
    59
slouken@411
    60
	/* Set the MikMod music format */
slouken@411
    61
	music_swap8 = 0;
slouken@411
    62
	music_swap16 = 0;
slouken@411
    63
	switch (mixerfmt->format) {
slouken@411
    64
slouken@411
    65
		case AUDIO_U8:
slouken@411
    66
		case AUDIO_S8: {
slouken@411
    67
			if ( mixerfmt->format == AUDIO_S8 ) {
slouken@411
    68
				music_swap8 = 1;
slouken@411
    69
			}
slouken@419
    70
			*mikmod.md_mode = 0;
slouken@411
    71
		}
slouken@411
    72
		break;
slouken@411
    73
slouken@411
    74
		case AUDIO_S16LSB:
slouken@411
    75
		case AUDIO_S16MSB: {
slouken@411
    76
			/* See if we need to correct MikMod mixing */
slouken@411
    77
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@411
    78
			if ( mixerfmt->format == AUDIO_S16MSB ) {
slouken@411
    79
#else
slouken@411
    80
			if ( mixerfmt->format == AUDIO_S16LSB ) {
slouken@411
    81
#endif
slouken@411
    82
				music_swap16 = 1;
slouken@411
    83
			}
slouken@419
    84
			*mikmod.md_mode = DMODE_16BITS;
slouken@411
    85
		}
slouken@411
    86
		break;
slouken@411
    87
slouken@411
    88
		default: {
slouken@411
    89
			Mix_SetError("Unknown hardware audio format");
slouken@411
    90
			return -1;
slouken@411
    91
		}
slouken@411
    92
	}
slouken@411
    93
	current_output_channels = mixerfmt->channels;
slouken@411
    94
	current_output_format = mixerfmt->format;
slouken@411
    95
	if ( mixerfmt->channels > 1 ) {
slouken@411
    96
		if ( mixerfmt->channels > MAX_OUTPUT_CHANNELS ) {
slouken@411
    97
			Mix_SetError("Hardware uses more channels than mixerfmt");
slouken@411
    98
			return -1;
slouken@411
    99
		}
slouken@419
   100
		*mikmod.md_mode |= DMODE_STEREO;
slouken@411
   101
	}
slouken@419
   102
	*mikmod.md_mixfreq = mixerfmt->freq;
slouken@419
   103
	*mikmod.md_device  = 0;
slouken@419
   104
	*mikmod.md_volume  = 96;
slouken@419
   105
	*mikmod.md_musicvolume = 128;
slouken@419
   106
	*mikmod.md_sndfxvolume = 128;
slouken@419
   107
	*mikmod.md_pansep  = 128;
slouken@419
   108
	*mikmod.md_reverb  = 0;
slouken@419
   109
	*mikmod.md_mode    |= DMODE_HQMIXER|DMODE_SOFT_MUSIC|DMODE_SURROUND;
slouken@419
   110
slouken@419
   111
	list = mikmod.MikMod_InfoDriver();
slouken@411
   112
	if ( list )
slouken@411
   113
	  free(list);
slouken@411
   114
	else
slouken@419
   115
	  mikmod.MikMod_RegisterDriver(mikmod.drv_nos);
slouken@419
   116
slouken@419
   117
	list = mikmod.MikMod_InfoLoader();
slouken@411
   118
	if ( list )
slouken@411
   119
	  free(list);
slouken@411
   120
	else
slouken@419
   121
	  mikmod.MikMod_RegisterAllLoaders();
slouken@419
   122
slouken@419
   123
	if ( mikmod.MikMod_Init(NULL) ) {
slouken@419
   124
		Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno));
slouken@411
   125
		return -1;
slouken@411
   126
	}
slouken@411
   127
slouken@411
   128
	return 0;
slouken@411
   129
}
slouken@411
   130
slouken@411
   131
/* Uninitialize the music players */
slouken@411
   132
void MOD_exit(void)
slouken@411
   133
{
slouken@419
   134
	mikmod.MikMod_Exit();
slouken@419
   135
	Mix_QuitMOD();
slouken@411
   136
}
slouken@411
   137
slouken@411
   138
/* Set the volume for a MOD stream */
slouken@411
   139
void MOD_setvolume(MODULE *music, int volume)
slouken@411
   140
{
slouken@419
   141
	mikmod.Player_SetVolume((SWORD)volume);
slouken@411
   142
}
slouken@411
   143
slouken@411
   144
/* Load a MOD stream from the given file */
slouken@411
   145
MODULE *MOD_new(const char *file)
slouken@411
   146
{
slouken@411
   147
	SDL_RWops *rw;
slouken@411
   148
slouken@411
   149
	rw = SDL_RWFromFile(file, "rb");
slouken@411
   150
	if ( rw == NULL ) {
slouken@411
   151
		/* FIXME: Free rw, need to free on delete */
slouken@411
   152
		SDL_SetError("Couldn't open %s", file);
slouken@411
   153
		return NULL;
slouken@411
   154
	}
slouken@411
   155
	return MOD_new_RW(rw);
slouken@411
   156
}
slouken@411
   157
slouken@411
   158
slouken@411
   159
typedef struct
slouken@411
   160
{
slouken@411
   161
	MREADER mr;
slouken@411
   162
	int offset;
slouken@411
   163
	int eof;
slouken@411
   164
	SDL_RWops *rw;
slouken@411
   165
} LMM_MREADER;
slouken@411
   166
slouken@411
   167
BOOL LMM_Seek(struct MREADER *mr,long to,int dir)
slouken@411
   168
{
slouken@419
   169
	LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
slouken@419
   170
	if ( dir == SEEK_SET ) {
slouken@419
   171
		to += lmmmr->offset;
slouken@419
   172
	}
slouken@419
   173
	return (SDL_RWseek(lmmmr->rw, to, dir) < lmmmr->offset);
slouken@411
   174
}
slouken@411
   175
long LMM_Tell(struct MREADER *mr)
slouken@411
   176
{
slouken@419
   177
	LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
slouken@419
   178
	return SDL_RWtell(lmmmr->rw) - lmmmr->offset;
slouken@411
   179
}
slouken@411
   180
BOOL LMM_Read(struct MREADER *mr,void *buf,size_t sz)
slouken@411
   181
{
slouken@419
   182
	LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
slouken@419
   183
	return SDL_RWread(lmmmr->rw, buf, sz, 1);
slouken@411
   184
}
slouken@411
   185
int LMM_Get(struct MREADER *mr)
slouken@411
   186
{
slouken@411
   187
	unsigned char c;
slouken@419
   188
	LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
slouken@419
   189
	if ( SDL_RWread(lmmmr->rw, &c, 1, 1) ) {
slouken@419
   190
		return c;
slouken@419
   191
	}
slouken@419
   192
	return EOF;
slouken@411
   193
}
slouken@411
   194
BOOL LMM_Eof(struct MREADER *mr)
slouken@411
   195
{
slouken@411
   196
	int offset;
slouken@419
   197
	LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
slouken@419
   198
	offset = LMM_Tell(mr);
slouken@419
   199
	return offset >= lmmmr->eof;
slouken@411
   200
}
slouken@411
   201
MODULE *MikMod_LoadSongRW(SDL_RWops *rw, int maxchan)
slouken@411
   202
{
slouken@411
   203
	LMM_MREADER lmmmr = {
slouken@411
   204
		{ LMM_Seek, LMM_Tell, LMM_Read, LMM_Get, LMM_Eof },
slouken@411
   205
		0,
slouken@411
   206
		0,
slouken@411
   207
		0
slouken@411
   208
	};
slouken@419
   209
	lmmmr.offset = SDL_RWtell(rw);
slouken@419
   210
	SDL_RWseek(rw, 0, SEEK_END);
slouken@419
   211
	lmmmr.eof = SDL_RWtell(rw);
slouken@419
   212
	SDL_RWseek(rw, lmmmr.offset, SEEK_SET);
slouken@411
   213
        lmmmr.rw = rw;
slouken@419
   214
	return mikmod.Player_LoadGeneric((MREADER*)&lmmmr, maxchan, 0);
slouken@411
   215
}
slouken@411
   216
slouken@411
   217
/* Load a MOD stream from an SDL_RWops object */
slouken@411
   218
MODULE *MOD_new_RW(SDL_RWops *rw)
slouken@411
   219
{
slouken@419
   220
	MODULE *module;
slouken@419
   221
slouken@419
   222
	module = MikMod_LoadSongRW(rw,64);
slouken@411
   223
	if (!module) {
slouken@419
   224
		Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno));
slouken@411
   225
		return NULL;
slouken@411
   226
	}
slouken@411
   227
slouken@411
   228
	/* Stop implicit looping, fade out and other flags. */
slouken@411
   229
	module->extspd  = 1;
slouken@411
   230
	module->panflag = 1;
slouken@411
   231
	module->wrap    = 0;
slouken@411
   232
	module->loop    = 0;
slouken@411
   233
#if 0 /* Don't set fade out by default - unfortunately there's no real way
slouken@411
   234
to query the status of the song or set trigger actions.  Hum. */
slouken@411
   235
	module->fadeout = 1;
slouken@411
   236
#endif
slouken@411
   237
	return module;
slouken@411
   238
}
slouken@411
   239
slouken@411
   240
/* Start playback of a given MOD stream */
slouken@411
   241
void MOD_play(MODULE *music)
slouken@411
   242
{
slouken@419
   243
	mikmod.Player_Start(music);
slouken@411
   244
}
slouken@411
   245
slouken@411
   246
/* Return non-zero if a stream is currently playing */
slouken@411
   247
int MOD_playing(MODULE *music)
slouken@411
   248
{
slouken@419
   249
	return mikmod.Player_Active();
slouken@411
   250
}
slouken@411
   251
slouken@411
   252
/* Play some of a stream previously started with MOD_play() */
slouken@411
   253
int MOD_playAudio(MODULE *music, Uint8 *stream, int len)
slouken@411
   254
{
slouken@411
   255
	if (current_output_channels > 2) {
slouken@411
   256
		int small_len = 2 * len / current_output_channels;
slouken@411
   257
		int i;
slouken@411
   258
		Uint8 *src, *dst;
slouken@411
   259
slouken@419
   260
		mikmod.VC_WriteBytes((SBYTE *)stream, small_len);
slouken@411
   261
		/* and extend to len by copying channels */
slouken@411
   262
		src = stream + small_len;
slouken@411
   263
		dst = stream + len;
slouken@411
   264
slouken@411
   265
		switch (current_output_format & 0xFF) {
slouken@411
   266
			case 8:
slouken@411
   267
				for ( i=small_len/2; i; --i ) {
slouken@411
   268
					src -= 2;
slouken@411
   269
					dst -= current_output_channels;
slouken@411
   270
					dst[0] = src[0];
slouken@411
   271
					dst[1] = src[1];
slouken@411
   272
					dst[2] = src[0];
slouken@411
   273
					dst[3] = src[1];
slouken@411
   274
					if (current_output_channels == 6) {
slouken@411
   275
						dst[4] = src[0];
slouken@411
   276
						dst[5] = src[1];
slouken@411
   277
					}
slouken@411
   278
				}
slouken@411
   279
				break;
slouken@411
   280
			case 16:
slouken@411
   281
				for ( i=small_len/4; i; --i ) {
slouken@411
   282
					src -= 4;
slouken@411
   283
					dst -= 2 * current_output_channels;
slouken@411
   284
					dst[0] = src[0];
slouken@411
   285
					dst[1] = src[1];
slouken@411
   286
					dst[2] = src[2];
slouken@411
   287
					dst[3] = src[3];
slouken@411
   288
					dst[4] = src[0];
slouken@411
   289
					dst[5] = src[1];
slouken@411
   290
					dst[6] = src[2];
slouken@411
   291
					dst[7] = src[3];
slouken@411
   292
					if (current_output_channels == 6) {
slouken@411
   293
						dst[8] = src[0];
slouken@411
   294
						dst[9] = src[1];
slouken@411
   295
						dst[10] = src[2];
slouken@411
   296
						dst[11] = src[3];
slouken@411
   297
					}
slouken@411
   298
				}
slouken@411
   299
				break;
slouken@411
   300
		}
slouken@411
   301
	} else {
slouken@419
   302
		mikmod.VC_WriteBytes((SBYTE *)stream, len);
slouken@411
   303
	}
slouken@411
   304
	if ( music_swap8 ) {
slouken@411
   305
		Uint8 *dst;
slouken@411
   306
		int i;
slouken@411
   307
slouken@411
   308
		dst = stream;
slouken@411
   309
		for ( i=len; i; --i ) {
slouken@411
   310
			*dst++ ^= 0x80;
slouken@411
   311
		}
slouken@411
   312
	} else
slouken@411
   313
	if ( music_swap16 ) {
slouken@411
   314
		Uint8 *dst, tmp;
slouken@411
   315
		int i;
slouken@411
   316
slouken@411
   317
		dst = stream;
slouken@411
   318
		for ( i=(len/2); i; --i ) {
slouken@411
   319
			tmp = dst[0];
slouken@411
   320
			dst[0] = dst[1];
slouken@411
   321
			dst[1] = tmp;
slouken@411
   322
			dst += 2;
slouken@411
   323
		}
slouken@411
   324
	}
slouken@411
   325
	return 0;
slouken@411
   326
}
slouken@411
   327
slouken@411
   328
/* Stop playback of a stream previously started with MOD_play() */
slouken@411
   329
void MOD_stop(MODULE *music)
slouken@411
   330
{
slouken@419
   331
	mikmod.Player_Stop();
slouken@411
   332
}
slouken@411
   333
slouken@411
   334
/* Close the given MOD stream */
slouken@411
   335
void MOD_delete(MODULE *music)
slouken@411
   336
{
slouken@419
   337
	mikmod.Player_Free(music);
slouken@411
   338
}
slouken@411
   339
slouken@411
   340
/* Jump (seek) to a given position (time is in seconds) */
slouken@411
   341
void MOD_jump_to_time(MODULE *music, double time)
slouken@411
   342
{
slouken@419
   343
	mikmod.Player_SetPosition((UWORD)time);
slouken@411
   344
}
slouken@411
   345
slouken@411
   346
#endif /* MOD_MUSIC */