1 /*  $Id: ADSRSource.h,v 1.1 2012/07/08 00:45:55 sarrazip Exp $
2 
3     flatzebra - SDL-based sound renderer
4     Copyright (C) 2011 Pierre Sarrazin <http://sarrazip.com/>
5 
6     This program is free software; you can redistribute it and/or
7     modify it under the terms of the GNU General Public License
8     as published by the Free Software Foundation; either version 2
9     of the License, or (at your option) any later version.
10 
11     This program 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
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public
17     License along with this program; if not, write to the Free
18     Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19     Boston, MA  02110-1301, USA.
20 */
21 
22 #pragma once
23 
24 #include <roundbeetle/SampleSource.h>
25 
26 #include <assert.h>
27 #include <math.h>
28 #include <iostream>
29 
30 
31 namespace roundbeetle {
32 
33 class ADSR;
34 
35 
36 class ADSRSource : public SampleSource
37 {
38 public:
39 
40     // _adsr: the total time in seconds multiplied by the renderer frequency
41     //        in Hertz must not exceed 2**32 - 1, i.e., 0xffffffff.
42     //
43     ADSRSource(SampleSource *_src, const ADSR &_adsr);
44 
45     virtual ~ADSRSource();
46 
47     // Returns mono PCM-16 signal.
48     // Returns no samples after the release time has passed.
49     //
50     virtual size_t getSamples(Sint16 *dest, size_t numRequested);
51 
52     // Return true if the source has finished or if the release time has passed.
53     //
54     virtual bool isFinished() const;
55 
56     virtual bool rewind();
57 
58 private:
59 
60     float getCurrentLevel() const;
61 
62     ADSRSource(const ADSRSource &);
63     ADSRSource &operator = (const ADSRSource &);
64 
65 private:
66 
67     SampleSource *src;
68     size_t idx;
69     const float attackLevel;
70     const float sustainLevel;
71     const size_t decayStartIdx;
72     const size_t sustainStartIdx;
73     const size_t releaseStartIdx;
74     const size_t endIdx;
75 
76 };
77 
78 
79 }  // namespace roundbeetle
80