1 | //dcblock.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
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 | int roardsp_dcblock_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
29 | struct roardsp_dcblock * inst = roar_mm_malloc(sizeof(struct roardsp_dcblock)); |
---|
30 | // roardsp_downmix_ctl(filter, ROARDSP_FCTL_MODE, &mode); |
---|
31 | |
---|
32 | if ( inst == NULL ) { |
---|
33 | ROAR_ERR("roardsp_dcblock_init(*): Can not alloc memory for filter dcblock: %s", strerror(errno)); |
---|
34 | filter->inst = NULL; |
---|
35 | return -1; |
---|
36 | } |
---|
37 | |
---|
38 | memset(inst, 0, sizeof(struct roardsp_dcblock)); |
---|
39 | |
---|
40 | filter->inst = inst; |
---|
41 | |
---|
42 | return 0; |
---|
43 | } |
---|
44 | |
---|
45 | int roardsp_dcblock_uninit (struct roardsp_filter * filter) { |
---|
46 | if ( filter == NULL ) |
---|
47 | return -1; |
---|
48 | |
---|
49 | if ( filter->inst == NULL ) |
---|
50 | return -1; |
---|
51 | |
---|
52 | roar_mm_free(filter->inst); |
---|
53 | |
---|
54 | return 0; |
---|
55 | } |
---|
56 | |
---|
57 | int roardsp_dcblock_reset (struct roardsp_filter * filter, int what) { |
---|
58 | if ( filter == NULL ) |
---|
59 | return -1; |
---|
60 | |
---|
61 | if ( filter->inst == NULL ) |
---|
62 | return -1; |
---|
63 | |
---|
64 | ROAR_DBG("roardsp_dcblock_reset(filter=%p, what=%i) = ?", filter, what); |
---|
65 | |
---|
66 | switch (what) { |
---|
67 | case ROARDSP_RESET_NONE: |
---|
68 | return 0; |
---|
69 | break; |
---|
70 | case ROARDSP_RESET_FULL: |
---|
71 | memset(filter->inst, 0, sizeof(struct roardsp_dcblock)); |
---|
72 | return 0; |
---|
73 | break; |
---|
74 | case ROARDSP_RESET_STATE: |
---|
75 | memset(((struct roardsp_dcblock*)filter->inst)->dc, 0, sizeof(int32_t)*ROARDSP_DCBLOCK_NUMBLOCKS); |
---|
76 | ((struct roardsp_dcblock*)filter->inst)->cur = 0; |
---|
77 | return 0; |
---|
78 | break; |
---|
79 | default: |
---|
80 | return -1; |
---|
81 | } |
---|
82 | |
---|
83 | return -1; |
---|
84 | } |
---|
85 | |
---|
86 | int roardsp_dcblock_calc16 (struct roardsp_filter * filter, void * data, size_t samples) { |
---|
87 | struct roardsp_dcblock * inst = filter->inst; |
---|
88 | int16_t * samp = (int16_t *) data; |
---|
89 | register int64_t s = 0; |
---|
90 | size_t i; |
---|
91 | |
---|
92 | for (i = 0; i < samples; i++) { |
---|
93 | // ROAR_WARN("roardsp_dcblock_calc16(*): s=%i, samp[i=%i]=%i", s, i, samp[i]); |
---|
94 | s += samp[i]; |
---|
95 | } |
---|
96 | |
---|
97 | ROAR_DBG("roardsp_dcblock_calc16(*): s=%i (current block of %i samples)", s, samples); |
---|
98 | |
---|
99 | s = (float)(s / samples); |
---|
100 | |
---|
101 | ROAR_DBG("roardsp_dcblock_calc16(*): inst=%p, inst->cur=%i", inst, inst->cur); |
---|
102 | inst->dc[(inst->cur)++] = s; |
---|
103 | |
---|
104 | if ( inst->cur == ROARDSP_DCBLOCK_NUMBLOCKS ) |
---|
105 | inst->cur = 0; |
---|
106 | |
---|
107 | s = 0; |
---|
108 | |
---|
109 | for (i = 0; i < ROARDSP_DCBLOCK_NUMBLOCKS; i++) |
---|
110 | s += inst->dc[i]; |
---|
111 | |
---|
112 | s /= ROARDSP_DCBLOCK_NUMBLOCKS; |
---|
113 | |
---|
114 | // s += (ROAR_INSTINT)filter->inst; |
---|
115 | |
---|
116 | ROAR_DBG("roardsp_dcblock_calc16(*): new DC value: %i", s); |
---|
117 | |
---|
118 | for (i = 0; i < samples; i++) |
---|
119 | samp[i] -= s; |
---|
120 | |
---|
121 | ROAR_DBG("roardsp_downmix_calc16(*) = 0"); |
---|
122 | return 0; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | //ll |
---|