1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2008 Andreas Persson * |
4 |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This program is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this program; if not, write to the Free Software * |
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * |
18 |
* MA 02110-1301 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#include <algorithm> |
22 |
#include <cassert> |
23 |
#include <cstdio> |
24 |
#include <cstdlib> |
25 |
#include <cstring> |
26 |
#include <fstream> |
27 |
#include <string> |
28 |
|
29 |
#include "PluginLv2.h" |
30 |
|
31 |
#define NS_ATOM "http://lv2plug.in/ns/ext/atom#" |
32 |
#define NS_LS "http://linuxsampler.org/schema#" |
33 |
|
34 |
namespace { |
35 |
|
36 |
PluginLv2::PluginLv2(const LV2_Descriptor* Descriptor, |
37 |
double SampleRate, const char* BundlePath, |
38 |
const LV2_Feature* const* Features) { |
39 |
Out[0] = 0; |
40 |
Out[1] = 0; |
41 |
UriMap = 0; |
42 |
MapPath = 0; |
43 |
MakePath = 0; |
44 |
for (int i = 0 ; Features[i] ; i++) { |
45 |
dmsg(2, ("linuxsampler: host feature: %s\n", Features[i]->URI)); |
46 |
if (!strcmp(Features[i]->URI, LV2_URI_MAP_URI)) { |
47 |
UriMap = (LV2_URI_Map_Feature*)Features[i]->data; |
48 |
} else if (!strcmp(Features[i]->URI, LV2_STATE_MAP_PATH_URI)) { |
49 |
MapPath = (LV2_State_Map_Path*)Features[i]->data; |
50 |
} else if (!strcmp(Features[i]->URI, LV2_STATE_MAKE_PATH_URI)) { |
51 |
MakePath = (LV2_State_Make_Path*)Features[i]->data; |
52 |
} |
53 |
} |
54 |
|
55 |
Init(SampleRate, 128); |
56 |
|
57 |
InitState(); |
58 |
|
59 |
DefaultState = GetState(); |
60 |
} |
61 |
|
62 |
void PluginLv2::ConnectPort(uint32_t Port, void* DataLocation) { |
63 |
if (Port == 2) { |
64 |
MidiBuf = static_cast<LV2_Event_Buffer*>(DataLocation); |
65 |
} else if (Port < 2) { |
66 |
Out[Port] = static_cast<float*>(DataLocation); |
67 |
} |
68 |
} |
69 |
|
70 |
void PluginLv2::Activate() { |
71 |
dmsg(2, ("linuxsampler: Activate\n")); |
72 |
} |
73 |
|
74 |
void PluginLv2::Run(uint32_t SampleCount) { |
75 |
int samplePos = 0; |
76 |
uint8_t* events = MidiBuf->data; |
77 |
int eventCount = MidiBuf->event_count; |
78 |
while (SampleCount) { |
79 |
int samples = std::min(SampleCount, 128U); |
80 |
|
81 |
for ( ; eventCount ; eventCount--) { |
82 |
LV2_Event* event = reinterpret_cast<LV2_Event*>(events); |
83 |
|
84 |
int time = event->frames - samplePos; |
85 |
if (time >= samples) break; |
86 |
|
87 |
uint8_t* data = events + sizeof(LV2_Event); |
88 |
events += (sizeof(LV2_Event) + event->size + 7) & ~7; |
89 |
|
90 |
pMidiDevice->Port()->DispatchRaw(data, time); |
91 |
} |
92 |
pAudioDevice->Channel(0)->SetBuffer(Out[0] + samplePos); |
93 |
pAudioDevice->Channel(1)->SetBuffer(Out[1] + samplePos); |
94 |
pAudioDevice->Render(samples); |
95 |
|
96 |
samplePos += samples; |
97 |
SampleCount -= samples; |
98 |
} |
99 |
} |
100 |
|
101 |
void PluginLv2::Deactivate() { |
102 |
dmsg(2, ("linuxsampler: Deactivate\n")); |
103 |
} |
104 |
|
105 |
String PluginLv2::PathToState(const String& path) { |
106 |
if (MapPath) { |
107 |
char* cstr = MapPath->abstract_path(MapPath->handle, |
108 |
path.c_str()); |
109 |
const String abstract_path(cstr); |
110 |
free(cstr); |
111 |
return abstract_path; |
112 |
} |
113 |
return path; |
114 |
} |
115 |
|
116 |
String PluginLv2::PathFromState(const String& path) { |
117 |
if (MapPath) { |
118 |
char* cstr = MapPath->absolute_path(MapPath->handle, |
119 |
path.c_str()); |
120 |
const String abstract_path(cstr); |
121 |
free(cstr); |
122 |
return abstract_path; |
123 |
} |
124 |
return path; |
125 |
} |
126 |
|
127 |
void PluginLv2::Save(LV2_State_Store_Function store, LV2_State_Handle handle) { |
128 |
if (MakePath) { |
129 |
char* path = MakePath->path(MapPath->handle, |
130 |
"linuxsampler"); |
131 |
dmsg(2, ("saving to file %s\n", path)); |
132 |
|
133 |
std::ofstream out(path); |
134 |
out << GetState(); |
135 |
|
136 |
store(handle, |
137 |
uri_to_id(NULL, NS_LS "state-file"), |
138 |
path, |
139 |
strlen(path) + 1, |
140 |
uri_to_id(NULL, LV2_STATE_PATH_URI), |
141 |
LV2_STATE_IS_PORTABLE); |
142 |
} else { |
143 |
dmsg(2, ("saving to string\n")); |
144 |
|
145 |
std::ostringstream out; |
146 |
out << GetState(); |
147 |
|
148 |
store(handle, |
149 |
uri_to_id(NULL, NS_LS "state-string"), |
150 |
out.str().c_str(), |
151 |
out.str().length() + 1, |
152 |
uri_to_id(NULL, NS_ATOM "String"), |
153 |
LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE); |
154 |
} |
155 |
dmsg(2, ("saving done\n")); |
156 |
} |
157 |
|
158 |
void PluginLv2::Restore(LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle) { |
159 |
size_t size; |
160 |
uint32_t type; |
161 |
uint32_t flags; |
162 |
|
163 |
// Attempt to restore from state-file |
164 |
const void* value = retrieve( |
165 |
handle, |
166 |
uri_to_id(NULL, NS_LS "state-file"), |
167 |
&size, &type, &flags); |
168 |
if (value) { |
169 |
assert(type == uri_to_id(NULL, LV2_STATE_PATH_URI)); |
170 |
const String path((const char*)value); |
171 |
dmsg(2, ("linuxsampler: restoring from file %s\n", path.c_str())); |
172 |
std::ifstream in(path.c_str()); |
173 |
String state; |
174 |
std::getline(in, state, '\0'); |
175 |
SetState(state); |
176 |
return; |
177 |
} |
178 |
|
179 |
// Attempt to restore from state-string |
180 |
value = retrieve( |
181 |
handle, |
182 |
uri_to_id(NULL, NS_LS "state-string"), |
183 |
&size, &type, &flags); |
184 |
if (value) { |
185 |
dmsg(2, ("linuxsampler: restoring from string\n")); |
186 |
assert(type == uri_to_id(NULL, NS_ATOM "String")); |
187 |
String state((const char*)value); |
188 |
SetState(state); |
189 |
return; |
190 |
} |
191 |
|
192 |
// No valid state found, reset to default state |
193 |
dmsg(2, ("linuxsampler: restoring default state\n")); |
194 |
SetState(DefaultState); |
195 |
} |
196 |
|
197 |
LV2_Handle instantiate(const LV2_Descriptor* descriptor, |
198 |
double sample_rate, const char* bundle_path, |
199 |
const LV2_Feature* const* features) { |
200 |
return new PluginLv2(descriptor, sample_rate, bundle_path, features); |
201 |
} |
202 |
|
203 |
void connect_port(LV2_Handle instance, uint32_t port, void* data_location) { |
204 |
static_cast<PluginLv2*>(instance)->ConnectPort(port, data_location); |
205 |
} |
206 |
|
207 |
void activate(LV2_Handle instance) { |
208 |
static_cast<PluginLv2*>(instance)->Activate(); |
209 |
} |
210 |
|
211 |
void run(LV2_Handle instance, uint32_t sample_count) { |
212 |
static_cast<PluginLv2*>(instance)->Run(sample_count); |
213 |
} |
214 |
|
215 |
void deactivate(LV2_Handle instance) { |
216 |
static_cast<PluginLv2*>(instance)->Deactivate(); |
217 |
} |
218 |
|
219 |
void cleanup(LV2_Handle instance) { |
220 |
delete static_cast<PluginLv2*>(instance); |
221 |
} |
222 |
|
223 |
void save(LV2_Handle handle, LV2_State_Store_Function store, LV2_State_Handle state, |
224 |
uint32_t flags, const LV2_Feature* const* features) { |
225 |
return static_cast<PluginLv2*>(handle)->Save(store, state); |
226 |
} |
227 |
|
228 |
void restore(LV2_Handle handle, LV2_State_Retrieve_Function retrieve, LV2_State_Handle state, |
229 |
uint32_t flags, const LV2_Feature* const* features) { |
230 |
return static_cast<PluginLv2*>(handle)->Restore(retrieve, state); |
231 |
} |
232 |
|
233 |
PluginInfo PluginInfo::Instance; |
234 |
|
235 |
PluginInfo::PluginInfo() { |
236 |
Lv2.URI = "http://linuxsampler.org/plugins/linuxsampler"; |
237 |
Lv2.activate = activate; |
238 |
Lv2.cleanup = cleanup; |
239 |
Lv2.connect_port = connect_port; |
240 |
Lv2.deactivate = deactivate; |
241 |
Lv2.instantiate = instantiate; |
242 |
Lv2.run = run; |
243 |
Lv2.extension_data = extension_data; |
244 |
StateInterface.save = save; |
245 |
StateInterface.restore = restore; |
246 |
} |
247 |
|
248 |
|
249 |
const void* extension_data(const char* uri) { |
250 |
dmsg(2, ("linuxsampler: extension_data %s\n", uri)); |
251 |
if (strcmp(uri, LV2_STATE_URI "#Interface") == 0) { |
252 |
return PluginInfo::Lv2StateInterface(); |
253 |
} |
254 |
return 0; |
255 |
} |
256 |
} |
257 |
|
258 |
|
259 |
extern "C" { |
260 |
LV2_SYMBOL_EXPORT |
261 |
const LV2_Descriptor* lv2_descriptor(uint32_t index) { |
262 |
return index == 0 ? PluginInfo::Lv2Descriptor() : 0; |
263 |
} |
264 |
} |