# HG changeset patch # User Matti Hamalainen # Date 1349036588 -10800 # Node ID 064d1d1d5b0fe314ce109d6e30cdacc9b0488d47 # Parent 048536ad01e0afb5c6cec97f23428bd70a20c065 Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP(). diff -r 048536ad01e0 -r 064d1d1d5b0f jss.h --- 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) diff -r 048536ad01e0 -r 064d1d1d5b0f jssmod.c --- 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. diff -r 048536ad01e0 -r 064d1d1d5b0f jssmod.h --- 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);