summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/d_math.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/d_math.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/d_math.c573
1 files changed, 0 insertions, 573 deletions
diff --git a/apps/plugins/pdbox/PDa/src/d_math.c b/apps/plugins/pdbox/PDa/src/d_math.c
index ee8a96692c..d64e2e3483 100644
--- a/apps/plugins/pdbox/PDa/src/d_math.c
+++ b/apps/plugins/pdbox/PDa/src/d_math.c
@@ -571,576 +571,3 @@ void d_math_setup(void)
571 class_sethelpsymbol(powtodb_tilde_class, s); 571 class_sethelpsymbol(powtodb_tilde_class, s);
572} 572}
573 573
574/* Copyright (c) 1997-2001 Miller Puckette and others.
575* For information on usage and redistribution, and for a DISCLAIMER OF ALL
576* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
577
578/* mathematical functions and other transfer functions, including tilde
579 versions of stuff from x_acoustics.c.
580*/
581
582#include "m_pd.h"
583#include <math.h>
584#define LOGTEN 2.302585092994
585
586/* ------------------------- clip~ -------------------------- */
587static t_class *clip_class;
588
589typedef struct _clip
590{
591 t_object x_obj;
592 float x_f;
593 t_sample x_lo;
594 t_sample x_hi;
595} t_clip;
596
597static void *clip_new(t_floatarg lo, t_floatarg hi)
598{
599 t_clip *x = (t_clip *)pd_new(clip_class);
600 x->x_lo = lo;
601 x->x_hi = hi;
602 outlet_new(&x->x_obj, gensym("signal"));
603 floatinlet_new(&x->x_obj, &x->x_lo);
604 floatinlet_new(&x->x_obj, &x->x_hi);
605 x->x_f = 0;
606 return (x);
607}
608
609static t_int *clip_perform(t_int *w)
610{
611 t_clip *x = (t_clip *)(w[1]);
612 t_float *in = (t_float *)(w[2]);
613 t_float *out = (t_float *)(w[3]);
614 int n = (int)(w[4]);
615 while (n--)
616 {
617 float f = *in++;
618 if (f < x->x_lo) f = x->x_lo;
619 if (f > x->x_hi) f = x->x_hi;
620 *out++ = f;
621 }
622 return (w+5);
623}
624
625static void clip_dsp(t_clip *x, t_signal **sp)
626{
627 dsp_add(clip_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
628}
629
630static void clip_setup(void)
631{
632 clip_class = class_new(gensym("clip~"), (t_newmethod)clip_new, 0,
633 sizeof(t_clip), 0, A_DEFFLOAT, A_DEFFLOAT, 0);
634 CLASS_MAINSIGNALIN(clip_class, t_clip, x_f);
635 class_addmethod(clip_class, (t_method)clip_dsp, gensym("dsp"), 0);
636}
637
638/* sigrsqrt - reciprocal square root good to 8 mantissa bits */
639
640#define DUMTAB1SIZE 256
641#define DUMTAB2SIZE 1024
642
643static float rsqrt_exptab[DUMTAB1SIZE], rsqrt_mantissatab[DUMTAB2SIZE];
644
645static void init_rsqrt(void)
646{
647 int i;
648 for (i = 0; i < DUMTAB1SIZE; i++)
649 {
650 float f;
651 long l = (i ? (i == DUMTAB1SIZE-1 ? DUMTAB1SIZE-2 : i) : 1)<< 23;
652 *(long *)(&f) = l;
653 rsqrt_exptab[i] = 1./sqrt(f);
654 }
655 for (i = 0; i < DUMTAB2SIZE; i++)
656 {
657 float f = 1 + (1./DUMTAB2SIZE) * i;
658 rsqrt_mantissatab[i] = 1./sqrt(f);
659 }
660}
661
662 /* these are used in externs like "bonk" */
663
664float q8_rsqrt(float f)
665{
666 long l = *(long *)(&f);
667 if (f < 0) return (0);
668 else return (rsqrt_exptab[(l >> 23) & 0xff] *
669 rsqrt_mantissatab[(l >> 13) & 0x3ff]);
670}
671
672float q8_sqrt(float f)
673{
674 long l = *(long *)(&f);
675 if (f < 0) return (0);
676 else return (f * rsqrt_exptab[(l >> 23) & 0xff] *
677 rsqrt_mantissatab[(l >> 13) & 0x3ff]);
678}
679
680 /* the old names are OK unless we're in IRIX N32 */
681
682#ifndef N32
683float qsqrt(float f) {return (q8_sqrt(f)); }
684float qrsqrt(float f) {return (q8_rsqrt(f)); }
685#endif
686
687
688
689typedef struct sigrsqrt
690{
691 t_object x_obj;
692 float x_f;
693} t_sigrsqrt;
694
695static t_class *sigrsqrt_class;
696
697static void *sigrsqrt_new(void)
698{
699 t_sigrsqrt *x = (t_sigrsqrt *)pd_new(sigrsqrt_class);
700 outlet_new(&x->x_obj, gensym("signal"));
701 x->x_f = 0;
702 return (x);
703}
704
705static t_int *sigrsqrt_perform(t_int *w)
706{
707 float *in = *(t_float **)(w+1), *out = *(t_float **)(w+2);
708 t_int n = *(t_int *)(w+3);
709 while (n--)
710 {
711 float f = *in;
712 long l = *(long *)(in++);
713 if (f < 0) *out++ = 0;
714 else
715 {
716 float g = rsqrt_exptab[(l >> 23) & 0xff] *
717 rsqrt_mantissatab[(l >> 13) & 0x3ff];
718 *out++ = 1.5 * g - 0.5 * g * g * g * f;
719 }
720 }
721 return (w + 4);
722}
723
724static void sigrsqrt_dsp(t_sigrsqrt *x, t_signal **sp)
725{
726 dsp_add(sigrsqrt_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
727}
728
729void sigrsqrt_setup(void)
730{
731 init_rsqrt();
732 sigrsqrt_class = class_new(gensym("rsqrt~"), (t_newmethod)sigrsqrt_new, 0,
733 sizeof(t_sigrsqrt), 0, 0);
734 /* an old name for it: */
735 class_addcreator(sigrsqrt_new, gensym("q8_rsqrt~"), 0);
736 CLASS_MAINSIGNALIN(sigrsqrt_class, t_sigrsqrt, x_f);
737 class_addmethod(sigrsqrt_class, (t_method)sigrsqrt_dsp, gensym("dsp"), 0);
738}
739
740
741/* sigsqrt - square root good to 8 mantissa bits */
742
743typedef struct sigsqrt
744{
745 t_object x_obj;
746 float x_f;
747} t_sigsqrt;
748
749static t_class *sigsqrt_class;
750
751static void *sigsqrt_new(void)
752{
753 t_sigsqrt *x = (t_sigsqrt *)pd_new(sigsqrt_class);
754 outlet_new(&x->x_obj, gensym("signal"));
755 x->x_f = 0;
756 return (x);
757}
758
759t_int *sigsqrt_perform(t_int *w) /* not static; also used in d_fft.c */
760{
761 float *in = *(t_float **)(w+1), *out = *(t_float **)(w+2);
762 t_int n = *(t_int *)(w+3);
763 while (n--)
764 {
765 float f = *in;
766 long l = *(long *)(in++);
767 if (f < 0) *out++ = 0;
768 else
769 {
770 float g = rsqrt_exptab[(l >> 23) & 0xff] *
771 rsqrt_mantissatab[(l >> 13) & 0x3ff];
772 *out++ = f * (1.5 * g - 0.5 * g * g * g * f);
773 }
774 }
775 return (w + 4);
776}
777
778static void sigsqrt_dsp(t_sigsqrt *x, t_signal **sp)
779{
780 dsp_add(sigsqrt_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
781}
782
783void sigsqrt_setup(void)
784{
785 sigsqrt_class = class_new(gensym("sqrt~"), (t_newmethod)sigsqrt_new, 0,
786 sizeof(t_sigsqrt), 0, 0);
787 class_addcreator(sigsqrt_new, gensym("q8_sqrt~"), 0); /* old name */
788 CLASS_MAINSIGNALIN(sigsqrt_class, t_sigsqrt, x_f);
789 class_addmethod(sigsqrt_class, (t_method)sigsqrt_dsp, gensym("dsp"), 0);
790}
791
792/* ------------------------------ wrap~ -------------------------- */
793
794typedef struct wrap
795{
796 t_object x_obj;
797 float x_f;
798} t_sigwrap;
799
800t_class *sigwrap_class;
801
802static void *sigwrap_new(void)
803{
804 t_sigwrap *x = (t_sigwrap *)pd_new(sigwrap_class);
805 outlet_new(&x->x_obj, gensym("signal"));
806 x->x_f = 0;
807 return (x);
808}
809
810static t_int *sigwrap_perform(t_int *w)
811{
812 float *in = *(t_float **)(w+1), *out = *(t_float **)(w+2);
813 t_int n = *(t_int *)(w+3);
814 while (n--)
815 {
816 float f = *in++;
817 int k = f;
818 if (f > 0) *out++ = f-k;
819 else *out++ = f - (k-1);
820 }
821 return (w + 4);
822}
823
824static void sigwrap_dsp(t_sigwrap *x, t_signal **sp)
825{
826 dsp_add(sigwrap_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
827}
828
829void sigwrap_setup(void)
830{
831 sigwrap_class = class_new(gensym("wrap~"), (t_newmethod)sigwrap_new, 0,
832 sizeof(t_sigwrap), 0, 0);
833 CLASS_MAINSIGNALIN(sigwrap_class, t_sigwrap, x_f);
834 class_addmethod(sigwrap_class, (t_method)sigwrap_dsp, gensym("dsp"), 0);
835}
836
837/* ------------------------------ mtof_tilde~ -------------------------- */
838
839typedef struct mtof_tilde
840{
841 t_object x_obj;
842 float x_f;
843} t_mtof_tilde;
844
845t_class *mtof_tilde_class;
846
847static void *mtof_tilde_new(void)
848{
849 t_mtof_tilde *x = (t_mtof_tilde *)pd_new(mtof_tilde_class);
850 outlet_new(&x->x_obj, gensym("signal"));
851 x->x_f = 0;
852 return (x);
853}
854
855static t_int *mtof_tilde_perform(t_int *w)
856{
857 float *in = *(t_float **)(w+1), *out = *(t_float **)(w+2);
858 t_int n = *(t_int *)(w+3);
859 for (; n--; in++, out++)
860 {
861 float f = *in;
862 if (f <= -1500) *out = 0;
863 else
864 {
865 if (f > 1499) f = 1499;
866 *out = 8.17579891564 * exp(.0577622650 * f);
867 }
868 }
869 return (w + 4);
870}
871
872static void mtof_tilde_dsp(t_mtof_tilde *x, t_signal **sp)
873{
874 dsp_add(mtof_tilde_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
875}
876
877void mtof_tilde_setup(void)
878{
879 mtof_tilde_class = class_new(gensym("mtof~"), (t_newmethod)mtof_tilde_new, 0,
880 sizeof(t_mtof_tilde), 0, 0);
881 CLASS_MAINSIGNALIN(mtof_tilde_class, t_mtof_tilde, x_f);
882 class_addmethod(mtof_tilde_class, (t_method)mtof_tilde_dsp, gensym("dsp"), 0);
883}
884
885/* ------------------------------ ftom_tilde~ -------------------------- */
886
887typedef struct ftom_tilde
888{
889 t_object x_obj;
890 float x_f;
891} t_ftom_tilde;
892
893t_class *ftom_tilde_class;
894
895static void *ftom_tilde_new(void)
896{
897 t_ftom_tilde *x = (t_ftom_tilde *)pd_new(ftom_tilde_class);
898 outlet_new(&x->x_obj, gensym("signal"));
899 x->x_f = 0;
900 return (x);
901}
902
903static t_int *ftom_tilde_perform(t_int *w)
904{
905 float *in = *(t_float **)(w+1), *out = *(t_float **)(w+2);
906 t_int n = *(t_int *)(w+3);
907 for (; n--; *in++, out++)
908 {
909 float f = *in;
910 *out = (f > 0 ? 17.3123405046 * log(.12231220585 * f) : -1500);
911 }
912 return (w + 4);
913}
914
915static void ftom_tilde_dsp(t_ftom_tilde *x, t_signal **sp)
916{
917 dsp_add(ftom_tilde_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
918}
919
920void ftom_tilde_setup(void)
921{
922 ftom_tilde_class = class_new(gensym("ftom~"), (t_newmethod)ftom_tilde_new, 0,
923 sizeof(t_ftom_tilde), 0, 0);
924 CLASS_MAINSIGNALIN(ftom_tilde_class, t_ftom_tilde, x_f);
925 class_addmethod(ftom_tilde_class, (t_method)ftom_tilde_dsp, gensym("dsp"), 0);
926}
927
928/* ------------------------------ dbtorms~ -------------------------- */
929
930typedef struct dbtorms_tilde
931{
932 t_object x_obj;
933 float x_f;
934} t_dbtorms_tilde;
935
936t_class *dbtorms_tilde_class;
937
938static void *dbtorms_tilde_new(void)
939{
940 t_dbtorms_tilde *x = (t_dbtorms_tilde *)pd_new(dbtorms_tilde_class);
941 outlet_new(&x->x_obj, gensym("signal"));
942 x->x_f = 0;
943 return (x);
944}
945
946static t_int *dbtorms_tilde_perform(t_int *w)
947{
948 float *in = *(t_float **)(w+1), *out = *(t_float **)(w+2);
949 t_int n = *(t_int *)(w+3);
950 for (; n--; in++, out++)
951 {
952 float f = *in;
953 if (f <= 0) *out = 0;
954 else
955 {
956 if (f > 485)
957 f = 485;
958 *out = exp((LOGTEN * 0.05) * (f-100.));
959 }
960 }
961 return (w + 4);
962}
963
964static void dbtorms_tilde_dsp(t_dbtorms_tilde *x, t_signal **sp)
965{
966 dsp_add(dbtorms_tilde_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
967}
968
969void dbtorms_tilde_setup(void)
970{
971 dbtorms_tilde_class = class_new(gensym("dbtorms~"), (t_newmethod)dbtorms_tilde_new, 0,
972 sizeof(t_dbtorms_tilde), 0, 0);
973 CLASS_MAINSIGNALIN(dbtorms_tilde_class, t_dbtorms_tilde, x_f);
974 class_addmethod(dbtorms_tilde_class, (t_method)dbtorms_tilde_dsp, gensym("dsp"), 0);
975}
976
977/* ------------------------------ rmstodb~ -------------------------- */
978
979typedef struct rmstodb_tilde
980{
981 t_object x_obj;
982 float x_f;
983} t_rmstodb_tilde;
984
985t_class *rmstodb_tilde_class;
986
987static void *rmstodb_tilde_new(void)
988{
989 t_rmstodb_tilde *x = (t_rmstodb_tilde *)pd_new(rmstodb_tilde_class);
990 outlet_new(&x->x_obj, gensym("signal"));
991 x->x_f = 0;
992 return (x);
993}
994
995static t_int *rmstodb_tilde_perform(t_int *w)
996{
997 float *in = *(t_float **)(w+1), *out = *(t_float **)(w+2);
998 t_int n = *(t_int *)(w+3);
999 for (; n--; in++, out++)
1000 {
1001 float f = *in;
1002 if (f <= 0) *out = 0;
1003 else
1004 {
1005 float g = 100 + 20./LOGTEN * log(f);
1006 *out = (g < 0 ? 0 : g);
1007 }
1008 }
1009 return (w + 4);
1010}
1011
1012static void rmstodb_tilde_dsp(t_rmstodb_tilde *x, t_signal **sp)
1013{
1014 dsp_add(rmstodb_tilde_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
1015}
1016
1017void rmstodb_tilde_setup(void)
1018{
1019 rmstodb_tilde_class = class_new(gensym("rmstodb~"), (t_newmethod)rmstodb_tilde_new, 0,
1020 sizeof(t_rmstodb_tilde), 0, 0);
1021 CLASS_MAINSIGNALIN(rmstodb_tilde_class, t_rmstodb_tilde, x_f);
1022 class_addmethod(rmstodb_tilde_class, (t_method)rmstodb_tilde_dsp, gensym("dsp"), 0);
1023}
1024
1025/* ------------------------------ dbtopow~ -------------------------- */
1026
1027typedef struct dbtopow_tilde
1028{
1029 t_object x_obj;
1030 float x_f;
1031} t_dbtopow_tilde;
1032
1033t_class *dbtopow_tilde_class;
1034
1035static void *dbtopow_tilde_new(void)
1036{
1037 t_dbtopow_tilde *x = (t_dbtopow_tilde *)pd_new(dbtopow_tilde_class);
1038 outlet_new(&x->x_obj, gensym("signal"));
1039 x->x_f = 0;
1040 return (x);
1041}
1042
1043static t_int *dbtopow_tilde_perform(t_int *w)
1044{
1045 float *in = *(t_float **)(w+1), *out = *(t_float **)(w+2);
1046 t_int n = *(t_int *)(w+3);
1047 for (; n--; in++, out++)
1048 {
1049 float f = *in;
1050 if (f <= 0) *out = 0;
1051 else
1052 {
1053 if (f > 870)
1054 f = 870;
1055 *out = exp((LOGTEN * 0.1) * (f-100.));
1056 }
1057 }
1058 return (w + 4);
1059}
1060
1061static void dbtopow_tilde_dsp(t_dbtopow_tilde *x, t_signal **sp)
1062{
1063 dsp_add(dbtopow_tilde_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
1064}
1065
1066void dbtopow_tilde_setup(void)
1067{
1068 dbtopow_tilde_class = class_new(gensym("dbtopow~"), (t_newmethod)dbtopow_tilde_new, 0,
1069 sizeof(t_dbtopow_tilde), 0, 0);
1070 CLASS_MAINSIGNALIN(dbtopow_tilde_class, t_dbtopow_tilde, x_f);
1071 class_addmethod(dbtopow_tilde_class, (t_method)dbtopow_tilde_dsp, gensym("dsp"), 0);
1072}
1073
1074/* ------------------------------ powtodb~ -------------------------- */
1075
1076typedef struct powtodb_tilde
1077{
1078 t_object x_obj;
1079 float x_f;
1080} t_powtodb_tilde;
1081
1082t_class *powtodb_tilde_class;
1083
1084static void *powtodb_tilde_new(void)
1085{
1086 t_powtodb_tilde *x = (t_powtodb_tilde *)pd_new(powtodb_tilde_class);
1087 outlet_new(&x->x_obj, gensym("signal"));
1088 x->x_f = 0;
1089 return (x);
1090}
1091
1092static t_int *powtodb_tilde_perform(t_int *w)
1093{
1094 float *in = *(t_float **)(w+1), *out = *(t_float **)(w+2);
1095 t_int n = *(t_int *)(w+3);
1096 for (; n--; in++, out++)
1097 {
1098 float f = *in;
1099 if (f <= 0) *out = 0;
1100 else
1101 {
1102 float g = 100 + 10./LOGTEN * log(f);
1103 *out = (g < 0 ? 0 : g);
1104 }
1105 }
1106 return (w + 4);
1107}
1108
1109static void powtodb_tilde_dsp(t_powtodb_tilde *x, t_signal **sp)
1110{
1111 dsp_add(powtodb_tilde_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
1112}
1113
1114void powtodb_tilde_setup(void)
1115{
1116 powtodb_tilde_class = class_new(gensym("powtodb~"), (t_newmethod)powtodb_tilde_new, 0,
1117 sizeof(t_powtodb_tilde), 0, 0);
1118 CLASS_MAINSIGNALIN(powtodb_tilde_class, t_powtodb_tilde, x_f);
1119 class_addmethod(powtodb_tilde_class, (t_method)powtodb_tilde_dsp, gensym("dsp"), 0);
1120}
1121
1122
1123/* ------------------------ global setup routine ------------------------- */
1124
1125void d_math_setup(void)
1126{
1127 t_symbol *s = gensym("acoustics~.pd");
1128 clip_setup();
1129 sigrsqrt_setup();
1130 sigsqrt_setup();
1131 sigwrap_setup();
1132 mtof_tilde_setup();
1133 ftom_tilde_setup();
1134 dbtorms_tilde_setup();
1135 rmstodb_tilde_setup();
1136 dbtopow_tilde_setup();
1137 powtodb_tilde_setup();
1138
1139 class_sethelpsymbol(mtof_tilde_class, s);
1140 class_sethelpsymbol(ftom_tilde_class, s);
1141 class_sethelpsymbol(dbtorms_tilde_class, s);
1142 class_sethelpsymbol(rmstodb_tilde_class, s);
1143 class_sethelpsymbol(dbtopow_tilde_class, s);
1144 class_sethelpsymbol(powtodb_tilde_class, s);
1145}
1146