[AD] Patch to allow Allegro to use signed samples (4.3.10)

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Hi all.

I have a problem: Allegro uses unsigned sample data only, and the Amiga OS's
sound system uses signed sample data only!  In future versions of Allegro
this certainly needs to be more flexible.  However in the 4.3.10 version it
can be modified fairly easy to allow this.  See the attached patch for
information.  It just adds an internal variable called
_sound_signed_samples that ports can set in their sound initialisation
functions if required.  The .voc and .wav loading routines then ensure that
all 8 bit and 16 bit samples are in signed format if this flag is set.  If
not then they just leave the data as unsigned.

Of course this only fixes samples being loaded from .voc and .wav files. 
Samples being loaded from packfiles will still be in unsigned format as
that is the type of data that is in the packfile.  In this case I was
considering adding a function such as convert_sample() in src/sound.c that
will convert the sample if the _sound_signed_samples flag is set. 
Otherwise I might look at adding a flag to the SAMPLE structure that says
whether the sample is signed or unsigned, and converts it automatically the
first time it is used.

Any comments?

-- 
/-------------------------------------------------------------------\
[Hitman/Code HQ - 6502/z80/68000/604e/80x86/ARM coder - Amiga rulez!]
[VZ-200/VIC-20/MZ-700/c16/c64*10/c128*8/Plus-4/CPC464/CD32/500*2    ]
[600/1000/1200*2/A4000/SNES/N64/Dreamcast/Athlon 1100/AmigaOne      ]
[Assembly Language: The most fun you can have with your clothes on! ]
\-------------------------------------------------------------------/
Index: src/sound.c
===================================================================
--- src/sound.c	(revision 11144)
+++ src/sound.c	(working copy)
@@ -134,6 +134,7 @@
 
 int _sound_installed = FALSE;             /* are we installed? */
 int _sound_input_installed = FALSE;
+int _sound_signed_samples = FALSE;        /* Set to TRUE to sound driver to use signed samples */
 
 static int digi_reserve = -1;             /* how many voices to reserve */
 static int midi_reserve = -1;
@@ -952,6 +953,14 @@
 	       destroy_sample(spl);
 	       spl = NULL;
 	    }
+	    else {
+	       /* 8 bit samples are in unsigned format in .voc files.  See if the platform */
+	       /* needs them converted to signed format and do so if required */
+	       if (_sound_signed_samples) {
+	       for (x=0; x<len; x++)
+		  ((signed char *)spl->data)[x] = ((unsigned char *)spl->data)[x]^0x80;
+	       }
+	    }
 	 }
 	 else {
 	    len /= 2;
@@ -961,7 +970,11 @@
 		  spl = NULL;
 		  break;
 	       }
-	       ((signed short *)spl->data)[x] = (signed short)s^0x8000;
+	       /* 16 bit samples are in signed format in .voc files.  See if the platform wants */
+	       /* them left this was or converted to unsigned format */
+	       if (!_sound_signed_samples) {
+		  ((signed short *)spl->data)[x] = (signed short)s^0x8000;
+	       }
 	    }
 	 }
       }
@@ -1018,6 +1031,7 @@
    int bits = 8;
    int channels = 1;
    int s;
+   int x;
    SAMPLE *spl = NULL;
    ASSERT(f);
 
@@ -1077,6 +1091,14 @@
 		  destroy_sample(spl);
 		  spl = NULL;
 	       }
+	       else {
+		  /* 8 bit samples are in unsigned format in .wav files.  See if the platform */
+		  /* needs them converted to signed format and do so if required */
+		  if (_sound_signed_samples) {
+		     for (x=0; x<len; x++)
+			((signed char *)spl->data)[x] = ((unsigned char *)spl->data)[x]^0x80;
+		  }
+	       }
 	    }
 	    else {
 	       for (i=0; i<len*channels; i++) {
@@ -1085,7 +1107,12 @@
 		     spl = NULL;
 		     break;
 		  }
-		  ((signed short *)spl->data)[i] = (signed short)s^0x8000;
+		  /* 16 bit samples are in signed format in .wav files.  See if the platform wants */
+		  /* them left this was or converted to unsigned format */
+		  if (!_sound_signed_samples)
+		     ((signed short *)spl->data)[i] = (signed short)s^0x8000;
+		  else
+		     ((signed short *)spl->data)[i] = (signed short)s;
 	       }
 	    }
 
Index: include/allegro/sound.h
===================================================================
--- include/allegro/sound.h	(revision 11144)
+++ include/allegro/sound.h	(working copy)
@@ -51,6 +51,8 @@
 AL_FUNC(int, get_mixer_voices, (void));
 AL_FUNC(int, get_mixer_buffer_length, (void));
 
+AL_VAR(int, _sound_signed_samples);
+
 #ifdef __cplusplus
    }
 #endif


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/