5 #include "../juce_IncludeCharacteristics.h"
7 #if JucePlugin_Build_LV2
11 #include "lv2/event.h"
12 #include "lv2/event_helpers.h"
13 #include "lv2/instance_access.h"
14 #include "lv2/uri_map.h"
16 #include "lv2/lv2_external_ui.h"
18 #include "../juce_PluginHeaders.h"
19 #include "../juce_PluginHostType.h"
21 //==============================================================================
22 // Same as juce_lv2_gen.cpp
25 return String("urn:" JucePlugin_Manufacturer
":" JucePlugin_Name
":" JucePlugin_VersionString
).replace(" ", "_");
28 String
get_juce_ui_uri()
30 return String("urn:" JucePlugin_Manufacturer
":" JucePlugin_Name
":JUCE-Native-UI").replace(" ", "_");
33 String
get_external_ui_uri()
35 return String("urn:" JucePlugin_Manufacturer
":" JucePlugin_Name
":JUCE-External-UI").replace(" ", "_");
38 static Array
<void*> activePlugins
;
40 extern AudioProcessor
* JUCE_CALLTYPE
createPluginFilter();
42 //==============================================================================
43 // Create a new JUCE LV2 Plugin
44 class JuceLV2Wrapper
: private Timer
47 JuceLV2Wrapper(const LV2_Descriptor
* descriptor_
, double sample_rate_
, const LV2_Feature
* const* features
) :
49 numInChans (JucePlugin_MaxNumInputChannels
),
50 numOutChans (JucePlugin_MaxNumOutputChannels
),
52 firstProcessCallback (true),
53 descriptor (descriptor_
),
54 sample_rate (sample_rate_
),
59 printf("JuceLV2Wrapper()\n");
60 filter
= createPluginFilter();
61 filter
->setPlayConfigDetails(numInChans
, numOutChans
, 0, 0);
64 #if JucePlugin_WantsMidiInput
67 #if JucePlugin_ProducesMidiOutput
70 port_count
+= numInChans
;
71 port_count
+= numOutChans
;
72 port_count
+= filter
->getNumParameters();
77 ports_ctrl
.insertMultiple(0, nullptr, filter
->getNumParameters());
78 ports_ctrl_last
.insertMultiple(0, 0.0f
, filter
->getNumParameters());
80 for (int i
=0; i
< numInChans
; i
++) {
81 ports_ain
[i
] = nullptr;
84 for (int i
=0; i
< numOutChans
; i
++) {
85 ports_aout
[i
] = nullptr;
88 for (int i
=0; i
< filter
->getNumParameters(); i
++) {
89 ports_ctrl_last
.set(i
, filter
->getParameter(i
));
93 for (uint16_t j
= 0; features
[j
]; j
++)
95 if (strcmp(features
[j
]->URI
, LV2_URI_MAP_URI
) == 0)
97 LV2_URI_Map_Feature
* uri_feature
= (LV2_URI_Map_Feature
*)features
[j
]->data
;
98 midi_uri_id
= uri_feature
->uri_to_id(uri_feature
->callback_data
, LV2_EVENT_URI
, "http://lv2plug.in/ns/ext/midi#MidiEvent");
103 activePlugins
.add (this);
119 deleteTempChannels();
122 ports_ctrl_last
.clear();
124 jassert (activePlugins
.contains (this));
125 activePlugins
.removeValue (this);
129 //==============================================================================
130 // LV2 Descriptor Calls
131 void do_connect_port(uint32_t port
, void* data_location
)
133 if (port
< port_count
)
138 #if JucePlugin_WantsMidiInput
140 port_min
= (LV2_Event_Buffer
*)data_location
;
146 #if JucePlugin_ProducesMidiOutput
148 port_mout
= (LV2_Event_Buffer
*)data_location
;
154 for (i
=0; i
< numInChans
; i
++) {
156 ports_ain
[i
] = (float*)data_location
;
162 for (i
=0; i
< numOutChans
; i
++) {
164 ports_aout
[i
] = (float*)data_location
;
170 for (i
=0; i
< filter
->getNumParameters(); i
++) {
172 ports_ctrl
.set(i
, (float*)data_location
);
182 if (filter
!= nullptr)
185 channels
.calloc (numInChans
+ numOutChans
);
187 jassert (sample_rate
> 0);
188 if (sample_rate
<= 0.0)
189 sample_rate
= 44100.0;
191 jassert (buffer_size
> 0);
193 firstProcessCallback
= true;
195 filter
->setNonRealtime (false);
196 filter
->setPlayConfigDetails (numInChans
, numOutChans
, sample_rate
, buffer_size
);
198 deleteTempChannels();
200 filter
->prepareToPlay (sample_rate
, buffer_size
);
202 midiEvents
.ensureSize (2048);
207 void do_run(uint32_t sample_count
)
209 if (firstProcessCallback
)
211 firstProcessCallback
= false;
213 // if this fails, the host hasn't called resume() before processing
214 jassert (isProcessing
);
216 // (tragically, some hosts actually need this, although it's stupid to have
221 filter
->setNonRealtime (false);
224 if (GetThreadPriority (GetCurrentThread()) <= THREAD_PRIORITY_NORMAL
225 && GetThreadPriority (GetCurrentThread()) >= THREAD_PRIORITY_LOWEST
)
226 filter
->setNonRealtime (true);
230 // Check if buffer size changed
231 if (buffer_size
!= sample_count
)
233 buffer_size
= sample_count
;
234 filter
->setPlayConfigDetails(numInChans
, numOutChans
, sample_rate
, buffer_size
);
235 filter
->prepareToPlay(sample_rate
, buffer_size
);
238 // Check for updated parameters
240 for (int i
= 0; i
< ports_ctrl
.size(); i
++)
242 if (ports_ctrl
[i
] != nullptr)
244 cur_value
= *(float*)ports_ctrl
[i
];
245 if (ports_ctrl_last
[i
] != cur_value
) {
246 filter
->setParameter(i
, cur_value
);
247 ports_ctrl_last
.setUnchecked(i
, cur_value
);
252 jassert (activePlugins
.contains (this));
255 const ScopedLock
sl (filter
->getCallbackLock());
257 const int numIn
= numInChans
;
258 const int numOut
= numOutChans
;
260 if (filter
->isSuspended())
262 for (int i
= 0; i
< numOut
; ++i
)
263 zeromem (ports_aout
[i
], sizeof (float) * sample_count
);
268 for (i
= 0; i
< numOut
; ++i
)
270 float* chan
= tempChannels
.getUnchecked(i
);
274 chan
= ports_aout
[i
];
276 // if some output channels are disabled, some hosts supply the same buffer
277 // for multiple channels - this buggers up our method of copying the
278 // inputs over the outputs, so we need to create unique temp buffers in this case..
279 for (int j
= i
; --j
>= 0;)
281 if (ports_aout
[j
] == chan
)
283 chan
= new float [buffer_size
* 2];
284 tempChannels
.set (i
, chan
);
290 if (i
< numIn
&& chan
!= ports_ain
[i
])
291 memcpy (chan
, ports_ain
[i
], sizeof (float) * sample_count
);
297 LV2_Event_Iterator iter
;
298 lv2_event_begin(&iter
, port_min
);
300 lv2_event_buffer_reset(port_min
, LV2_EVENT_AUDIO_STAMP
, (uint8_t*)(port_min
+ 1));
302 for (uint32_t i
=0; i
< iter
.buf
->event_count
; ++i
) {
304 LV2_Event
* ev
= lv2_event_get(&iter
, &data
);
305 midiEvents
.addEvent(data
, ev
->size
, ev
->frames
);
306 lv2_event_increment(&iter
);
309 for (; i
< numIn
; ++i
)
310 channels
[i
] = ports_ain
[i
];
312 AudioSampleBuffer
chans (channels
, jmax (numIn
, numOut
), sample_count
);
314 filter
->processBlock (chans
, midiEvents
);
318 if (! midiEvents
.isEmpty())
320 #if JucePlugin_ProducesMidiOutput
321 const int numEvents
= midiEvents
.getNumEvents();
323 LV2_Event_Iterator iter
;
324 lv2_event_buffer_reset(port_mout
, LV2_EVENT_AUDIO_STAMP
, (uint8_t*)(port_mout
+ 1));
325 lv2_event_begin(&iter
, port_mout
);
327 const JUCE_NAMESPACE::uint8
* midiEventData
;
328 int midiEventSize
, midiEventPosition
;
329 MidiBuffer::Iterator
i (midiEvents
);
331 while (i
.getNextEvent (midiEventData
, midiEventSize
, midiEventPosition
))
333 jassert (midiEventPosition
>= 0 && midiEventPosition
< sample_count
);
335 lv2_event_write(&iter
, midiEventPosition
, 0, midi_uri_id
, midiEventSize
, midiEventData
);
344 if (filter
!= nullptr)
346 filter
->releaseResources();
348 isProcessing
= false;
351 deleteTempChannels();
361 free((void*)descriptor
->URI
);
366 //==============================================================================
369 // TODO - set/get chunk
371 AudioProcessor
* getFilter() { return filter
; }
375 if (chunkMemoryTime
> 0 && chunkMemoryTime
< JUCE_NAMESPACE::Time::getApproximateMillisecondCounter() - 2000)
378 chunkMemory
.setSize (0);
382 //==============================================================================
384 AudioProcessor
* filter
;
385 JUCE_NAMESPACE::MemoryBlock chunkMemory
;
386 JUCE_NAMESPACE::uint32 chunkMemoryTime
;
387 MidiBuffer midiEvents
;
388 int numInChans
, numOutChans
;
389 bool isProcessing
, firstProcessCallback
;
390 HeapBlock
<float*> channels
;
391 Array
<float*> tempChannels
; // see note in do_run()
393 const LV2_Descriptor
* descriptor
;
397 uint16_t midi_uri_id
;
400 LV2_Event_Buffer
* port_min
;
401 LV2_Event_Buffer
* port_mout
;
402 float* ports_ain
[JucePlugin_MaxNumInputChannels
];
403 float* ports_aout
[JucePlugin_MaxNumOutputChannels
];
404 Array
<float*> ports_ctrl
;
405 Array
<float> ports_ctrl_last
;
407 //==============================================================================
408 void deleteTempChannels()
410 for (int i
= tempChannels
.size(); --i
>= 0;)
411 delete[] (tempChannels
.getUnchecked(i
));
413 tempChannels
.clear();
415 if (filter
!= nullptr)
416 tempChannels
.insertMultiple (0, 0, filter
->getNumInputChannels() + filter
->getNumOutputChannels());
419 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceLV2Wrapper
);
422 //==============================================================================
423 // Create a new JUCE Editor
424 class JuceLv2Editor
: public AudioProcessorListener
427 JuceLv2Editor (JuceLV2Wrapper
* const wrapper_
, const LV2UI_Descriptor
* ui_descriptor_
, LV2UI_Write_Function write_function_
, LV2UI_Controller controller_
, LV2UI_Widget
* widget_
, const LV2_Feature
* const* features
) :
429 ui_descriptor(ui_descriptor_
),
430 write_function(write_function_
),
431 controller(controller_
),
434 initialiseJuce_GUI();
436 filter
= wrapper
->getFilter();
438 //printf("TEST - Before\n");
439 //editor = filter->createEditorIfNeeded();
440 //printf("TEST - After\n");
442 // Padding for control ports
444 #if JucePlugin_WantsMidiInput
447 #if JucePlugin_ProducesMidiOutput
450 ctrl_pad
+= JucePlugin_MaxNumInputChannels
;
451 ctrl_pad
+= JucePlugin_MaxNumOutputChannels
;
456 deleteAndZero (editor
);
460 void do_port_event(uint32_t port_index
, float value
)
462 filter
->setParameter(port_index
-ctrl_pad
, value
);
469 free((void*)ui_descriptor
->URI
);
470 delete ui_descriptor
;
474 void audioProcessorParameterChanged (AudioProcessor
*, int index
, float newValue
)
476 if (controller
&& write_function
)
477 write_function(controller
, index
+ctrl_pad
, sizeof(float), 0, &newValue
);
480 void audioProcessorChanged (AudioProcessor
*) {}
483 JuceLV2Wrapper
* wrapper
;
484 const LV2UI_Descriptor
* ui_descriptor
;
485 LV2UI_Write_Function write_function
;
486 LV2UI_Controller controller
;
487 LV2UI_Widget
* widget
;
489 AudioProcessor
* filter
;
490 AudioProcessorEditor
* editor
;
494 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceLv2Editor
);
497 //==============================================================================
498 // LV2 descriptor functions
499 LV2_Handle
juce_lv2_instantiate(const LV2_Descriptor
* descriptor
, double sample_rate
, const char* bundle_path
, const LV2_Feature
* const* features
)
501 JuceLV2Wrapper
* wrapper
= new JuceLV2Wrapper(descriptor
, sample_rate
, features
);
505 void juce_lv2_connect_port(LV2_Handle instance
, uint32_t port
, void* data_location
)
507 JuceLV2Wrapper
* wrapper
= (JuceLV2Wrapper
*)instance
;
508 wrapper
->do_connect_port(port
, data_location
);
511 void juce_lv2_activate(LV2_Handle instance
)
513 JuceLV2Wrapper
* wrapper
= (JuceLV2Wrapper
*)instance
;
514 wrapper
->do_activate();
517 void juce_lv2_run(LV2_Handle instance
, uint32_t sample_count
)
519 JuceLV2Wrapper
* wrapper
= (JuceLV2Wrapper
*)instance
;
520 wrapper
->do_run(sample_count
);
523 void juce_lv2_deactivate(LV2_Handle instance
)
525 JuceLV2Wrapper
* wrapper
= (JuceLV2Wrapper
*)instance
;
526 wrapper
->do_deactivate();
529 void juce_lv2_cleanup(LV2_Handle instance
)
531 JuceLV2Wrapper
* wrapper
= (JuceLV2Wrapper
*)instance
;
532 wrapper
->do_cleanup();
536 const void* juce_lv2_extension_data(const char* uri
)
538 printf("TODO :: juce_lv2_extension_data()\n");
542 //==============================================================================
543 LV2UI_Handle
juce_lv2ui_instantiate(const LV2UI_Descriptor
* descriptor
, const char* plugin_uri
, const char* bundle_path
, LV2UI_Write_Function write_function
, LV2UI_Controller controller
, LV2UI_Widget
* widget
, const LV2_Feature
* const* features
)
545 for (uint16_t i
= 0; features
[i
]; i
++)
547 if (strcmp(features
[i
]->URI
, LV2_INSTANCE_ACCESS_URI
) == 0 && features
[i
]->data
)
549 JuceLV2Wrapper
* wrapper
= (JuceLV2Wrapper
*)features
[i
]->data
;
550 JuceLv2Editor
* editor
= new JuceLv2Editor(wrapper
, descriptor
, write_function
, controller
, widget
, features
);
554 printf("Host does not support instance data - cannot use UI\n");
558 void juce_lv2ui_port_event(LV2UI_Handle instance
, uint32_t port_index
, uint32_t buffer_size
, uint32_t format
, const void* buffer
)
560 JuceLv2Editor
* editor
= (JuceLv2Editor
*)instance
;
562 if (buffer_size
== sizeof(float) && format
== 0)
564 float value
= *(float*)buffer
;
565 editor
->do_port_event(port_index
, value
);
569 void juce_lv2ui_cleanup(LV2UI_Handle instance
)
571 JuceLv2Editor
* editor
= (JuceLv2Editor
*)instance
;
572 editor
->do_cleanup();
576 //==============================================================================
577 // Create new LV2 objects
578 LV2_Descriptor
* getNewLv2Plugin()
580 LV2_Descriptor
* const Lv2Plugin
= new LV2_Descriptor
;
581 Lv2Plugin
->URI
= strdup((const char*)get_uri().toUTF8());
582 Lv2Plugin
->instantiate
= juce_lv2_instantiate
;
583 Lv2Plugin
->connect_port
= juce_lv2_connect_port
;
584 Lv2Plugin
->activate
= juce_lv2_activate
;
585 Lv2Plugin
->run
= juce_lv2_run
;
586 Lv2Plugin
->deactivate
= juce_lv2_deactivate
;
587 Lv2Plugin
->cleanup
= juce_lv2_cleanup
;
588 Lv2Plugin
->extension_data
= juce_lv2_extension_data
;
592 LV2UI_Descriptor
* getNewLv2UI(bool external
)
594 LV2UI_Descriptor
* Lv2UI
= new LV2UI_Descriptor
;
595 Lv2UI
->URI
= strdup((const char*) (external
? get_external_ui_uri() : get_juce_ui_uri()).toUTF8());
596 Lv2UI
->instantiate
= juce_lv2ui_instantiate
;
597 Lv2UI
->cleanup
= juce_lv2ui_cleanup
;
598 Lv2UI
->port_event
= juce_lv2ui_port_event
;
599 Lv2UI
->extension_data
= juce_lv2_extension_data
;
603 //==============================================================================
604 // Mac startup code..
607 extern "C" __attribute__ ((visibility("default"))) const LV2_Descriptor
* lv2_descriptor(uint32_t index
)
610 return (index
== 0) ? getNewLv2Plugin() : nullptr;
613 extern "C" __attribute__ ((visibility("default"))) const LV2UI_Descriptor
* lv2ui_descriptor(uint32_t index
)
615 return (index
<= 1) ? getNewLv2UI((index
== 0)) : nullptr;
618 //==============================================================================
619 // Linux startup code..
622 extern "C" __attribute__ ((visibility("default"))) const LV2_Descriptor
* lv2_descriptor(uint32_t index
)
624 return (index
== 0) ? getNewLv2Plugin() : nullptr;
627 extern "C" __attribute__ ((visibility("default"))) const LV2UI_Descriptor
* lv2ui_descriptor(uint32_t index
)
629 return (index
<= 1) ? getNewLv2UI((index
== 0)) : nullptr;
632 // don't put initialiseJuce_GUI or shutdownJuce_GUI in these... it will crash!
633 __attribute__((constructor
)) void myPluginInit() {}
634 __attribute__((destructor
)) void myPluginFini() {}
636 //==============================================================================
637 // Win32 startup code..
640 extern "C" __declspec (dllexport
) const LV2_Descriptor
* lv2_descriptor(uint32_t index
)
642 return (index
== 0) ? getNewLv2Plugin() : nullptr;
645 extern "C" __declspec (dllexport
) const LV2UI_Descriptor
* lv2ui_descriptor(uint32_t index
)
647 return (index
<= 1) ? getNewLv2UI((index
== 0)) : nullptr;