File: sc_ugen_factory.hpp

package info (click to toggle)
supercollider 1%3A3.11.2%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 71,152 kB
  • sloc: cpp: 387,846; lisp: 80,328; ansic: 76,515; sh: 22,779; python: 7,932; makefile: 2,333; perl: 1,123; javascript: 915; java: 677; xml: 582; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (192 lines) | stat: -rw-r--r-- 6,944 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//  prototype of a supercollider-synthdef-based synth prototype
//  Copyright (C) 2009 Tim Blechmann
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; see the file COPYING.  If not, write to
//  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//  Boston, MA 02111-1307, USA.

#pragma once

#include <boost/intrusive/set.hpp>
#include <boost/intrusive/unordered_set.hpp>
#include <boost/checked_delete.hpp>

#include "sc_synthdef.hpp"
#include "sc_synth.hpp"
#include "sc_plugin_interface.hpp"

#include "SC_InterfaceTable.h"
#include "SC_Unit.h"

#include "utilities/named_hash_entry.hpp"

namespace nova {
namespace bi = boost::intrusive;

struct sc_unitcmd_def : public named_hash_entry {
    const UnitCmdFunc func;

    sc_unitcmd_def(const char* cmd_name, UnitCmdFunc func): named_hash_entry(cmd_name), func(func) {}

    void run(Unit* unit, struct sc_msg_iter* args) { (func)(unit, args); }
};

class sc_ugen_def : public aligned_class, public named_hash_entry {
private:
    const size_t alloc_size;
    const UnitCtorFunc ctor;
    const UnitDtorFunc dtor;
    const uint32_t flags;

    static const std::size_t unitcmd_set_bucket_count = 4;
    typedef bi::unordered_set<sc_unitcmd_def, bi::constant_time_size<false>, bi::power_2_buckets<true>,
                              bi::store_hash<true>>
        unitcmd_set_type;

    unitcmd_set_type::bucket_type unitcmd_set_buckets[unitcmd_set_bucket_count];
    unitcmd_set_type unitcmd_set;

public:
    sc_ugen_def(const char* inUnitClassName, size_t inAllocSize, UnitCtorFunc inCtor, UnitDtorFunc inDtor,
                uint32 inFlags):
        named_hash_entry(inUnitClassName),
        alloc_size(inAllocSize),
        ctor(inCtor),
        dtor(inDtor),
        flags(inFlags),
        unitcmd_set(unitcmd_set_type::bucket_traits(unitcmd_set_buckets, unitcmd_set_bucket_count)) {}

    Unit* construct(sc_synthdef::unit_spec_t const& unit_spec, sc_synth* parent, int parentIndex, World* world,
                    linear_allocator& allocator);

    void initialize(Unit* unit) { (*ctor)(unit); }

    void destruct(Unit* unit) {
        if (dtor)
            (*dtor)(unit);
    }

    bool cant_alias(void) const { return flags & kUnitDef_CantAliasInputsToOutputs; }

    std::size_t memory_requirement(void) const {
        return alloc_size + 64; // overallocate to allow alignment
    }

    bool add_command(const char* cmd_name, UnitCmdFunc func);
    void run_unit_command(const char* cmd_name, Unit* unit, struct sc_msg_iter* args);
};

struct sc_bufgen_def : public named_hash_entry {
    const BufGenFunc func;

    sc_bufgen_def(const char* name, BufGenFunc func): named_hash_entry(name), func(func) {}

    sample* run(World* world, uint32_t buffer_index, struct sc_msg_iter* args);
};

struct sc_cmdplugin_def : public named_hash_entry {
    const PlugInCmdFunc func;
    void* user_data;

    sc_cmdplugin_def(const char* name, PlugInCmdFunc func, void* user_data):
        named_hash_entry(name),
        func(func),
        user_data(user_data) {}

    void run(World* world, struct sc_msg_iter* args, void* replyAddr) { (func)(world, user_data, args, replyAddr); }
};

class sc_plugin_container {
    static const std::size_t ugen_set_bucket_count = 512;
    typedef bi::unordered_set<sc_ugen_def, bi::constant_time_size<false>, bi::power_2_buckets<true>,
                              bi::store_hash<true>>
        ugen_set_type;

    static const std::size_t bufgen_set_bucket_count = 64;
    typedef bi::unordered_set<sc_bufgen_def, bi::constant_time_size<false>, bi::power_2_buckets<true>,
                              bi::store_hash<true>>
        bufgen_set_type;

    static const std::size_t cmdplugin_set_bucket_count = 8;
    typedef bi::unordered_set<sc_cmdplugin_def, bi::constant_time_size<false>, bi::power_2_buckets<true>,
                              bi::store_hash<true>>
        cmdplugin_set_type;

    ugen_set_type::bucket_type ugen_set_buckets[ugen_set_bucket_count];
    ugen_set_type ugen_set;

    bufgen_set_type::bucket_type bufgen_set_buckets[bufgen_set_bucket_count];
    bufgen_set_type bufgen_set;

    cmdplugin_set_type::bucket_type cmdplugin_set_buckets[cmdplugin_set_bucket_count];
    cmdplugin_set_type cmdplugin_set;

protected:
    sc_plugin_container(void):
        ugen_set(ugen_set_type::bucket_traits(ugen_set_buckets, ugen_set_bucket_count)),
        bufgen_set(bufgen_set_type::bucket_traits(bufgen_set_buckets, bufgen_set_bucket_count)),
        cmdplugin_set(cmdplugin_set_type::bucket_traits(cmdplugin_set_buckets, cmdplugin_set_bucket_count)) {}

    ~sc_plugin_container(void) {
        ugen_set.clear_and_dispose(boost::checked_deleter<sc_ugen_def>());
        bufgen_set.clear_and_dispose(boost::checked_deleter<sc_bufgen_def>());
        cmdplugin_set.clear_and_dispose(boost::checked_deleter<sc_cmdplugin_def>());
    }

public:
    void register_ugen(const char* inUnitClassName, size_t inAllocSize, UnitCtorFunc inCtor, UnitDtorFunc inDtor,
                       uint32 inFlags);

    void register_bufgen(const char* name, BufGenFunc func);

    sc_ugen_def* find_ugen(symbol const& name);

    bool register_ugen_command_function(const char* ugen_name, const char* cmd_name, UnitCmdFunc);
    bool register_cmd_plugin(const char* cmd_name, PlugInCmdFunc func, void* user_data);

    sample* run_bufgen(World* world, const char* name, uint32_t buffer_index, struct sc_msg_iter* args);
    bool run_cmd_plugin(World* world, const char* name, struct sc_msg_iter* args, void* replyAddr);
};

/** factory class for supercollider ugens
 *
 *  \todo do we need to take care of thread safety? */
class sc_ugen_factory : public sc_plugin_interface, public sc_plugin_container {
public:
    sc_ugen_factory() = default;

    ~sc_ugen_factory(void) { close_handles(); }

    /* @{ */
    /** ugen count handling */
    void allocate_ugens(uint32_t count) { ugen_count_ += count; }

    void free_ugens(uint32_t count) { ugen_count_ -= count; }

    uint32_t ugen_count(void) const { return ugen_count_; }
    /* @} */

    void load_plugin_folder(boost::filesystem::path const& path);
    void load_plugin(boost::filesystem::path const& path);

private:
    void close_handles(void);

    uint32_t ugen_count_ = 0;
    std::vector<void*> open_handles;
};

extern std::unique_ptr<sc_ugen_factory> sc_factory;

} /* namespace nova */