~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to arts/modules/synth_cdelay_impl.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    /*
 
2
 
 
3
    Copyright (C) 2000 Jeff Tranter
 
4
                       tranter@pobox.com
 
5
 
 
6
    This library is free software; you can redistribute it and/or
 
7
    modify it under the terms of the GNU Library General Public
 
8
    License as published by the Free Software Foundation; either
 
9
    version 2 of the License, or (at your option) any later version.
 
10
  
 
11
    This library is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
    Library General Public License for more details.
 
15
   
 
16
    You should have received a copy of the GNU Library General Public License
 
17
    along with this library; see the file COPYING.LIB.  If not, write to
 
18
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
    Boston, MA 02111-1307, USA.
 
20
 
 
21
    */
 
22
 
 
23
#include "artsmodules.h"
 
24
#include "stdsynthmodule.h"
 
25
 
 
26
using namespace Arts;
 
27
 
 
28
// This delays the input signal for an amount of time. The time
 
29
// specification must be between 0 and 1 for a delay between 0 and 1
 
30
// second. The delay is constant during the calculation, that means it
 
31
// can't be modified. This saves computing time as no interpolation is
 
32
// done, and is useful for recursive structures. See the description
 
33
// for Synth_DELAY.
 
34
 
 
35
class Synth_CDELAY_impl : virtual public Synth_CDELAY_skel,
 
36
                                                        virtual public StdSynthModule
 
37
{
 
38
protected:
 
39
        float _time;
 
40
        static const int SAMPLINGRATE = 44100;
 
41
        static const int MAXDELAY = SAMPLINGRATE;
 
42
        float *dbuffer; // holds one second of past data
 
43
 
 
44
public:
 
45
        Synth_CDELAY_impl()
 
46
        {
 
47
                dbuffer=new float[MAXDELAY];
 
48
        }
 
49
        ~Synth_CDELAY_impl()
 
50
        {
 
51
                delete [] dbuffer;
 
52
        }
 
53
        
 
54
        float time() { return _time; }
 
55
 
 
56
        void time(float newTime) { _time = newTime; }         
 
57
 
 
58
        void streamInit()
 
59
        {
 
60
                // initialize buffer to all zeroes
 
61
                for (int i=0; i < MAXDELAY; i++)
 
62
                        dbuffer[i] = 0.0;
 
63
        }
 
64
 
 
65
        void calculateBlock(unsigned long samples)
 
66
        {
 
67
                // make sure delay is in range
 
68
                if (_time < 0.0) _time = 0.0;
 
69
                if (_time > 1.0) _time = 1.0;
 
70
 
 
71
                // calculate delay in samples
 
72
                unsigned long delay = (unsigned long) (_time * SAMPLINGRATE);
 
73
 
 
74
// Calculating output data
 
75
//
 
76
// 0                 MAXDELAY 0         samples
 
77
// +------------------------+ +---------+
 
78
// |        dbuffer         | | invalue |
 
79
// +------------------------+ +---------+
 
80
//                    <-delay->
 
81
//                    +----------+
 
82
//                    | outvalue |
 
83
//                    +----------+
 
84
//                    0         samples
 
85
 
 
86
                for (unsigned long i=0; i<samples; i++)
 
87
                {
 
88
                        if (i >= delay)
 
89
                                outvalue[i] = invalue[i - delay];
 
90
                        else
 
91
                                outvalue[i] = dbuffer[MAXDELAY - delay + i];
 
92
                }
 
93
 
 
94
// Updating delay buffer
 
95
//
 
96
// 0                 MAXDELAY 0         samples
 
97
// +------------------------+ +---------+
 
98
// |        dbuffer         | | invalue |
 
99
// +------------------------+ +---------+
 
100
//             +------------------------+
 
101
//             |        dbuffer         |
 
102
//             +------------------------+
 
103
//             0                      MAXDELAY
 
104
 
 
105
                // shift dbuffer
 
106
                for (unsigned long i=0; i < MAXDELAY - samples; i++)
 
107
                {
 
108
                        dbuffer[i] = dbuffer[i + samples];
 
109
                }
 
110
 
 
111
                // copy input buffer to the end of it
 
112
                for (unsigned long i=0; i<samples; i++)
 
113
                {
 
114
                        dbuffer[MAXDELAY - samples + i] = invalue[i];
 
115
                }
 
116
        }
 
117
};
 
118
 
 
119
REGISTER_IMPLEMENTATION(Synth_CDELAY_impl);