Update libardour GPL boilerplate and (C) from git log
[ardour.git] / libs / ardour / thread_buffers.cc
1 /*
2  * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2010-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2013-2017 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 <iostream>
22 #include <algorithm>
23
24 #include "ardour/audioengine.h"
25 #include "ardour/buffer_set.h"
26 #include "ardour/thread_buffers.h"
27
28 using namespace ARDOUR;
29 using namespace std;
30
31 ThreadBuffers::ThreadBuffers ()
32         : silent_buffers (new BufferSet)
33         , scratch_buffers (new BufferSet)
34         , noinplace_buffers (new BufferSet)
35         , route_buffers (new BufferSet)
36         , mix_buffers (new BufferSet)
37         , gain_automation_buffer (0)
38         , trim_automation_buffer (0)
39         , send_gain_automation_buffer (0)
40         , scratch_automation_buffer (0)
41         , pan_automation_buffer (0)
42         , npan_buffers (0)
43 {
44 }
45
46 void
47 ThreadBuffers::ensure_buffers (ChanCount howmany, size_t custom)
48 {
49         // std::cerr << "ThreadBuffers " << this << " resize buffers with count = " << howmany << std::endl;
50
51         /* this is all protected by the process lock in the Session
52          */
53
54         /* we always need at least 1 midi buffer */
55         if (howmany.n_midi() < 1) {
56                 howmany.set_midi(1);
57         }
58
59         if (howmany.n_audio() == 0 && howmany.n_midi() == 1) {
60                 return;
61         }
62
63         AudioEngine* _engine = AudioEngine::instance ();
64
65         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
66                 size_t count = std::max (scratch_buffers->available().get(*t), howmany.get(*t));
67                 size_t size;
68                 if (custom > 0) {
69                         size = custom;
70                 } else {
71                         size = (*t == DataType::MIDI)
72                                 ? _engine->raw_buffer_size (*t)
73                                 : _engine->raw_buffer_size (*t) / sizeof (Sample);
74                 }
75
76                 scratch_buffers->ensure_buffers (*t, count, size);
77                 noinplace_buffers->ensure_buffers (*t, count, size);
78                 mix_buffers->ensure_buffers (*t, count, size);
79                 silent_buffers->ensure_buffers (*t, count, size);
80                 route_buffers->ensure_buffers (*t, count, size);
81         }
82
83         size_t audio_buffer_size = custom > 0 ? custom : _engine->raw_buffer_size (DataType::AUDIO) / sizeof (Sample);
84
85         delete [] gain_automation_buffer;
86         gain_automation_buffer = new gain_t[audio_buffer_size];
87         delete [] trim_automation_buffer;
88         trim_automation_buffer = new gain_t[audio_buffer_size];
89         delete [] send_gain_automation_buffer;
90         send_gain_automation_buffer = new gain_t[audio_buffer_size];
91         delete [] scratch_automation_buffer;
92         scratch_automation_buffer = new gain_t[audio_buffer_size];
93
94         allocate_pan_automation_buffers (audio_buffer_size, howmany.n_audio(), false);
95 }
96
97 void
98 ThreadBuffers::allocate_pan_automation_buffers (samplecnt_t nframes, uint32_t howmany, bool force)
99 {
100         /* we always need at least 2 pan buffers */
101
102         howmany = max (2U, howmany);
103
104         if (!force && howmany <= npan_buffers) {
105                 return;
106         }
107
108         if (pan_automation_buffer) {
109
110                 for (uint32_t i = 0; i < npan_buffers; ++i) {
111                         delete [] pan_automation_buffer[i];
112                 }
113
114                 delete [] pan_automation_buffer;
115         }
116
117         pan_automation_buffer = new pan_t*[howmany];
118
119         for (uint32_t i = 0; i < howmany; ++i) {
120                 pan_automation_buffer[i] = new pan_t[nframes];
121         }
122
123         npan_buffers = howmany;
124 }