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