Fix scvim regsitry file for updated filename (thanks Carlo Capocasa)
[supercollider.git] / server / supernova / sc / sc_synthdef.hpp
blob3c6610b5648939fc741e1ddf4111ccf6d2dbb1e6
1 // supercollider-style synthdef
2 // Copyright (C) 2008-2012 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_SYNTHDEF_HPP
20 #define SC_SYNTHDEF_HPP
22 #include <string>
23 #include <vector>
24 #include <map>
26 #include <boost/cstdint.hpp>
27 #include <boost/filesystem/path.hpp>
29 #include "utilities/malloc_aligned.hpp"
30 #include "utilities/named_hash_entry.hpp"
33 #include "SC_Types.h"
34 #include "SC_Wire.h"
36 namespace nova {
38 class sc_synthdef
40 typedef c_string string;
41 typedef std::vector<float, aligned_allocator<float> > fvector;
42 typedef std::vector<c_string, aligned_allocator<string> > svector;
44 typedef std::vector<char, aligned_allocator<char> > char_vector;
46 public:
47 typedef std::map<string, int32_t, std::less<string>, aligned_allocator<string> > parameter_map_t;
49 struct input_spec
51 input_spec(int32_t source, int32_t index):
52 source(source), index(index)
55 bool operator<(input_spec const & rhs) const
57 if (source < rhs.source)
58 return true;
59 if (source > rhs.source)
60 return false;
61 return index < rhs.index;
64 int32_t source; /* index of ugen or -1 for constant */
65 int32_t index; /* number of output or constant index */
67 typedef std::vector<input_spec, aligned_allocator<input_spec> > input_spec_vector;
69 struct unit_spec_t
71 explicit unit_spec_t(const char *& buffer, int version);
73 unit_spec_t(string const & name, int16_t rate, int16_t special_index,
74 input_spec_vector const & in_specs,
75 char_vector const & out_specs):
76 name(name), rate(rate), special_index(special_index),
77 input_specs(in_specs), output_specs(out_specs)
80 #ifdef BOOST_HAS_RVALUE_REFS
81 unit_spec_t(unit_spec_t && rhs):
82 name(std::move(rhs.name)), rate(rhs.rate), special_index(rhs.special_index),
83 input_specs(std::move(rhs.input_specs)), output_specs(std::move(rhs.output_specs)),
84 buffer_mapping(std::move(rhs.buffer_mapping)), prototype(rhs.prototype)
87 unit_spec_t(unit_spec_t const & rhs) = default;
88 unit_spec_t & operator=(unit_spec_t const & rhs) = default;
89 #endif
90 string name;
91 int16_t rate; /* 0: scalar rate, 1: buffer rate, 2: full rate, 3: demand rate */
92 int16_t special_index;
94 input_spec_vector input_specs;
95 char_vector output_specs; /* calculation rates */
96 std::vector<int32_t, aligned_allocator<int32_t> > buffer_mapping;
98 std::size_t memory_requirement(void)
100 return input_specs.size() * (sizeof(Wire*) + sizeof(float*)) +
101 output_specs.size() * (sizeof(Wire*) + sizeof(float*)) +
102 output_specs.size() * sizeof(Wire);
105 struct sc_ugen_def * prototype;
108 friend class sc_synth_prototype;
109 friend class sc_synth;
110 friend class sc_ugen_factory;
111 friend class sc_ugen_def;
113 typedef std::vector<unit_spec_t, aligned_allocator<unit_spec_t> > graph_t;
114 typedef std::vector<int32_t, aligned_allocator<int32_t> > calc_units_t;
116 sc_synthdef(const char *& buffer, int version);
118 #ifdef BOOST_HAS_RVALUE_REFS
119 sc_synthdef(sc_synthdef && rhs):
120 name_(std::move(rhs.name_)), constants(std::move(rhs.constants)), parameters(std::move(rhs.parameters)),
121 parameter_map(std::move(rhs.parameter_map)), graph(std::move(rhs.graph)), buffer_count(rhs.buffer_count),
122 calc_unit_indices(std::move(rhs.calc_unit_indices)), memory_requirement_(rhs.memory_requirement_)
125 sc_synthdef(sc_synthdef const & rhs) = default;
126 sc_synthdef& operator=(sc_synthdef const & rhs) = default;
127 #endif
129 std::string dump(void) const;
131 string const & name(void) const
133 return name_;
136 std::size_t parameter_count(void) const
138 return parameters.size();
141 std::size_t unit_count(void) const
143 return graph.size();
146 std::size_t calc_unit_count(void) const
148 return calc_unit_indices.size();
151 std::size_t memory_requirement(void) const
153 assert(memory_requirement_);
154 return memory_requirement_;
157 private:
158 void read_synthdef(const char *&, int version);
160 /** assign buffers, collect memory requirement & cache ugen prototype */
161 void prepare(void);
163 string name_;
164 fvector constants;
165 fvector parameters;
166 parameter_map_t parameter_map;
168 graph_t graph;
169 boost::uint16_t buffer_count;
170 calc_units_t calc_unit_indices; /**< indices of the units, that need to be calculated */
171 std::size_t memory_requirement_;
174 std::vector<sc_synthdef> read_synthdefs(const char * buf_ptr);
175 std::vector<sc_synthdef> read_synthdef_file(boost::filesystem::path const & filename);
177 } /* namespace nova */
179 #endif /* SC_SYNTHDEF_HPP */