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