source: roaraudio/libroardsp/filter_responsecurve.c @ 5961:06e7fd9e4c25

Last change on this file since 5961:06e7fd9e4c25 was 5961:06e7fd9e4c25, checked in by phi, 10 years ago

Updates of copyright and license headers

File size: 4.7 KB
Line 
1//filter_amp.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2014
5 *
6 *  This file is part of libroardsp a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroardsp is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "libroardsp.h"
27
28#ifdef ROAR_HAVE_LIBM
29int roardsp_responsecurve_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) {
30 struct roardsp_responsecurve * self = roar_mm_malloc(sizeof(struct roardsp_responsecurve));
31
32 (void)stream, (void)id;
33
34 ROAR_DBG("roardsp_responsecurve_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id);
35
36 if ( self == NULL )
37  return -1;
38
39 memset(self, 0, sizeof(struct roardsp_responsecurve));
40
41 filter->inst = (void*) self;
42
43 roardsp_filter_reset(filter, ROARDSP_RESET_FULL);
44
45 return 0;
46}
47
48int roardsp_responsecurve_uninit(struct roardsp_filter * filter) {
49
50 roar_mm_free(filter->inst);
51 return 0;
52}
53
54static double __func_pass(double i, double N) {
55 (void)N;
56 return i;
57}
58
59static double __func_linear(double i, double N) {
60 return -pow(1. - i, N + 1.) + 1.;
61}
62
63static double __func_ilinear(double i, double N) {
64 return pow(i, N + 1.);
65}
66
67static double __func_sin(double i, double N) {
68 return pow(sin(M_PI_2 * i), N);
69}
70
71static double __func_isin(double i, double N) {
72 return 1. - pow(sin((1. - i) * M_PI_2), N);
73}
74
75static double __func_cos(double i, double N) {
76 (void)N;
77 return 0.5 - cos(i * M_PI)/2.;
78}
79
80static double __func_icos(double i, double N) {
81 (void)N;
82 return 2.*i - 0.5 + cos(i * M_PI)/2.;
83}
84
85static inline double _oddify(double x, double N, double (*func)(double, double)) {
86 if ( x < 0. ) {
87  return -func(0. - x, N);
88 } else {
89  return func(x, N);
90 }
91}
92
93static inline double _calc(double x, struct roardsp_responsecurve * self, double N) {
94 double (*func)(double, double) = NULL;
95 enum roardsp_responsecurve_mode mode;
96
97 if ( x > 0. ) {
98  mode = self->mode_pos;
99 } else {
100  mode = self->mode_neg;
101 }
102
103 switch (mode) {
104  case ROARDSP_RESPONSECURVE_MODE_PASS: func = __func_pass; break;
105  case ROARDSP_RESPONSECURVE_MODE_LIN:  func = __func_linear; break;
106  case ROARDSP_RESPONSECURVE_MODE_ILIN: func = __func_ilinear; break;
107  case ROARDSP_RESPONSECURVE_MODE_SIN:  func = __func_sin; break;
108  case ROARDSP_RESPONSECURVE_MODE_ISIN: func = __func_isin; break;
109  case ROARDSP_RESPONSECURVE_MODE_COS:  func = __func_cos; break;
110  case ROARDSP_RESPONSECURVE_MODE_ICOS: func = __func_icos; break;
111  default: func = __func_linear; break;
112 }
113
114 return _oddify(x, N, func);
115}
116
117#define _calcX(bits,fact) \
118int roardsp_responsecurve_calc##bits  (struct roardsp_filter * filter, void * data, size_t samples) { \
119 struct roardsp_responsecurve * self = (struct roardsp_responsecurve *) filter->inst; \
120 int##bits##_t * samp = (int##bits##_t *) data; \
121 register double s; \
122 size_t i; \
123\
124 for (i = 0; i < samples; i++) { \
125  s        = samp[i]; \
126  s       /= (double)(fact); \
127  s        = _calc(s, self, self->N); \
128  s       *= (double)(fact); \
129  samp[i]  = s; \
130 }; \
131\
132 return 0; \
133}
134
135_calcX(8,128.)
136_calcX(16,32768.)
137_calcX(32,2147483648.)
138
139int roardsp_responsecurve_ctl   (struct roardsp_filter * filter, int cmd, void * data) {
140 struct roardsp_responsecurve * self = (struct roardsp_responsecurve *) filter->inst;
141 int32_t old;
142
143 if ( cmd == ROARDSP_FCTL_N ) {
144  old = self->N;
145  self->N = *(int32_t*)data;
146  *(int32_t*)data = old;
147 } else if ( cmd == ROARDSP_FCTL_MODE ) {
148  old = self->mode_pos;
149  self->mode_pos = *(int32_t*)data;
150  *(int32_t*)data = old;
151 } else if ( cmd == ROARDSP_FCTL_Q ) {
152  old = self->mode_neg;
153  self->mode_neg = *(int32_t*)data;
154  *(int32_t*)data = old;
155 } else {
156  return -1;
157 }
158
159
160 return 0;
161}
162
163int roardsp_responsecurve_reset (struct roardsp_filter * filter, int what) {
164 struct roardsp_responsecurve * self;
165
166 if ( filter == NULL )
167  return -1;
168
169 if ( filter->inst == NULL )
170  return -1;
171
172 self = filter->inst;
173
174 switch (what) {
175  case ROARDSP_RESET_NONE:
176  case ROARDSP_RESET_STATE:
177    return  0;
178   break;
179  case ROARDSP_RESET_FULL:
180    self->mode_pos = self->mode_neg = ROARDSP_RESPONSECURVE_MODE_LIN;
181    self->N    = 1;
182    return  0;
183   break;
184  default:
185    return -1;
186 }
187
188 return -1;
189}
190#endif
191
192//ll
Note: See TracBrowser for help on using the repository browser.