[4668] | 1 | //filter_lowp.c: |
---|
[656] | 2 | |
---|
[658] | 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
[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 | |
---|
[673] | 30 | int roardsp_lowp_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
[3001] | 31 | struct roardsp_lowp * self = roar_mm_malloc(sizeof(struct roardsp_lowp)); |
---|
[673] | 32 | |
---|
[5270] | 33 | (void)stream, (void)id; |
---|
| 34 | |
---|
[885] | 35 | ROAR_DBG("roardsp_lowp_init(*): self=%p", self); |
---|
| 36 | |
---|
[673] | 37 | if ( self == NULL ) |
---|
| 38 | return -1; |
---|
| 39 | |
---|
| 40 | memset(self, 0, sizeof(struct roardsp_lowp)); |
---|
| 41 | |
---|
| 42 | filter->inst = (void*) self; |
---|
| 43 | |
---|
[5181] | 44 | return roardsp_filter_reset(filter, ROARDSP_RESET_FULL); |
---|
[673] | 45 | } |
---|
| 46 | |
---|
| 47 | int roardsp_lowp_uninit(struct roardsp_filter * filter) { |
---|
[3001] | 48 | roar_mm_free(filter->inst); |
---|
[673] | 49 | return 0; |
---|
| 50 | } |
---|
| 51 | |
---|
[5181] | 52 | #define _calcX(bits,twobits) \ |
---|
| 53 | int roardsp_lowp_calc##bits (struct roardsp_filter * filter, void * data, size_t samples) { \ |
---|
| 54 | struct roardsp_lowp * self = (struct roardsp_lowp *) filter->inst; \ |
---|
| 55 | int##bits##_t * samp = (int##bits##_t *) data; \ |
---|
| 56 | register int##twobits##_t s; \ |
---|
[5270] | 57 | size_t i, c; \ |
---|
| 58 | size_t channels = filter->channels; \ |
---|
[5181] | 59 | \ |
---|
| 60 | if ( channels > ROAR_MAX_CHANNELS ) \ |
---|
| 61 | return -1; \ |
---|
| 62 | \ |
---|
| 63 | samples /= channels; \ |
---|
| 64 | \ |
---|
| 65 | ROAR_DBG("roardsp_lowp_calc16(*): filtering %i frames of %i channels...", samples, channels); \ |
---|
| 66 | \ |
---|
| 67 | /* * output[N] = input[N] * A + output[N-1] * B */ \ |
---|
| 68 | \ |
---|
| 69 | for (i = 0; i < samples; i++) { \ |
---|
| 70 | for (c = 0; c < channels; c++) { \ |
---|
| 71 | s = (int##twobits##_t)samp[i*channels + c] * self->a + (int##twobits##_t)self->old[c] * self->b; \ |
---|
| 72 | \ |
---|
| 73 | s /= 65536; \ |
---|
| 74 | \ |
---|
| 75 | samp[i*channels + c] = s; \ |
---|
| 76 | self->old[ c] = s; \ |
---|
| 77 | } \ |
---|
| 78 | } \ |
---|
| 79 | \ |
---|
| 80 | return 0; \ |
---|
| 81 | } |
---|
[680] | 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) |
---|
[673] | 86 | |
---|
| 87 | int roardsp_lowp_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
| 88 | struct roardsp_lowp * self = (struct roardsp_lowp *) filter->inst; |
---|
| 89 | float lp; |
---|
| 90 | float oldfreq; |
---|
[680] | 91 | float newfreq; |
---|
[673] | 92 | |
---|
[5181] | 93 | if ( cmd != ROARDSP_FCTL_FREQ ) { |
---|
| 94 | roar_err_set(ROAR_ERROR_BADRQC); |
---|
[673] | 95 | return -1; |
---|
[5181] | 96 | } |
---|
[673] | 97 | |
---|
[680] | 98 | newfreq = *(float*)data; |
---|
| 99 | |
---|
[5485] | 100 | lp = exp(-2.0 * M_PI * newfreq / (float) filter->rate) * 65536.; |
---|
[673] | 101 | |
---|
| 102 | self->b = lp; |
---|
[5181] | 103 | self->a = 65536. - lp; |
---|
[673] | 104 | |
---|
[5181] | 105 | oldfreq = self->freq / 1000.; |
---|
| 106 | self->freq = newfreq * 1000.; |
---|
[673] | 107 | |
---|
| 108 | *(float*)data = oldfreq; |
---|
| 109 | |
---|
[684] | 110 | ROAR_DBG("roardsp_lowp_ctl(*): oldfreq=%f, newfreq=%f", oldfreq, newfreq); |
---|
[680] | 111 | |
---|
[673] | 112 | return 0; |
---|
| 113 | } |
---|
[666] | 114 | |
---|
[1141] | 115 | int roardsp_lowp_reset (struct roardsp_filter * filter, int what) { |
---|
| 116 | struct roardsp_lowp * self; |
---|
| 117 | float freq; |
---|
| 118 | |
---|
| 119 | if ( filter == NULL ) |
---|
| 120 | return -1; |
---|
| 121 | |
---|
| 122 | if ( filter->inst == NULL ) |
---|
| 123 | return -1; |
---|
| 124 | |
---|
| 125 | self = filter->inst; |
---|
[5170] | 126 | freq = filter->rate/2.45; |
---|
[1141] | 127 | |
---|
| 128 | switch (what) { |
---|
| 129 | case ROARDSP_RESET_NONE: |
---|
| 130 | return 0; |
---|
| 131 | break; |
---|
| 132 | case ROARDSP_RESET_FULL: |
---|
| 133 | roardsp_lowp_ctl(filter, ROARDSP_FCTL_FREQ, &freq); |
---|
| 134 | case ROARDSP_RESET_STATE: |
---|
| 135 | memset(self->old, 0, sizeof(int32_t)*ROAR_MAX_CHANNELS); |
---|
| 136 | return 0; |
---|
| 137 | break; |
---|
| 138 | default: |
---|
| 139 | return -1; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | return -1; |
---|
| 143 | } |
---|
| 144 | |
---|
[1104] | 145 | #endif |
---|
| 146 | |
---|
[656] | 147 | //ll |
---|