Skip to content

Commit

Permalink
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Browse files Browse the repository at this point in the history
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
  • Loading branch information
slouken committed Aug 21, 2004
1 parent 077d7fd commit 2bcd318
Show file tree
Hide file tree
Showing 22 changed files with 3,867 additions and 379 deletions.
3 changes: 3 additions & 0 deletions CHANGES
@@ -1,4 +1,7 @@
1.2.6:
Greg Lee - Wed, 14 Jul 2004 05:13:14 -1000
* Added 4 and 6 channel surround sound output support
* Added support for RMID format MIDI files
Sam Lantinga - Wed Nov 19 00:23:44 PST 2003
* Updated libtool support for new mingw32 DLL build process
Ryan C. Gordon - Sun Nov 9 23:34:47 EST 2003
Expand Down
1,100 changes: 1,054 additions & 46 deletions effect_position.c

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions mixer.c
Expand Up @@ -260,6 +260,7 @@ static void PrintFormat(char *title, SDL_AudioSpec *fmt)
{
printf("%s: %d bit %s audio (%s) at %u Hz\n", title, (fmt->format&0xFF),
(fmt->format&0x8000) ? "signed" : "unsigned",
(fmt->channels > 2) ? "surround" :
(fmt->channels > 1) ? "stereo" : "mono", fmt->freq);
}

Expand Down Expand Up @@ -635,6 +636,16 @@ int Mix_ReserveChannels(int num)
return num;
}

static int checkchunkintegral(Mix_Chunk *chunk)
{
int frame_width = 1;

if ((mixer.format & 0xFF) == 16) frame_width = 2;
frame_width *= mixer.channels;
while (chunk->alen % frame_width) chunk->alen--;
return chunk->alen;
}

/* Play an audio chunk on a specific channel.
If the specified channel is -1, play on the first free channel.
'ticks' is the number of milliseconds at most to play the sample, or -1
Expand All @@ -650,6 +661,10 @@ int Mix_PlayChannelTimed(int which, Mix_Chunk *chunk, int loops, int ticks)
Mix_SetError("Tried to play a NULL chunk");
return(-1);
}
if ( !checkchunkintegral(chunk)) {
Mix_SetError("Tried to play a chunk with a bad frame");
return(-1);
}

/* Lock the mixer while modifying the playing channels */
SDL_LockAudio();
Expand Down Expand Up @@ -717,6 +732,10 @@ int Mix_FadeInChannelTimed(int which, Mix_Chunk *chunk, int loops, int ms, int t
if ( chunk == NULL ) {
return(-1);
}
if ( !checkchunkintegral(chunk)) {
Mix_SetError("Tried to play a chunk with a bad frame");
return(-1);
}

/* Lock the mixer while modifying the playing channels */
SDL_LockAudio();
Expand Down
81 changes: 76 additions & 5 deletions music.c
Expand Up @@ -31,6 +31,14 @@

#include "SDL_mixer.h"

#define SDL_SURROUND

#ifdef SDL_SURROUND
#define MAX_OUTPUT_CHANNELS 6
#else
#define MAX_OUTPUT_CHANNELS 2
#endif

/* The music command hack is UNIX specific */
#ifndef unix
#undef CMD_MUSIC
Expand Down Expand Up @@ -131,6 +139,10 @@ static int native_midi_ok;
#endif
#endif

/* Reference for converting mikmod output to 4/6 channels */
static int current_output_channels;
static Uint16 current_output_format;

/* Used to calculate fading steps */
static int ms_per_step;

Expand Down Expand Up @@ -210,7 +222,57 @@ void music_mixer(void *udata, Uint8 *stream, int len)
#endif
#ifdef MOD_MUSIC
case MUS_MOD:
VC_WriteBytes((SBYTE *)stream, len);
if (current_output_channels > 2) {
int small_len = 2 * len / current_output_channels;
int i;
Uint8 *src, *dst;

VC_WriteBytes((SBYTE *)stream, small_len);
/* and extend to len by copying channels */
src = stream + small_len;
dst = stream + len;

switch (current_output_format & 0xFF) {
case 8:
for ( i=small_len/2; i; --i ) {
src -= 2;
dst -= current_output_channels;
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[0];
dst[3] = src[1];
if (current_output_channels == 6) {
dst[4] = src[0];
dst[5] = src[1];
}
}
break;
case 16:
for ( i=small_len/4; i; --i ) {
src -= 4;
dst -= 2 * current_output_channels;
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
dst[4] = src[0];
dst[5] = src[1];
dst[6] = src[2];
dst[7] = src[3];
if (current_output_channels == 6) {
dst[8] = src[0];
dst[9] = src[1];
dst[10] = src[2];
dst[11] = src[3];
}
}
break;
}



}
else VC_WriteBytes((SBYTE *)stream, len);
if ( music_swap8 ) {
Uint8 *dst;
int i;
Expand Down Expand Up @@ -306,8 +368,10 @@ int open_music(SDL_AudioSpec *mixer)
++music_error;
}
}
current_output_channels = mixer->channels;
current_output_format = mixer->format;
if ( mixer->channels > 1 ) {
if ( mixer->channels > 2 ) {
if ( mixer->channels > MAX_OUTPUT_CHANNELS ) {
Mix_SetError("Hardware uses more channels than mixer");
++music_error;
}
Expand Down Expand Up @@ -385,7 +449,7 @@ Mix_Music *Mix_LoadMUS(const char *file)
{
FILE *fp;
char *ext;
Uint8 magic[5];
Uint8 magic[5], moremagic[9];
Mix_Music *music;

/* Figure out what kind of file this is */
Expand All @@ -397,7 +461,12 @@ Mix_Music *Mix_LoadMUS(const char *file)
Mix_SetError("Couldn't read from '%s'", file);
return(NULL);
}
if (!fread(moremagic, 8, 1, fp)) {
Mix_SetError("Couldn't read from '%s'", file);
return(NULL);
}
magic[4] = '\0';
moremagic[8] = '\0';
fclose(fp);

/* Figure out the file extension, so we can determine the type */
Expand Down Expand Up @@ -426,7 +495,7 @@ Mix_Music *Mix_LoadMUS(const char *file)
AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
*/
if ( (ext && MIX_string_equals(ext, "WAV")) ||
(strcmp((char *)magic, "RIFF") == 0) ||
((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
(strcmp((char *)magic, "FORM") == 0) ) {
music->type = MUS_WAV;
music->data.wave = WAVStream_LoadSong(file, (char *)magic);
Expand All @@ -440,7 +509,9 @@ Mix_Music *Mix_LoadMUS(const char *file)
/* MIDI files have the magic four bytes "MThd" */
if ( (ext && MIX_string_equals(ext, "MID")) ||
(ext && MIX_string_equals(ext, "MIDI")) ||
strcmp((char *)magic, "MThd") == 0 ) {
strcmp((char *)magic, "MThd") == 0 ||
( strcmp((char *)magic, "RIFF") == 0 &&
strcmp((char *)(moremagic+4), "RMID") == 0 ) ) {
music->type = MUS_MID;
#ifdef USE_NATIVE_MIDI
if ( native_midi_ok ) {
Expand Down
19 changes: 9 additions & 10 deletions native_midi_gpl/native_midi_gpl.c
Expand Up @@ -223,7 +223,6 @@ void native_midi_start(NativeMidiSong *song)

int i, error = 0, j;


for (i = 0; i < 16; i++)
{
useprog[i] = 0; /* reset options */
Expand Down Expand Up @@ -380,7 +379,7 @@ int synth_setup()
{
if( gus_dev >= 0 )
{
play_gus = 1;
play_gus = -1;
awe_dev = -1;
sb_dev = -1;
ext_dev = -1;
Expand All @@ -394,7 +393,7 @@ int synth_setup()
{
if( awe_dev >= 0 )
{
play_awe = 1;
play_awe = -1;
gus_dev = -1;
sb_dev = -1;
ext_dev = -1;
Expand All @@ -408,7 +407,7 @@ int synth_setup()
{
if( sb_dev >= 0 && fm_patch_aviable )
{
play_fm = 1;
play_fm = -1;
gus_dev = -1;
awe_dev = -1;
ext_dev = -1;
Expand All @@ -423,7 +422,7 @@ int synth_setup()
{
if( sb_dev >= 0 && opl3_patch_aviable )
{
play_fm = 1;
play_fm = -1;
gus_dev = -1;
awe_dev = -1;
ext_dev = -1;
Expand All @@ -438,7 +437,7 @@ int synth_setup()
{
if( ext_dev >= 0 )
{
play_ext = 1;
play_ext = -1;
gus_dev = -1;
awe_dev = -1;
sb_dev = -1;
Expand All @@ -453,31 +452,31 @@ int synth_setup()
/* autoselect best device */
if( gus_dev >= 0 )
{
play_gus = 1;
play_gus = -1;
awe_dev = -1;
sb_dev = -1;
ext_dev = -1;
return 1;
}
if( awe_dev >= 0 )
{
play_awe = 1;
play_awe = -1;
gus_dev = -1;
sb_dev = -1;
ext_dev = -1;
return 1;
}
if( sb_dev >= 0 && fm_patch_aviable )
{
play_fm = 1;
play_fm = -1;
gus_dev = -1;
awe_dev = -1;
ext_dev = -1;
return 2; /* return 1 if use FM befor Timidity */
}
if( ext_dev >= 0 )
{
play_ext = 1;
play_ext = -1;
gus_dev = -1;
awe_dev = -1;
sb_dev = -1;
Expand Down
15 changes: 10 additions & 5 deletions playmus.c
Expand Up @@ -57,7 +57,7 @@ void CleanUp(void)

void Usage(char *argv0)
{
fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-b buffers] [-v N] <musicfile>\n", argv0);
fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] <musicfile>\n", argv0);
}

void Menu(void)
Expand Down Expand Up @@ -114,6 +114,13 @@ int main(int argc, char *argv[])
++i;
audio_rate = atoi(argv[i]);
} else
if ( strcmp(argv[i], "-m") == 0 ) {
audio_channels = 1;
} else
if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
++i;
audio_channels = atoi(argv[i]);
} else
if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
++i;
audio_buffers = atoi(argv[i]);
Expand All @@ -122,9 +129,6 @@ int main(int argc, char *argv[])
++i;
audio_volume = atoi(argv[i]);
} else
if ( strcmp(argv[i], "-m") == 0 ) {
audio_channels = 1;
} else
if ( strcmp(argv[i], "-l") == 0 ) {
looping = -1;
} else
Expand All @@ -148,6 +152,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
return(255);
}

atexit(CleanUp);
signal(SIGINT, IntHandler);
signal(SIGTERM, exit);
Expand All @@ -160,7 +165,7 @@ int main(int argc, char *argv[])
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
(audio_format&0xFF),
(audio_channels > 1) ? "stereo" : "mono",
(audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono",
audio_buffers );
}
audio_open = 1;
Expand Down
7 changes: 6 additions & 1 deletion playwave.c
Expand Up @@ -266,7 +266,7 @@ static void CleanUp(void)

static void Usage(char *argv0)
{
fprintf(stderr, "Usage: %s [-8] [-r rate] [-f] [-F] [-l] [-m] <wavefile>\n", argv0);
fprintf(stderr, "Usage: %s [-8] [-r rate] [-c channels] [-f] [-F] [-l] [-m] <wavefile>\n", argv0);
}


Expand Down Expand Up @@ -360,6 +360,10 @@ int main(int argc, char *argv[])
if ( strcmp(argv[i], "-m") == 0 ) {
audio_channels = 1;
} else
if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
++i;
audio_channels = atoi(argv[i]);
} else
if ( strcmp(argv[i], "-l") == 0 ) {
loops = -1;
} else
Expand Down Expand Up @@ -398,6 +402,7 @@ int main(int argc, char *argv[])
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
printf("Opened audio at %d Hz %d bit %s", audio_rate,
(audio_format&0xFF),
(audio_channels > 2) ? "surround" :
(audio_channels > 1) ? "stereo" : "mono");
if ( loops ) {
printf(" (looping)\n");
Expand Down
5 changes: 5 additions & 0 deletions timidity/common.h
Expand Up @@ -25,6 +25,11 @@ extern char *program_name, current_filename[];

extern FILE *msgfp;

extern int num_ochannels;

#define MULTICHANNEL_OUT
#define MAX_OUT_CHANNELS 6

typedef struct {
char *path;
void *next;
Expand Down

0 comments on commit 2bcd318

Please sign in to comment.