freebirth (0.3.2-8) raw_wave.c

Summary

 raw_wave.c |   36 +++++++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 7 deletions(-)

    
download this patch

Patch contents

--- freebirth-0.3.2.orig/raw_wave.c
+++ freebirth-0.3.2/raw_wave.c
@@ -21,18 +21,36 @@
 #include "raw_wave.h"
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <math.h>
+#include <endian.h>
+
+/* borrowed from glib2 */
+#define SHORT_SWAP_LE_BE(val)  ((short) ( \
+    (short) ((short) (val) >> 8) |      \
+    (short) ((short) (val) << 8)))
+static void swap_endian(short *data, int length)
+{
+       int i;
+       for (i = 0; i < length; i += 1, data++)
+               *data = SHORT_SWAP_LE_BE(*data);
+}
 
 static char *get_full_path(char *fn)
 {
   char *full_fn;
   char *fb_samples;
 
-  if (fn == NULL || fn[0] == '/') return fn;
   fb_samples = getenv("FB_SAMPLES");
   if (fb_samples == NULL) fb_samples = FB_SAMPLES;
-  sprintf(full_fn = (char *)malloc(strlen(fb_samples) + 1 + strlen(fn) + 1),
-	  "%s/%s", fb_samples, fn);
+  full_fn = (char *)malloc(strlen(fb_samples) + 1 + strlen(fn) + 1);
+   
+  if (fn == NULL || fn[0] == '/') { /* path is valid already */
+     sprintf(full_fn, "%s", fn); 
+  } else {
+     sprintf(full_fn, "%s/%s", fb_samples, fn); /* add path */
+  }
+   
   return full_fn;
 }
 
@@ -76,10 +94,13 @@
 
   }
   this->length += count;
+#if __BYTE_ORDER == __BIG_ENDIAN
+  swap_endian(tmp, this->length);
+#endif
   this->table = (sample *)malloc(sizeof(sample) * this->length);
-  for(i = 0; i < this->length;i++)
+  for(i = 0; i < this->length;i++) {
     this->table[i] = (sample)tmp[i];
-  
+  }
   fclose(in);
   free(tmp);
 }
@@ -126,6 +147,7 @@
 	
 	}
     }
+
   return this->buffer;  
 }
   
@@ -145,7 +167,7 @@
 void raw_wave_set_sample_file(raw_wave* this, char *filename)
 {
   FILE *in;
-  if (this->filename != 0) free(this->filename);
+  if (this->filename != NULL) free(this->filename);
   this->filename = get_full_path(filename);
   in = fopen(this->filename,"r");
   if( in==NULL )
@@ -243,7 +265,7 @@
   /* set table to zero here check if it is zero in */
   /* _fill_table */
   out->table = 0;
-  out->filename = 0;
+  out->filename = NULL;
   raw_wave_set_sample_file(out, filename);
 
   out->buffer   = (sample *)malloc(sizeof(sample) * TBASS_BUFF_SIZE);