diff jssmod.c @ 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 033c660c25f5
line wrap: on
line diff
--- 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.