rename a Buffer/BufferSet method to be a little clearer as to its intent (is_silent...
[ardour.git] / libs / ardour / ardour / 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_buffer_h__
20 #define __ardour_buffer_h__
21
22 #include <cstdlib>
23 #include <cassert>
24 #include <cstring>
25 #include <iostream>
26 #include <boost/utility.hpp>
27
28 #include "ardour/types.h"
29 #include "ardour/data_type.h"
30 #include "ardour/runtime_functions.h"
31
32 namespace ARDOUR {
33
34
35 /** A buffer of recordable/playable data.
36  *
37  * This is a datatype-agnostic base class for all buffers (there are no
38  * methods to actually access the data).  This provides a way for code that
39  * doesn't care about the data type to still deal with buffers (which is
40  * why the base class can't be a template).
41  *
42  * To actually read/write buffer contents, use the appropriate derived class.
43  */
44 class Buffer : public boost::noncopyable
45 {
46 public:
47         virtual ~Buffer() {}
48
49         /** Factory function */
50         static Buffer* create(DataType type, size_t capacity);
51
52         /** Maximum capacity of buffer.
53          * Note in some cases the entire buffer may not contain valid data, use size. */
54         size_t capacity() const { return _capacity; }
55
56         /** Amount of valid data in buffer.  Use this over capacity almost always. */
57         size_t size() const { return _size; }
58
59         /** Return true if the buffer contains no data, false otherwise */
60         virtual bool empty() const { return _size == 0; }
61
62         /** Type of this buffer.
63          * Based on this you can static cast a Buffer* to the desired type. */
64         DataType type() const { return _type; }
65
66         bool silent() const { return _silent; }
67         void set_is_silent(bool yn) { _silent = yn; }
68
69         /** Reallocate the buffer used internally to handle at least @a size_t units of data.
70          *
71          * The buffer is not silent after this operation. the @a capacity argument
72          * passed to the constructor must have been non-zero.
73          */
74         virtual void resize (size_t) = 0;
75
76         /** Clear (eg zero, or empty) buffer */
77         virtual void silence (framecnt_t len, framecnt_t offset = 0) = 0;
78
79         /** Clear the entire buffer */
80         virtual void clear() { silence(_capacity, 0); }
81
82         virtual void read_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) = 0;
83         virtual void merge_from (const Buffer& src, framecnt_t len, framecnt_t dst_offset = 0, framecnt_t src_offset = 0) = 0;
84
85   protected:
86         Buffer(DataType type, size_t capacity)
87                 : _type(type), _capacity(capacity), _size(0), _silent(true)
88         {}
89
90         DataType  _type;
91         pframes_t _capacity;
92         pframes_t _size;
93         bool      _silent;
94 };
95
96
97 } // namespace ARDOUR
98
99 #endif // __ardour_buffer_h__