[xiph-cvs] cvs commit: vorbis/vorbis-tools/libao ao_wav.c

Kenneth C. Arnold kcarnold at xiph.org
Tue Sep 5 15:30:51 PDT 2000



kcarnold    00/09/05 15:30:50

  Modified:    vorbis-tools Tag: branch_postbeta2 ogg123.c
               vorbis-tools/libao Tag: branch_postbeta2 ao_wav.c
  Removed:     vorbis-tools Tag: branch_postbeta2 README
  Log:
  libao WAV output endian fix applied. Hopefully we're all endian-happy now.

Revision  Changes    Path
No                   revision

No                   revision

1.16.2.2  +5 -3      vorbis/vorbis-tools/ogg123.c

Index: ogg123.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis/vorbis-tools/ogg123.c,v
retrieving revision 1.16.2.1
retrieving revision 1.16.2.2
diff -u -r1.16.2.1 -r1.16.2.2
--- ogg123.c	2000/08/31 08:05:48	1.16.2.1
+++ ogg123.c	2000/09/05 22:30:49	1.16.2.2
@@ -14,7 +14,7 @@
  *                                                                  *
  ********************************************************************
 
- last mod: $Id: ogg123.c,v 1.16.2.1 2000/08/31 08:05:48 xiphmont Exp $
+ last mod: $Id: ogg123.c,v 1.16.2.2 2000/09/05 22:30:49 kcarnold Exp $
 
  ********************************************************************/
 
@@ -317,7 +317,8 @@
   int current_section = -1, eos = 0, ret;
   long t_min = 0, c_min = 0, r_min = 0;
   double t_sec = 0, c_sec = 0, r_sec = 0;
- 
+  int is_big_endian = ao_is_big_endian();
+
   if (strcmp (param.read_file, "-"))	/* input file not stdin */
     {
       if (!strncmp (param.read_file, "http://", 7))
@@ -462,7 +463,8 @@
 
   while (! eos)
     {
-      switch ((ret = ov_read (&vf, convbuffer, sizeof (convbuffer), 0, 2, 1, &current_section))) {
+      switch ((ret = ov_read (&vf, convbuffer, sizeof (convbuffer), 
+			      is_big_endian, 2, 1, &current_section))) {
       case 0: /* End of file */
         eos = 1;
         break;

No                   revision

No                   revision

1.2.4.1   +48 -2     vorbis/vorbis-tools/libao/ao_wav.c

Index: ao_wav.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis/vorbis-tools/libao/ao_wav.c,v
retrieving revision 1.2
retrieving revision 1.2.4.1
diff -u -r1.2 -r1.2.4.1
--- ao_wav.c	2000/08/01 14:13:26	1.2
+++ ao_wav.c	2000/09/05 22:30:50	1.2.4.1
@@ -49,6 +49,8 @@
 #define WRITE_U16(buf, x) *(buf)     = (unsigned char)(x&0xff);\
                                                   *((buf)+1) = (unsigned char)((x>>8)&0xff);
 
+#define DEFAULT_SWAP_BUFFER_SIZE 2048
+
 struct riff_struct 
 {
   unsigned char id[4];   /* RIFF */
@@ -94,6 +96,9 @@
 {
         char *output_file;
         int fd;
+	int byte_swap;
+	char *swap_buffer;
+	int buffer_size;
         struct wave_header wave;
 } ao_wav_internal_t;
 
@@ -147,7 +152,19 @@
 
         // Grab options here
         ao_wav_parse_options(state, options);
-
+	state->byte_swap = (bits == 16) && (ao_is_big_endian());
+	if (state->byte_swap)
+	{
+		state->buffer_size = DEFAULT_SWAP_BUFFER_SIZE;
+		state->swap_buffer = calloc(sizeof(char), state->buffer_size);
+	       
+		if (state->swap_buffer == NULL)
+		{
+			fprintf(stderr, "ao_wav: Could not allocate byte-swapping buffer.\n");
+			goto ERR;
+		}
+	}
+		
         state->fd=open(state->output_file,O_WRONLY | O_TRUNC | O_CREAT, 0644);
 
         if(state->fd < 0) 
@@ -201,7 +218,36 @@
 static void
 ao_wav_play(ao_internal_t *state, void *output_samples, uint_32 num_bytes)
 {
-  	write( ((ao_wav_internal_t *) state)->fd, output_samples, num_bytes );
+	int i;
+	ao_wav_internal_t *s = (ao_wav_internal_t *) state;
+
+	/* Swap all of the bytes if things are not little_endian */
+	if (s->byte_swap)
+	{
+		/* Resize buffer larger if needed */
+		if (num_bytes > s->buffer_size)
+		{
+			s->swap_buffer = realloc(s->swap_buffer, sizeof(char)*num_bytes);
+			if (s->swap_buffer == NULL) {
+				fprintf(stderr, "ao_wav: Could not resize swap buffer.\n");
+				return;
+			}
+			else
+				s->buffer_size = num_bytes;
+		}
+
+		/* Swap the bytes into the swap buffer (so we don't
+		 mess up the output_samples buffer) */
+		for(i = 0; i < num_bytes/2; i+=2)
+		{
+			s->swap_buffer[i]   = ((char *) output_samples)[i+1];
+			s->swap_buffer[i+1] = ((char *) output_samples)[i];
+		}
+
+		write(s->fd, s->swap_buffer, num_bytes );
+	}
+	else    /* Otherwise just write the output buffer directly */
+		write(s->fd, output_samples, num_bytes );
 }
 
 

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list