~ubuntu-branches/ubuntu/utopic/glame/utopic

« back to all changes in this revision

Viewing changes to src/plugins/distortion.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2002-04-09 17:14:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020409171412-jzpnov7mbz2w6zsr
Tags: upstream-0.6.2
ImportĀ upstreamĀ versionĀ 0.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * distortion.c
 
3
 * $Id: distortion.c,v 1.9 2001/11/19 10:04:50 richi Exp $ 
 
4
 *
 
5
 * Copyright (C) 2001 Alexander Ehlert
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 *
 
21
 */
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
#include <config.h>
 
25
#endif
 
26
 
 
27
#include <sys/types.h>
 
28
#include <unistd.h>
 
29
#include <stdlib.h>
 
30
#include "filter.h"
 
31
#include "util.h"
 
32
#include "glplugin.h"
 
33
#include "math.h"
 
34
 
 
35
PLUGIN(distortion)
 
36
 
 
37
static int distortion_f(filter_t *n)
 
38
{
 
39
        filter_pipe_t *in, *out;
 
40
        filter_buffer_t *buf;
 
41
        filter_param_t *param, *preg_param, *fxg_param, *clip_param, *asym_param;
 
42
        
 
43
        SAMPLE  *s, w;
 
44
        int     i;
 
45
        float   pregain, pos_clip, neg_clip, asym, clip, sign, gain, fxgain;
 
46
        
 
47
        in = filterport_get_pipe(filterportdb_get_port(filter_portdb(n), PORTNAME_IN));
 
48
        out = filterport_get_pipe(filterportdb_get_port(filter_portdb(n), PORTNAME_OUT));
 
49
        if (!in || !out)
 
50
                FILTER_ERROR_RETURN("no in/output connected");
 
51
        
 
52
        preg_param = filterparamdb_get_param(filter_paramdb(n), "pregain");
 
53
        
 
54
        fxg_param = filterparamdb_get_param(filter_paramdb(n), "fxgain");
 
55
        
 
56
        clip_param = filterparamdb_get_param(filter_paramdb(n), "clip");
 
57
        
 
58
        asym_param = filterparamdb_get_param(filter_paramdb(n), "asym");
 
59
 
 
60
        param=filterparamdb_get_param(filter_paramdb(n), "mode");
 
61
        
 
62
        pos_clip = asym + clip;
 
63
        neg_clip = asym - clip;
 
64
 
 
65
        gain = 1.0 / (1.0 + fxgain);
 
66
        
 
67
        FILTER_AFTER_INIT;
 
68
        
 
69
        switch (filterparam_val_int(param))
 
70
        {
 
71
                case 0:
 
72
                        goto entry;
 
73
                        break;
 
74
                case 1:
 
75
                        goto entry1;
 
76
                        break;
 
77
                case 2: 
 
78
                        goto entry2;
 
79
                        break;
 
80
                default:
 
81
                        goto exitus;
 
82
        }
 
83
        
 
84
        while (buf) {
 
85
                FILTER_CHECK_STOP;
 
86
                
 
87
                asym = filterparam_val_float(asym_param);
 
88
                clip = filterparam_val_float(clip_param);
 
89
 
 
90
                pregain = filterparam_val_float(preg_param);
 
91
                
 
92
                pos_clip = asym + clip;
 
93
                neg_clip = asym - clip;
 
94
                fxgain = filterparam_val_float(fxg_param);
 
95
                gain = 1.0 / (1.0 + fxgain);
 
96
                
 
97
                /* got an input buffer */
 
98
                s = &sbuf_buf(buf)[0];
 
99
                for(i=0; i < sbuf_size(buf); i++) {
 
100
                        w = *s;
 
101
                        sign = (w >= 0.0 ? 1.0 : -1.0);
 
102
                        w *= pregain;
 
103
                        w *= w * sign;
 
104
 
 
105
                        w = (w > pos_clip ? pos_clip :
 
106
                                ( w < neg_clip ? neg_clip : w));
 
107
                        *s += w * fxgain;
 
108
                        *s++ *= gain;
 
109
                }
 
110
 
 
111
                sbuf_queue(out, buf);
 
112
 
 
113
                switch (filterparam_val_int(param))
 
114
                {
 
115
                        case 0:
 
116
                                goto entry;
 
117
                                break;
 
118
                        case 1:
 
119
                                goto entry1;
 
120
                                break;
 
121
                        case 2: 
 
122
                                goto entry2;
 
123
                                break;
 
124
                        default:
 
125
                                goto exitus;
 
126
                }
 
127
entry:
 
128
                buf = sbuf_make_private(sbuf_get(in));
 
129
        };
 
130
        
 
131
        goto exitus;
 
132
 
 
133
        /* Here starts the full wave rectifier distortion */
 
134
        while (buf) {
 
135
                FILTER_CHECK_STOP;
 
136
                
 
137
                asym = filterparam_val_float(asym_param);
 
138
                clip = filterparam_val_float(clip_param);
 
139
 
 
140
                pregain = filterparam_val_float(preg_param);
 
141
                
 
142
                pos_clip = asym + clip;
 
143
                neg_clip = asym - clip;
 
144
                fxgain = filterparam_val_float(fxg_param);
 
145
                gain = 1.0 / (1.0 + fxgain);
 
146
                
 
147
                /* got an input buffer */
 
148
                s = &sbuf_buf(buf)[0];
 
149
                for(i=0; i < sbuf_size(buf); i++) {
 
150
                        w = *s;
 
151
                        w *= w * pregain;
 
152
                        w = (w > clip ? clip : w);
 
153
                        *s += w * fxgain;
 
154
                        *s++ *= gain;
 
155
                }
 
156
 
 
157
                sbuf_queue(out, buf);
 
158
                
 
159
                switch (filterparam_val_int(param))
 
160
                {
 
161
                        case 0:
 
162
                                goto entry;
 
163
                                break;
 
164
                        case 1:
 
165
                                goto entry1;
 
166
                                break;
 
167
                        case 2: 
 
168
                                goto entry2;
 
169
                                break;
 
170
                        default:
 
171
                                goto exitus;
 
172
                }
 
173
entry1:
 
174
                buf = sbuf_make_private(sbuf_get(in));
 
175
        };
 
176
 
 
177
        goto exitus;
 
178
        
 
179
        /* Simple sinusoider */
 
180
 
 
181
 
 
182
        while (buf) {
 
183
                FILTER_CHECK_STOP;
 
184
 
 
185
                pregain = filterparam_val_float(preg_param);
 
186
                fxgain  = filterparam_val_float(fxg_param);
 
187
                clip    = filterparam_val_float(clip_param);
 
188
                asym = filterparam_val_float(asym_param);
 
189
 
 
190
                pos_clip = asym + clip;
 
191
                neg_clip = asym - clip;
 
192
                
 
193
                pregain *= M_PI;
 
194
                if (pregain < M_PI*0.5)
 
195
                        fxgain *= 1.0/sinf(clip);
 
196
        
 
197
                gain = 1.0 / (1.0 + fxgain);
 
198
                
 
199
                /* got an input buffer */
 
200
                s = &sbuf_buf(buf)[0];
 
201
                for(i=0; i < sbuf_size(buf); i++) {
 
202
                        *s = gain * (*s + fxgain * sinf(*s * pregain));
 
203
                        if (*s > pos_clip)
 
204
                                *s = pos_clip;
 
205
                        else if (*s < neg_clip)
 
206
                                *s = neg_clip;
 
207
                        s++;
 
208
                }
 
209
 
 
210
                sbuf_queue(out, buf);
 
211
                
 
212
                switch (filterparam_val_int(param))
 
213
                {
 
214
                        case 0:
 
215
                                goto entry;
 
216
                                break;
 
217
                        case 1:
 
218
                                goto entry1;
 
219
                                break;
 
220
                        case 2: 
 
221
                                goto entry2;
 
222
                                break;
 
223
                        default:
 
224
                                goto exitus;
 
225
                }
 
226
entry2:
 
227
                buf = sbuf_make_private(sbuf_get(in));
 
228
        }
 
229
        
 
230
exitus:
 
231
        sbuf_queue(out, buf);
 
232
        
 
233
        FILTER_BEFORE_STOPCLEANUP;
 
234
        FILTER_BEFORE_CLEANUP;
 
235
 
 
236
        FILTER_RETURN;
 
237
}
 
238
 
 
239
int distortion_register(plugin_t *p)
 
240
{
 
241
        filter_t *f;
 
242
        filter_paramdb_t *param;
 
243
        
 
244
        if (!(f = filter_creat(NULL)))
 
245
                return -1;
 
246
        f->f = distortion_f;
 
247
 
 
248
        filterportdb_add_port(filter_portdb(f), PORTNAME_IN,
 
249
                              FILTER_PORTTYPE_SAMPLE,
 
250
                              FILTER_PORTFLAG_INPUT,
 
251
                              FILTERPORT_DESCRIPTION, "audio stream in",
 
252
                              FILTERPORT_END);
 
253
        filterportdb_add_port(filter_portdb(f), PORTNAME_OUT,
 
254
                              FILTER_PORTTYPE_SAMPLE,
 
255
                              FILTER_PORTFLAG_OUTPUT,
 
256
                              FILTERPORT_DESCRIPTION, "audio stream out",
 
257
                              FILTERPORT_END);
 
258
        
 
259
        param = filter_paramdb(f);
 
260
        filterparamdb_add_param_float(param, "pregain", FILTER_PARAMTYPE_FLOAT, 10.0,
 
261
                                    FILTERPARAM_DESCRIPTION, "gain before distortion",
 
262
                                    FILTERPARAM_END);
 
263
        
 
264
        filterparamdb_add_param_float(param, "fxgain", FILTER_PARAMTYPE_FLOAT, 1.0,
 
265
                                    FILTERPARAM_DESCRIPTION, "distortion effect gain",
 
266
                                    FILTERPARAM_END);
 
267
        
 
268
        filterparamdb_add_param_float(param, "clip", FILTER_PARAMTYPE_FLOAT, 0.8,
 
269
                                    FILTERPARAM_DESCRIPTION, "clip range",
 
270
                                    FILTERPARAM_END);
 
271
 
 
272
        filterparamdb_add_param_float(param, "asym", FILTER_PARAMTYPE_FLOAT, 0.0,
 
273
                                    FILTERPARAM_DESCRIPTION, 
 
274
                                    "midpoint for asymmetrical clipping",
 
275
                                    FILTERPARAM_END);
 
276
        
 
277
        filterparamdb_add_param_int(param,"mode", 
 
278
                                    FILTER_PARAMTYPE_INT, 0, 
 
279
                                    FILTERPARAM_DESCRIPTION,
 
280
                                    "(0) halfwave/asymmetrical" 
 
281
                                    "(1) fullwave rectifier"
 
282
                                    "(2) sinusoider",
 
283
                                    FILTERPARAM_GLADEXML,
 
284
"<?xml version=\"1.0\"?><GTK-Interface><widget> 
 
285
        <class>GtkOptionMenu</class> 
 
286
        <name>widget</name> 
 
287
        <can_focus>True</can_focus> 
 
288
        <items>Half wave rectifier 
 
289
Full Wave rectifier
 
290
Sinusoider</items> 
 
291
        <initial_choice>0</initial_choice> 
 
292
</widget></GTK-Interface>", 
 
293
        FILTERPARAM_END);
 
294
 
 
295
 
 
296
        plugin_set(p, PLUGIN_DESCRIPTION, "distortion effect");
 
297
        plugin_set(p, PLUGIN_PIXMAP, "distortion.png"); 
 
298
        plugin_set(p, PLUGIN_CATEGORY, "Effects");
 
299
        plugin_set(p, PLUGIN_GUI_HELP_PATH, "Distortion");
 
300
 
 
301
        return filter_register(f, p);
 
302
}