[4668] | 1 | //filter_clip.c: |
---|
[979] | 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
[979] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[979] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "libroardsp.h" |
---|
| 27 | |
---|
[5173] | 28 | int roardsp_clip_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
[5270] | 29 | (void)stream, (void)id; |
---|
| 30 | |
---|
[5173] | 31 | if ( (filter->inst = roar_mm_malloc(sizeof(struct roardsp_clip))) == NULL ) |
---|
| 32 | return -1; |
---|
| 33 | |
---|
| 34 | return roardsp_filter_reset(filter, ROARDSP_RESET_FULL); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | int roardsp_clip_uninit(struct roardsp_filter * filter) { |
---|
| 38 | roar_mm_free(filter->inst); |
---|
| 39 | return 0; |
---|
| 40 | } |
---|
| 41 | |
---|
[5177] | 42 | #define _clipX(bits) \ |
---|
| 43 | static inline int##bits##_t _clip##bits (int##bits##_t s, struct roardsp_clip * self) { \ |
---|
| 44 | switch (self->mode) { \ |
---|
| 45 | case ROARDSP_CLIP_MODE_LIMIT: \ |
---|
| 46 | return s > 0 ? self->limit : -self->limit; \ |
---|
| 47 | break; \ |
---|
| 48 | case ROARDSP_CLIP_MODE_ZERO: \ |
---|
| 49 | return 0; \ |
---|
| 50 | break; \ |
---|
| 51 | case ROARDSP_CLIP_MODE_WARP: \ |
---|
| 52 | if ( s > 0 ) { \ |
---|
| 53 | return s - 2*self->limit; \ |
---|
| 54 | } else { \ |
---|
| 55 | return s + 2*self->limit; \ |
---|
| 56 | } \ |
---|
| 57 | break; \ |
---|
| 58 | case ROARDSP_CLIP_MODE_NOISE: \ |
---|
| 59 | return (s > 0 ? 1 : -1) * (self->limit - (roar_random_uint16() & 0xFF)); \ |
---|
| 60 | break; \ |
---|
| 61 | } \ |
---|
| 62 | \ |
---|
| 63 | ROAR_WARN("_clip16(s=%i, self=%p{.mode=%i, ...}) = 0 // ERROR: Bad mode", (int)s, self, (int)self->mode); \ |
---|
| 64 | return 0; \ |
---|
[5173] | 65 | } |
---|
| 66 | |
---|
[5177] | 67 | #define _calcX(bits) \ |
---|
| 68 | int roardsp_clip_calc##bits (struct roardsp_filter * filter, void * data, size_t samples) { \ |
---|
| 69 | struct roardsp_clip * self = filter->inst; \ |
---|
| 70 | int##bits##_t * samp = (int##bits##_t *) data; \ |
---|
| 71 | register int32_t s = self->limit; \ |
---|
| 72 | size_t i; \ |
---|
| 73 | \ |
---|
| 74 | for (i = 0; i < samples; i++) { \ |
---|
| 75 | if ( samp[i] > s ) { \ |
---|
| 76 | samp[i] = _clip##bits (samp[i], self); \ |
---|
| 77 | } else if ( -samp[i] > s ) { \ |
---|
| 78 | samp[i] = _clip##bits (samp[i], self); \ |
---|
| 79 | } \ |
---|
| 80 | } \ |
---|
| 81 | \ |
---|
| 82 | ROAR_DBG("roardsp_quantify_calc16(*) = 0"); \ |
---|
| 83 | return 0; \ |
---|
| 84 | } |
---|
[979] | 85 | |
---|
[5177] | 86 | #define _setX(bits) \ |
---|
| 87 | _clipX(bits) \ |
---|
| 88 | _calcX(bits) |
---|
[979] | 89 | |
---|
[5177] | 90 | _setX(8) |
---|
| 91 | _setX(16) |
---|
| 92 | _setX(32) |
---|
[979] | 93 | |
---|
| 94 | int roardsp_clip_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
[5173] | 95 | struct roardsp_clip * self = filter->inst; |
---|
[979] | 96 | int32_t old; |
---|
| 97 | |
---|
[5173] | 98 | switch (cmd) { |
---|
| 99 | case ROARDSP_FCTL_LIMIT: |
---|
| 100 | old = self->limit; |
---|
| 101 | self->limit = labs(*(int32_t*)data); |
---|
| 102 | *(int32_t*)data = old; |
---|
| 103 | break; |
---|
| 104 | case ROARDSP_FCTL_MODE: |
---|
| 105 | old = self->mode; |
---|
| 106 | self->mode = *(int32_t*)data; |
---|
| 107 | *(int32_t*)data = old; |
---|
| 108 | break; |
---|
| 109 | default: |
---|
| 110 | ROAR_DBG("roardsp_clip_ctl(*) = -1"); |
---|
| 111 | return -1; |
---|
| 112 | break; |
---|
[979] | 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | ROAR_DBG("roardsp_clip_ctl(*) = 0"); |
---|
| 117 | return 0; |
---|
| 118 | } |
---|
| 119 | |
---|
[1141] | 120 | int roardsp_clip_reset (struct roardsp_filter * filter, int what) { |
---|
[5173] | 121 | int32_t mode = ROARDSP_CLIP_MODE_LIMIT; |
---|
[5177] | 122 | int32_t n; |
---|
[1141] | 123 | |
---|
| 124 | if ( filter == NULL ) |
---|
| 125 | return -1; |
---|
| 126 | |
---|
[5177] | 127 | switch (filter->bits) { |
---|
| 128 | case 8: n = 64L; break; |
---|
| 129 | case 16: n = 16384L; break; |
---|
| 130 | case 32: n = 1073741824L; break; |
---|
| 131 | default: |
---|
| 132 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 133 | return -1; |
---|
| 134 | break; |
---|
| 135 | } |
---|
| 136 | |
---|
[1141] | 137 | switch (what) { |
---|
| 138 | case ROARDSP_RESET_NONE: |
---|
| 139 | case ROARDSP_RESET_STATE: |
---|
| 140 | return 0; |
---|
| 141 | break; |
---|
| 142 | case ROARDSP_RESET_FULL: |
---|
| 143 | roardsp_clip_ctl(filter, ROARDSP_FCTL_LIMIT, &n); |
---|
[5173] | 144 | roardsp_clip_ctl(filter, ROARDSP_FCTL_MODE, &mode); |
---|
[1141] | 145 | return 0; |
---|
| 146 | break; |
---|
| 147 | default: |
---|
| 148 | return -1; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | return -1; |
---|
| 152 | } |
---|
| 153 | |
---|
[979] | 154 | //ll |
---|