[5548] | 1 | //filter_goertzel.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
| 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, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
| 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "libroardsp.h" |
---|
| 27 | |
---|
| 28 | #ifdef ROAR_HAVE_LIBM |
---|
| 29 | |
---|
| 30 | #define DEFAULT_FREQ 1000.0 |
---|
| 31 | |
---|
| 32 | struct roardsp_goertzel { |
---|
| 33 | float freq; |
---|
| 34 | int32_t coeff; |
---|
| 35 | int32_t old[ROAR_MAX_CHANNELS][2]; |
---|
| 36 | }; |
---|
| 37 | |
---|
| 38 | int roardsp_goertzel_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
| 39 | struct roardsp_goertzel * self = roar_mm_malloc(sizeof(struct roardsp_goertzel)); |
---|
| 40 | |
---|
| 41 | (void)stream, (void)id; |
---|
| 42 | |
---|
| 43 | if ( self == NULL ) |
---|
| 44 | return -1; |
---|
| 45 | |
---|
| 46 | memset(self, 0, sizeof(struct roardsp_goertzel)); |
---|
| 47 | |
---|
| 48 | filter->inst = (void*) self; |
---|
| 49 | |
---|
| 50 | roardsp_goertzel_reset(filter, ROARDSP_RESET_FULL); |
---|
| 51 | |
---|
| 52 | return 0; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | int roardsp_goertzel_uninit(struct roardsp_filter * filter) { |
---|
| 56 | |
---|
| 57 | roar_mm_free(filter->inst); |
---|
| 58 | return 0; |
---|
| 59 | } |
---|
| 60 | |
---|
[5551] | 61 | #define _calcX(bits,twobits,limit) \ |
---|
[5548] | 62 | int roardsp_goertzel_calc##bits (struct roardsp_filter * filter, void * data, size_t samples) { \ |
---|
| 63 | struct roardsp_goertzel * self = (struct roardsp_goertzel *) filter->inst; \ |
---|
| 64 | int##bits##_t * samp = (int##bits##_t *) data; \ |
---|
| 65 | /*register int##twobits##_t s, g, h;*/ \ |
---|
| 66 | register float s, g, h; \ |
---|
| 67 | /*register int64_t s, g, h;*/ \ |
---|
| 68 | int channel; \ |
---|
| 69 | size_t i; \ |
---|
| 70 | \ |
---|
| 71 | roardsp_goertzel_reset(filter, ROARDSP_RESET_STATE); \ |
---|
| 72 | \ |
---|
| 73 | for (i = 0, channel = 0; i < samples; i++, channel = channel == (filter->channels - 1) ? 0 : channel + 1) { \ |
---|
| 74 | g = self->old[channel][0]; \ |
---|
| 75 | g *= self->coeff; \ |
---|
| 76 | g /= 32767; \ |
---|
| 77 | s = samp[i]; \ |
---|
[5651] | 78 | ROAR_DBG("roardsp_goertzel_calc*(*): channel=%i, g=%f, s=%f, old[0]=%li, old[1]=%li", channel, g, s, (long int)self->old[channel][0], (long int)self->old[channel][1]); \ |
---|
[5548] | 79 | s += g; \ |
---|
| 80 | s -= self->old[channel][1]; \ |
---|
| 81 | self->old[channel][1] = self->old[channel][0]; \ |
---|
| 82 | self->old[channel][0] = s; \ |
---|
| 83 | /* samp[i] = s; */ \ |
---|
| 84 | g = s; \ |
---|
| 85 | g *= self->old[channel][1]; \ |
---|
| 86 | g *= self->coeff; \ |
---|
| 87 | g /= 32767; \ |
---|
| 88 | h = self->old[channel][1]; \ |
---|
| 89 | h *= self->old[channel][1]; \ |
---|
| 90 | s *= s; \ |
---|
| 91 | s += h; \ |
---|
| 92 | s -= g; \ |
---|
[5551] | 93 | s *= 0.0001; \ |
---|
| 94 | s /= (float)((i+1)*(i+1)); \ |
---|
| 95 | if ( s > (limit) ) \ |
---|
| 96 | s = (limit); \ |
---|
[5548] | 97 | samp[i] = s; \ |
---|
[5551] | 98 | } \ |
---|
[5548] | 99 | \ |
---|
| 100 | return 0; \ |
---|
| 101 | } |
---|
| 102 | |
---|
[5551] | 103 | _calcX(8,16,127.) |
---|
| 104 | _calcX(16,32,32767.) |
---|
| 105 | _calcX(32,64,2147483647.) |
---|
[5548] | 106 | |
---|
| 107 | int roardsp_goertzel_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
| 108 | struct roardsp_goertzel * self = (struct roardsp_goertzel *) filter->inst; |
---|
| 109 | float old; |
---|
| 110 | |
---|
| 111 | switch (cmd) { |
---|
| 112 | case ROARDSP_FCTL_FREQ: |
---|
| 113 | old = self->freq; |
---|
| 114 | self->freq = *(float*)data; |
---|
| 115 | *(float*)data = old; |
---|
| 116 | old = 2.*cos(2.*M_PI*self->freq/(float)filter->rate); |
---|
| 117 | self->coeff = old*32767.; |
---|
| 118 | ROAR_DBG("roardsp_goertzel_ctl(filter=%p, cmd=%i, data=%p): self->coeff=%li (%f)", filter, cmd, data, (long int)self->coeff, old); |
---|
| 119 | break; |
---|
| 120 | default: |
---|
| 121 | roar_err_set(ROAR_ERROR_BADRQC); |
---|
| 122 | return -1; |
---|
| 123 | break; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return 0; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | int roardsp_goertzel_reset (struct roardsp_filter * filter, int what) { |
---|
| 130 | struct roardsp_goertzel * self; |
---|
| 131 | float freq; |
---|
| 132 | |
---|
| 133 | if ( filter == NULL ) |
---|
| 134 | return -1; |
---|
| 135 | |
---|
| 136 | if ( filter->inst == NULL ) |
---|
| 137 | return -1; |
---|
| 138 | |
---|
| 139 | self = filter->inst; |
---|
| 140 | |
---|
| 141 | switch (what) { |
---|
| 142 | case ROARDSP_RESET_NONE: |
---|
| 143 | return 0; |
---|
| 144 | break; |
---|
| 145 | case ROARDSP_RESET_FULL: |
---|
| 146 | freq = self->freq = DEFAULT_FREQ; |
---|
| 147 | roardsp_goertzel_ctl(filter, ROARDSP_FCTL_FREQ, &freq); |
---|
| 148 | case ROARDSP_RESET_STATE: |
---|
| 149 | memset(self->old, 0, sizeof(self->old)); |
---|
| 150 | return 0; |
---|
| 151 | break; |
---|
| 152 | default: |
---|
| 153 | roar_err_set(ROAR_ERROR_BADRQC); |
---|
| 154 | return -1; |
---|
| 155 | break; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | return -1; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | #endif |
---|
| 162 | |
---|
| 163 | //ll |
---|