changeset 44:064d1d1d5b0f

Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 30 Sep 2012 23:23:08 +0300
parents 048536ad01e0
children b30430da2815
files jss.h jssmod.c jssmod.h
diffstat 3 files changed, 63 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/jss.h	Sun Sep 30 23:20:22 2012 +0300
+++ b/jss.h	Sun Sep 30 23:23:08 2012 +0300
@@ -31,6 +31,8 @@
 #define jsfLooped       (0x01)      // Is sample looped
 #define jsfBiDi         (0x02)      // Bi-directional loop?
 #define jsf16bit        (0x04)      // 16-bit?
+#define jsfFP           (0x08)      // Floating point sample
+
 #define JSFSET(a, b)    do { a |= b; } while (0)
 #define JSFUNSET(a, b)  do { a &= (0xff ^ b); } while (0)
 
--- a/jssmod.c	Sun Sep 30 23:20:22 2012 +0300
+++ b/jssmod.c	Sun Sep 30 23:23:08 2012 +0300
@@ -117,6 +117,38 @@
     }
     return TRUE;
 }
+
+
+int jssConvertSampleFromFP(void **dst, void * src, const size_t len, const int flags)
+{
+    // Convert from floating point to 8/16bit
+    size_t count = len;
+    float *in = (float *) src;
+    
+    if (flags & jsf16bit)
+    {
+        Sint16 *out;
+        *dst = out = dmMalloc(sizeof(sizeof(Sint16)) * len);
+        if (out == NULL)
+            return DMERR_MALLOC;
+
+        while (count--)
+            *(out++) = (*(in++) * 32767.0f);
+    }
+    else
+    {
+        Uint8 *out;
+        *dst = out = dmMalloc(sizeof(sizeof(Uint8)) * len);
+        if (out == NULL)
+            return DMERR_MALLOC;
+
+        while (count--)
+            *(out++) = 128 + (int) (*(in++) * 127.0f);
+    }
+    
+    return DMERR_OK;
+}
+
 #endif
 
 
@@ -197,6 +229,33 @@
 }
 
 
+int jssConvertSampleToFP(void **dst, void * src, const size_t len, const int flags)
+{
+    // Convert from 8/16bit to floating point
+    size_t count = len;
+    float *out;
+
+    *dst = out = dmMalloc(sizeof(float) * len);
+    if (out == NULL)
+        return DMERR_MALLOC;
+    
+    if (flags & jsf16bit)
+    {
+        Sint16 *in = (Sint16 *) src;
+        while (count--)
+            *(out++) = (float) (*(in++)) / 32768.0f;
+    }
+    else
+    {
+        Uint8 *in = (Uint8 *) src;
+        while (count--)
+            *(out++) = (float) (*(in++) - 128) / 128.0f;
+    }
+    
+    return DMERR_OK;
+}
+
+
 /* Allocates a new module structure or returns errorvalue if failed.
  * Memory is allocated only for the basic structure. Sample- and pattern
  * areas must be allocated separately with appropriate routines.
--- a/jssmod.h	Sun Sep 30 23:20:22 2012 +0300
+++ b/jssmod.h	Sun Sep 30 23:23:08 2012 +0300
@@ -287,9 +287,11 @@
 char*               jssASCIItoStr(char *, const char, const size_t);
 BOOL                jssEncodeSample8(Uint8 *, const size_t, const int);
 BOOL                jssEncodeSample16(Uint16 *, const size_t, const int);
+int                 jssConvertSampleFromFP(void **dst, void * src, const size_t len, const int flags);
 #endif
 BOOL                jssDecodeSample8(Uint8 *, const size_t, const int);
 BOOL                jssDecodeSample16(Uint16 *, const size_t, const int);
+int                 jssConvertSampleToFP(void **dst, void * src, const size_t len, const int flags);
 JSSModule *         jssAllocateModule(void);
 int                 jssFreeModule(JSSModule *);
 JSSPattern *        jssAllocatePattern(int, int);