source: roaraudio/libroardsp/filter_responsecurve.c @ 5269:b914e5b61703

Last change on this file since 5269:b914e5b61703 was 5269:b914e5b61703, checked in by phi, 12 years ago

start of a response curve filter

File size: 4.7 KB
Line 
1//filter_amp.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
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
28int roardsp_responsecurve_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) {
29 struct roardsp_responsecurve * self = roar_mm_malloc(sizeof(struct roardsp_responsecurve));
30
31 ROAR_DBG("roardsp_responsecurve_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id);
32
33 if ( self == NULL )
34  return -1;
35
36 memset(self, 0, sizeof(struct roardsp_responsecurve));
37
38 filter->inst = (void*) self;
39
40 roardsp_filter_reset(filter, ROARDSP_RESET_FULL);
41
42 return 0;
43}
44
45int roardsp_responsecurve_uninit(struct roardsp_filter * filter) {
46
47 roar_mm_free(filter->inst);
48 return 0;
49}
50
51static double __func_pass(double i, double N) {
52 return i;
53}
54
55static double __func_linear(double i, double N) {
56 return -pow(1. - i, N + 1.) + 1.;
57}
58
59static double __func_ilinear(double i, double N) {
60 return pow(i, N + 1.);
61}
62
63static double __func_sin(double i, double N) {
64 return pow(sin(M_PI_2 * i), N);
65}
66
67static double __func_isin(double i, double N) {
68 return 1. - pow(sin((1. - i) * M_PI_2), N);
69}
70
71static double __func_cos(double i, double N) {
72 return 0.5 - cos(i * M_PI)/2.;
73}
74
75static double __func_icos(double i, double N) {
76 return 2.*i - 0.5 + cos(i * M_PI)/2.;
77}
78
79static inline double _oddify(double x, double N, double (*func)(double, double)) {
80 if ( x < 0. ) {
81  return -func(0. - x, N);
82 } else {
83  return func(x, N);
84 }
85}
86
87static inline double _calc(double x, struct roardsp_responsecurve * self, double N) {
88 double (*func)(double, double) = NULL;
89 enum roardsp_responsecurve_mode mode;
90
91 if ( x > 0. ) {
92  mode = self->mode_pos;
93 } else {
94  mode = self->mode_neg;
95 }
96
97 switch (mode) {
98  case ROARDSP_RESPONSECURVE_MODE_PASS: func = __func_pass; break;
99  case ROARDSP_RESPONSECURVE_MODE_LIN:  func = __func_linear; break;
100  case ROARDSP_RESPONSECURVE_MODE_ILIN: func = __func_ilinear; break;
101  case ROARDSP_RESPONSECURVE_MODE_SIN:  func = __func_sin; break;
102  case ROARDSP_RESPONSECURVE_MODE_ISIN: func = __func_isin; break;
103  case ROARDSP_RESPONSECURVE_MODE_COS:  func = __func_cos; break;
104  case ROARDSP_RESPONSECURVE_MODE_ICOS: func = __func_icos; break;
105  default: func = __func_linear; break;
106 }
107
108 return _oddify(x, N, func);
109}
110
111#define _calcX(bits,fact) \
112int roardsp_responsecurve_calc##bits  (struct roardsp_filter * filter, void * data, size_t samples) { \
113 struct roardsp_responsecurve * self = (struct roardsp_responsecurve *) filter->inst; \
114 int##bits##_t * samp = (int##bits##_t *) data; \
115 register double s; \
116 size_t i; \
117\
118 for (i = 0; i < samples; i++) { \
119  s        = samp[i]; \
120  s       /= (double)(fact); \
121  s        = _calc(s, self, self->N); \
122  s       *= (double)(fact); \
123  samp[i]  = s; \
124 }; \
125\
126 return 0; \
127}
128
129_calcX(8,128.)
130_calcX(16,32768.)
131_calcX(32,2147483648.)
132
133int roardsp_responsecurve_ctl   (struct roardsp_filter * filter, int cmd, void * data) {
134 struct roardsp_responsecurve * self = (struct roardsp_responsecurve *) filter->inst;
135 int32_t old;
136
137 if ( cmd == ROARDSP_FCTL_N ) {
138  old = self->N;
139  self->N = *(int32_t*)data;
140  *(int32_t*)data = old;
141 } else if ( cmd == ROARDSP_FCTL_MODE ) {
142  old = self->mode_pos;
143  self->mode_pos = *(int32_t*)data;
144  *(int32_t*)data = old;
145 } else if ( cmd == ROARDSP_FCTL_Q ) {
146  old = self->mode_neg;
147  self->mode_neg = *(int32_t*)data;
148  *(int32_t*)data = old;
149 } else {
150  return -1;
151 }
152
153
154 return 0;
155}
156
157int roardsp_responsecurve_reset (struct roardsp_filter * filter, int what) {
158 struct roardsp_responsecurve * self;
159
160 if ( filter == NULL )
161  return -1;
162
163 if ( filter->inst == NULL )
164  return -1;
165
166 self = filter->inst;
167
168 switch (what) {
169  case ROARDSP_RESET_NONE:
170  case ROARDSP_RESET_STATE:
171    return  0;
172   break;
173  case ROARDSP_RESET_FULL:
174    self->mode_pos = self->mode_neg = ROARDSP_RESPONSECURVE_MODE_LIN;
175    self->N    = 1;
176    return  0;
177   break;
178  default:
179    return -1;
180 }
181
182 return -1;
183}
184
185//ll
Note: See TracBrowser for help on using the repository browser.