changeset 51:36e2f910219c

A non-working implementation of floating point audio mixing.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 01 Oct 2012 03:48:21 +0300
parents 4cbdaa3f5d74
children 1d17ecc5486c
files Makefile.gen jmix_c_in.c jmixtmpl_c.h jssmix.c jssmix.h jssmod.c
diffstat 6 files changed, 43 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.gen	Mon Oct 01 03:46:10 2012 +0300
+++ b/Makefile.gen	Mon Oct 01 03:48:21 2012 +0300
@@ -211,7 +211,7 @@
 ###
 
 $(DMLIB)jmix_c.c: $(DMLIB)jmix_c_in.c $(DMLIB)jmixtmpl_c.h $(DMLIB)jmix_post_c.h
-	(echo "#include \"jssmix.h\"" && cpp $<) | sed "s/^# .*//g" > $@
+	(echo "#include \"jssmix.h\"" && cpp $< $(DM_CFLAGS)) | sed "s/^# .*//g" > $@
 
 $(OBJPATH)dmimage.o: $(DMLIB)dmimage.c $(DMLIB)stb_image.c
 	@echo " CC $+"
--- a/jmix_c_in.c	Mon Oct 01 03:46:10 2012 +0300
+++ b/jmix_c_in.c	Mon Oct 01 03:48:21 2012 +0300
@@ -7,10 +7,17 @@
 //#define JMIXER_DEBUG fprintf(stderr, "[%.8x:%.8x / %.8x]: %d\n", FP_GETH(tmpPos), FP_GETL(tmpPos), endPos, sp[FP_GETH(tmpPos)]);
 #define JMIXER_DEBUG
 
+#ifndef JMIXER_ADDBUF_TYPE
+#ifdef JSS_MIX_FP
+#define JMIXER_ADDBUF_TYPE double
+#else
+#define JMIXER_ADDBUF_TYPE Sint32
+#endif
+#endif
+
 /* Mono / Linear Interpolation
  */
 #ifdef JSS_MIX_FP
-#define JMIXER_ADDBUF_TYPE float
 #define JMIXER_SAMPLE_TYPE float
 #define JMIXER_INIT        const float vol = (float) (chn->chVolume * mixer->globalVol) / (256.0f * 64.0f);
 #define JMIXER_FUNC        tr = (sp[FP_GETH(tmpPos)] * vol + tr) / 2.0f; *(ap++) += tr;
@@ -27,11 +34,6 @@
 #define JMIXER_ENDCOND     (FP_GETH(tmpPos) > endPos)
 #include "jmixtmpl_c.h"
 
-#undef JMIXER_ADDBUF_TYPE
-#undef JMIXER_SAMPLE_TYPE
-#undef JMIXER_INIT
-#undef JMIXER_FUNC
-
 #else
 
 #define JMIXER_ADDBUF_TYPE Sint32
@@ -49,23 +51,21 @@
 #define JMIXER_ENDCOND     (FP_GETH(tmpPos) > endPos)
 #include "jmixtmpl_c.h"
 
-#undef JMIXER_ADDBUF_TYPE
+#endif
+
 #undef JMIXER_SAMPLE_TYPE
 #undef JMIXER_INIT
 #undef JMIXER_FUNC
 
-#endif
-
 /* Stereo / Linear Interpolation
  */
 #ifdef JSS_MIX_FP
-#define JMIXER_ADDBUF_TYPE float
 #define JMIXER_SAMPLE_TYPE float
-#define JMIXER_INIT        const float vol_l = (chn->chVolume * mixer->globalVol) / (256.0f * 64.0f), \
-                                       vol_r = (chn->chVolume * mixer->globalVol) / (256.0f * 64.0f);
+#define JMIXER_INIT        const double vol_l = (chn->chVolume * mixer->globalVol) / 256.0f, \
+                                       vol_r = (chn->chVolume * mixer->globalVol) / 256.0f;
 
-#define JMIXER_FUNC        tl = (sp[FP_GETH(tmpPos)] * vol_l + tl) / 2.0f; *(ap++) += tl; \
-                           tr = (sp[FP_GETH(tmpPos)] * vol_r + tr) / 2.0f; *(ap++) += tr;
+#define JMIXER_FUNC        tl = ((double)sp[FP_GETH(tmpPos)] * vol_l + tl) / (2.0f * 256.0f); *(ap++) += tl; \
+                           tr = ((double)sp[FP_GETH(tmpPos)] * vol_r + tr) / (2.0f * 256.0f); *(ap++) += tr;
 
 #define JMIXER_NAME        jvmMix_Stereo_C_FW
 #define JMIXER_NEXT        FP_ADD(tmpPos, tmpDelta);
@@ -77,14 +77,8 @@
 #define JMIXER_ENDCOND     (FP_GETH(tmpPos) > endPos)
 #include "jmixtmpl_c.h"
 
-#undef JMIXER_ADDBUF_TYPE
-#undef JMIXER_SAMPLE_TYPE
-#undef JMIXER_INIT
-#undef JMIXER_FUNC
-
 #else
 
-#define JMIXER_ADDBUF_TYPE Sint32
 #define JMIXER_SAMPLE_TYPE Sint16
 #define JMIXER_INIT        const Sint32 vol_l = (chn->chVolume * mixer->globalVol) / 256, \
                                         vol_r = (chn->chVolume * mixer->globalVol) / 256;
@@ -102,22 +96,20 @@
 #define JMIXER_ENDCOND     (FP_GETH(tmpPos) > endPos)
 #include "jmixtmpl_c.h"
 
-#undef JMIXER_ADDBUF_TYPE
+#endif
+
 #undef JMIXER_SAMPLE_TYPE
 #undef JMIXER_INIT
 #undef JMIXER_FUNC
 
-#endif
 
 /* Post processing functions
  */
 #ifdef JSS_MIX_FP
 
-#define JMIXER_ADDBUF_TYPE float
-
 #define JMIXER_CLAMP                  \
     if (t < -1.0f) t = -1.0f; else    \
-    if (t >  1.0f) t =  1.0f;         \
+    if (t >  1.0f) t =  1.0f;
 
 #define JMIXER_NAME jvmPostProcess_U8_C
 #define JMIXER_TYPE Uint8
@@ -145,11 +137,9 @@
 
 #else
 
-#define JMIXER_ADDBUF_TYPE Sint32
-
 #define JMIXER_CLAMP \
     if (t < JVM_LIMIT_16_NEG) t = JVM_LIMIT_16_NEG; else    \
-    if (t > JVM_LIMIT_16_POS) t = JVM_LIMIT_16_POS;         \
+    if (t > JVM_LIMIT_16_POS) t = JVM_LIMIT_16_POS;
 
 #define JMIXER_NAME jvmPostProcess_U8_C
 #define JMIXER_TYPE Uint8
--- a/jmixtmpl_c.h	Mon Oct 01 03:46:10 2012 +0300
+++ b/jmixtmpl_c.h	Mon Oct 01 03:48:21 2012 +0300
@@ -5,7 +5,7 @@
  */
 
 #ifdef JMIXER_HEADER
-int JMIXER_NAME (JSSMixer *, JSSChannel *, Sint32 *, const int, const Sint32);
+int JMIXER_NAME (JSSMixer *, JSSChannel *, JMIXER_ADDBUF_TYPE *, const int, const Sint32);
 #else
 int JMIXER_NAME (JSSMixer *mixer, JSSChannel *chn, JMIXER_ADDBUF_TYPE *addBuffer, const int mixLength, const Sint32 endPos)
 {
--- a/jssmix.c	Mon Oct 01 03:46:10 2012 +0300
+++ b/jssmix.c	Mon Oct 01 03:48:21 2012 +0300
@@ -27,9 +27,10 @@
     int    mixerID;
     int    outFormat;
     int    outChannels;
-    int    (*jvmMixChannel_FW)(JSSMixer *, JSSChannel *, Sint32 *, const int, const Sint32);
-    int    (*jvmMixChannel_BW)(JSSMixer *, JSSChannel *, Sint32 *, const int, const Sint32);
-    void   (*jvmPostProcess)(Sint32 *, void *, const int);
+
+    int    (*jvmMixChannel_FW)(JSSMixer *, JSSChannel *, JMIXER_ADDBUF_TYPE *, const int, const Sint32);
+    int    (*jvmMixChannel_BW)(JSSMixer *, JSSChannel *, JMIXER_ADDBUF_TYPE *, const int, const Sint32);
+    void   (*jvmPostProcess)(JMIXER_ADDBUF_TYPE *, void *, const int);
 } JSSMixingRoutine;
 
 
@@ -148,7 +149,7 @@
     
     // Allocate addBuffer
     mixer->addBufSize = outChannels * outFreq;
-    mixer->addBuffer = dmCalloc(mixer->addBufSize, sizeof(Sint32));
+    mixer->addBuffer = dmCalloc(mixer->addBufSize, sizeof(JMIXER_ADDBUF_TYPE));
     if (mixer->addBuffer == NULL)
     {
         JSSERROR(DMERR_MALLOC, NULL,
@@ -228,10 +229,10 @@
 }
 
 
-static void jvmMixChannel(JSSMixer *mixer, JSSChannel *chn, Sint32 *addBuffer, const int mixLength)
+static void jvmMixChannel(JSSMixer *mixer, JSSChannel *chn, JMIXER_ADDBUF_TYPE *addBuffer, const int mixLength)
 {
     int mixDone = mixLength, mixResult;
-    Sint32 *ab = addBuffer;
+    JMIXER_ADDBUF_TYPE *ab = addBuffer;
     
     if (!chn->chPlaying || chn->chMute)
         return;
@@ -361,7 +362,7 @@
 void jvmRenderAudio(JSSMixer *mixer, void *mixBuffer, const int mixLength)
 {
     int i, blockLength, mixLeft;
-    Sint32 *ab;
+    JMIXER_ADDBUF_TYPE *ab;
 
     JSS_LOCK(mixer);
 
@@ -371,7 +372,7 @@
     assert(mixLength * mixer->outChannels < mixer->addBufSize);    
 
     // Clear mixer->addBuffer
-    memset(mixer->addBuffer, 0, mixLength * mixer->outChannels * sizeof(Sint32));
+    memset(mixer->addBuffer, 0, mixLength * mixer->outChannels * sizeof(JMIXER_ADDBUF_TYPE));
     
     ab = mixer->addBuffer;
     mixLeft = mixLength;
--- a/jssmix.h	Mon Oct 01 03:46:10 2012 +0300
+++ b/jssmix.h	Mon Oct 01 03:48:21 2012 +0300
@@ -11,6 +11,13 @@
 #define DBG(...) do { /* stub */ } while (0)
 
 
+#ifdef JSS_MIX_FP
+#define JMIXER_ADDBUF_TYPE double
+#else
+#define JMIXER_ADDBUF_TYPE Sint32
+#endif
+
+
 // Mixing limits
 #define JVM_LIMIT_16_POS    (32767)
 #define JVM_LIMIT_16_NEG    (-32767)
@@ -40,7 +47,7 @@
     int     chVolume,       // Volume
             chPanning;      // Panning
 
-    Sint32  chPrevR, chPrevL;
+    JMIXER_ADDBUF_TYPE chPrevR, chPrevL;
 } JSSChannel;
 
 
@@ -60,7 +67,7 @@
     JSSChannel      channels[jsetNChannels];
     
     int             addBufSize;
-    Sint32         *addBuffer;
+    JMIXER_ADDBUF_TYPE *addBuffer;
 
     // Callback handling
     int             cbFreq, cbCounter;
@@ -68,9 +75,9 @@
     void            (*cbFunction)(void *, void *);
 
     // Mixing routine pointers
-    int (*jvmMixChannel_FW)(JSSMixer *, JSSChannel *, Sint32 *, const int, const Sint32);
-    int (*jvmMixChannel_BW)(JSSMixer *, JSSChannel *, Sint32 *, const int, const Sint32);
-    void (*jvmPostProcess)(Sint32 *, void *mixBuffer, int);
+    int    (*jvmMixChannel_FW)(JSSMixer *, JSSChannel *, JMIXER_ADDBUF_TYPE *, const int, const Sint32);
+    int    (*jvmMixChannel_BW)(JSSMixer *, JSSChannel *, JMIXER_ADDBUF_TYPE *, const int, const Sint32);
+    void   (*jvmPostProcess)(JMIXER_ADDBUF_TYPE *, void *, const int);
 
     // Device locking
 #ifdef JSS_SUP_THREADS
--- a/jssmod.c	Mon Oct 01 03:46:10 2012 +0300
+++ b/jssmod.c	Mon Oct 01 03:48:21 2012 +0300
@@ -222,7 +222,7 @@
         Uint8 *in = (Uint8 *) src;
         while (count--)
         {
-            *(out++) = (float) (*(in++) - 128) / 128.0f;
+            *(out++) = ((float) *(in++) - 128.0f) / 56.0f;
         }
     }
     
@@ -280,7 +280,7 @@
                 return res;
             
             inst->flags &= !(jsf16bit);
-            inst->flags |= jfsFP;
+            inst->flags |= jsfFP;
 #else
             if (inst->flags & jsf16bit)
                 continue;