more work on the suprisingly ongoing filename/path/origin issue
[ardour.git] / libs / ardour / thread_buffers.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21 #include <algorithm>
22
23 #include "ardour/audioengine.h"
24 #include "ardour/buffer_set.h"
25 #include "ardour/thread_buffers.h"
26
27 using namespace ARDOUR;
28 using namespace std;
29
30 ThreadBuffers::ThreadBuffers ()
31         : silent_buffers (new BufferSet)
32         , scratch_buffers (new BufferSet)
33         , mix_buffers (new BufferSet)
34         , gain_automation_buffer (0)
35         , send_gain_automation_buffer (0)
36         , pan_automation_buffer (0)
37         , npan_buffers (0)
38 {
39 }
40
41 void
42 ThreadBuffers::ensure_buffers (ChanCount howmany)
43 {
44         // std::cerr << "ThreadBuffers " << this << " resize buffers with count = " << howmany << std::endl;
45
46         /* this is all protected by the process lock in the Session
47          */
48
49         /* we always need at least 1 midi buffer */
50         if (howmany.n_midi() < 1) {
51                 howmany.set_midi(1);
52         }
53
54         if (howmany.n_audio() == 0 && howmany.n_midi() == 1) {
55                 return;
56         }
57
58         AudioEngine* _engine = AudioEngine::instance ();
59
60         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
61                 size_t count = std::max (scratch_buffers->available().get(*t), howmany.get(*t));
62                 size_t size = _engine->raw_buffer_size (*t);
63
64                 scratch_buffers->ensure_buffers (*t, count, size);
65                 mix_buffers->ensure_buffers (*t, count, size);
66                 silent_buffers->ensure_buffers (*t, count, size);
67         }
68
69         delete [] gain_automation_buffer;
70         gain_automation_buffer = new gain_t[_engine->raw_buffer_size (DataType::AUDIO)];
71         delete [] send_gain_automation_buffer;
72         send_gain_automation_buffer = new gain_t[_engine->raw_buffer_size (DataType::AUDIO)];
73
74         allocate_pan_automation_buffers (_engine->raw_buffer_size (DataType::AUDIO), howmany.n_audio(), false);
75 }
76
77 void
78 ThreadBuffers::allocate_pan_automation_buffers (framecnt_t nframes, uint32_t howmany, bool force)
79 {
80         /* we always need at least 2 pan buffers */
81
82         howmany = max (2U, howmany);
83
84         if (!force && howmany <= npan_buffers) {
85                 return;
86         }
87
88         if (pan_automation_buffer) {
89
90                 for (uint32_t i = 0; i < npan_buffers; ++i) {
91                         delete [] pan_automation_buffer[i];
92                 }
93
94                 delete [] pan_automation_buffer;
95         }
96
97         pan_automation_buffer = new pan_t*[howmany];
98
99         for (uint32_t i = 0; i < howmany; ++i) {
100                 pan_automation_buffer[i] = new pan_t[nframes];
101         }
102
103         npan_buffers = howmany;
104 }