Mercurial > audlegacy
annotate src/libSAD/noicegen.c @ 4830:901cd268f0ca
Do NOT save the config if we are crashing. This is a *hideously* bad idea.
author | William Pitcock <nenolod@atheme.org> |
---|---|
date | Sun, 01 Mar 2009 23:17:11 -0600 |
parents | bb0638143fc8 |
children |
rev | line source |
---|---|
4232
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
1 #include <stdio.h> |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
2 #include <assert.h> |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
3 #include "../../config.h" |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
4 |
4233
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
5 #ifdef HAVE_SSE2 |
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
6 # define SSE2 1 |
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
7 #endif |
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
8 |
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
9 #ifdef HAVE_ALTIVEC |
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
10 # define ALTIVEC 1 |
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
11 #endif |
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
12 |
4232
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
13 #define MEXP 19937 |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
14 |
4251
1046f9c3174d
- compile fix: add extra.mk to Makefile
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4250
diff
changeset
|
15 #include "SFMT.h" |
4232
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
16 #include "SFMT.c" |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
17 |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
18 #include "noicegen.h" |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
19 |
4757
bb0638143fc8
move libSAD's DEBUG_MSG out of global scope, as it breaks timidity and possible something else
Andrew O. Shadoura <bugzilla@tut.by>
parents:
4256
diff
changeset
|
20 #include "debug.h" |
bb0638143fc8
move libSAD's DEBUG_MSG out of global scope, as it breaks timidity and possible something else
Andrew O. Shadoura <bugzilla@tut.by>
parents:
4256
diff
changeset
|
21 |
4232
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
22 int triangular_dither_noise(int nbits) |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
23 { |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
24 // parameter nbits : the peak-to-peak amplitude desired (in bits) |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
25 // use with nbits set to 2 + nber of bits to be trimmed. |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
26 // (because triangular is made from two uniformly distributed processes, |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
27 // it starts at 2 bits peak-to-peak amplitude) |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
28 // see The Theory of Dithered Quantization by Robert Alexander Wannamaker |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
29 // for complete proof of why that's optimal |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
30 |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
31 int v = (gen_rand32() / 2 - gen_rand32() / 2); // in ]-2^31, 2^31[ |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
32 //int signe = (v>0) ? 1 : -1; |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
33 int P = 1 << (32 - nbits); // the power of 2 |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
34 v /= P; |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
35 // now v in ]-2^(nbits-1), 2^(nbits-1) [ |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
36 |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
37 return v; |
704607c1f858
1st attempt to integrate dithering and RG engine
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff
changeset
|
38 } |
4233
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
39 |
4256
b0ca963fd965
adaptive scaler added, disabled hard limiter
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4251
diff
changeset
|
40 double triangular_dither_noise_f() { |
4233
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
41 // Сonditionally assume we have 16 bits in fractional part |
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
42 // Please, check it thoroughly: is this assumption correct in floatin-point arithmetic? |
4256
b0ca963fd965
adaptive scaler added, disabled hard limiter
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4251
diff
changeset
|
43 return (double) triangular_dither_noise(17) / 65536.0; |
4233
74c6f3d3cf1d
it buids successfully :)
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4232
diff
changeset
|
44 } |
4234 | 45 |
46 void noicegen_init_rand(uint32_t seed) { | |
47 init_gen_rand(seed); | |
48 } |