substantive change: use the JACK wait API and provide "thread buffers" separately...
[ardour.git] / libs / ardour / process_thread.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 "ardour/audioengine.h"
22 #include "ardour/buffer.h"
23 #include "ardour/buffer_manager.h"
24 #include "ardour/buffer_set.h"
25 #include "ardour/process_thread.h"
26 #include "ardour/thread_buffers.h"
27
28 using namespace ARDOUR;
29 using namespace Glib;
30 using namespace std;
31
32 Private<ThreadBuffers>* ProcessThread::_private_thread_buffers = 0;
33
34 static void 
35 release_thread_buffer (void* arg)
36 {
37         BufferManager::put_thread_buffers ((ThreadBuffers*) arg);
38 }
39
40 void
41 ProcessThread::init ()
42 {
43         _private_thread_buffers = new Private<ThreadBuffers> (release_thread_buffer);
44 }
45
46 ProcessThread::ProcessThread ()
47         : _thread (0)
48 {
49 }
50
51 ProcessThread::~ProcessThread ()
52 {
53 }
54
55 void
56 ProcessThread::get_buffers ()
57 {
58         ThreadBuffers* tb = BufferManager::get_thread_buffers ();
59
60         assert (tb);
61         _private_thread_buffers->set (tb);
62         cerr << "ProcThread " << this << " using TBs at " << tb << " (aka. " << _private_thread_buffers->get() << endl;
63 }
64
65 void
66 ProcessThread::drop_buffers ()
67 {
68         ThreadBuffers* tb = _private_thread_buffers->get();
69         assert (tb);
70         BufferManager::put_thread_buffers (tb);
71         _private_thread_buffers->set (0);
72         cerr << "ProcThread " << this << " dropped TBs\n";
73 }
74
75 BufferSet&
76 ProcessThread::get_silent_buffers (ChanCount count)
77 {
78         ThreadBuffers* tb = _private_thread_buffers->get();
79         assert (tb);
80
81         BufferSet* sb = tb->silent_buffers;
82         assert (sb);
83
84         assert(sb->available() >= count);
85         sb->set_count(count);
86
87         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
88                 for (size_t i= 0; i < count.get(*t); ++i) {
89                         sb->get(*t, i).clear();
90                 }
91         }
92
93         return *sb;
94 }
95
96 BufferSet&
97 ProcessThread::get_scratch_buffers (ChanCount count)
98 {
99         ThreadBuffers* tb = _private_thread_buffers->get();
100         assert (tb);
101
102         BufferSet* sb = tb->scratch_buffers;
103         assert (sb);
104
105         if (count != ChanCount::ZERO) {
106                 assert(sb->available() >= count);
107                 sb->set_count (count);
108         } else {
109                 sb->set_count (sb->available());
110         }
111
112         return *sb;
113 }
114
115 BufferSet&
116 ProcessThread::get_mix_buffers (ChanCount count)
117 {
118         ThreadBuffers* tb = _private_thread_buffers->get();
119         assert (tb);
120
121         BufferSet* mb = tb->mix_buffers;
122
123         assert (mb);
124         assert (mb->available() >= count);
125         mb->set_count(count);
126         return *mb;
127 }
128
129 gain_t*
130 ProcessThread::gain_automation_buffer()
131 {
132         ThreadBuffers* tb = _private_thread_buffers->get();
133         assert (tb);
134
135         gain_t *g =  tb->gain_automation_buffer;
136         assert (g);
137         return g;
138 }
139
140 pan_t**
141 ProcessThread::pan_automation_buffer()
142 {
143         ThreadBuffers* tb = _private_thread_buffers->get();
144         assert (tb);
145
146         pan_t** p = tb->pan_automation_buffer;
147         assert (p);
148         return p;
149 }