source: roaraudio/libroardsp/filter_responsecurve.c @ 5270:e25346c13638

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

fixed some gcc -Wextra warnings

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