comparison projects/heavy/envelopeTrigger/SignalBiquad.c @ 162:c3e8226a5651 heavy-updated

- added additional flags to C rules (-DNDEBUG, -mfpu=neon) - sample-accurate envelope triggering pd/heavy example
author chnrx <chris.heinrichs@gmail.com>
date Thu, 12 Nov 2015 14:59:46 +0000
parents
children
comparison
equal deleted inserted replaced
161:07735c9d95c8 162:c3e8226a5651
1 /**
2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include "SignalBiquad.h"
18
19 // http://reanimator-web.appspot.com/articles/simdiir
20 // http://musicdsp.org/files/Audio-EQ-Cookbook.txt
21
22 hv_size_t sBiquad_init(SignalBiquad *o) {
23 #if HV_SIMD_AVX
24 o->xm1 = _mm256_setzero_ps();
25 o->xm2 = _mm256_setzero_ps();
26 #elif HV_SIMD_SSE
27 o->xm1 = _mm_setzero_ps();
28 o->xm2 = _mm_setzero_ps();
29 #elif HV_SIMD_NEON
30 o->xm1 = vdupq_n_f32(0.0f);
31 o->xm2 = vdupq_n_f32(0.0f);
32 #else // HV_SIMD_NONE
33 o->x1 = 0.0f;
34 o->x2 = 0.0f;
35 #endif
36 o->y1 = 0.0f;
37 o->y2 = 0.0f;
38 return 0;
39 }
40
41 void __hv_biquad_f(SignalBiquad *o, hv_bInf_t bIn, hv_bInf_t bX0, hv_bInf_t bX1, hv_bInf_t bX2, hv_bInf_t bY1, hv_bInf_t bY2, hv_bOutf_t bOut) {
42 #if HV_SIMD_AVX
43 __m256 a = _mm256_mul_ps(bIn, bX0);
44 __m256 b = _mm256_mul_ps(o->xm1, bX1);
45 __m256 c = _mm256_mul_ps(o->xm2, bX2);
46 __m256 d = _mm256_add_ps(a, b);
47 __m256 e = _mm256_add_ps(c, d); // bIn*bX0 + o->x1*bX1 + o->x2*bX2
48 float y0 = e[0] - o->y1*bY1[0] - o->y2*bY2[0];
49 float y1 = e[1] - y0*bY1[1] - o->y1*bY2[1];
50 float y2 = e[2] - y1*bY1[2] - y0*bY2[2];
51 float y3 = e[3] - y2*bY1[3] - y1*bY2[3];
52 float y4 = e[4] - y3*bY1[4] - y2*bY2[4];
53 float y5 = e[5] - y4*bY1[5] - y3*bY2[5];
54 float y6 = e[6] - y5*bY1[6] - y4*bY2[6];
55 float y7 = e[7] - y6*bY1[7] - y5*bY2[7];
56
57 o->xm2 = o->xm1;
58 o->xm1 = bIn;
59 o->y1 = y7;
60 o->y2 = y6;
61
62 *bOut = _mm256_set_ps(y7, y6, y5, y4, y3, y2, y1, y0);
63 #elif HV_SIMD_SSE
64 __m128 a = _mm_mul_ps(bIn, bX0);
65 __m128 b = _mm_mul_ps(o->xm1, bX1);
66 __m128 c = _mm_mul_ps(o->xm2, bX2);
67 __m128 d = _mm_add_ps(a, b);
68 __m128 e = _mm_add_ps(c, d);
69 float y0 = e[0] - o->y1*bY1[0] - o->y2*bY2[0];
70 float y1 = e[1] - y0*bY1[1] - o->y1*bY2[1];
71 float y2 = e[2] - y1*bY1[2] - y0*bY2[2];
72 float y3 = e[3] - y2*bY1[3] - y1*bY2[3];
73
74 o->xm2 = o->xm1;
75 o->xm1 = bIn;
76 o->y1 = y3;
77 o->y2 = y2;
78
79 *bOut = _mm_set_ps(y3, y2, y1, y0);
80 #elif HV_SIMD_NEON
81 float32x4_t a = vmulq_f32(bIn, bX0);
82 float32x4_t b = vmulq_f32(o->xm1, bX1);
83 float32x4_t c = vmulq_f32(o->xm2, bX2);
84 float32x4_t d = vaddq_f32(a, b);
85 float32x4_t e = vaddq_f32(c, d);
86 float y0 = e[0] - o->y1*bY1[0] - o->y2*bY2[0];
87 float y1 = e[1] - y0*bY1[1] - o->y1*bY2[1];
88 float y2 = e[2] - y1*bY1[2] - y0*bY2[2];
89 float y3 = e[3] - y2*bY1[3] - y1*bY2[3];
90
91 o->xm2 = o->xm1;
92 o->xm1 = bIn;
93 o->y1 = y3;
94 o->y2 = y2;
95
96 *bOut = (float32x4_t) {y0, y1, y2, y3};
97 #else
98 const float y = bIn*bX0 + o->x1*bX1 + o->x2*bX2 - o->y1*bY1 - o->y2*bY2;
99 o->x2 = o->x1; o->x1 = bIn;
100 o->y2 = o->y1; o->y1 = y;
101 *bOut = y;
102 #endif
103 }
104
105 static void sBiquad_k_updateCoefficients(SignalBiquad_k *const o) {
106 // calculate all filter coefficients in the double domain
107 #if HV_SIMD_AVX || HV_SIMD_SSE || HV_SIMD_NEON
108 double b0 = (double) o->b0;
109 double b1 = (double) o->b1;
110 double b2 = (double) o->b2;
111 double a1 = (double) -o->a1;
112 double a2 = (double) -o->a2;
113
114 double coeffs[4][8] =
115 {
116 { 0, 0, 0, b0, b1, b2, a1, a2 },
117 { 0, 0, b0, b1, b2, 0, a2, 0 },
118 { 0, b0, b1, b2, 0, 0, 0, 0 },
119 { b0, b1, b2, 0, 0, 0, 0, 0 },
120 };
121
122 for (int i = 0; i < 8; i++) {
123 coeffs[1][i] += a1*coeffs[0][i];
124 coeffs[2][i] += a1*coeffs[1][i] + a2*coeffs[0][i];
125 coeffs[3][i] += a1*coeffs[2][i] + a2*coeffs[1][i];
126 }
127
128 #if HV_SIMD_AVX || HV_SIMD_SSE
129 o->coeff_xp3 = _mm_set_ps((float) coeffs[3][0], (float) coeffs[2][0], (float) coeffs[1][0], (float) coeffs[0][0]);
130 o->coeff_xp2 = _mm_set_ps((float) coeffs[3][1], (float) coeffs[2][1], (float) coeffs[1][1], (float) coeffs[0][1]);
131 o->coeff_xp1 = _mm_set_ps((float) coeffs[3][2], (float) coeffs[2][2], (float) coeffs[1][2], (float) coeffs[0][2]);
132 o->coeff_x0 = _mm_set_ps((float) coeffs[3][3], (float) coeffs[2][3], (float) coeffs[1][3], (float) coeffs[0][3]);
133 o->coeff_xm1 = _mm_set_ps((float) coeffs[3][4], (float) coeffs[2][4], (float) coeffs[1][4], (float) coeffs[0][4]);
134 o->coeff_xm2 = _mm_set_ps((float) coeffs[3][5], (float) coeffs[2][5], (float) coeffs[1][5], (float) coeffs[0][5]);
135 o->coeff_ym1 = _mm_set_ps((float) coeffs[3][6], (float) coeffs[2][6], (float) coeffs[1][6], (float) coeffs[0][6]);
136 o->coeff_ym2 = _mm_set_ps((float) coeffs[3][7], (float) coeffs[2][7], (float) coeffs[1][7], (float) coeffs[0][7]);
137 #else // HV_SIMD_NEON
138 o->coeff_xp3 = (float32x4_t) {(float) coeffs[0][0], (float) coeffs[1][0], (float) coeffs[2][0], (float) coeffs[3][0]};
139 o->coeff_xp2 = (float32x4_t) {(float) coeffs[0][1], (float) coeffs[1][1], (float) coeffs[2][1], (float) coeffs[3][1]};
140 o->coeff_xp1 = (float32x4_t) {(float) coeffs[0][2], (float) coeffs[1][2], (float) coeffs[2][2], (float) coeffs[3][2]};
141 o->coeff_x0 = (float32x4_t) {(float) coeffs[0][3], (float) coeffs[1][3], (float) coeffs[2][3], (float) coeffs[3][3]};
142 o->coeff_xm1 = (float32x4_t) {(float) coeffs[0][4], (float) coeffs[1][4], (float) coeffs[2][4], (float) coeffs[3][4]};
143 o->coeff_xm2 = (float32x4_t) {(float) coeffs[0][5], (float) coeffs[1][5], (float) coeffs[2][5], (float) coeffs[3][5]};
144 o->coeff_ym1 = (float32x4_t) {(float) coeffs[0][6], (float) coeffs[1][6], (float) coeffs[2][6], (float) coeffs[3][6]};
145 o->coeff_ym2 = (float32x4_t) {(float) coeffs[0][7], (float) coeffs[1][7], (float) coeffs[2][7], (float) coeffs[3][7]};
146 #endif
147 #endif
148 // NOTE(mhroth): not necessary to calculate any coefficients for HV_SIMD_NONE case
149 }
150
151 hv_size_t sBiquad_k_init(SignalBiquad_k *o, float b0, float b1, float b2, float a1, float a2) {
152 // initialise filter coefficients
153 o->b0 = b0;
154 o->b1 = b1;
155 o->b2 = b2;
156 o->a1 = a1;
157 o->a2 = a2;
158 sBiquad_k_updateCoefficients(o);
159
160 // clear filter state
161 #if HV_SIMD_AVX || HV_SIMD_SSE
162 o->xm1 = _mm_setzero_ps();
163 o->xm2 = _mm_setzero_ps();
164 o->ym1 = _mm_setzero_ps();
165 o->ym2 = _mm_setzero_ps();
166 #elif HV_SIMD_NEON
167 o->xm1 = vdupq_n_f32(0.0f);
168 o->xm2 = vdupq_n_f32(0.0f);
169 o->ym1 = vdupq_n_f32(0.0f);
170 o->ym2 = vdupq_n_f32(0.0f);
171 #else // HV_SIMD_NONE
172 o->xm1 = 0.0f;
173 o->xm2 = 0.0f;
174 o->ym1 = 0.0f;
175 o->ym2 = 0.0f;
176 #endif
177 return 0;
178 }
179
180 void sBiquad_k_onMessage(SignalBiquad_k *o, int letIn, const HvMessage *const m) {
181 if (msg_isFloat(m,0)) {
182 switch (letIn) {
183 case 1: o->b0 = msg_getFloat(m,0); break;
184 case 2: o->b1 = msg_getFloat(m,0); break;
185 case 3: o->b2 = msg_getFloat(m,0); break;
186 case 4: o->a1 = msg_getFloat(m,0); break;
187 case 5: o->a2 = msg_getFloat(m,0); break;
188 default: return;
189 }
190 sBiquad_k_updateCoefficients(o);
191 }
192 }