Update libardour GPL boilerplate and (C) from git log
[ardour.git] / libs / ardour / audio_buffer.cc
1 /*
2  * Copyright (C) 2007-2010 David Robillard <d@drobilla.net>
3  * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2013-2014 Robin Gareus <robin@gareus.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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 License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <errno.h>
22
23 #include "ardour/audio_buffer.h"
24 #include "pbd/error.h"
25 #include "pbd/malign.h"
26
27 #include "pbd/i18n.h"
28
29 using namespace PBD;
30 using namespace ARDOUR;
31
32 AudioBuffer::AudioBuffer(size_t capacity)
33         : Buffer (DataType::AUDIO)
34         , _owns_data (false)
35         , _data (0)
36 {
37         if (capacity) {
38                 _owns_data = true; // prevent resize() from gagging
39                 resize (capacity);
40                 _silent = false; // force silence on the intial buffer state
41                 clear ();
42         }
43 }
44
45 AudioBuffer::~AudioBuffer()
46 {
47         if (_owns_data)
48                 cache_aligned_free(_data);
49 }
50
51 void
52 AudioBuffer::resize (size_t size)
53 {
54         if (!_owns_data) {
55                 /* XXX how the hell is this enforced? */
56                 _capacity = size;
57                 return;
58         }
59
60         if (_data && size < _capacity) {
61                 /* buffer is already large enough */
62                 return;
63         }
64
65         cache_aligned_free (_data);
66
67         cache_aligned_malloc ((void**) &_data, sizeof (Sample) * size);
68
69         _capacity = size;
70         _silent = false;
71 }
72
73 bool
74 AudioBuffer::check_silence (pframes_t nframes, pframes_t& n) const
75 {
76         for (n = 0; n < nframes; ++n) {
77                 if (_data[n] != Sample (0)) {
78                         return false;
79                 }
80         }
81         return true;
82 }
83
84 void
85 AudioBuffer::silence (samplecnt_t len, samplecnt_t offset) {
86
87         if (!_silent) {
88                 assert(_capacity > 0);
89                 assert(offset + len <= _capacity);
90                 memset(_data + offset, 0, sizeof (Sample) * len);
91                 if (len == _capacity) {
92                         _silent = true;
93                 }
94         }
95         _written = true;
96 }