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