playmus.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 21 Aug 2004 12:27:02 +0000
changeset 245 63b3650714de
parent 241 503416fca921
child 256 6d7e102500c9
permissions -rw-r--r--
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
surround sound on Linux using the Alsa driver. To use them, naturally
you need a sound card that will do 4 or 6 channels and probably also a
recent version of the Alsa drivers and library. Since the only SDL
output driver that knows about surround sound is the Alsa driver,
you���ll want to choose it, using:

export SDL_AUDIODRIVER=alsa

There are no syntactic changes to the programming API. No new
library calls, no differences in arguments.

There are two semantic changes:

(1) For library calls with number of channels as an argument, formerly
you could use only 1 or 2 for the number of channels. Now you
can also use 4 or 6.

(2) The two "left" and "right" arguments to Mix_SetPanning, for the
case of 4 or 6 channels, no longer simply control the volumes of
the left and right channels. Now the "left" argument is converted
to an angle and Mix_SetPosition is called, and the "right" argu-
ment is ignored.

With two exceptions, so far as I know, the modified SDL12 and
SDL_mixer work the same way as the original versions, when opened for
1 or 2 channel output. The two exceptions are bugs which I fixed.
Well, the first, anyway, is a bug for sure. When rate conversions up
or down by a factor of two are applied (in src/audio/SDL_audiocvt.c),
streams with different numbers of channels (that is, mono and stereo)
are treated the same way: either each sample is copied or every other
sample is omitted. This is ok for mono, but for stereo, it is frames
that should be copied or omitted, where by "frame" I mean a portion of
the stream containing one sample for each channel. (In the SDL source,
confusingly, sometimes frames are called "samples".) So for these
rate conversions, stereo streams have to be treated differently, and
they are, in my modified version.

The other problem that might be characterized as a bug arises
when SDL_mixer is passed a multichannel chunk which does not have an
integral number of frames. Due to the way the effect_position code
loops over frames, when the chunk ends with a partial frame, memory
outside the chunk buffer will be accessed. In the case of stereo,
it���s possible that because malloc may give more memory than requested,
this potential problem never actually causes a segment fault. I don���t
know. For 6 channel chunks, I do know, and it does cause segment
faults.


If SDL_mixer is passed defective chunks and this causes a segment
fault, arguably, that���s not a bug in SDL_mixer. Still, whether or not
it counts as a bug, it���s easy to protect against, so why not? I added
code in mixer.c to discard any partial frame at the end of a chunk.

Then what about when SDL or SDL_mixer is opened for 4 or 6 chan-
nel output? What happens with the parts of the current library
designed for stereo? I don���t know whether I���ve covered all the bases,
but I���ve tried:

(1) For playing 2 channel waves, or other cases where SDL knows it has
to match up a 2 channel source with a 4 or 6 channel output, I���ve
added code in SDL_audiocvt.c to make the necessary conversions.

(2) For playing midis using timidity, I���ve converted timidity to do 4
or 6 channel output, upon request.

(3) For playing mods using mikmod, I put ad hoc code in music.c to
convert the stereo output that mikmod produces to 4 or 6 chan-
nels. Obviously it would be better to change the mikmod code to
mix down into 4 or 6 channels, but I have a hard time following
the code in mikmod, so I didn���t do that.

(4) For playing mp3s, I put ad hoc code in smpeg to copy channels in
the case when 4 or 6 channel output is needed.

(5) There seems to be no problem with .ogg files - stereo .oggs can be
up converted as .wavs are.

(6) The effect_position code in SDL_mixer is now generalized to in-
clude the cases of 4 and 6 channel streams.

I���ve done a very limited amount of compatibility testing for some
of the games using SDL I happen to have. For details, see the file
TESTS.

I���ve put into a separate archive, Surround-SDL-testfiles.tgz, a
couple of 6 channel wave files for testing and a 6 channel ogg file.
If you have the right hardware and version of Alsa, you should be able
to play the wave files with the Alsa utility aplay (and hear all
channels, except maybe lfe, for chan-id.wav, since it���s rather faint).
Don���t expect aplay to give good sound, though. There���s something
wrong with the current version of aplay.

The canyon.ogg file is to test loading of 6 channel oggs. After
patching and compiling, you can play it with playmus. (My version of
ogg123 will not play it, and I had to patch mplayer to get it to play
6 channel oggs.)

Greg Lee <greg@ling.lll.hawaii.edu>
Thus, July 1, 2004
     1 /*
     2     PLAYMUS:  A test application for the SDL mixer library.
     3     Copyright (C) 1997-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* $Id$ */
    24 
    25 #include <stdlib.h>
    26 #include <stdio.h>
    27 #include <string.h>
    28 #include <signal.h>
    29 #ifdef unix
    30 #include <unistd.h>
    31 #endif
    32 
    33 #include "SDL.h"
    34 #include "SDL_mixer.h"
    35 
    36 
    37 static int audio_open = 0;
    38 static Mix_Music *music = NULL;
    39 static int next_track = 0;
    40 
    41 void CleanUp(void)
    42 {
    43 	if( Mix_PlayingMusic() ) {
    44 		Mix_FadeOutMusic(1500);
    45 		SDL_Delay(1500);
    46 	}
    47 	if ( music ) {
    48 		Mix_FreeMusic(music);
    49 		music = NULL;
    50 	}
    51 	if ( audio_open ) {
    52 		Mix_CloseAudio();
    53 		audio_open = 0;
    54 	}
    55 	SDL_Quit();
    56 }
    57 
    58 void Usage(char *argv0)
    59 {
    60 	fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] <musicfile>\n", argv0);
    61 }
    62 
    63 void Menu(void)
    64 {
    65 	char buf[10];
    66 
    67 	printf("Available commands: (p)ause (r)esume (h)alt > ");
    68 	fflush(stdin);
    69 	scanf("%s",buf);
    70 	switch(buf[0]){
    71 	case 'p': case 'P':
    72 		Mix_PauseMusic();
    73 		break;
    74 	case 'r': case 'R':
    75 		Mix_ResumeMusic();
    76 		break;
    77 	case 'h': case 'H':
    78 		Mix_HaltMusic();
    79 		break;
    80 	}
    81 	printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", 
    82 		   Mix_PausedMusic() ? "yes" : "no");
    83 }
    84 
    85 void IntHandler(int sig)
    86 {
    87 	switch (sig) {
    88 	        case SIGINT:
    89 			next_track++;
    90 			break;
    91 	}
    92 }
    93 
    94 int main(int argc, char *argv[])
    95 {
    96 	int audio_rate;
    97 	Uint16 audio_format;
    98 	int audio_channels;
    99 	int audio_buffers;
   100 	int audio_volume = MIX_MAX_VOLUME;
   101 	int looping = 0;
   102 	int interactive = 0;
   103 	int i;
   104 
   105 	/* Initialize variables */
   106 	audio_rate = 22050;
   107 	audio_format = AUDIO_S16;
   108 	audio_channels = 2;
   109 	audio_buffers = 4096;
   110 
   111 	/* Check command line usage */
   112 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   113 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   114 			++i;
   115 			audio_rate = atoi(argv[i]);
   116 		} else
   117 		if ( strcmp(argv[i], "-m") == 0 ) {
   118 			audio_channels = 1;
   119 		} else
   120 		if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
   121 			++i;
   122 			audio_channels = atoi(argv[i]);
   123 		} else
   124 		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
   125 			++i;
   126 			audio_buffers = atoi(argv[i]);
   127 		} else
   128 		if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
   129 			++i;
   130 			audio_volume = atoi(argv[i]);
   131 		} else
   132 		if ( strcmp(argv[i], "-l") == 0 ) {
   133 			looping = -1;
   134 		} else
   135 		if ( strcmp(argv[i], "-i") == 0 ) {
   136 			interactive = 1;
   137 		} else
   138 		if ( strcmp(argv[i], "-8") == 0 ) {
   139 			audio_format = AUDIO_U8;
   140 		} else {
   141 			Usage(argv[0]);
   142 			return(1);
   143 		}
   144 	}
   145 	if ( ! argv[i] ) {
   146 		Usage(argv[0]);
   147 		return(1);
   148 	}
   149 
   150 	/* Initialize the SDL library */
   151 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   152 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   153 		return(255);
   154 	}
   155 
   156 	atexit(CleanUp);
   157 	signal(SIGINT, IntHandler);
   158 	signal(SIGTERM, exit);
   159 
   160 	/* Open the audio device */
   161 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
   162 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   163 		return(2);
   164 	} else {
   165 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   166 		printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
   167 			(audio_format&0xFF),
   168 			(audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono", 
   169 			audio_buffers );
   170 	}
   171 	audio_open = 1;
   172 
   173 	/* Set the music volume */
   174 	Mix_VolumeMusic(audio_volume);
   175 
   176 	/* Set the external music player, if any */
   177 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   178 
   179 	while (argv[i]) {
   180 		next_track = 0;
   181 		
   182 		/* Load the requested music file */
   183 		music = Mix_LoadMUS(argv[i]);
   184 		if ( music == NULL ) {
   185 			fprintf(stderr, "Couldn't load %s: %s\n",
   186 				argv[i], SDL_GetError());
   187 			return(2);
   188 		}
   189 		
   190 		/* Play and then exit */
   191 		printf("Playing %s\n", argv[i]);
   192 		Mix_FadeInMusic(music,looping,2000);
   193 		while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
   194 			if(interactive)
   195 				Menu();
   196 			else
   197 				SDL_Delay(100);
   198 		}
   199 		Mix_FreeMusic(music);
   200 		music = NULL;
   201 
   202 		/* If the user presses Ctrl-C more than once, exit. */
   203 		SDL_Delay(500);
   204 		if ( next_track > 1 ) break;
   205 		
   206 		i++;
   207 	}
   208 	return(0);
   209 }