~ubuntu-branches/ubuntu/quantal/tetraproc/quantal

« back to all changes in this revision

Viewing changes to source/dither.cc

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2011-03-24 14:56:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110324145648-r9qi83fs3wej4801
Tags: upstream-0.8.2
ImportĀ upstreamĀ versionĀ 0.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -------------------------------------------------------------------------
 
2
//
 
3
//    Copyright (C) 2010 Fons Adriaensen <fons@linuxaudio.org>
 
4
//    
 
5
//    This program is free software; you can redistribute it and/or modify
 
6
//    it under the terms of the GNU General Public License as published by
 
7
//    the Free Software Foundation; either version 2 of the License, or
 
8
//    (at your option) any later version.
 
9
//
 
10
//    This program is distributed in the hope that it will be useful,
 
11
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
//    GNU General Public License for more details.
 
14
//
 
15
//    You should have received a copy of the GNU General Public License
 
16
//    along with this program; if not, write to the Free Software
 
17
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
//
 
19
// -------------------------------------------------------------------------
 
20
 
 
21
 
 
22
#include <string.h>
 
23
#include <math.h>
 
24
#include "dither.h"
 
25
 
 
26
 
 
27
float Dither::_div = 0;
 
28
 
 
29
#define SCALE 32768.0f
 
30
#define LIMIT 32767
 
31
 
 
32
 
 
33
 
 
34
Dither::Dither (void)
 
35
{
 
36
    reset ();
 
37
    _div = ldexpf (1.0f, 32);
 
38
}
 
39
 
 
40
 
 
41
void Dither::reset (void)
 
42
{
 
43
    memset (_err, 0, (SIZE + 4) * sizeof(float));
 
44
    _ind = SIZE - 1;
 
45
    _ran = 1234567;
 
46
}
 
47
 
 
48
 
 
49
void Dither::proc_rectangular (const float *srce, int16_t *dest, int step, int nsam)
 
50
{
 
51
    float    v, r;
 
52
    int32_t  k;
 
53
 
 
54
    while (nsam--)
 
55
    {
 
56
        r = genrand () - 0.5f;
 
57
        v = *srce * SCALE + r;
 
58
        k = lrintf (v);
 
59
        if      (k < -LIMIT) k = -LIMIT;
 
60
        else if (k >  LIMIT) k =  LIMIT;
 
61
        *dest = k;
 
62
        srce += step;
 
63
        dest += step;
 
64
    }
 
65
}
 
66
 
 
67
 
 
68
void Dither::proc_triangular (const float *srce, int16_t *dest, int step, int nsam)
 
69
{
 
70
    float    v, r0, r1;
 
71
    int32_t  k;
 
72
 
 
73
    r1 = *_err;
 
74
    while (nsam--)
 
75
    {
 
76
        r0 = genrand ();
 
77
        v = *srce * SCALE + r0 - r1;
 
78
        r1 = r0;
 
79
        k = lrintf (v);
 
80
        if      (k < -LIMIT) k = -LIMIT;
 
81
        else if (k >  LIMIT) k =  LIMIT;
 
82
        *dest = k;
 
83
        srce += step;
 
84
        dest += step;
 
85
    }
 
86
    *_err = r1;
 
87
}
 
88
 
 
89
 
 
90
void Dither::proc_lipschitz (const float *srce, int16_t *dest, int step, int nsam)
 
91
{
 
92
    float    e, u, v, *p;
 
93
    int      i;
 
94
    int32_t  k;
 
95
 
 
96
    i = _ind;
 
97
    while (nsam--)
 
98
    {
 
99
        p = _err + i;
 
100
        u = *srce * SCALE
 
101
            - 2.033f * p [0]
 
102
            + 2.165f * p [1]
 
103
            - 1.959f * p [2]
 
104
            + 1.590f * p [3]
 
105
            - 0.615f * p [4];
 
106
        v = u + genrand () - genrand ();
 
107
        k = lrintf (v);
 
108
        e = k - u;
 
109
        if      (k < -LIMIT) k = -LIMIT;
 
110
        else if (k >  LIMIT) k =  LIMIT;
 
111
        *dest = k;
 
112
        if (--i < 0)
 
113
        {
 
114
            _err [SIZE + 0] = _err [0];
 
115
            _err [SIZE + 1] = _err [1];
 
116
            _err [SIZE + 2] = _err [2];
 
117
            _err [SIZE + 3] = _err [3];
 
118
            i += SIZE;
 
119
        }
 
120
        _err [i] = e;
 
121
        srce += step;
 
122
        dest += step;
 
123
    }
 
124
    _ind = i;
 
125
}
 
126
 
 
127