Mercurial > hg > audiostuff
comparison spandsp-0.0.3/spandsp-0.0.3/src/noise.c @ 5:f762bf195c4b
import spandsp-0.0.3
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 16:00:21 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:26cd8f1ef0b1 | 5:f762bf195c4b |
---|---|
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 General Public License version 2, as | |
15 * 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 General Public License for more details. | |
21 * | |
22 * You should have received a copy of the GNU General Public License | |
23 * 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.17 2006/11/28 16:59:56 steveu Exp $ | |
27 */ | |
28 | |
29 /*! \file */ | |
30 | |
31 #ifdef 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 | |
46 #include "spandsp/telephony.h" | |
47 #include "spandsp/dc_restore.h" | |
48 #include "spandsp/noise.h" | |
49 | |
50 int16_t noise(noise_state_t *s) | |
51 { | |
52 int32_t val; | |
53 int i; | |
54 | |
55 /* The central limit theorem says if you add a few random numbers together, | |
56 the result starts to look Gaussian. Quantities above 7 give diminishing | |
57 returns. Quantites above 20 are exceedingly Gaussian. */ | |
58 val = 0; | |
59 for (i = 0; i < s->quality; i++) | |
60 { | |
61 s->rndnum = 1664525U*s->rndnum + 1013904223U; | |
62 val += ((int32_t) s->rndnum) >> 22; | |
63 } | |
64 if (s->class_of_noise == NOISE_CLASS_HOTH) | |
65 { | |
66 /* Hoth noise is room-like. It should be sculpted, at the high and low ends, | |
67 and roll off at 5dB/octave across the main part of the band. However, | |
68 merely rolling off at 6dB/octave across the band gets you close | |
69 to the subjective effect. */ | |
70 s->state = (3*val + 5*s->state) >> 3; | |
71 /* Bring the overall power level back to the pre-filtered level. This | |
72 simple approx. leaves the signal about 0.35dB low. */ | |
73 val = s->state << 1; | |
74 } | |
75 return saturate((val*s->rms) >> 10); | |
76 } | |
77 /*- End of function --------------------------------------------------------*/ | |
78 | |
79 noise_state_t *noise_init_dbm0(noise_state_t *s, int seed, float level, int class_of_noise, int quality) | |
80 { | |
81 return noise_init_dbov(s, seed, (level - DBM0_MAX_POWER), class_of_noise, quality); | |
82 } | |
83 /*- End of function --------------------------------------------------------*/ | |
84 | |
85 noise_state_t *noise_init_dbov(noise_state_t *s, int seed, float level, int class_of_noise, int quality) | |
86 { | |
87 float rms; | |
88 | |
89 memset(s, 0, sizeof(*s)); | |
90 s->rndnum = (uint32_t) seed; | |
91 rms = 32768.0f*powf(10.0f, level/20.0f); | |
92 if (quality < 4) | |
93 s->quality = 4; | |
94 else if (quality > 20) | |
95 s->quality = 20; | |
96 else | |
97 s->quality = quality; | |
98 if (class_of_noise == NOISE_CLASS_HOTH) | |
99 { | |
100 /* Allow for the gain of the filter */ | |
101 rms *= 1.043f; | |
102 } | |
103 s->rms = (int32_t) (rms*sqrt(12.0/quality)); | |
104 s->class_of_noise = class_of_noise; | |
105 return s; | |
106 } | |
107 /*- End of function --------------------------------------------------------*/ | |
108 /*- End of file ------------------------------------------------------------*/ |