[4668] | 1 | //filter_highp.c: |
---|
[656] | 2 | |
---|
[658] | 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
[658] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[658] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[656] | 26 | #include "libroardsp.h" |
---|
| 27 | |
---|
[1104] | 28 | #ifdef ROAR_HAVE_LIBM |
---|
| 29 | |
---|
[683] | 30 | int roardsp_highp_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
[3001] | 31 | struct roardsp_highp * self = roar_mm_malloc(sizeof(struct roardsp_highp)); |
---|
[683] | 32 | |
---|
| 33 | if ( self == NULL ) |
---|
| 34 | return -1; |
---|
| 35 | |
---|
| 36 | memset(self, 0, sizeof(struct roardsp_highp)); |
---|
| 37 | |
---|
| 38 | filter->inst = (void*) self; |
---|
| 39 | |
---|
[5181] | 40 | return roardsp_filter_reset(filter, ROARDSP_RESET_FULL); |
---|
[683] | 41 | } |
---|
| 42 | |
---|
| 43 | int roardsp_highp_uninit(struct roardsp_filter * filter) { |
---|
| 44 | |
---|
[3001] | 45 | roar_mm_free(filter->inst); |
---|
[683] | 46 | return 0; |
---|
| 47 | } |
---|
| 48 | |
---|
[5181] | 49 | #define _calcX(bits,twobits) \ |
---|
| 50 | int roardsp_highp_calc##bits (struct roardsp_filter * filter, void * data, size_t samples) { \ |
---|
| 51 | struct roardsp_highp * self = (struct roardsp_highp *) filter->inst; \ |
---|
| 52 | int##bits##_t * samp = (int##bits##_t *) data; \ |
---|
| 53 | register int##twobits##_t s, h; \ |
---|
| 54 | int i, c; \ |
---|
| 55 | int channels = filter->channels; \ |
---|
| 56 | \ |
---|
| 57 | if ( channels > ROAR_MAX_CHANNELS ) \ |
---|
| 58 | return -1; \ |
---|
| 59 | \ |
---|
| 60 | samples /= channels; \ |
---|
| 61 | \ |
---|
| 62 | ROAR_DBG("roardsp_highp_calc16(*): filtering %i frames of %i channels...", samples, channels); \ |
---|
| 63 | \ |
---|
| 64 | /* * output[N] = A0 * input[N] + A1 * input[N-1] + B1 * output[N-1] */ \ |
---|
| 65 | \ |
---|
| 66 | \ |
---|
| 67 | for (i = 0; i < samples; i++) { \ |
---|
| 68 | for (c = 0; c < channels; c++) { \ |
---|
| 69 | h = samp[i*channels + c]; \ |
---|
| 70 | s = (int##twobits##_t)samp[i*channels + c] * self->a + (int##twobits##_t)self->oldin[c] * self->b + (int##twobits##_t)self->oldout[c] * self->c; \ |
---|
| 71 | \ |
---|
| 72 | s /= 65536; \ |
---|
| 73 | \ |
---|
| 74 | self->oldin[ c] = h; \ |
---|
| 75 | samp[i*channels + c] = s; \ |
---|
| 76 | self->oldout[ c] = s; \ |
---|
| 77 | } \ |
---|
| 78 | } \ |
---|
| 79 | \ |
---|
| 80 | return 0; \ |
---|
| 81 | } |
---|
[683] | 82 | |
---|
[5181] | 83 | _calcX(8,32) /* we need 32 bit not 16 bit here as we multiplay with 65536 internally */ |
---|
| 84 | _calcX(16,32) |
---|
| 85 | _calcX(32,64) |
---|
[683] | 86 | |
---|
| 87 | int roardsp_highp_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
| 88 | struct roardsp_highp * self = (struct roardsp_highp *) filter->inst; |
---|
| 89 | float lp; |
---|
| 90 | float oldfreq; |
---|
| 91 | float newfreq; |
---|
| 92 | |
---|
| 93 | if ( cmd != ROARDSP_FCTL_FREQ ) |
---|
| 94 | return -1; |
---|
| 95 | |
---|
| 96 | newfreq = *(float*)data; |
---|
| 97 | |
---|
[883] | 98 | lp = exp(-2.0 * M_PI * newfreq / (float) filter->rate) * 65536; |
---|
[683] | 99 | |
---|
[883] | 100 | self->a = (65536 + lp)/2.0; |
---|
| 101 | self->b = -(65536 + lp)/2.0; |
---|
[683] | 102 | self->c = lp; |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | oldfreq = self->freq / 1000; |
---|
| 106 | self->freq = newfreq * 1000; |
---|
| 107 | |
---|
| 108 | *(float*)data = oldfreq; |
---|
| 109 | |
---|
[684] | 110 | ROAR_DBG("roardsp_highp_ctl(*): oldfreq=%f, newfreq=%f", oldfreq, newfreq); |
---|
| 111 | ROAR_DBG("roardsp_highp_ctl(*): a=%i, b=%i, c=%i", self->a, self->b, self->c); |
---|
[683] | 112 | |
---|
| 113 | return 0; |
---|
| 114 | } |
---|
| 115 | |
---|
[1141] | 116 | int roardsp_highp_reset (struct roardsp_filter * filter, int what) { |
---|
| 117 | struct roardsp_highp * self; |
---|
[5181] | 118 | float freq = 25.; |
---|
[1141] | 119 | |
---|
| 120 | if ( filter == NULL ) |
---|
| 121 | return -1; |
---|
| 122 | |
---|
| 123 | if ( filter->inst == NULL ) |
---|
| 124 | return -1; |
---|
| 125 | |
---|
| 126 | self = filter->inst; |
---|
| 127 | |
---|
| 128 | switch (what) { |
---|
| 129 | case ROARDSP_RESET_NONE: |
---|
| 130 | return 0; |
---|
| 131 | break; |
---|
| 132 | case ROARDSP_RESET_FULL: |
---|
| 133 | roardsp_highp_ctl(filter, ROARDSP_FCTL_FREQ, &freq); |
---|
| 134 | case ROARDSP_RESET_STATE: |
---|
| 135 | memset(self->oldin, 0, sizeof(int32_t)*ROAR_MAX_CHANNELS); |
---|
| 136 | memset(self->oldout, 0, sizeof(int32_t)*ROAR_MAX_CHANNELS); |
---|
| 137 | return 0; |
---|
| 138 | break; |
---|
| 139 | default: |
---|
| 140 | return -1; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | return -1; |
---|
| 144 | } |
---|
| 145 | |
---|
[1104] | 146 | #endif |
---|
| 147 | |
---|
[656] | 148 | //ll |
---|