handrumr.cpp 3.44 KB
Newer Older
jhammen's avatar
jhammen committed
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
/*
 * This file is part of handrumr.lv2.
 *
 * handrumr.lv2 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 3 of the License, or
 * (at your option) any later version.
 *
 * handrumr.lv2 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 handrumr.lv2.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <math.h>
#include <string.h>
#include <vector>
#include <stdexcept>

#include "sample.h"
#include "plugin.h"

using std::vector;
using handrumr::Plugin;

// --------------------- initializiation

static LV2_Handle
instantiate(const LV2_Descriptor* descriptor, double rate, const char *path,
            const LV2_Feature* const* features)
{
    try {
35
36
37
        uint16_t channels = 1;
        if(!strcmp(descriptor->URI, HANDRUMR2_URI)) {
            channels = 2;
jhammen's avatar
jhammen committed
38
39
        } else if(!strcmp(descriptor->URI, HANDRUMR16_URI)) {
            channels = 16;
40
41
        }
        return (LV2_Handle)new Plugin(features, channels);
jhammen's avatar
jhammen committed
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
    }
    catch(const std::logic_error &e) {
        return NULL;
    }
}

static void connect_port(LV2_Handle instance, uint32_t port, void* data)
{
    ((Plugin*)instance)->connectPort(port, data);
}

static void
cleanup(LV2_Handle instance)
{
    delete (Plugin*)instance;
}

static void
activate(LV2_Handle instance)
{
    ((Plugin*)instance)->activated(true);
}

static void
deactivate(LV2_Handle instance)
{
    ((Plugin*)instance)->activated(false);
}

// ----------------- process

static void run(LV2_Handle instance, uint32_t sample_count)
{
    ((Plugin*)instance)->run(sample_count);
}

// ----------------- state

static LV2_State_Status save(LV2_Handle instance, LV2_State_Store_Function store,
                             LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* features)
{
    return ((Plugin*)instance)->save(store, handle, flags, features);
}

static LV2_State_Status restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve,
                                LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* features)
{
    return ((Plugin*)instance)->restore(retrieve, handle, flags, features);
}

// ----------------- extensions

static const void* extension_data(const char* uri)
{
	static const LV2_State_Interface  state  = { save, restore };
	if (!strcmp(uri, LV2_STATE__interface)) {
		return &state;
    }
	return NULL;
}

// ----------------- plugin descriptor

static const LV2_Descriptor descriptor = {
    HANDRUMR_URI,
    instantiate,
    connect_port,
    activate,
    run,
    deactivate,
    cleanup,
    extension_data
};

116
117
118
119
120
121
122
123
124
125
126
static const LV2_Descriptor descriptor2 = {
    HANDRUMR2_URI,
    instantiate,
    connect_port,
    activate,
    run,
    deactivate,
    cleanup,
    extension_data
};

jhammen's avatar
jhammen committed
127
128
129
130
131
132
133
134
135
136
137
static const LV2_Descriptor descriptor16 = {
    HANDRUMR16_URI,
    instantiate,
    connect_port,
    activate,
    run,
    deactivate,
    cleanup,
    extension_data
};

jhammen's avatar
jhammen committed
138
139
140
141
142
LV2_SYMBOL_EXPORT const LV2_Descriptor* lv2_descriptor(uint32_t index)
{
	switch (index) {
	case 0:
		return &descriptor;
143
144
    case 1:
        return &descriptor2;
jhammen's avatar
jhammen committed
145
146
147
    case 2:
        return &descriptor16;
    default:
jhammen's avatar
jhammen committed
148
149
150
		return NULL;
	}
}