1 |
persson |
1777 |
/*************************************************************************** |
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 <cstdio> |
23 |
|
|
#include <cstdlib> |
24 |
|
|
#include <cstring> |
25 |
|
|
#include <fstream> |
26 |
|
|
#include <string> |
27 |
|
|
|
28 |
|
|
#include "PluginLv2.h" |
29 |
|
|
|
30 |
|
|
namespace { |
31 |
|
|
|
32 |
|
|
PluginLv2::PluginLv2(const LV2_Descriptor* Descriptor, |
33 |
|
|
double SampleRate, const char* BundlePath, |
34 |
|
|
const LV2_Feature* const* Features) { |
35 |
|
|
Out[0] = 0; |
36 |
|
|
Out[1] = 0; |
37 |
|
|
for (int i = 0 ; Features[i] ; i++) { |
38 |
|
|
dmsg(2, ("linuxsampler: host feature: %s\n", Features[i]->URI)); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
Init(SampleRate, 128); |
42 |
|
|
|
43 |
|
|
InitState(); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
void PluginLv2::ConnectPort(uint32_t Port, void* DataLocation) { |
47 |
|
|
if (Port == 2) { |
48 |
|
|
MidiBuf = static_cast<LV2_Event_Buffer*>(DataLocation); |
49 |
|
|
} else if (Port < 2) { |
50 |
|
|
Out[Port] = static_cast<float*>(DataLocation); |
51 |
|
|
} |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
void PluginLv2::Activate() { |
55 |
|
|
dmsg(2, ("linuxsampler: Activate\n")); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
void PluginLv2::Run(uint32_t SampleCount) { |
59 |
|
|
int samplePos = 0; |
60 |
|
|
uint8_t* events = MidiBuf->data; |
61 |
|
|
int eventCount = MidiBuf->event_count; |
62 |
|
|
while (SampleCount) { |
63 |
|
|
int samples = std::min(SampleCount, 128U); |
64 |
|
|
|
65 |
|
|
for ( ; eventCount ; eventCount--) { |
66 |
|
|
LV2_Event* event = reinterpret_cast<LV2_Event*>(events); |
67 |
|
|
|
68 |
|
|
int time = event->frames - samplePos; |
69 |
|
|
if (time >= samples) break; |
70 |
|
|
|
71 |
|
|
uint8_t* data = events + sizeof(LV2_Event); |
72 |
|
|
events += (sizeof(LV2_Event) + event->size + 7) & ~7; |
73 |
|
|
|
74 |
|
|
pMidiDevice->Port()->DispatchRaw(data, time); |
75 |
|
|
} |
76 |
|
|
pAudioDevice->Channel(0)->SetBuffer(Out[0] + samplePos); |
77 |
|
|
pAudioDevice->Channel(1)->SetBuffer(Out[1] + samplePos); |
78 |
|
|
pAudioDevice->Render(samples); |
79 |
|
|
|
80 |
|
|
samplePos += samples; |
81 |
|
|
SampleCount -= samples; |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
void PluginLv2::Deactivate() { |
86 |
|
|
dmsg(2, ("linuxsampler: Deactivate\n")); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
char* PluginLv2::Save(const char* Directory, LV2SR_File*** Files) { |
90 |
|
|
dmsg(2, ("linuxsampler: Save Directory=%s\n", Directory)); |
91 |
|
|
|
92 |
|
|
String filename(Directory); |
93 |
|
|
filename += "/linuxsampler"; |
94 |
|
|
dmsg(2, ("saving to %s\n", filename.c_str())); |
95 |
|
|
std::ofstream out(filename.c_str()); |
96 |
|
|
out << GetState(); |
97 |
|
|
out.close(); |
98 |
|
|
|
99 |
|
|
LV2SR_File** filearr = static_cast<LV2SR_File**>(malloc(sizeof(LV2SR_File*) * 2)); |
100 |
|
|
|
101 |
|
|
LV2SR_File* file = static_cast<LV2SR_File*>(malloc(sizeof(LV2SR_File))); |
102 |
|
|
file->name = strdup("linuxsampler"); |
103 |
|
|
file->path = strdup(filename.c_str()); |
104 |
|
|
file->must_copy = 0; |
105 |
|
|
|
106 |
|
|
filearr[0] = file; |
107 |
|
|
filearr[1] = 0; |
108 |
|
|
|
109 |
|
|
*Files = filearr; |
110 |
|
|
|
111 |
|
|
dmsg(2, ("saving done\n")); |
112 |
|
|
return 0; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
char* PluginLv2::Restore(const LV2SR_File** Files) { |
116 |
|
|
dmsg(2, ("linuxsampler: restore\n")); |
117 |
|
|
for (int i = 0 ; Files[i] ; i++) { |
118 |
|
|
dmsg(2, (" name=%s path=%s\n", Files[i]->name, Files[i]->path)); |
119 |
|
|
if (strcmp(Files[i]->name, "linuxsampler") == 0) { |
120 |
|
|
std::ifstream in(Files[0]->path); |
121 |
|
|
String state; |
122 |
|
|
std::getline(in, state, '\0'); |
123 |
|
|
SetState(state); |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
return 0; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
LV2_Handle instantiate(const LV2_Descriptor* descriptor, |
130 |
|
|
double sample_rate, const char* bundle_path, |
131 |
|
|
const LV2_Feature* const* features) { |
132 |
|
|
return new PluginLv2(descriptor, sample_rate, bundle_path, features); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
void connect_port(LV2_Handle instance, uint32_t port, void* data_location) { |
136 |
|
|
static_cast<PluginLv2*>(instance)->ConnectPort(port, data_location); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
void activate(LV2_Handle instance) { |
140 |
|
|
static_cast<PluginLv2*>(instance)->Activate(); |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
void run(LV2_Handle instance, uint32_t sample_count) { |
144 |
|
|
static_cast<PluginLv2*>(instance)->Run(sample_count); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
void deactivate(LV2_Handle instance) { |
148 |
|
|
static_cast<PluginLv2*>(instance)->Deactivate(); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
void cleanup(LV2_Handle instance) { |
152 |
|
|
delete static_cast<PluginLv2*>(instance); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
char* save(LV2_Handle handle, const char* directory, LV2SR_File*** files) { |
156 |
|
|
return static_cast<PluginLv2*>(handle)->Save(directory, files); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
char* restore(LV2_Handle handle, const LV2SR_File** files) { |
160 |
|
|
return static_cast<PluginLv2*>(handle)->Restore(files); |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
PluginInfo PluginInfo::Instance; |
164 |
|
|
|
165 |
|
|
PluginInfo::PluginInfo() { |
166 |
|
|
Lv2.URI = "http://linuxsampler.org/plugins/linuxsampler"; |
167 |
|
|
Lv2.activate = activate; |
168 |
|
|
Lv2.cleanup = cleanup; |
169 |
|
|
Lv2.connect_port = connect_port; |
170 |
|
|
Lv2.deactivate = deactivate; |
171 |
|
|
Lv2.instantiate = instantiate; |
172 |
|
|
Lv2.run = run; |
173 |
|
|
Lv2.extension_data = extension_data; |
174 |
|
|
Lv2sr.save = save; |
175 |
|
|
Lv2sr.restore = restore; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
|
179 |
|
|
const void* extension_data(const char* uri) { |
180 |
|
|
dmsg(2, ("linuxsampler: extension_data %s\n", uri)); |
181 |
|
|
if (strcmp(uri, LV2_SAVERESTORE_URI) == 0) { |
182 |
|
|
return PluginInfo::Lv2srDescriptor(); |
183 |
|
|
} |
184 |
|
|
return 0; |
185 |
|
|
} |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
|
189 |
|
|
extern "C" { |
190 |
|
|
LV2_SYMBOL_EXPORT |
191 |
|
|
const LV2_Descriptor* lv2_descriptor(uint32_t index) { |
192 |
|
|
return index == 0 ? PluginInfo::Lv2Descriptor() : 0; |
193 |
|
|
} |
194 |
|
|
} |