From 6fac50f530e9179e466f61ab96f4f59968ccf5a9 Mon Sep 17 00:00:00 2001
From: pranaygupta36 <pranayguptastudent@gmail.com>
Date: Sat, 2 Jun 2018 17:47:36 +0530
Subject: [PATCH] changes made to incorporate double values of factors in
 soundfile access routines

---
 pd/src/d_soundfile.c | 54 +++++++++++++++++++++++++++++++-------------
 1 file changed, 38 insertions(+), 16 deletions(-)

diff --git a/pd/src/d_soundfile.c b/pd/src/d_soundfile.c
index 891c191a9..03bb88fea 100644
--- a/pd/src/d_soundfile.c
+++ b/pd/src/d_soundfile.c
@@ -49,6 +49,12 @@ objects use Posix-like threads.  */
 typedef unsigned short uint16;
 typedef unsigned int uint32;
 
+typedef union
+{
+    float f;
+    uint32_t ui;
+}t_aliasfloatuint;
+
 #define FORMAT_WAVE 0
 #define FORMAT_AIFF 1
 #define FORMAT_NEXT 2
@@ -463,6 +469,7 @@ static void soundfile_xferin_sample(int sfchannels, int nvecs, t_sample **vecs,
     int i, j;
     unsigned char *sp, *sp2;
     t_sample *fp;
+    t_aliasfloatuint alias;
     int nchannels = (sfchannels < nvecs ? sfchannels : nvecs);
     int bytesperframe = bytespersamp * sfchannels;
     for (i = 0, sp = buf; i < nchannels; i++, sp += bytespersamp)
@@ -504,16 +511,22 @@ static void soundfile_xferin_sample(int sfchannels, int nvecs, t_sample **vecs,
             if (bigendian)
             {
                 for (j = 0, sp2 = sp, fp=vecs[i] + spread * itemsread;
-                    j < nitems; j++, sp2 += bytesperframe, fp += spread)
-                        *(long *)fp = ((sp2[0] << 24) | (sp2[1] << 16)
+                     j < nitems; j++, sp2 += bytesperframe, fp += spread)
+                    {
+                        alias.ui = ((sp2[0] << 24) | (sp2[1] << 16)
                             | (sp2[2] << 8) | sp2[3]);
+                        *fp = (t_float)alias.f;
+                    }
             }
             else
             {
                 for (j = 0, sp2 = sp, fp=vecs[i] + spread * itemsread;
-                    j < nitems; j++, sp2 += bytesperframe, fp += spread)
-                        *(long *)fp = ((sp2[3] << 24) | (sp2[2] << 16)
+                     j < nitems; j++, sp2 += bytesperframe, fp += spread)
+                    {
+                        alias.ui = ((sp2[3] << 24) | (sp2[2] << 16)
                             | (sp2[1] << 8) | sp2[0]);
+                        *fp = (t_float)alias.f;
+                    }
             }
         }
     }
@@ -531,6 +544,7 @@ static void soundfile_xferin_float(int sfchannels, int nvecs, t_float **vecs,
     int i, j;
     unsigned char *sp, *sp2;
     t_float *fp;
+    t_aliasfloatuint alias;
     int nchannels = (sfchannels < nvecs ? sfchannels : nvecs);
     int bytesperframe = bytespersamp * sfchannels;
     for (i = 0, sp = buf; i < nchannels; i++, sp += bytespersamp)
@@ -572,16 +586,22 @@ static void soundfile_xferin_float(int sfchannels, int nvecs, t_float **vecs,
             if (bigendian)
             {
                 for (j = 0, sp2 = sp, fp=vecs[i] + spread * itemsread;
-                    j < nitems; j++, sp2 += bytesperframe, fp += spread)
-                        *(long *)fp = ((sp2[0] << 24) | (sp2[1] << 16)
+                     j < nitems; j++, sp2 += bytesperframe, fp += spread)
+                    {
+                        alias.ui = ((sp2[0] << 24) | (sp2[1] << 16)
                             | (sp2[2] << 8) | sp2[3]);
+                        *fp = (t_float)alias.f;
+                    }
             }
             else
             {
                 for (j = 0, sp2 = sp, fp=vecs[i] + spread * itemsread;
-                    j < nitems; j++, sp2 += bytesperframe, fp += spread)
-                        *(long *)fp = ((sp2[3] << 24) | (sp2[2] << 16)
+                     j < nitems; j++, sp2 += bytesperframe, fp += spread)
+                    {
+                        alias.ui = ((sp2[3] << 24) | (sp2[2] << 16)
                             | (sp2[1] << 8) | sp2[0]);
+                        *fp = (t_float)alias.f;
+                    }
             }
         }
     }
@@ -932,6 +952,7 @@ static void soundfile_xferout_sample(int nchannels, t_sample **vecs,
     int i, j;
     unsigned char *sp, *sp2;
     t_sample *fp;
+    t_aliasfloatuint alias;
     int bytesperframe = bytespersamp * nchannels;
     long xx;
     for (i = 0, sp = buf; i < nchannels; i++, sp += bytespersamp)
@@ -1013,8 +1034,8 @@ static void soundfile_xferout_sample(int nchannels, t_sample **vecs,
                 for (j = 0, sp2 = sp, fp=vecs[i] + onset;
                     j < nitems; j++, sp2 += bytesperframe, fp += spread)
                 {
-                    t_sample f2 = *fp * normalfactor;
-                    xx = *(long *)&f2;
+                    alias.f = (float)(*fp * normalfactor);
+                    xx = alias.ui;
                     sp2[0] = (xx >> 24); sp2[1] = (xx >> 16);
                     sp2[2] = (xx >> 8); sp2[3] = xx;
                 }
@@ -1024,8 +1045,8 @@ static void soundfile_xferout_sample(int nchannels, t_sample **vecs,
                 for (j = 0, sp2 = sp, fp=vecs[i] + onset;
                     j < nitems; j++, sp2 += bytesperframe, fp += spread)
                 {
-                    t_sample f2 = *fp * normalfactor;
-                    xx = *(long *)&f2;
+                    alias.f = (float)(*fp * normalfactor);
+                    xx = alias.ui;
                     sp2[3] = (xx >> 24); sp2[2] = (xx >> 16);
                     sp2[1] = (xx >> 8); sp2[0] = xx;
                 }
@@ -1040,6 +1061,7 @@ static void soundfile_xferout_float(int nchannels, t_float **vecs,
     int i, j;
     unsigned char *sp, *sp2;
     t_float *fp;
+    t_aliasfloatuint alias;
     int bytesperframe = bytespersamp * nchannels;
     long xx;
     for (i = 0, sp = buf; i < nchannels; i++, sp += bytespersamp)
@@ -1121,8 +1143,8 @@ static void soundfile_xferout_float(int nchannels, t_float **vecs,
                 for (j = 0, sp2 = sp, fp=vecs[i] + onset;
                     j < nitems; j++, sp2 += bytesperframe, fp += spread)
                 {
-                    t_sample f2 = *fp * normalfactor;
-                    xx = *(long *)&f2;
+                    alias.f = (float)(*fp * normalfactor);
+                    xx = alias.ui;
                     sp2[0] = (xx >> 24); sp2[1] = (xx >> 16);
                     sp2[2] = (xx >> 8); sp2[3] = xx;
                 }
@@ -1132,8 +1154,8 @@ static void soundfile_xferout_float(int nchannels, t_float **vecs,
                 for (j = 0, sp2 = sp, fp=vecs[i] + onset;
                     j < nitems; j++, sp2 += bytesperframe, fp += spread)
                 {
-                    t_sample f2 = *fp * normalfactor;
-                    xx = *(long *)&f2;
+                    alias.f = (float)(*fp * normalfactor);
+                    xx = alias.ui;
                     sp2[3] = (xx >> 24); sp2[2] = (xx >> 16);
                     sp2[1] = (xx >> 8); sp2[0] = xx;
                 }
-- 
GitLab