first pass at internal sends. this is a very tentative work in progress, and it is...
[ardour.git] / libs / ardour / ardour / audio_buffer.h
1 /*
2     Copyright (C) 2006 Paul Davis 
3     
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8     
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more details.
13     
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #ifndef __ardour_audio_buffer_h__
20 #define __ardour_audio_buffer_h__
21
22 #include <cstring>
23 #include <ardour/buffer.h>
24
25 namespace ARDOUR {
26
27 class AudioBuffer : public Buffer
28 {
29 public:
30         AudioBuffer(size_t capacity);
31         
32         ~AudioBuffer();
33
34         void silence(nframes_t len, nframes_t offset = 0) {
35                 if (!_silent) {
36                         assert(_capacity > 0);
37                         assert(offset + len <= _capacity);
38                         memset(_data + offset, 0, sizeof (Sample) * len);
39                         if (offset == 0 && len == _capacity) {
40                                 _silent = true;
41                         }
42                 }
43         }
44         
45         /** Read @a len frames FROM THE START OF @a src into self at @a offset */
46         void read_from(const Buffer& src, nframes_t len, nframes_t offset) {
47                 assert(&src != this);
48                 assert(_capacity > 0);
49                 assert(src.type() == DataType::AUDIO);
50                 assert(offset + len <= _capacity);
51                 memcpy(_data + offset, ((AudioBuffer&)src).data(), sizeof(Sample) * len);
52                 _silent = src.silent();
53         }
54         
55         /** Accumulate (add)@a len frames FROM THE START OF @a src into self at @a offset */
56         void accumulate_from(const AudioBuffer& src, nframes_t len, nframes_t offset) {
57                 assert(_capacity > 0);
58                 assert(offset + len <= _capacity);
59
60                 Sample*       const dst_raw = _data + offset;
61                 const Sample* const src_raw = src.data();
62
63                 mix_buffers_no_gain(dst_raw, src_raw, len);
64
65                 _silent = (src.silent() && _silent);
66         }
67         
68         /** Accumulate (add) @a len frames FROM THE START OF @a src into self at @a offset
69          * scaling by @a gain_coeff */
70         void accumulate_with_gain_from(const AudioBuffer& src, nframes_t len, nframes_t offset, gain_t gain_coeff) {
71
72                 assert(_capacity > 0);
73                 assert(offset + len <= _capacity);
74
75                 if (src.silent()) {
76                         return;
77                 }
78
79                 Sample*       const dst_raw = _data + offset;
80                 const Sample* const src_raw = src.data();
81
82                 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
83
84                 _silent = ( (src.silent() && _silent) || (_silent && gain_coeff == 0) );
85         }
86
87         /** Accumulate (add) @a len frames FROM THE START OF @a src into self at @a offset
88          * scaling by @a gain_coeff */
89         void accumulate_with_gain_from(const Sample* src_raw, nframes_t len, nframes_t offset, gain_t gain_coeff) {
90
91                 assert(_capacity > 0);
92                 assert(offset + len <= _capacity);
93
94                 Sample*       const dst_raw = _data + offset;
95
96                 mix_buffers_with_gain (dst_raw, src_raw, len, gain_coeff);
97
98                 _silent = (_silent && gain_coeff == 0);
99         }
100         
101         void apply_gain(gain_t gain, nframes_t len, nframes_t offset=0) {
102                 apply_gain_to_buffer (_data + offset, len, gain);
103         }
104
105         /** Set the data contained by this buffer manually (for setting directly to jack buffer).
106          * 
107          * Constructor MUST have been passed capacity=0 or this will die (to prevent mem leaks).
108          */
109         void set_data (Sample* data, size_t size) {
110                 assert(!_owns_data); // prevent leaks
111                 _capacity = size;
112                 _size = size;
113                 _data = data;
114                 _silent = false;
115         }
116
117         /** Reallocate the buffer used internally to handle at least @nframes of data
118          * 
119          * Constructor MUST have been passed capacity!=0 or this will die (to prevent mem leaks).
120          */
121         void resize (size_t nframes);
122
123         const Sample* data () const { return _data; }
124         Sample* data () { return _data; }
125
126         const Sample* data(nframes_t nframes, nframes_t offset) const
127                 { assert(offset + nframes <= _capacity); return _data + offset; }
128
129         Sample* data (nframes_t nframes, nframes_t offset)
130                 { assert(offset + nframes <= _capacity); return _data + offset; }
131
132         void replace_data (size_t nframes);
133
134         void drop_data () {
135                 assert (_owns_data);
136                 assert (_data);
137
138                 free (_data);
139                 _data = 0;
140                 _size = 0;
141                 _capacity = 0;
142                 _silent = false;
143         }
144
145         void copy_to_internal (Sample* p, nframes_t cnt, nframes_t offset);
146
147   private:
148         bool    _owns_data;
149         Sample* _data; ///< Actual buffer contents
150 };
151
152
153 } // namespace ARDOUR
154
155 #endif // __ardour_audio_audio_buffer_h__