[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) { |
---|
| 29 | if ( (filter->inst = roar_mm_malloc(sizeof(struct roardsp_clip))) == NULL ) |
---|
| 30 | return -1; |
---|
| 31 | |
---|
| 32 | return roardsp_filter_reset(filter, ROARDSP_RESET_FULL); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | int roardsp_clip_uninit(struct roardsp_filter * filter) { |
---|
| 36 | roar_mm_free(filter->inst); |
---|
| 37 | return 0; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | static inline int16_t _clip16(int16_t s, struct roardsp_clip * self) { |
---|
| 41 | switch (self->mode) { |
---|
| 42 | case ROARDSP_CLIP_MODE_LIMIT: |
---|
| 43 | return s > 0 ? self->limit : -self->limit; |
---|
| 44 | break; |
---|
| 45 | case ROARDSP_CLIP_MODE_ZERO: |
---|
| 46 | return 0; |
---|
| 47 | break; |
---|
| 48 | case ROARDSP_CLIP_MODE_WARP: |
---|
| 49 | if ( s > 0 ) { |
---|
| 50 | return s - self->limit - 32768; |
---|
| 51 | } else { |
---|
| 52 | return 32767 - s + self->limit; |
---|
| 53 | } |
---|
| 54 | break; |
---|
| 55 | case ROARDSP_CLIP_MODE_NOISE: |
---|
| 56 | return (s > 0 ? 1 : -1) * (self->limit - (roar_random_uint16() & 0xFF)); |
---|
| 57 | break; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | ROAR_WARN("_clip16(s=%i, self=%p{.mode=%i, ...}) = 0 // ERROR: Bad mode", (int)s, self, (int)self->mode); |
---|
| 61 | return 0; |
---|
| 62 | } |
---|
| 63 | |
---|
[979] | 64 | int roardsp_clip_calc16 (struct roardsp_filter * filter, void * data, size_t samples) { |
---|
[5173] | 65 | struct roardsp_clip * self = filter->inst; |
---|
[979] | 66 | int16_t * samp = (int16_t *) data; |
---|
[5173] | 67 | register int32_t s = self->limit; |
---|
[979] | 68 | size_t i; |
---|
| 69 | |
---|
| 70 | for (i = 0; i < samples; i++) { |
---|
| 71 | if ( samp[i] > s ) { |
---|
[5173] | 72 | samp[i] = _clip16(samp[i], self); |
---|
[979] | 73 | } else if ( -samp[i] > s ) { |
---|
[5173] | 74 | samp[i] = _clip16(samp[i], self); |
---|
[979] | 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | ROAR_DBG("roardsp_quantify_calc16(*) = 0"); |
---|
| 79 | return 0; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | int roardsp_clip_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
[5173] | 83 | struct roardsp_clip * self = filter->inst; |
---|
[979] | 84 | int32_t old; |
---|
| 85 | |
---|
[5173] | 86 | switch (cmd) { |
---|
| 87 | case ROARDSP_FCTL_LIMIT: |
---|
| 88 | old = self->limit; |
---|
| 89 | self->limit = labs(*(int32_t*)data); |
---|
| 90 | *(int32_t*)data = old; |
---|
| 91 | break; |
---|
| 92 | case ROARDSP_FCTL_MODE: |
---|
| 93 | old = self->mode; |
---|
| 94 | self->mode = *(int32_t*)data; |
---|
| 95 | *(int32_t*)data = old; |
---|
| 96 | break; |
---|
| 97 | default: |
---|
| 98 | ROAR_DBG("roardsp_clip_ctl(*) = -1"); |
---|
| 99 | return -1; |
---|
| 100 | break; |
---|
[979] | 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | ROAR_DBG("roardsp_clip_ctl(*) = 0"); |
---|
| 105 | return 0; |
---|
| 106 | } |
---|
| 107 | |
---|
[1141] | 108 | int roardsp_clip_reset (struct roardsp_filter * filter, int what) { |
---|
| 109 | int32_t n = 16384; |
---|
[5173] | 110 | int32_t mode = ROARDSP_CLIP_MODE_LIMIT; |
---|
[1141] | 111 | |
---|
| 112 | if ( filter == NULL ) |
---|
| 113 | return -1; |
---|
| 114 | |
---|
| 115 | switch (what) { |
---|
| 116 | case ROARDSP_RESET_NONE: |
---|
| 117 | case ROARDSP_RESET_STATE: |
---|
| 118 | return 0; |
---|
| 119 | break; |
---|
| 120 | case ROARDSP_RESET_FULL: |
---|
| 121 | roardsp_clip_ctl(filter, ROARDSP_FCTL_LIMIT, &n); |
---|
[5173] | 122 | roardsp_clip_ctl(filter, ROARDSP_FCTL_MODE, &mode); |
---|
[1141] | 123 | return 0; |
---|
| 124 | break; |
---|
| 125 | default: |
---|
| 126 | return -1; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | return -1; |
---|
| 130 | } |
---|
| 131 | |
---|
[979] | 132 | //ll |
---|