summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/d_filter.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/d_filter.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/d_filter.c546
1 files changed, 0 insertions, 546 deletions
diff --git a/apps/plugins/pdbox/PDa/src/d_filter.c b/apps/plugins/pdbox/PDa/src/d_filter.c
index 05bb7cd58e..8b81a3a0d3 100644
--- a/apps/plugins/pdbox/PDa/src/d_filter.c
+++ b/apps/plugins/pdbox/PDa/src/d_filter.c
@@ -545,550 +545,4 @@ void d_filter_setup(void)
545 sigbiquad_setup(); 545 sigbiquad_setup();
546 sigsamphold_setup(); 546 sigsamphold_setup();
547} 547}
548/* Copyright (c) 1997-1999 Miller Puckette.
549* For information on usage and redistribution, and for a DISCLAIMER OF ALL
550* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
551
552/* "filters", both linear and nonlinear.
553*/
554#include "m_pd.h"
555#include <math.h>
556
557/* ---------------- hip~ - 1-pole 1-zero hipass filter. ----------------- */
558
559typedef struct hipctl
560{
561 float c_x;
562 float c_coef;
563} t_hipctl;
564
565typedef struct sighip
566{
567 t_object x_obj;
568 float x_sr;
569 float x_hz;
570 t_hipctl x_cspace;
571 t_hipctl *x_ctl;
572 float x_f;
573} t_sighip;
574
575t_class *sighip_class;
576static void sighip_ft1(t_sighip *x, t_floatarg f);
577
578static void *sighip_new(t_floatarg f)
579{
580 t_sighip *x = (t_sighip *)pd_new(sighip_class);
581 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
582 outlet_new(&x->x_obj, gensym("signal"));
583 x->x_sr = 44100;
584 x->x_ctl = &x->x_cspace;
585 x->x_cspace.c_x = 0;
586 sighip_ft1(x, f);
587 x->x_f = 0;
588 return (x);
589}
590
591static void sighip_ft1(t_sighip *x, t_floatarg f)
592{
593 if (f < 0) f = 0;
594 x->x_hz = f;
595 x->x_ctl->c_coef = 1 - f * (2 * 3.14159) / x->x_sr;
596 if (x->x_ctl->c_coef < 0)
597 x->x_ctl->c_coef = 0;
598 else if (x->x_ctl->c_coef > 1)
599 x->x_ctl->c_coef = 1;
600}
601
602static t_int *sighip_perform(t_int *w)
603{
604 float *in = (float *)(w[1]);
605 float *out = (float *)(w[2]);
606 t_hipctl *c = (t_hipctl *)(w[3]);
607 int n = (t_int)(w[4]);
608 int i;
609 float last = c->c_x;
610 float coef = c->c_coef;
611 if (coef < 1)
612 {
613 for (i = 0; i < n; i++)
614 {
615 float new = *in++ + coef * last;
616 *out++ = new - last;
617 last = new;
618 }
619 if (PD_BIGORSMALL(last))
620 last = 0;
621 c->c_x = last;
622 }
623 else
624 {
625 for (i = 0; i < n; i++)
626 *out++ = *in++;
627 c->c_x = 0;
628 }
629 return (w+5);
630}
631
632static void sighip_dsp(t_sighip *x, t_signal **sp)
633{
634 x->x_sr = sp[0]->s_sr;
635 sighip_ft1(x, x->x_hz);
636 dsp_add(sighip_perform, 4,
637 sp[0]->s_vec, sp[1]->s_vec,
638 x->x_ctl, sp[0]->s_n);
639
640}
641
642static void sighip_clear(t_sighip *x, t_floatarg q)
643{
644 x->x_cspace.c_x = 0;
645}
646
647void sighip_setup(void)
648{
649 sighip_class = class_new(gensym("hip~"), (t_newmethod)sighip_new, 0,
650 sizeof(t_sighip), 0, A_DEFFLOAT, 0);
651 CLASS_MAINSIGNALIN(sighip_class, t_sighip, x_f);
652 class_addmethod(sighip_class, (t_method)sighip_dsp, gensym("dsp"), 0);
653 class_addmethod(sighip_class, (t_method)sighip_ft1,
654 gensym("ft1"), A_FLOAT, 0);
655 class_addmethod(sighip_class, (t_method)sighip_clear, gensym("clear"), 0);
656}
657
658/* ---------------- lop~ - 1-pole lopass filter. ----------------- */
659
660typedef struct lopctl
661{
662 float c_x;
663 float c_coef;
664} t_lopctl;
665
666typedef struct siglop
667{
668 t_object x_obj;
669 float x_sr;
670 float x_hz;
671 t_lopctl x_cspace;
672 t_lopctl *x_ctl;
673 float x_f;
674} t_siglop;
675
676t_class *siglop_class;
677
678static void siglop_ft1(t_siglop *x, t_floatarg f);
679
680static void *siglop_new(t_floatarg f)
681{
682 t_siglop *x = (t_siglop *)pd_new(siglop_class);
683 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
684 outlet_new(&x->x_obj, gensym("signal"));
685 x->x_sr = 44100;
686 x->x_ctl = &x->x_cspace;
687 x->x_cspace.c_x = 0;
688 siglop_ft1(x, f);
689 x->x_f = 0;
690 return (x);
691}
692
693static void siglop_ft1(t_siglop *x, t_floatarg f)
694{
695 if (f < 0) f = 0;
696 x->x_hz = f;
697 x->x_ctl->c_coef = f * (2 * 3.14159) / x->x_sr;
698 if (x->x_ctl->c_coef > 1)
699 x->x_ctl->c_coef = 1;
700 else if (x->x_ctl->c_coef < 0)
701 x->x_ctl->c_coef = 0;
702}
703
704static void siglop_clear(t_siglop *x, t_floatarg q)
705{
706 x->x_cspace.c_x = 0;
707}
708
709static t_int *siglop_perform(t_int *w)
710{
711 float *in = (float *)(w[1]);
712 float *out = (float *)(w[2]);
713 t_lopctl *c = (t_lopctl *)(w[3]);
714 int n = (t_int)(w[4]);
715 int i;
716 float last = c->c_x;
717 float coef = c->c_coef;
718 float feedback = 1 - coef;
719 for (i = 0; i < n; i++)
720 last = *out++ = coef * *in++ + feedback * last;
721 if (PD_BIGORSMALL(last))
722 last = 0;
723 c->c_x = last;
724 return (w+5);
725}
726
727static void siglop_dsp(t_siglop *x, t_signal **sp)
728{
729 x->x_sr = sp[0]->s_sr;
730 siglop_ft1(x, x->x_hz);
731 dsp_add(siglop_perform, 4,
732 sp[0]->s_vec, sp[1]->s_vec,
733 x->x_ctl, sp[0]->s_n);
734
735}
736
737void siglop_setup(void)
738{
739 siglop_class = class_new(gensym("lop~"), (t_newmethod)siglop_new, 0,
740 sizeof(t_siglop), 0, A_DEFFLOAT, 0);
741 CLASS_MAINSIGNALIN(siglop_class, t_siglop, x_f);
742 class_addmethod(siglop_class, (t_method)siglop_dsp, gensym("dsp"), 0);
743 class_addmethod(siglop_class, (t_method)siglop_ft1,
744 gensym("ft1"), A_FLOAT, 0);
745 class_addmethod(siglop_class, (t_method)siglop_clear, gensym("clear"), 0);
746}
747
748/* ---------------- bp~ - 2-pole bandpass filter. ----------------- */
749
750typedef struct bpctl
751{
752 float c_x1;
753 float c_x2;
754 float c_coef1;
755 float c_coef2;
756 float c_gain;
757} t_bpctl;
758
759typedef struct sigbp
760{
761 t_object x_obj;
762 float x_sr;
763 float x_freq;
764 float x_q;
765 t_bpctl x_cspace;
766 t_bpctl *x_ctl;
767 float x_f;
768} t_sigbp;
769
770t_class *sigbp_class;
771
772static void sigbp_docoef(t_sigbp *x, t_floatarg f, t_floatarg q);
773
774static void *sigbp_new(t_floatarg f, t_floatarg q)
775{
776 t_sigbp *x = (t_sigbp *)pd_new(sigbp_class);
777 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
778 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft2"));
779 outlet_new(&x->x_obj, gensym("signal"));
780 x->x_sr = 44100;
781 x->x_ctl = &x->x_cspace;
782 x->x_cspace.c_x1 = 0;
783 x->x_cspace.c_x2 = 0;
784 sigbp_docoef(x, f, q);
785 x->x_f = 0;
786 return (x);
787}
788
789static float sigbp_qcos(float f)
790{
791 if (f >= -(0.5f*3.14159f) && f <= 0.5f*3.14159f)
792 {
793 float g = f*f;
794 return (((g*g*g * (-1.0f/720.0f) + g*g*(1.0f/24.0f)) - g*0.5) + 1);
795 }
796 else return (0);
797}
798
799static void sigbp_docoef(t_sigbp *x, t_floatarg f, t_floatarg q)
800{
801 float r, oneminusr, omega;
802 if (f < 0.001) f = 10;
803 if (q < 0) q = 0;
804 x->x_freq = f;
805 x->x_q = q;
806 omega = f * (2.0f * 3.14159f) / x->x_sr;
807 if (q < 0.001) oneminusr = 1.0f;
808 else oneminusr = omega/q;
809 if (oneminusr > 1.0f) oneminusr = 1.0f;
810 r = 1.0f - oneminusr;
811 x->x_ctl->c_coef1 = 2.0f * sigbp_qcos(omega) * r;
812 x->x_ctl->c_coef2 = - r * r;
813 x->x_ctl->c_gain = 2 * oneminusr * (oneminusr + r * omega);
814 /* post("r %f, omega %f, coef1 %f, coef2 %f",
815 r, omega, x->x_ctl->c_coef1, x->x_ctl->c_coef2); */
816}
817
818static void sigbp_ft1(t_sigbp *x, t_floatarg f)
819{
820 sigbp_docoef(x, f, x->x_q);
821}
822
823static void sigbp_ft2(t_sigbp *x, t_floatarg q)
824{
825 sigbp_docoef(x, x->x_freq, q);
826}
827
828static void sigbp_clear(t_sigbp *x, t_floatarg q)
829{
830 x->x_ctl->c_x1 = x->x_ctl->c_x2 = 0;
831}
832 548
833static t_int *sigbp_perform(t_int *w)
834{
835 float *in = (float *)(w[1]);
836 float *out = (float *)(w[2]);
837 t_bpctl *c = (t_bpctl *)(w[3]);
838 int n = (t_int)(w[4]);
839 int i;
840 float last = c->c_x1;
841 float prev = c->c_x2;
842 float coef1 = c->c_coef1;
843 float coef2 = c->c_coef2;
844 float gain = c->c_gain;
845 for (i = 0; i < n; i++)
846 {
847 float output = *in++ + coef1 * last + coef2 * prev;
848 *out++ = gain * output;
849 prev = last;
850 last = output;
851 }
852 if (PD_BIGORSMALL(last))
853 last = 0;
854 if (PD_BIGORSMALL(prev))
855 prev = 0;
856 c->c_x1 = last;
857 c->c_x2 = prev;
858 return (w+5);
859}
860
861static void sigbp_dsp(t_sigbp *x, t_signal **sp)
862{
863 x->x_sr = sp[0]->s_sr;
864 sigbp_docoef(x, x->x_freq, x->x_q);
865 dsp_add(sigbp_perform, 4,
866 sp[0]->s_vec, sp[1]->s_vec,
867 x->x_ctl, sp[0]->s_n);
868
869}
870
871void sigbp_setup(void)
872{
873 sigbp_class = class_new(gensym("bp~"), (t_newmethod)sigbp_new, 0,
874 sizeof(t_sigbp), 0, A_DEFFLOAT, A_DEFFLOAT, 0);
875 CLASS_MAINSIGNALIN(sigbp_class, t_sigbp, x_f);
876 class_addmethod(sigbp_class, (t_method)sigbp_dsp, gensym("dsp"), 0);
877 class_addmethod(sigbp_class, (t_method)sigbp_ft1,
878 gensym("ft1"), A_FLOAT, 0);
879 class_addmethod(sigbp_class, (t_method)sigbp_ft2,
880 gensym("ft2"), A_FLOAT, 0);
881 class_addmethod(sigbp_class, (t_method)sigbp_clear, gensym("clear"), 0);
882}
883
884/* ---------------- biquad~ - raw biquad filter ----------------- */
885
886typedef struct biquadctl
887{
888 float c_x1;
889 float c_x2;
890 float c_fb1;
891 float c_fb2;
892 float c_ff1;
893 float c_ff2;
894 float c_ff3;
895} t_biquadctl;
896
897typedef struct sigbiquad
898{
899 t_object x_obj;
900 float x_f;
901 t_biquadctl x_cspace;
902 t_biquadctl *x_ctl;
903} t_sigbiquad;
904
905t_class *sigbiquad_class;
906
907static void sigbiquad_list(t_sigbiquad *x, t_symbol *s, int argc, t_atom *argv);
908
909static void *sigbiquad_new(t_symbol *s, int argc, t_atom *argv)
910{
911 t_sigbiquad *x = (t_sigbiquad *)pd_new(sigbiquad_class);
912 outlet_new(&x->x_obj, gensym("signal"));
913 x->x_ctl = &x->x_cspace;
914 x->x_cspace.c_x1 = x->x_cspace.c_x2 = 0;
915 sigbiquad_list(x, s, argc, argv);
916 x->x_f = 0;
917 return (x);
918}
919
920static t_int *sigbiquad_perform(t_int *w)
921{
922 float *in = (float *)(w[1]);
923 float *out = (float *)(w[2]);
924 t_biquadctl *c = (t_biquadctl *)(w[3]);
925 int n = (t_int)(w[4]);
926 int i;
927 float last = c->c_x1;
928 float prev = c->c_x2;
929 float fb1 = c->c_fb1;
930 float fb2 = c->c_fb2;
931 float ff1 = c->c_ff1;
932 float ff2 = c->c_ff2;
933 float ff3 = c->c_ff3;
934 for (i = 0; i < n; i++)
935 {
936 float output = *in++ + fb1 * last + fb2 * prev;
937 if (PD_BIGORSMALL(output))
938 output = 0;
939 *out++ = ff1 * output + ff2 * last + ff3 * prev;
940 prev = last;
941 last = output;
942 }
943 c->c_x1 = last;
944 c->c_x2 = prev;
945 return (w+5);
946}
947
948static void sigbiquad_list(t_sigbiquad *x, t_symbol *s, int argc, t_atom *argv)
949{
950 float fb1 = atom_getfloatarg(0, argc, argv);
951 float fb2 = atom_getfloatarg(1, argc, argv);
952 float ff1 = atom_getfloatarg(2, argc, argv);
953 float ff2 = atom_getfloatarg(3, argc, argv);
954 float ff3 = atom_getfloatarg(4, argc, argv);
955 float discriminant = fb1 * fb1 + 4 * fb2;
956 t_biquadctl *c = x->x_ctl;
957 if (discriminant < 0) /* imaginary roots -- resonant filter */
958 {
959 /* they're conjugates so we just check that the product
960 is less than one */
961 if (fb2 >= -1.0f) goto stable;
962 }
963 else /* real roots */
964 {
965 /* check that the parabola 1 - fb1 x - fb2 x^2 has a
966 vertex between -1 and 1, and that it's nonnegative
967 at both ends, which implies both roots are in [1-,1]. */
968 if (fb1 <= 2.0f && fb1 >= -2.0f &&
969 1.0f - fb1 -fb2 >= 0 && 1.0f + fb1 - fb2 >= 0)
970 goto stable;
971 }
972 /* if unstable, just bash to zero */
973 fb1 = fb2 = ff1 = ff2 = ff3 = 0;
974stable:
975 c->c_fb1 = fb1;
976 c->c_fb2 = fb2;
977 c->c_ff1 = ff1;
978 c->c_ff2 = ff2;
979 c->c_ff3 = ff3;
980}
981
982static void sigbiquad_set(t_sigbiquad *x, t_symbol *s, int argc, t_atom *argv)
983{
984 t_biquadctl *c = x->x_ctl;
985 c->c_x1 = atom_getfloatarg(0, argc, argv);
986 c->c_x2 = atom_getfloatarg(1, argc, argv);
987}
988
989static void sigbiquad_dsp(t_sigbiquad *x, t_signal **sp)
990{
991 dsp_add(sigbiquad_perform, 4,
992 sp[0]->s_vec, sp[1]->s_vec,
993 x->x_ctl, sp[0]->s_n);
994
995}
996
997void sigbiquad_setup(void)
998{
999 sigbiquad_class = class_new(gensym("biquad~"), (t_newmethod)sigbiquad_new,
1000 0, sizeof(t_sigbiquad), 0, A_GIMME, 0);
1001 CLASS_MAINSIGNALIN(sigbiquad_class, t_sigbiquad, x_f);
1002 class_addmethod(sigbiquad_class, (t_method)sigbiquad_dsp, gensym("dsp"), 0);
1003 class_addlist(sigbiquad_class, sigbiquad_list);
1004 class_addmethod(sigbiquad_class, (t_method)sigbiquad_set, gensym("set"),
1005 A_GIMME, 0);
1006 class_addmethod(sigbiquad_class, (t_method)sigbiquad_set, gensym("clear"),
1007 A_GIMME, 0);
1008}
1009
1010/* ---------------- samphold~ - sample and hold ----------------- */
1011
1012typedef struct sigsamphold
1013{
1014 t_object x_obj;
1015 float x_f;
1016 float x_lastin;
1017 float x_lastout;
1018} t_sigsamphold;
1019
1020t_class *sigsamphold_class;
1021
1022static void *sigsamphold_new(void)
1023{
1024 t_sigsamphold *x = (t_sigsamphold *)pd_new(sigsamphold_class);
1025 inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
1026 outlet_new(&x->x_obj, gensym("signal"));
1027 x->x_lastin = 0;
1028 x->x_lastout = 0;
1029 x->x_f = 0;
1030 return (x);
1031}
1032
1033static t_int *sigsamphold_perform(t_int *w)
1034{
1035 float *in1 = (float *)(w[1]);
1036 float *in2 = (float *)(w[2]);
1037 float *out = (float *)(w[3]);
1038 t_sigsamphold *x = (t_sigsamphold *)(w[4]);
1039 int n = (t_int)(w[5]);
1040 int i;
1041 float lastin = x->x_lastin;
1042 float lastout = x->x_lastout;
1043 for (i = 0; i < n; i++, *in1++)
1044 {
1045 float next = *in2++;
1046 if (next < lastin) lastout = *in1;
1047 *out++ = lastout;
1048 lastin = next;
1049 }
1050 x->x_lastin = lastin;
1051 x->x_lastout = lastout;
1052 return (w+6);
1053}
1054
1055static void sigsamphold_dsp(t_sigsamphold *x, t_signal **sp)
1056{
1057 dsp_add(sigsamphold_perform, 5,
1058 sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec,
1059 x, sp[0]->s_n);
1060}
1061
1062static void sigsamphold_reset(t_sigsamphold *x)
1063{
1064 x->x_lastin = 1e20;
1065}
1066
1067static void sigsamphold_set(t_sigsamphold *x, t_float f)
1068{
1069 x->x_lastout = f;
1070}
1071
1072void sigsamphold_setup(void)
1073{
1074 sigsamphold_class = class_new(gensym("samphold~"),
1075 (t_newmethod)sigsamphold_new, 0, sizeof(t_sigsamphold), 0, 0);
1076 CLASS_MAINSIGNALIN(sigsamphold_class, t_sigsamphold, x_f);
1077 class_addmethod(sigsamphold_class, (t_method)sigsamphold_set,
1078 gensym("set"), A_FLOAT, 0);
1079 class_addmethod(sigsamphold_class, (t_method)sigsamphold_reset,
1080 gensym("reset"), 0);
1081 class_addmethod(sigsamphold_class, (t_method)sigsamphold_dsp,
1082 gensym("dsp"), 0);
1083}
1084
1085/* ------------------------ setup routine ------------------------- */
1086
1087void d_filter_setup(void)
1088{
1089 sighip_setup();
1090 siglop_setup();
1091 sigbp_setup();
1092 sigbiquad_setup();
1093 sigsamphold_setup();
1094}