Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/src/noise.c @ 4:26cd8f1ef0b1
import spandsp-0.0.6pre17
| author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
|---|---|
| date | Fri, 25 Jun 2010 15:50:58 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 3:c6c5a16ce2f2 | 4:26cd8f1ef0b1 |
|---|---|
| 1 /* | |
| 2 * SpanDSP - a series of DSP components for telephony | |
| 3 * | |
| 4 * noise.c - A low complexity audio noise generator, suitable for | |
| 5 * real time generation (current AWGN, and Hoth) | |
| 6 * | |
| 7 * Written by Steve Underwood <steveu@coppice.org> | |
| 8 * | |
| 9 * Copyright (C) 2005 Steve Underwood | |
| 10 * | |
| 11 * All rights reserved. | |
| 12 * | |
| 13 * This program is free software; you can redistribute it and/or modify | |
| 14 * it under the terms of the GNU Lesser General Public License version 2.1, | |
| 15 * as published by the Free Software Foundation. | |
| 16 * | |
| 17 * This program is distributed in the hope that it will be useful, | |
| 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 20 * GNU Lesser General Public License for more details. | |
| 21 * | |
| 22 * You should have received a copy of the GNU Lesser General Public | |
| 23 * License along with this program; if not, write to the Free Software | |
| 24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 25 * | |
| 26 * $Id: noise.c,v 1.31 2009/02/10 13:06:46 steveu Exp $ | |
| 27 */ | |
| 28 | |
| 29 /*! \file */ | |
| 30 | |
| 31 #if defined(HAVE_CONFIG_H) | |
| 32 #include "config.h" | |
| 33 #endif | |
| 34 | |
| 35 #include <stdlib.h> | |
| 36 #include <stdio.h> | |
| 37 #include <inttypes.h> | |
| 38 #include <memory.h> | |
| 39 #if defined(HAVE_TGMATH_H) | |
| 40 #include <tgmath.h> | |
| 41 #endif | |
| 42 #if defined(HAVE_MATH_H) | |
| 43 #include <math.h> | |
| 44 #endif | |
| 45 #include "floating_fudge.h" | |
| 46 | |
| 47 #include "spandsp/telephony.h" | |
| 48 #include "spandsp/fast_convert.h" | |
| 49 #include "spandsp/saturated.h" | |
| 50 #include "spandsp/noise.h" | |
| 51 | |
| 52 #include "spandsp/private/noise.h" | |
| 53 | |
| 54 SPAN_DECLARE(int16_t) noise(noise_state_t *s) | |
| 55 { | |
| 56 int32_t val; | |
| 57 int i; | |
| 58 | |
| 59 /* The central limit theorem says if you add a few random numbers together, | |
| 60 the result starts to look Gaussian. Quantities above 7 give diminishing | |
| 61 returns. Quantites above 20 are exceedingly Gaussian. */ | |
| 62 val = 0; | |
| 63 for (i = 0; i < s->quality; i++) | |
| 64 { | |
| 65 s->rndnum = 1664525U*s->rndnum + 1013904223U; | |
| 66 val += ((int32_t) s->rndnum) >> 22; | |
| 67 } | |
| 68 if (s->class_of_noise == NOISE_CLASS_HOTH) | |
| 69 { | |
| 70 /* Hoth noise is room-like. It should be sculpted, at the high and low ends, | |
| 71 and roll off at 5dB/octave across the main part of the band. However, | |
| 72 merely rolling off at 6dB/octave across the band gets you close | |
| 73 to the subjective effect. */ | |
| 74 s->state = (3*val + 5*s->state) >> 3; | |
| 75 /* Bring the overall power level back to the pre-filtered level. This | |
| 76 simple approx. leaves the signal about 0.35dB low. */ | |
| 77 val = s->state << 1; | |
| 78 } | |
| 79 return saturate((val*s->rms) >> 10); | |
| 80 } | |
| 81 /*- End of function --------------------------------------------------------*/ | |
| 82 | |
| 83 SPAN_DECLARE(noise_state_t *) noise_init_dbov(noise_state_t *s, int seed, float level, int class_of_noise, int quality) | |
| 84 { | |
| 85 float rms; | |
| 86 | |
| 87 if (s == NULL) | |
| 88 { | |
| 89 if ((s = (noise_state_t *) malloc(sizeof(*s))) == NULL) | |
| 90 return NULL; | |
| 91 } | |
| 92 memset(s, 0, sizeof(*s)); | |
| 93 s->rndnum = (uint32_t) seed; | |
| 94 rms = 32768.0f*powf(10.0f, level/20.0f); | |
| 95 if (quality < 4) | |
| 96 s->quality = 4; | |
| 97 else if (quality > 20) | |
| 98 s->quality = 20; | |
| 99 else | |
| 100 s->quality = quality; | |
| 101 if (class_of_noise == NOISE_CLASS_HOTH) | |
| 102 { | |
| 103 /* Allow for the gain of the filter */ | |
| 104 rms *= 1.043f; | |
| 105 } | |
| 106 s->rms = (int32_t) (rms*sqrtf(12.0f/s->quality)); | |
| 107 s->class_of_noise = class_of_noise; | |
| 108 return s; | |
| 109 } | |
| 110 /*- End of function --------------------------------------------------------*/ | |
| 111 | |
| 112 SPAN_DECLARE(noise_state_t *) noise_init_dbm0(noise_state_t *s, int seed, float level, int class_of_noise, int quality) | |
| 113 { | |
| 114 return noise_init_dbov(s, seed, (level - DBM0_MAX_POWER), class_of_noise, quality); | |
| 115 } | |
| 116 /*- End of function --------------------------------------------------------*/ | |
| 117 | |
| 118 SPAN_DECLARE(int) noise_release(noise_state_t *s) | |
| 119 { | |
| 120 return 0; | |
| 121 } | |
| 122 /*- End of function --------------------------------------------------------*/ | |
| 123 | |
| 124 SPAN_DECLARE(int) noise_free(noise_state_t *s) | |
| 125 { | |
| 126 free(s); | |
| 127 return 0; | |
| 128 } | |
| 129 /*- End of function --------------------------------------------------------*/ | |
| 130 /*- End of file ------------------------------------------------------------*/ |
