[656] | 1 | //lowp.c: |
---|
| 2 | |
---|
[658] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - August 2008 |
---|
| 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 |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
[656] | 25 | #include "libroardsp.h" |
---|
| 26 | |
---|
[673] | 27 | int roardsp_lowp_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
| 28 | struct roardsp_lowp * self = malloc(sizeof(struct roardsp_lowp)); |
---|
[680] | 29 | float freq = filter->rate/2; |
---|
[673] | 30 | |
---|
| 31 | if ( self == NULL ) |
---|
| 32 | return -1; |
---|
| 33 | |
---|
| 34 | memset(self, 0, sizeof(struct roardsp_lowp)); |
---|
| 35 | |
---|
| 36 | filter->inst = (void*) self; |
---|
| 37 | |
---|
| 38 | roardsp_lowp_ctl(filter, ROARDSP_FCTL_FREQ, &freq); |
---|
| 39 | |
---|
| 40 | return 0; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | int roardsp_lowp_uninit(struct roardsp_filter * filter) { |
---|
| 44 | |
---|
| 45 | free(filter->inst); |
---|
| 46 | return 0; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | int roardsp_lowp_calc16 (struct roardsp_filter * filter, void * data, size_t samples) { |
---|
| 50 | struct roardsp_lowp * self = (struct roardsp_lowp *) filter->inst; |
---|
| 51 | int16_t * samp = (int16_t *) data; |
---|
| 52 | register int32_t s; |
---|
| 53 | int i, c; |
---|
| 54 | int channels = filter->channels; |
---|
| 55 | |
---|
| 56 | if ( channels > ROAR_MAX_CHANNELS ) |
---|
| 57 | return -1; |
---|
| 58 | |
---|
| 59 | samples /= channels; |
---|
| 60 | |
---|
[680] | 61 | ROAR_DBG("roardsp_lowp_calc16(*): filtering %i frames of %i channels...", samples, channels); |
---|
| 62 | |
---|
[673] | 63 | // * output[N] = input[N] * A + output[N-1] * B |
---|
| 64 | |
---|
| 65 | for (i = 0; i < samples; i++) { |
---|
| 66 | for (c = 0; c < channels; c++) { |
---|
| 67 | s = samp[i*channels + c] * self->a + self->old[c] * self->b; |
---|
| 68 | |
---|
| 69 | s /= 65536; |
---|
| 70 | |
---|
| 71 | samp[i*channels + c] = s; |
---|
| 72 | self->old[ c] = s; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | return 0; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | int roardsp_lowp_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
| 80 | struct roardsp_lowp * self = (struct roardsp_lowp *) filter->inst; |
---|
| 81 | float lp; |
---|
| 82 | float oldfreq; |
---|
[680] | 83 | float newfreq; |
---|
[673] | 84 | |
---|
| 85 | if ( cmd != ROARDSP_FCTL_FREQ ) |
---|
| 86 | return -1; |
---|
| 87 | |
---|
[680] | 88 | newfreq = *(float*)data; |
---|
| 89 | |
---|
| 90 | lp = exp(-2 * M_PI * newfreq / filter->rate) * 65536; |
---|
[673] | 91 | |
---|
| 92 | self->b = lp; |
---|
| 93 | self->a = 65536 - lp; |
---|
| 94 | |
---|
| 95 | oldfreq = self->freq / 1000; |
---|
[680] | 96 | self->freq = newfreq * 1000; |
---|
[673] | 97 | |
---|
| 98 | *(float*)data = oldfreq; |
---|
| 99 | |
---|
[684] | 100 | ROAR_DBG("roardsp_lowp_ctl(*): oldfreq=%f, newfreq=%f", oldfreq, newfreq); |
---|
[680] | 101 | |
---|
[673] | 102 | return 0; |
---|
| 103 | } |
---|
[666] | 104 | |
---|
[656] | 105 | //ll |
---|