~ubuntu-branches/ubuntu/quantal/zita-lrx/quantal

« back to all changes in this revision

Viewing changes to source/delayproc.h

  • Committer: Package Import Robot
  • Author(s): Jaromír Mikeš
  • Date: 2012-02-06 01:45:26 UTC
  • Revision ID: package-import@ubuntu.com-20120206014526-f2a1vieinfb0nw3q
Tags: upstream-0.1.0
Import upstream version 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  ------------------------------------------------------------------------
 
2
//
 
3
//  Copyright (C) 2011-2012 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 3 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, see <http://www.gnu.org/licenses/>.
 
17
//
 
18
//  ------------------------------------------------------------------------
 
19
 
 
20
 
 
21
#ifndef __DELAYPROC_H
 
22
#define __DELAYPROC_H
 
23
 
 
24
 
 
25
#include <string.h>
 
26
 
 
27
 
 
28
// Audio delay class.
 
29
// 
 
30
//   maxdel = maximum delay in samples.
 
31
//   period = number of samples guaranteed to be available for
 
32
//            linear reading or writing, i.e. without wraparound. 
 
33
//
 
34
// To read or write: 
 
35
//    * Get pointer.
 
36
//    * Read or write up to 'period' samples.
 
37
//    * Commit number of samples used.
 
38
 
 
39
class Delayproc
 
40
{
 
41
public:
 
42
 
 
43
    Delayproc (int maxdel, int period);
 
44
    ~Delayproc (void);
 
45
 
 
46
    // Clear buffer.
 
47
    void reset (void);
 
48
 
 
49
    // Delay must be in the range 0...'maxdel'.
 
50
    void set_delay (int k)
 
51
    {
 
52
        _rdind = _wrind - k;
 
53
        if (_rdind < 0) _rdind += _length;
 
54
    }
 
55
 
 
56
    // Get write or read pointer. Either can be used as
 
57
    // the base of an array of size 'period' samples.
 
58
    float *wr_ptr (void) const { return _data + _wrind; }
 
59
    float *rd_ptr (void) const { return _data + _rdind; }
 
60
 
 
61
    // Commit writing of k samples.
 
62
    void wr_commit (int k)
 
63
    {
 
64
        int    a, b, n;
 
65
        float  *p, *q;
 
66
 
 
67
        // This ensures that the first and last period sized
 
68
        // blocks remain identical, allowing linear access
 
69
        // for both reading and writing.
 
70
        a = _wrind;
 
71
        b = a + k;
 
72
        n = b - _length;
 
73
        if (n >= 0)
 
74
        {
 
75
            _wrind = n;
 
76
            if (n > 0)
 
77
            {
 
78
                p = _data;
 
79
                q = p + _length;
 
80
                memcpy (p, q, n * sizeof (float));
 
81
            }
 
82
        }
 
83
        else
 
84
        {
 
85
            _wrind = b;
 
86
            n = _period - a;
 
87
            if (n > 0)
 
88
            {
 
89
                q = _data + a;
 
90
                p = q + _length;
 
91
                if (n > k) n = k;
 
92
                memcpy (p, q, n * sizeof (float));
 
93
            }
 
94
        }
 
95
    }
 
96
 
 
97
    // Commit reading of k samples.
 
98
    void rd_commit (int k)
 
99
    {
 
100
        _rdind += k;
 
101
        if (_rdind >= _length) _rdind -= _length;
 
102
    }
 
103
 
 
104
private:
 
105
 
 
106
    int     _length;
 
107
    int     _period;
 
108
    int     _wrind;
 
109
    int     _rdind;
 
110
    float  *_data;
 
111
};
 
112
 
 
113
 
 
114
#endif