Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / server / supernova / sc / sc_plugin_interface.hpp
blob7f6e9756927b8ae0cc56c6464b1d3019efc52ff8
1 // interface for supercollider plugins
2 // Copyright (C) 2009 Tim Blechmann
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.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; see the file COPYING. If not, write to
16 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 // Boston, MA 02111-1307, USA.
19 #ifndef SC_PLUGIN_INTERFACE_HPP
20 #define SC_PLUGIN_INTERFACE_HPP
22 #include <mutex>
23 #include <vector>
25 #include "../server/audio_bus_manager.hpp"
26 #include "../server/node_types.hpp"
27 #include "../server/synth.hpp"
28 #include "../server/memory_pool.hpp"
30 #include "SC_InterfaceTable.h"
31 #include "SC_World.h"
33 #include <boost/scoped_array.hpp>
35 #include "../../common/SC_SndFileHelpers.hpp"
37 namespace nova {
39 class sc_done_action_handler
41 public:
42 void add_pause_node(server_node * node)
44 spin_lock::scoped_lock lock(cmd_lock);
45 pause_nodes.push_back(node);
48 void add_resume_node(server_node * node)
50 spin_lock::scoped_lock lock(cmd_lock);
51 resume_nodes.push_back(node);
54 void add_done_node(server_node * node)
56 spin_lock::scoped_lock lock(cmd_lock);
57 done_nodes.push_back(node);
60 void add_freeDeep_node(abstract_group * node)
62 spin_lock::scoped_lock lock(cmd_lock);
63 freeDeep_nodes.push_back(node);
66 void add_freeAll_node(abstract_group * node)
68 spin_lock::scoped_lock lock(cmd_lock);
69 freeAll_nodes.push_back(node);
72 void update_nodegraph(void);
74 protected:
75 typedef rt_pool_allocator<server_node*> server_node_alloc;
76 typedef rt_pool_allocator<abstract_group*> abstract_group_alloc;
77 std::vector<server_node*, server_node_alloc> done_nodes, pause_nodes, resume_nodes;
78 std::vector<abstract_group*, abstract_group_alloc> freeAll_nodes, freeDeep_nodes;
80 private:
81 spin_lock cmd_lock; /* multiple synths can be scheduled for removal, so we need to guard this
82 later we can try different approaches like a lockfree stack or bitmask */
85 class sc_plugin_interface:
86 public sc_done_action_handler
88 public:
89 void initialize(class server_arguments const & args, float * control_busses);
90 void reset_sampling_rate(int sr);
92 sc_plugin_interface(void):
93 synths_to_initialize(false)
96 ~sc_plugin_interface(void);
98 InterfaceTable sc_interface;
99 World world;
101 audio_bus_manager audio_busses;
103 int buf_counter(void) const
105 return world.mBufCounter;
108 /* @{ */
109 /* audio buffer handling */
110 SndBuf* allocate_buffer(uint32_t index, uint32_t frames, uint32_t channels);
111 int allocate_buffer(SndBuf * buf, uint32_t frames, uint32_t channels, double samplerate);
112 int buffer_read_alloc(uint32_t index, const char * filename, uint32_t start, uint32_t frames);
113 int buffer_alloc_read_channels(uint32_t index, const char * filename, uint32_t start, uint32_t frames, uint32_t channel_count,
114 const uint32_t * channel_data);
115 int buffer_read(uint32_t index, const char * filename, uint32_t start_file, uint32_t frames, uint32_t start_buffer,
116 bool leave_open);
117 int buffer_read_channel(uint32_t index, const char * filename, uint32_t start_file, uint32_t frames, uint32_t start_buffer,
118 bool leave_open, uint32_t channel_count, const uint32_t * channel_data);
120 sample * get_nrt_mirror_buffer(uint32_t index)
122 return world.mSndBufsNonRealTimeMirror[index].data;
125 sample * get_buffer(uint32_t index)
127 return world.mSndBufs[index].data;
130 SndBuf * get_buffer_struct(uint32_t index)
132 return world.mSndBufs + index;
135 int buffer_write(uint32_t index, const char * filename, const char * header_format, const char * sample_format,
136 uint32_t start, uint32_t frames, bool leave_open);
138 void buffer_zero(uint32_t index);
139 void buffer_close(uint32_t index);
141 sample * buffer_generate(uint32_t index, const char * cmd_name, struct sc_msg_iter & msg);
143 void increment_write_updates(uint32_t index)
145 world.mSndBufUpdates[index].writes++;
148 void free_buffer(uint32_t index);
150 typedef std::lock_guard<std::mutex> buffer_lock_t;
152 std::mutex & buffer_guard(size_t index)
154 return async_buffer_guards[index];
157 private:
158 boost::scoped_array<std::mutex> async_buffer_guards;
159 /* @} */
161 public:
162 /* copies nrt mirror to rt buffers */
163 void buffer_sync(uint32_t index);
165 /* @{ */
166 /* control bus handling */
168 private:
169 void controlbus_set_unchecked(uint32_t bus, float value)
171 world.mControlBus[bus] = value;
172 world.mControlBusTouched[bus] = world.mBufCounter;
175 public:
176 void controlbus_set(uint32_t bus, float value)
178 if (bus < world.mNumControlBusChannels)
179 controlbus_set_unchecked(bus, value);
182 void controlbus_fill(uint32_t bus, size_t count, float value)
184 if (bus + count >= world.mNumControlBusChannels)
185 count = world.mNumAudioBusChannels - bus;
187 for (size_t i = 0; i != count; ++i)
188 controlbus_set_unchecked(bus + i, value);
191 sample controlbus_get(uint32_t bus)
193 if (bus < world.mNumControlBusChannels)
194 return world.mControlBus[bus];
195 else
196 return 0.f;
199 void controlbus_getn(uint32_t bus, size_t count, size_t * r_count, float * r_values)
201 size_t remain = count;
202 if (bus + count >= world.mNumControlBusChannels)
203 remain = world.mNumAudioBusChannels - bus;
204 *r_count = remain;
206 for (size_t i = 0; i != remain; ++i)
207 r_values[i] = world.mControlBus[i];
209 /* @}*/
211 /* @{ */
212 /** synth initialization. called in the beginning of each dsp tick */
213 void initialize_synths(void)
215 if (likely(!synths_to_initialize))
216 return; // fast-path
218 initialize_synths_perform();
221 void schedule_for_preparation(abstract_synth * synth)
223 synths_to_initialize = true;
224 uninitialized_synths.push_back(synth);
225 synth->add_ref();
228 private:
229 bool synths_to_initialize;
231 void initialize_synths_perform(void);
232 std::vector<abstract_synth*, rt_pool_allocator<abstract_synth*> > uninitialized_synths;
233 /* @} */
236 } /* namespace nova */
238 #endif /* SC_PLUGIN_INTERFACE_HPP */