//filter_clip.c: /* * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 * * This file is part of libroardsp a part of RoarAudio, * a cross-platform sound system for both, home and professional use. * See README for details. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 * as published by the Free Software Foundation. * * libroardsp is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software; see the file COPYING. If not, write to * the Free Software Foundation, 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #include "libroardsp.h" int roardsp_clip_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { if ( (filter->inst = roar_mm_malloc(sizeof(struct roardsp_clip))) == NULL ) return -1; return roardsp_filter_reset(filter, ROARDSP_RESET_FULL); } int roardsp_clip_uninit(struct roardsp_filter * filter) { roar_mm_free(filter->inst); return 0; } static inline int16_t _clip16(int16_t s, struct roardsp_clip * self) { switch (self->mode) { case ROARDSP_CLIP_MODE_LIMIT: return s > 0 ? self->limit : -self->limit; break; case ROARDSP_CLIP_MODE_ZERO: return 0; break; case ROARDSP_CLIP_MODE_WARP: if ( s > 0 ) { return s - self->limit - 32768; } else { return 32767 - s + self->limit; } break; case ROARDSP_CLIP_MODE_NOISE: return (s > 0 ? 1 : -1) * (self->limit - (roar_random_uint16() & 0xFF)); break; } ROAR_WARN("_clip16(s=%i, self=%p{.mode=%i, ...}) = 0 // ERROR: Bad mode", (int)s, self, (int)self->mode); return 0; } int roardsp_clip_calc16 (struct roardsp_filter * filter, void * data, size_t samples) { struct roardsp_clip * self = filter->inst; int16_t * samp = (int16_t *) data; register int32_t s = self->limit; size_t i; for (i = 0; i < samples; i++) { if ( samp[i] > s ) { samp[i] = _clip16(samp[i], self); } else if ( -samp[i] > s ) { samp[i] = _clip16(samp[i], self); } } ROAR_DBG("roardsp_quantify_calc16(*) = 0"); return 0; } int roardsp_clip_ctl (struct roardsp_filter * filter, int cmd, void * data) { struct roardsp_clip * self = filter->inst; int32_t old; switch (cmd) { case ROARDSP_FCTL_LIMIT: old = self->limit; self->limit = labs(*(int32_t*)data); *(int32_t*)data = old; break; case ROARDSP_FCTL_MODE: old = self->mode; self->mode = *(int32_t*)data; *(int32_t*)data = old; break; default: ROAR_DBG("roardsp_clip_ctl(*) = -1"); return -1; break; } ROAR_DBG("roardsp_clip_ctl(*) = 0"); return 0; } int roardsp_clip_reset (struct roardsp_filter * filter, int what) { int32_t n = 16384; int32_t mode = ROARDSP_CLIP_MODE_LIMIT; if ( filter == NULL ) return -1; switch (what) { case ROARDSP_RESET_NONE: case ROARDSP_RESET_STATE: return 0; break; case ROARDSP_RESET_FULL: roardsp_clip_ctl(filter, ROARDSP_FCTL_LIMIT, &n); roardsp_clip_ctl(filter, ROARDSP_FCTL_MODE, &mode); return 0; break; default: return -1; } return -1; } //ll