scide: implement selectionLength for openDocument
[supercollider.git] / server / supernova / sc / sc_synthdef.hpp
blobfbf7e7bdba580b04691ceeb623a0d82c9a5e62d9
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 <cstdint>
23 #include <map>
24 #include <string>
25 #include <vector>
27 #include <boost/filesystem/path.hpp>
29 #include "utilities/malloc_aligned.hpp"
30 #include "utilities/named_hash_entry.hpp"
32 #include "SC_Types.h"
33 #include "SC_Wire.h"
35 namespace nova {
37 class sc_synthdef
39 typedef c_string string;
40 typedef std::vector<float, aligned_allocator<float> > fvector;
41 typedef std::vector<c_string, aligned_allocator<string> > svector;
43 typedef std::vector<char, aligned_allocator<char> > char_vector;
45 public:
46 typedef std::map<string, int32_t, std::less<string>, aligned_allocator<string> > parameter_map_t;
48 struct input_spec
50 input_spec(int32_t source, int32_t index):
51 source(source), index(index)
54 bool operator<(input_spec const & rhs) const
56 if (source < rhs.source)
57 return true;
58 if (source > rhs.source)
59 return false;
60 return index < rhs.index;
63 int32_t source; /* index of ugen or -1 for constant */
64 int32_t index; /* number of output or constant index */
66 typedef std::vector<input_spec, aligned_allocator<input_spec> > input_spec_vector;
68 struct unit_spec_t
70 explicit unit_spec_t(const char *& buffer, int version);
72 unit_spec_t(string const & name, int16_t rate, int16_t special_index,
73 input_spec_vector const & in_specs,
74 char_vector const & out_specs):
75 name(name), rate(rate), special_index(special_index),
76 input_specs(in_specs), output_specs(out_specs)
79 unit_spec_t(unit_spec_t && rhs):
80 name(std::move(rhs.name)), rate(rhs.rate), special_index(rhs.special_index),
81 input_specs(std::move(rhs.input_specs)), output_specs(std::move(rhs.output_specs)),
82 buffer_mapping(std::move(rhs.buffer_mapping)), prototype(rhs.prototype)
85 unit_spec_t(unit_spec_t const & rhs) = default;
86 unit_spec_t & operator=(unit_spec_t const & rhs) = default;
88 string name;
89 int16_t rate; /* 0: scalar rate, 1: buffer rate, 2: full rate, 3: demand rate */
90 int16_t special_index;
92 input_spec_vector input_specs;
93 char_vector output_specs; /* calculation rates */
94 std::vector<int32_t, aligned_allocator<int32_t> > buffer_mapping;
96 std::size_t memory_requirement(void)
98 return input_specs.size() * (sizeof(Wire*) + sizeof(float*)) +
99 output_specs.size() * (sizeof(Wire*) + sizeof(float*)) +
100 output_specs.size() * sizeof(Wire);
103 struct sc_ugen_def * prototype;
106 friend class sc_synth_prototype;
107 friend class sc_synth;
108 friend class sc_ugen_factory;
109 friend class sc_ugen_def;
111 typedef std::vector<unit_spec_t, aligned_allocator<unit_spec_t> > graph_t;
112 typedef std::vector<int32_t, aligned_allocator<int32_t> > calc_units_t;
114 sc_synthdef(const char *& buffer, int version);
116 sc_synthdef(sc_synthdef && rhs):
117 name_(std::move(rhs.name_)), constants(std::move(rhs.constants)), parameters(std::move(rhs.parameters)),
118 parameter_map(std::move(rhs.parameter_map)), graph(std::move(rhs.graph)), buffer_count(rhs.buffer_count),
119 calc_unit_indices(std::move(rhs.calc_unit_indices)), memory_requirement_(rhs.memory_requirement_)
122 sc_synthdef(sc_synthdef const & rhs) = default;
123 sc_synthdef& operator=(sc_synthdef const & rhs) = default;
125 std::string dump(void) const;
127 string const & name(void) const
129 return name_;
132 std::size_t parameter_count(void) const
134 return parameters.size();
137 std::size_t unit_count(void) const
139 return graph.size();
142 std::size_t calc_unit_count(void) const
144 return calc_unit_indices.size();
147 std::size_t memory_requirement(void) const
149 assert(memory_requirement_);
150 return memory_requirement_;
153 private:
154 void read_synthdef(const char *&, int version);
156 /** assign buffers, collect memory requirement & cache ugen prototype */
157 void prepare(void);
159 string name_;
160 fvector constants;
161 fvector parameters;
162 parameter_map_t parameter_map;
164 graph_t graph;
165 std::uint16_t buffer_count;
166 calc_units_t calc_unit_indices; /**< indices of the units, that need to be calculated */
167 std::size_t memory_requirement_;
170 std::vector<sc_synthdef> read_synthdefs(const char * buf_ptr);
171 std::vector<sc_synthdef> read_synthdef_file(boost::filesystem::path const & filename);
173 } /* namespace nova */
175 #endif /* SC_SYNTHDEF_HPP */