1 // interface for supercollider plugins
2 // Copyright (C) 2009 Tim Blechmann
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.
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
24 #include "../server/audio_bus_manager.hpp"
25 #include "../server/node_types.hpp"
26 #include "../server/synth.hpp"
27 #include "../server/memory_pool.hpp"
29 #include "SC_InterfaceTable.h"
32 #include <boost/scoped_array.hpp>
33 #include <boost/thread/mutex.hpp>
35 #include "../../common/SC_SndFileHelpers.hpp"
40 class sc_done_action_handler
43 void add_pause_node(server_node
* node
)
45 spin_lock::scoped_lock
lock(cmd_lock
);
46 pause_nodes
.push_back(node
);
49 void add_resume_node(server_node
* node
)
51 spin_lock::scoped_lock
lock(cmd_lock
);
52 resume_nodes
.push_back(node
);
55 void add_done_node(server_node
* node
)
57 spin_lock::scoped_lock
lock(cmd_lock
);
58 done_nodes
.push_back(node
);
61 void add_freeDeep_node(abstract_group
* node
)
63 spin_lock::scoped_lock
lock(cmd_lock
);
64 freeDeep_nodes
.push_back(node
);
67 void add_freeAll_node(abstract_group
* node
)
69 spin_lock::scoped_lock
lock(cmd_lock
);
70 freeAll_nodes
.push_back(node
);
73 void update_nodegraph(void);
76 typedef rt_pool_allocator
<server_node
*> server_node_alloc
;
77 typedef rt_pool_allocator
<abstract_group
*> abstract_group_alloc
;
78 std::vector
<server_node
*, server_node_alloc
> done_nodes
, pause_nodes
, resume_nodes
;
79 std::vector
<abstract_group
*, abstract_group_alloc
> freeAll_nodes
, freeDeep_nodes
;
82 spin_lock cmd_lock
; /* multiple synths can be scheduled for removal, so we need to guard this
83 later we can try different approaches like a lockfree stack or bitmask */
86 class sc_plugin_interface
:
87 public sc_done_action_handler
90 void initialize(class server_arguments
const & args
, float * control_busses
);
91 void reset_sampling_rate(int sr
);
93 sc_plugin_interface(void):
94 synths_to_initialize(false)
97 ~sc_plugin_interface(void);
99 InterfaceTable sc_interface
;
102 audio_bus_manager audio_busses
;
104 int buf_counter(void) const
106 return world
.mBufCounter
;
110 /* audio buffer handling */
111 SndBuf
* allocate_buffer(uint32_t index
, uint32_t frames
, uint32_t channels
);
112 int allocate_buffer(SndBuf
* buf
, uint32_t frames
, uint32_t channels
, double samplerate
);
113 int buffer_read_alloc(uint32_t index
, const char * filename
, uint32_t start
, uint32_t frames
);
114 int buffer_alloc_read_channels(uint32_t index
, const char * filename
, uint32_t start
, uint32_t frames
, uint32_t channel_count
,
115 const uint32_t * channel_data
);
116 int buffer_read(uint32_t index
, const char * filename
, uint32_t start_file
, uint32_t frames
, uint32_t start_buffer
,
118 int buffer_read_channel(uint32_t index
, const char * filename
, uint32_t start_file
, uint32_t frames
, uint32_t start_buffer
,
119 bool leave_open
, uint32_t channel_count
, const uint32_t * channel_data
);
121 sample
* get_nrt_mirror_buffer(uint32_t index
)
123 return world
.mSndBufsNonRealTimeMirror
[index
].data
;
126 sample
* get_buffer(uint32_t index
)
128 return world
.mSndBufs
[index
].data
;
131 SndBuf
* get_buffer_struct(uint32_t index
)
133 return world
.mSndBufs
+ index
;
136 int buffer_write(uint32_t index
, const char * filename
, const char * header_format
, const char * sample_format
,
137 uint32_t start
, uint32_t frames
, bool leave_open
);
139 void buffer_zero(uint32_t index
);
140 void buffer_close(uint32_t index
);
142 sample
* buffer_generate(uint32_t index
, const char * cmd_name
, struct sc_msg_iter
& msg
);
144 void increment_write_updates(uint32_t index
)
146 world
.mSndBufUpdates
[index
].writes
++;
149 void free_buffer(uint32_t index
);
151 typedef boost::mutex::scoped_lock buffer_lock_t
;
153 boost::mutex
& buffer_guard(size_t index
)
155 return async_buffer_guards
[index
];
159 boost::scoped_array
<boost::mutex
> async_buffer_guards
;
163 /* copies nrt mirror to rt buffers */
164 void buffer_sync(uint32_t index
);
167 /* control bus handling */
170 void controlbus_set_unchecked(uint32_t bus
, float value
)
172 world
.mControlBus
[bus
] = value
;
173 world
.mControlBusTouched
[bus
] = world
.mBufCounter
;
177 void controlbus_set(uint32_t bus
, float value
)
179 if (bus
< world
.mNumControlBusChannels
)
180 controlbus_set_unchecked(bus
, value
);
183 void controlbus_fill(uint32_t bus
, size_t count
, float value
)
185 if (bus
+ count
>= world
.mNumControlBusChannels
)
186 count
= world
.mNumAudioBusChannels
- bus
;
188 for (size_t i
= 0; i
!= count
; ++i
)
189 controlbus_set_unchecked(bus
+ i
, value
);
192 sample
controlbus_get(uint32_t bus
)
194 if (bus
< world
.mNumControlBusChannels
)
195 return world
.mControlBus
[bus
];
200 void controlbus_getn(uint32_t bus
, size_t count
, size_t * r_count
, float * r_values
)
202 size_t remain
= count
;
203 if (bus
+ count
>= world
.mNumControlBusChannels
)
204 remain
= world
.mNumAudioBusChannels
- bus
;
207 for (size_t i
= 0; i
!= remain
; ++i
)
208 r_values
[i
] = world
.mControlBus
[i
];
213 /** synth initialization. called in the beginning of each dsp tick */
214 void initialize_synths(void)
216 if (likely(!synths_to_initialize
))
219 initialize_synths_perform();
222 void schedule_for_preparation(abstract_synth
* synth
)
224 synths_to_initialize
= true;
225 uninitialized_synths
.push_back(synth
);
230 bool synths_to_initialize
;
232 void initialize_synths_perform(void);
233 std::vector
<abstract_synth
*, rt_pool_allocator
<abstract_synth
*> > uninitialized_synths
;
237 } /* namespace nova */
239 #endif /* SC_PLUGIN_INTERFACE_HPP */