vdsp 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f53e044e333e5757fde388054223a280d2fe15ac5ec165ae8cb5d1145b4751ff
4
- data.tar.gz: 245581561fbf5ef0083f4596629a732e14f6cc49b7e72901d386a543c6e331e4
3
+ metadata.gz: a5d8f8b6a88c276a1379130a5714a2021405272cce868a929dc298e8fe53ffcd
4
+ data.tar.gz: a2519330af53222ed133f6acdf0b4d5fde5cdfe451a0d61f662a3d2c99db8405
5
5
  SHA512:
6
- metadata.gz: e236a62a90d732d8e399acef737fafbeca4621d4f2a5cfc0a4cdd365ad0dc5ace6910b94aa4db03a743554f5a34434a9f53f615eb98aa13a7c3c3a79a299e61a
7
- data.tar.gz: 8003b2c57bc0a67849dd5a005fe376d89bc094afe13a8911a2e05f21f72fcb17decd8a16d5479cb8d749048ca89d05521713220a3d98b7d9777874e11523b596
6
+ metadata.gz: 6557320864d0952107d09d705fd19fd1584e78cabacbada432b941a474de14ef0847cfaf5ffa7d468a1a4f8e678c63c74146222c8541562c5912cc7859cf1247
7
+ data.tar.gz: f346a56cca2958994da7d367bbfc788fb9a6aba0409e8475a4159af149ce0b80fa0f69dae7bb77162ea2c123d4fe179c4ec1af28d1b9d6d0d9e7b7061f1a7930
data/ext/vdsp/vdsp.c CHANGED
@@ -5,18 +5,21 @@ VALUE rb_mVdsp;
5
5
 
6
6
  VALUE rb_mVdspScalar;
7
7
  VALUE rb_mVdspArray;
8
+ VALUE rb_mVdspBiquad;
8
9
 
9
10
  VALUE rb_cDoubleScalar;
10
11
  VALUE rb_cDoubleArray;
11
12
  VALUE rb_mUnsafeDouble;
13
+ VALUE rb_cDoubleBiquad;
12
14
 
13
15
 
14
- // Native resource
16
+ // Array native resource
15
17
 
16
18
  void vdsp_array_native_resource_delete(VdspArrayNativeResource * p)
17
19
  {
18
20
  if (p->v.ptr) {
19
21
  free(p->v.ptr);
22
+ p->v.ptr = NULL;
20
23
  }
21
24
  free(p);
22
25
  }
@@ -38,23 +41,6 @@ VdspArrayNativeResource* get_vdsp_array_native_resource(VALUE va)
38
41
  return p;
39
42
  }
40
43
 
41
- VdspArrayValue get_vdsp_array_value(VALUE va)
42
- {
43
- VdspArrayNativeResource *p = get_vdsp_array_native_resource(va);
44
- return p->v;
45
- }
46
-
47
- double* get_double_array_value(VALUE va)
48
- {
49
- return get_vdsp_array_value(va).d;
50
- }
51
-
52
- long get_vdsp_array_length(VALUE va)
53
- {
54
- VdspArrayNativeResource *_a = get_vdsp_array_native_resource(va);
55
- return _a->length;
56
- }
57
-
58
44
  void array_param(VdspArrayParam *param, VALUE arr0, VALUE offset, VALUE stride)
59
45
  {
60
46
  param->res0 = get_vdsp_array_native_resource(arr0);
@@ -71,6 +57,54 @@ void array_param2(VdspArrayParam *param, VALUE arr0, VALUE arr1, VALUE offset, V
71
57
  }
72
58
 
73
59
 
60
+ // Biquad native resource
61
+
62
+ void vdsp_biquad_native_resource_delete(VdspBiquadNativeResource * p)
63
+ {
64
+ if (p->setup.ptr) {
65
+ if (p->type=='d') {
66
+ vDSP_biquad_DestroySetupD(p->setup.d);
67
+ p->setup.ptr = NULL;
68
+ }
69
+ }
70
+ if (p->delay.ptr) {
71
+ free(p->delay.ptr);
72
+ p->delay.ptr = NULL;
73
+ }
74
+ free(p);
75
+ }
76
+
77
+ VdspBiquadNativeResource* get_vdsp_biquad_native_resource(VALUE vb)
78
+ {
79
+ if (!rb_obj_is_kind_of(vb, rb_mVdspBiquad)) {
80
+ rb_raise(rb_eArgError, "Vdsp::Biquad required");
81
+ }
82
+
83
+ VALUE resource = rb_iv_get(vb, "native_resource");
84
+ if (resource==Qnil) {
85
+ return NULL;
86
+ }
87
+
88
+ VdspBiquadNativeResource *p;
89
+ Data_Get_Struct(resource, VdspBiquadNativeResource, p);
90
+
91
+ return p;
92
+ }
93
+
94
+ VdspBiquadSetup get_vdsp_biquad_setup(VALUE vb)
95
+ {
96
+ VdspBiquadNativeResource *p = get_vdsp_biquad_native_resource(vb);
97
+ return p->setup;
98
+ }
99
+
100
+ long get_vdsp_biquad_sections(VALUE vb)
101
+ {
102
+ VdspBiquadNativeResource *_b = get_vdsp_biquad_native_resource(vb);
103
+ return _b->sections;
104
+ }
105
+
106
+
107
+
74
108
  // Vdsp::DoubleScalar
75
109
 
76
110
  VALUE rb_double_scalar_initialize(VALUE self, VALUE val)
@@ -128,7 +162,8 @@ VALUE rb_double_scalar_div(VALUE self, VALUE other)
128
162
 
129
163
  VALUE rb_vdsp_array_length(VALUE self)
130
164
  {
131
- return LONG2NUM(get_vdsp_array_length(self));
165
+ VdspArrayNativeResource *p = get_vdsp_array_native_resource(self);
166
+ return LONG2NUM(p->length);
132
167
  }
133
168
 
134
169
 
@@ -621,6 +656,97 @@ VALUE rb_double_array_svs(VALUE self)
621
656
  }
622
657
 
623
658
 
659
+ // Vdsp::Biquad
660
+
661
+ VALUE rb_vdsp_biquad_sections(VALUE self)
662
+ {
663
+ return LONG2NUM(get_vdsp_biquad_sections(self));
664
+ }
665
+
666
+
667
+ // Vdsp::DoubleBiquad
668
+
669
+ VALUE rb_double_biquad_initialize(VALUE self, VALUE coefficients)
670
+ {
671
+ coefficients = rb_ary_new3(1, coefficients);
672
+ coefficients = rb_funcall(coefficients, rb_intern("flatten"), 0);
673
+
674
+ VALUE rb_cBiquadCoefficient = rb_const_get(rb_mVdspBiquad, rb_intern("Coefficient"));
675
+
676
+ long sections = RARRAY_LEN(coefficients);
677
+ for (long i=0; i<sections; i++) {
678
+ VALUE coefficient = RARRAY_AREF(coefficients, i);
679
+ if (!rb_obj_is_kind_of(coefficient, rb_cBiquadCoefficient)) {
680
+ rb_raise(rb_eArgError, "Vdsp::Biquad::Coefficient required");
681
+ }
682
+ }
683
+
684
+ VdspBiquadNativeResource *p = ALLOC(VdspBiquadNativeResource);
685
+ p->type = 'd';
686
+ p->coefs.ptr = NULL;
687
+ p->delay.ptr = NULL;
688
+ p->setup.ptr = NULL;
689
+ p->sections = 0;
690
+
691
+ VALUE resource = Data_Wrap_Struct(CLASS_OF(self), 0, vdsp_biquad_native_resource_delete, p);
692
+ rb_iv_set(self, "native_resource", resource);
693
+
694
+ p->coefs.ptr = calloc(sections*5, sizeof(double));
695
+ for (long i=0; i<sections; i++) {
696
+ VALUE coefficient = RARRAY_AREF(coefficients, i);
697
+ p->coefs.d[i*5+0] = NUM2DBL(rb_funcall(coefficient, rb_intern("b0"), 0));
698
+ p->coefs.d[i*5+1] = NUM2DBL(rb_funcall(coefficient, rb_intern("b1"), 0));
699
+ p->coefs.d[i*5+2] = NUM2DBL(rb_funcall(coefficient, rb_intern("b2"), 0));
700
+ p->coefs.d[i*5+3] = NUM2DBL(rb_funcall(coefficient, rb_intern("a1"), 0));
701
+ p->coefs.d[i*5+4] = NUM2DBL(rb_funcall(coefficient, rb_intern("a2"), 0));
702
+ }
703
+ p->delay.ptr = calloc(sections*2+2, sizeof(double));
704
+ p->setup.d = vDSP_biquad_CreateSetupD(p->coefs.d, sections);
705
+ p->sections = sections;
706
+
707
+ return self;
708
+ }
709
+
710
+ VALUE rb_double_biquad_apply(VALUE self, VALUE x)
711
+ {
712
+ //x = rb_funcall(x, rb_intern("to_da"), 0);
713
+ x = rb_funcall(rb_cDoubleArray, rb_intern("create"), 1, x);
714
+ VdspArrayNativeResource *_x = get_vdsp_array_native_resource(x);
715
+
716
+ VALUE lenv = LONG2NUM(_x->length);
717
+ VALUE y = rb_class_new_instance(1, &lenv, rb_cDoubleArray);
718
+ VdspArrayNativeResource *_y = get_vdsp_array_native_resource(y);
719
+
720
+ VdspBiquadNativeResource *_b = get_vdsp_biquad_native_resource(self);
721
+
722
+ vDSP_biquadD(_b->setup.d, _b->delay.d, _x->v.d, 1, _y->v.d, 1, _x->length);
723
+
724
+ return y;
725
+ }
726
+
727
+ VALUE rb_double_biquad_coefficients(VALUE self)
728
+ {
729
+ VdspBiquadNativeResource *p = get_vdsp_biquad_native_resource(self);
730
+ VALUE ret = rb_ary_new2(p->sections);
731
+
732
+ VALUE rb_cBiquadCoefficient = rb_const_get(rb_mVdspBiquad, rb_intern("Coefficient"));
733
+
734
+ for (unsigned long i=0; i<p->sections; i++) {
735
+ VALUE argv[5] = {
736
+ DBL2NUM(p->coefs.d[i*5+0]),
737
+ DBL2NUM(p->coefs.d[i*5+1]),
738
+ DBL2NUM(p->coefs.d[i*5+2]),
739
+ DBL2NUM(p->coefs.d[i*5+3]),
740
+ DBL2NUM(p->coefs.d[i*5+4])
741
+ };
742
+ VALUE coef = rb_class_new_instance(5, argv, rb_cBiquadCoefficient);
743
+ rb_ary_push(ret, coef);
744
+ }
745
+
746
+ return ret;
747
+ }
748
+
749
+
624
750
  // Vdsp static method
625
751
 
626
752
  // c[i] = a[i] + b
@@ -1921,6 +2047,31 @@ VALUE rb_double_svs(
1921
2047
  return DBL2NUM(_c);
1922
2048
  }
1923
2049
 
2050
+ VALUE rb_double_biquad(
2051
+ VALUE cls,
2052
+ VALUE biquad,
2053
+ VALUE x, VALUE x_offset, VALUE x_stride,
2054
+ VALUE y, VALUE y_offset, VALUE y_stride,
2055
+ VALUE n)
2056
+ {
2057
+ VdspArrayParam _x;
2058
+ VdspArrayParam _y;
2059
+
2060
+ VdspBiquadNativeResource *_b = get_vdsp_biquad_native_resource(biquad);
2061
+ array_param(&_x, x, x_offset, x_stride);
2062
+ array_param(&_y, y, y_offset, y_stride);
2063
+ vDSP_Stride _n = NUM2LONG(n);
2064
+
2065
+ vDSP_biquadD(
2066
+ _b->setup.d,
2067
+ _b->delay.d,
2068
+ _x.res0->v.d+_x.offset, _x.stride,
2069
+ _y.res0->v.d+_y.offset, _y.stride,
2070
+ _n);
2071
+
2072
+ return y;
2073
+ }
2074
+
1924
2075
 
1925
2076
  // Init
1926
2077
 
@@ -1997,6 +2148,17 @@ void Init_vdsp()
1997
2148
  rb_define_method(rb_cDoubleArray, "sve_svesq", rb_double_array_sve_svesq, 0);
1998
2149
  rb_define_method(rb_cDoubleArray, "svs", rb_double_array_svs, 0);
1999
2150
 
2151
+ // Vdsp::Biquad
2152
+ rb_mVdspBiquad = rb_define_module_under(rb_mVdsp, "Biquad");
2153
+ rb_define_method(rb_mVdspBiquad, "sections", rb_vdsp_biquad_sections, 0);
2154
+
2155
+ // Vdsp::DoubleBiquad
2156
+ rb_cDoubleBiquad = rb_define_class_under(rb_mVdsp, "DoubleBiquad", rb_cObject);
2157
+ rb_include_module(rb_cDoubleBiquad, rb_mVdspBiquad);
2158
+ rb_define_method(rb_cDoubleBiquad, "initialize", rb_double_biquad_initialize, 1);
2159
+ rb_define_method(rb_cDoubleBiquad, "apply", rb_double_biquad_apply, 1);
2160
+ rb_define_method(rb_cDoubleBiquad, "coefficients", rb_double_biquad_coefficients, 0);
2161
+
2000
2162
  // Vdsp::UnsafeDouble
2001
2163
  rb_mUnsafeDouble = rb_define_module_under(rb_mVdsp, "UnsafeDouble");
2002
2164
 
@@ -2060,4 +2222,7 @@ void Init_vdsp()
2060
2222
  rb_define_singleton_method(rb_mUnsafeDouble, "svesq", rb_double_svesq, 4);
2061
2223
  rb_define_singleton_method(rb_mUnsafeDouble, "sve_svesq", rb_double_sve_svesq, 4);
2062
2224
  rb_define_singleton_method(rb_mUnsafeDouble, "svs", rb_double_svs, 4);
2225
+
2226
+ // Vdsp::UnsafeDouble Biquadratic IIR Filters
2227
+ rb_define_singleton_method(rb_mUnsafeDouble, "biquad", rb_double_biquad, 8);
2063
2228
  }
data/ext/vdsp/vdsp.h CHANGED
@@ -1,6 +1,9 @@
1
1
  #include <ruby.h>
2
2
  #include <Accelerate/Accelerate.h>
3
3
 
4
+
5
+ // VdspArray
6
+
4
7
  typedef union {
5
8
  void *ptr;
6
9
  double *d;
@@ -18,6 +21,29 @@ typedef struct {
18
21
  vDSP_Stride stride;
19
22
  } VdspArrayParam;
20
23
 
24
+
25
+ // VdspBiquad
26
+
27
+ typedef union {
28
+ void *ptr;
29
+ struct vDSP_biquad_SetupStructD *d;
30
+ } VdspBiquadSetup;
31
+
32
+ typedef struct {
33
+ char type;
34
+ union {
35
+ void *ptr;
36
+ double *d;
37
+ } coefs;
38
+ union {
39
+ void *ptr;
40
+ double *d;
41
+ } delay;
42
+ VdspBiquadSetup setup;
43
+ unsigned long sections;
44
+ } VdspBiquadNativeResource;
45
+
46
+
21
47
  extern VALUE rb_double_array_plus(VALUE self, VALUE other);
22
48
  extern VALUE rb_double_array_mul(VALUE self, VALUE other);
23
49
 
data/lib/vdsp.rb CHANGED
@@ -4,4 +4,8 @@ require "vdsp/vdsp"
4
4
  module Vdsp
5
5
  class Error < StandardError; end
6
6
  # Your code goes here...
7
+
8
+ module Biquad
9
+ Coefficient = Struct.new("Coefficient", :b0, :b1, :b2, :a1, :a2)
10
+ end
7
11
  end
data/lib/vdsp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vdsp
2
- VERSION = "1.4.0"
2
+ VERSION = "1.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vdsp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshida
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-21 00:00:00.000000000 Z
11
+ date: 2019-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler