~ubuntu-branches/ubuntu/gutsy/audacity/gutsy

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/pa_common/pa_dither.c

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2007-05-17 02:36:41 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517023641-5yjqt9434cbcb6ph
Tags: 1.3.2-4
* debian/patches:
   - desktop_file.patch: fixed broken Category entry
* debian/audacity.mime:
   - added entry for application/x-audacity-project

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id: pa_dither.c,v 1.1.2.2 2006/04/08 16:12:25 richardash1981 Exp $
3
 
 * Portable Audio I/O Library triangular dither generator
4
 
 *
5
 
 * Based on the Open Source API proposed by Ross Bencina
6
 
 * Copyright (c) 1999-2002 Phil Burk, Ross Bencina
7
 
 *
8
 
 * Permission is hereby granted, free of charge, to any person obtaining
9
 
 * a copy of this software and associated documentation files
10
 
 * (the "Software"), to deal in the Software without restriction,
11
 
 * including without limitation the rights to use, copy, modify, merge,
12
 
 * publish, distribute, sublicense, and/or sell copies of the Software,
13
 
 * and to permit persons to whom the Software is furnished to do so,
14
 
 * subject to the following conditions:
15
 
 *
16
 
 * The above copyright notice and this permission notice shall be
17
 
 * included in all copies or substantial portions of the Software.
18
 
 *
19
 
 * Any person wishing to distribute modifications to the Software is
20
 
 * requested to send the modifications to the original developer so that
21
 
 * they can be incorporated into the canonical version.
22
 
 *
23
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26
 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
27
 
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
28
 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
 
 */
31
 
 
32
 
/** @file
33
 
 @brief Functions for generating dither noise
34
 
*/
35
 
 
36
 
 
37
 
#include "pa_dither.h"
38
 
#include "pa_types.h"
39
 
 
40
 
#define PA_DITHER_BITS_   (15)
41
 
 
42
 
 
43
 
void PaUtil_InitializeTriangularDitherState( PaUtilTriangularDitherGenerator *state )
44
 
{
45
 
    state->previous = 0;
46
 
    state->randSeed1 = 22222;
47
 
    state->randSeed2 = 5555555;
48
 
}
49
 
 
50
 
 
51
 
signed long PaUtil_Generate16BitTriangularDither( PaUtilTriangularDitherGenerator *state )
52
 
{
53
 
    signed long current, highPass;
54
 
 
55
 
    /* Generate two random numbers. */
56
 
    state->randSeed1 = (state->randSeed1 * 196314165) + 907633515;
57
 
    state->randSeed2 = (state->randSeed2 * 196314165) + 907633515;
58
 
 
59
 
    /* Generate triangular distribution about 0.
60
 
     * Shift before adding to prevent overflow which would skew the distribution.
61
 
     * Also shift an extra bit for the high pass filter. 
62
 
     */
63
 
#define DITHER_SHIFT_  ((SIZEOF_LONG*8 - PA_DITHER_BITS_) + 1)
64
 
    current = (((signed long)state->randSeed1)>>DITHER_SHIFT_) +
65
 
              (((signed long)state->randSeed2)>>DITHER_SHIFT_);
66
 
 
67
 
    /* High pass filter to reduce audibility. */
68
 
    highPass = current - state->previous;
69
 
    state->previous = current;
70
 
    return highPass;
71
 
}
72
 
 
73
 
 
74
 
/* Multiply by PA_FLOAT_DITHER_SCALE_ to get a float between -2.0 and +1.99999 */
75
 
#define PA_FLOAT_DITHER_SCALE_  (1.0f / ((1<<PA_DITHER_BITS_)-1))
76
 
static const float const_float_dither_scale_ = PA_FLOAT_DITHER_SCALE_;
77
 
 
78
 
float PaUtil_GenerateFloatTriangularDither( PaUtilTriangularDitherGenerator *state )
79
 
{
80
 
    signed long current, highPass;
81
 
 
82
 
    /* Generate two random numbers. */
83
 
    state->randSeed1 = (state->randSeed1 * 196314165) + 907633515;
84
 
    state->randSeed2 = (state->randSeed2 * 196314165) + 907633515;
85
 
 
86
 
    /* Generate triangular distribution about 0.
87
 
     * Shift before adding to prevent overflow which would skew the distribution.
88
 
     * Also shift an extra bit for the high pass filter. 
89
 
     */
90
 
#define DITHER_SHIFT_  ((SIZEOF_LONG*8 - PA_DITHER_BITS_) + 1)
91
 
    current = (((signed long)state->randSeed1)>>DITHER_SHIFT_) +
92
 
              (((signed long)state->randSeed2)>>DITHER_SHIFT_);
93
 
 
94
 
    /* High pass filter to reduce audibility. */
95
 
    highPass = current - state->previous;
96
 
    state->previous = current;
97
 
    return ((float)highPass) * const_float_dither_scale_;
98
 
}
99
 
 
100
 
 
101
 
/*
102
 
The following alternate dither algorithms (from musicdsp.org) could be
103
 
considered
104
 
*/
105
 
 
106
 
/*Noise shaped dither  (March 2000)
107
 
-------------------
108
 
 
109
 
This is a simple implementation of highpass triangular-PDF dither with
110
 
2nd-order noise shaping, for use when truncating floating point audio
111
 
data to fixed point.
112
 
 
113
 
The noise shaping lowers the noise floor by 11dB below 5kHz (@ 44100Hz
114
 
sample rate) compared to triangular-PDF dither. The code below assumes
115
 
input data is in the range +1 to -1 and doesn't check for overloads!
116
 
 
117
 
To save time when generating dither for multiple channels you can do
118
 
things like this:  r3=(r1 & 0x7F)<<8; instead of calling rand() again.
119
 
 
120
 
 
121
 
 
122
 
  int   r1, r2;                //rectangular-PDF random numbers
123
 
  float s1, s2;                //error feedback buffers
124
 
  float s = 0.5f;              //set to 0.0f for no noise shaping
125
 
  float w = pow(2.0,bits-1);   //word length (usually bits=16)
126
 
  float wi= 1.0f/w;            
127
 
  float d = wi / RAND_MAX;     //dither amplitude (2 lsb)
128
 
  float o = wi * 0.5f;         //remove dc offset
129
 
  float in, tmp;
130
 
  int   out;
131
 
 
132
 
 
133
 
//for each sample...
134
 
 
135
 
  r2=r1;                               //can make HP-TRI dither by
136
 
  r1=rand();                           //subtracting previous rand()
137
 
    
138
 
  in += s * (s1 + s1 - s2);            //error feedback
139
 
  tmp = in + o + d * (float)(r1 - r2); //dc offset and dither 
140
 
  
141
 
  out = (int)(w * tmp);                //truncate downwards
142
 
  if(tmp<0.0f) out--;                  //this is faster than floor()
143
 
 
144
 
  s2 = s1;                            
145
 
  s1 = in - wi * (float)out;           //error
146
 
 
147
 
 
148
 
 
149
 
paul.kellett@maxim.abel.co.uk
150
 
http://www.maxim.abel.co.uk
151
 
*/
152
 
 
153
 
 
154
 
/*
155
 
16-to-8-bit first-order dither
156
 
 
157
 
Type : First order error feedforward dithering code
158
 
References : Posted by Jon Watte
159
 
 
160
 
Notes : 
161
 
This is about as simple a dithering algorithm as you can implement, but it's
162
 
likely to sound better than just truncating to N bits.
163
 
 
164
 
Note that you might not want to carry forward the full difference for infinity.
165
 
It's probably likely that the worst performance hit comes from the saturation
166
 
conditionals, which can be avoided with appropriate instructions on many DSPs
167
 
and integer SIMD type instructions, or CMOV.
168
 
 
169
 
Last, if sound quality is paramount (such as when going from > 16 bits to 16
170
 
bits) you probably want to use a higher-order dither function found elsewhere
171
 
on this site. 
172
 
 
173
 
 
174
 
Code : 
175
 
// This code will down-convert and dither a 16-bit signed short 
176
 
// mono signal into an 8-bit unsigned char signal, using a first 
177
 
// order forward-feeding error term dither. 
178
 
 
179
 
#define uchar unsigned char 
180
 
 
181
 
void dither_one_channel_16_to_8( short * input, uchar * output, int count, int * memory ) 
182
 
183
 
  int m = *memory; 
184
 
  while( count-- > 0 ) { 
185
 
    int i = *input++; 
186
 
    i += m; 
187
 
    int j = i + 32768 - 128; 
188
 
    uchar o; 
189
 
    if( j < 0 ) { 
190
 
      o = 0; 
191
 
    } 
192
 
    else if( j > 65535 ) { 
193
 
      o = 255; 
194
 
    } 
195
 
    else { 
196
 
      o = (uchar)((j>>8)&0xff); 
197
 
    } 
198
 
    m = ((j-32768+128)-i); 
199
 
    *output++ = o; 
200
 
  } 
201
 
  *memory = m; 
202
 
203
 
*/