@@ -189,23 +189,35 @@ syn123_soft_clip( void *buf, int encoding, size_t samples
189189 if (!mixenc || !mixframe || !inframe )
190190 return 0 ;
191191 // Use the whole workbuf, both halves.
192- int mbufblock = 2 * bufblock * sizeof (double )/mixframe ;
192+ int mbufblock = sizeof (sh -> workbuf )/mixframe ;
193193 mdebug ("mbufblock=%i (enc %i)" , mbufblock , mixenc );
194194 while (samples )
195195 {
196196 int block = (int )smin (samples , mbufblock );
197- int err = syn123_conv (
198- sh -> workbuf , mixenc , sizeof (sh -> workbuf )
199- , cbuf , encoding , inframe * block
200- , NULL , NULL , NULL );
197+ int err = mixenc == MPG123_ENC_FLOAT_64
198+ ? syn123_conv (
199+ sh -> workbuf .f64 , mixenc , sizeof (sh -> workbuf .f64 )
200+ , cbuf , encoding , inframe * block
201+ , NULL , NULL , NULL )
202+ : syn123_conv (
203+ sh -> workbuf .f32 , mixenc , sizeof (sh -> workbuf .f32 )
204+ , cbuf , encoding , inframe * block
205+ , NULL , NULL , NULL );
201206 if (!err )
202207 {
203- clipped += syn123_soft_clip (sh -> workbuf , mixenc , block , limit , width , NULL );
208+ clipped += mixenc == MPG123_ENC_FLOAT_64
209+ ? syn123_soft_clip (sh -> workbuf .f64 , mixenc , block , limit , width , NULL )
210+ : syn123_soft_clip (sh -> workbuf .f32 , mixenc , block , limit , width , NULL );
204211 // No additional clipping can happen here.
205- err = syn123_conv (
206- cbuf , encoding , inframe * block
207- , sh -> workbuf , mixenc , mixframe * block
208- , NULL , NULL , NULL );
212+ err = mixenc == MPG123_ENC_FLOAT_64
213+ ? syn123_conv (
214+ cbuf , encoding , inframe * block
215+ , sh -> workbuf .f64 , mixenc , mixframe * block
216+ , NULL , NULL , NULL )
217+ : syn123_conv (
218+ cbuf , encoding , inframe * block
219+ , sh -> workbuf .f32 , mixenc , mixframe * block
220+ , NULL , NULL , NULL );
209221 }
210222 if (err )
211223 {
@@ -542,7 +554,7 @@ syn123_conv( void * MPG123_RESTRICT dst, int dst_enc, size_t dst_size
542554 if (!mixenc || !mixframe )
543555 return SYN123_BAD_CONV ;
544556 // Use the whole workbuf, both halves.
545- int mbufblock = 2 * bufblock * sizeof (double )/mixframe ;
557+ int mbufblock = sizeof (sh -> workbuf )/mixframe ;
546558 mdebug ("mbufblock=%i (enc %i)" , mbufblock , mixenc );
547559 // Abuse the handle workbuf for intermediate storage.
548560 size_t samples_left = samples ;
@@ -555,16 +567,26 @@ syn123_conv( void * MPG123_RESTRICT dst, int dst_enc, size_t dst_size
555567 {
556568 int block = (int )smin (samples_left , mbufblock );
557569 size_t clipped_now = 0 ;
558- int err = syn123_conv (
559- sh -> workbuf , mixenc , sizeof (sh -> workbuf )
560- , csrc , src_enc , srcframe * block
561- , NULL , NULL , NULL );
570+ int err = mixenc == MPG123_ENC_FLOAT_64
571+ ? syn123_conv (
572+ sh -> workbuf .f64 , mixenc , sizeof (sh -> workbuf .f64 )
573+ , csrc , src_enc , srcframe * block
574+ , NULL , NULL , NULL )
575+ : syn123_conv (
576+ sh -> workbuf .f32 , mixenc , sizeof (sh -> workbuf .f32 )
577+ , csrc , src_enc , srcframe * block
578+ , NULL , NULL , NULL );
562579 sh -> do_dither = do_dither ; // possibly dither now
563580 if (!err )
564- err = syn123_conv (
565- cdst , dst_enc , dstframe * block
566- , sh -> workbuf , mixenc , mixframe * block
567- , NULL , & clipped_now , sh );
581+ err = mixenc == MPG123_ENC_FLOAT_64
582+ ? syn123_conv (
583+ cdst , dst_enc , dstframe * block
584+ , sh -> workbuf .f64 , mixenc , mixframe * block
585+ , NULL , & clipped_now , sh )
586+ : syn123_conv (
587+ cdst , dst_enc , dstframe * block
588+ , sh -> workbuf .f32 , mixenc , mixframe * block
589+ , NULL , & clipped_now , sh );
568590 if (err )
569591 {
570592 mdebug ("conv error: %i" , err );
@@ -861,6 +883,38 @@ static void syn123_mix_f64( double * MPG123_RESTRICT dst, int dst_channels
861883 SYN123_MIX_FUNC (double )
862884}
863885
886+ // Call with f64 or f32 for conversion mixer code piece.
887+ #define CONVMIX (fXX ) \
888+ err = syn123_conv( \
889+ sh->workbuf.fXX[0], mixenc, sizeof(sh->workbuf.fXX[0]) \
890+ , csrc, src_enc, srcframe*block \
891+ , NULL, NULL, NULL ); \
892+ if(err) \
893+ goto mix_end; \
894+ /* Initialize to zero or convert from old output signal. */ \
895+ if (silence ) \
896+ for (int i = 0 ; i < block * dst_channels ; ++ i ) \
897+ sh -> workbuf .fXX [1 ][i ] = 0. ; \
898+ else \
899+ err = syn123_conv ( \
900+ sh -> workbuf .fXX [1 ], mixenc , sizeof (sh -> workbuf .fXX [1 ]) \
901+ , cdst , dst_enc , dstframe * block \
902+ , NULL , NULL , NULL ); \
903+ /* Now mix one work buffer into the other. */ \
904+ if (err ) \
905+ goto mix_end ; \
906+ err = syn123_mix ( sh -> workbuf .fXX [1 ], mixenc , dst_channels \
907+ , sh -> workbuf .fXX [0 ], mixenc , src_channels , mixmatrix , block \
908+ , 0 , NULL , NULL ); \
909+ if (err ) \
910+ goto mix_end ; \
911+ /* And convert to the final output format. */ \
912+ err = syn123_conv ( \
913+ cdst , dst_enc , dstframe * block \
914+ , sh -> workbuf .fXX [1 ], mixenc , mixoutframe * block \
915+ , NULL , & clips_block , NULL );
916+
917+
864918int attribute_align_arg
865919syn123_mix ( void * MPG123_RESTRICT dst , int dst_enc , int dst_channels
866920, void * MPG123_RESTRICT src , int src_enc , int src_channels
@@ -926,7 +980,7 @@ syn123_mix( void * MPG123_RESTRICT dst, int dst_enc, int dst_channels
926980 goto mix_end ;
927981 }
928982 // Mix from buffblock[0] to buffblock[1].
929- int mbufblock = bufblock * sizeof (double ) /mixframe ;
983+ int mbufblock = sizeof (sh -> workbuf )/ 2 /mixframe ;
930984 mdebug ("mbufblock=%i (enc %i)" , mbufblock , mixenc );
931985 // Need at least one sample per round to avoid endless loop.
932986 // Of course, we would prefer more, but it's your fault for
@@ -940,38 +994,15 @@ syn123_mix( void * MPG123_RESTRICT dst, int dst_enc, int dst_channels
940994 while (samples )
941995 {
942996 int block = (int )smin (samples , mbufblock );
943- err = syn123_conv ( sh -> workbuf [0 ], mixenc , sizeof (sh -> workbuf [0 ])
944- , csrc , src_enc , srcframe * block
945- , NULL , NULL , NULL );
946- if (err )
947- goto mix_end ;
948- // Initialize to zero or convert from old output signal.
949- if (silence )
997+ size_t clips_block = 0 ;
998+ // The same logic for f64 and f32, just written explictly for buffer type clarity.
999+ if (mixenc == MPG123_ENC_FLOAT_64 )
1000+ {
1001+ CONVMIX (f64 )
1002+ } else
9501003 {
951- if (mixenc == MPG123_ENC_FLOAT_32 )
952- for (int i = 0 ; i < block * dst_channels ; ++ i )
953- ((float * )(sh -> workbuf [1 ]))[i ] = 0. ;
954- else
955- for (int i = 0 ; i < block * dst_channels ; ++ i )
956- sh -> workbuf [1 ][i ] = 0. ;
1004+ CONVMIX (f32 )
9571005 }
958- else
959- err = syn123_conv ( sh -> workbuf [1 ], mixenc , sizeof (sh -> workbuf [1 ])
960- , cdst , dst_enc , dstframe * block
961- , NULL , NULL , NULL );
962- // Now mix one work buffer into the other.
963- if (!err )
964- err = syn123_mix ( sh -> workbuf [1 ], mixenc , dst_channels
965- , sh -> workbuf [0 ], mixenc , src_channels , mixmatrix , block
966- , 0 , NULL , NULL );
967- if (err )
968- goto mix_end ;
969- // And convert to the final output format.
970- size_t clips_block = 0 ;
971- err = syn123_conv (
972- cdst , dst_enc , dstframe * block
973- , sh -> workbuf [1 ], mixenc , mixoutframe * block
974- , NULL , & clips_block , NULL );
9751006 clips += clips_block ;
9761007 if (err )
9771008 goto mix_end ;
0 commit comments