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