1 /* Code for DSSI plugins */
3 #include "carla_backend.h"
4 #include "audio_plugin.h"
10 #include "dssi/dssi+chunks.h"
13 extern const char* unique_names
[MAX_PLUGINS
];
14 extern AudioPlugin
* AudioPlugins
[MAX_PLUGINS
];
15 extern ExternalMidiNote ExternalMidiNotes
[MAX_MIDI_EVENTS
];
17 extern volatile double ains_peak
[MAX_PLUGINS
*2];
18 extern volatile double aouts_peak
[MAX_PLUGINS
*2];
21 extern jack_client_t
* carla_jack_client
;
24 extern carla_options_t carla_options
;
27 void carla_jack_register_plugin(AudioPlugin
* plugin
);
29 class DssiOscGuiThread
: public QThread
32 DssiOscGuiThread(QObject
* parent
=0);
33 void set_plugin_id(short plugin_id
);
40 class DssiAudioPlugin
: public AudioPlugin
44 const LADSPA_Descriptor
* ldescriptor
;
45 const DSSI_Descriptor
* descriptor
;
46 snd_seq_event_t midi_events
[MAX_MIDI_EVENTS
];
50 qDebug("DssiAudioPlugin::DssiAudioPlugin()");
55 ldescriptor
= nullptr;
57 for (unsigned int i
=0; i
<MAX_MIDI_EVENTS
; i
++)
58 memset(&midi_events
[i
], 0, sizeof(snd_seq_event_t
));
63 qDebug("DssiAudioPlugin::~DssiAudioPlugin()");
66 if (hints
& PLUGIN_HAS_GUI
)
69 osc_send_hide(&osc
.data
);
71 osc_send_quit(&osc
.data
);
77 if (!osc
.thread
->wait(3000)) // 3 sec
78 qWarning("Failed to properly stop DSSI GUI thread");
83 osc_clear_data(&osc
.data
);
86 if (handle
&& ldescriptor
->deactivate
&& active_before
)
87 ldescriptor
->deactivate(handle
);
89 if (handle
&& ldescriptor
->cleanup
)
90 ldescriptor
->cleanup(handle
);
95 qDebug("DssiAudioPlugin::delete_me()");
99 int set_osc_bridge_info(PluginOscBridgeInfoType
, lo_arg
**)
104 PluginCategory
get_category()
106 if (min
.count
> 0 && aout
.count
> 0)
107 return PLUGIN_CATEGORY_SYNTH
;
109 return PLUGIN_CATEGORY_NONE
;
112 void get_label(char* buf_str
)
114 strncpy(buf_str
, ldescriptor
->Label
, STR_MAX
);
117 void get_maker(char* buf_str
)
119 strncpy(buf_str
, ldescriptor
->Maker
, STR_MAX
);
122 void get_copyright(char* buf_str
)
124 strncpy(buf_str
, ldescriptor
->Copyright
, STR_MAX
);
127 void get_real_name(char* buf_str
)
129 strncpy(buf_str
, ldescriptor
->Name
, STR_MAX
);
134 return ldescriptor
->UniqueID
;
137 void get_parameter_name(uint32_t rindex
, char* buf_str
)
139 strncpy(buf_str
, ldescriptor
->PortNames
[rindex
], STR_MAX
);
142 void get_parameter_symbol(uint32_t, char* buf_str
)
147 void get_parameter_label(uint32_t, char* buf_str
)
152 uint32_t get_scalepoint_count(uint32_t)
157 double get_scalepoint_value(uint32_t, uint32_t)
162 void get_scalepoint_label(uint32_t, uint32_t, char* buf_str
)
167 int32_t get_chunk_data(void** data_ptr
)
169 unsigned long long_data_size
= 0;
170 if (descriptor
->getCustomData(handle
, data_ptr
, &long_data_size
))
171 return long_data_size
;
176 void x_set_parameter_value(uint32_t parameter_id
, double value
, bool gui_send
)
178 if (gui_send
&& gui
.visible
)
179 osc_send_control(&osc
.data
, param
.data
[parameter_id
].rindex
, value
);
182 void x_set_program(uint32_t, bool, bool)
186 void x_set_midi_program(uint32_t midi_program_id
, bool gui_send
, bool block
)
188 if (!descriptor
->get_program
|| !descriptor
->select_program
)
191 if (block
) carla_proc_lock();
192 descriptor
->select_program(handle
, midiprog
.data
[midi_program_id
].bank
, midiprog
.data
[midi_program_id
].program
);
193 if (block
) carla_proc_unlock();
195 if (gui_send
&& gui
.visible
)
196 osc_send_program_as_midi(&osc
.data
, midiprog
.data
[midi_program_id
].bank
, midiprog
.data
[midi_program_id
].program
);
199 void x_set_custom_data(CustomDataType
, const char* key
, const char* value
, bool gui_send
)
201 descriptor
->configure(handle
, key
, value
);
203 if (gui_send
&& gui
.visible
)
204 osc_send_configure(&osc
.data
, key
, value
);
206 if (strcmp(key
, "reloadprograms") == 0 || strcmp(key
, "load") == 0 || strncmp(key
, "patches", 7) == 0)
208 reload_programs(false);
210 else if (strcmp(key
, "names") == 0) // Not in the API!
214 osc_send_set_program_count(&osc
.data
, id
, prog
.count
);
217 int j
, k
, last_str_n
= 0;
218 int str_len
= strlen(value
);
221 for (uint32_t i
=0; i
< prog
.count
; i
++)
223 for (j
=0, k
=last_str_n
; j
<256 && k
+j
<str_len
; j
++)
225 name
[j
] = value
[k
+j
];
226 if (value
[k
+j
] == ',')
230 free((void*)prog
.names
[i
]);
231 prog
.names
[i
] = strdup(name
);
236 osc_send_set_program_name(&osc
.data
, id
, i
, prog
.names
[i
]);
239 callback_action(CALLBACK_RELOAD_PROGRAMS
, id
, 0, 0, 0.0f
);
244 void set_chunk_data(const char* string_data
)
246 QByteArray chunk
= QByteArray::fromBase64(string_data
);
247 descriptor
->setCustomData(handle
, chunk
.data(), chunk
.size());
250 void set_gui_data(int, void*)
254 void show_gui(bool yesno
)
260 // 40 re-tries, 4 secs
261 for (int j
=1; j
<40 && gui
.show_now
; j
++)
265 osc_send_show(&osc
.data
);
274 qDebug("DssiAudioPlugin::show_gui() - GUI timeout");
275 callback_action(CALLBACK_SHOW_GUI
, id
, 0, 0, 0.0);
280 osc_send_hide(&osc
.data
);
281 osc_send_quit(&osc
.data
);
282 osc_clear_data(&osc
.data
);
292 qDebug("DssiAudioPlugin::reload()");
295 // Safely disable plugin for reload
299 if (carla_options
.global_jack_client
== false)
300 jack_deactivate(jack_client
);
304 // Unregister jack ports
310 uint32_t ains
, aouts
, mins
, params
, j
;
311 ains
= aouts
= mins
= params
= 0;
313 const unsigned long PortCount
= ldescriptor
->PortCount
;
314 for (unsigned long i
=0; i
<PortCount
; i
++)
316 LADSPA_PortDescriptor PortType
= ldescriptor
->PortDescriptors
[i
];
317 if (LADSPA_IS_PORT_AUDIO(PortType
))
319 if (LADSPA_IS_PORT_INPUT(PortType
))
321 else if (LADSPA_IS_PORT_OUTPUT(PortType
))
324 else if (LADSPA_IS_PORT_CONTROL(PortType
))
328 if (descriptor
->run_synth
|| descriptor
->run_multiple_synths
)
333 ain
.rindexes
= new uint32_t[ains
];
334 ain
.ports
= new jack_port_t
*[ains
];
339 aout
.rindexes
= new uint32_t[aouts
];
340 aout
.ports
= new jack_port_t
*[aouts
];
345 min
.ports
= new jack_port_t
*[mins
];
350 param
.buffers
= new float[params
];
351 param
.data
= new ParameterData
[params
];
352 param
.ranges
= new ParameterRanges
[params
];
355 const int port_name_size
= jack_port_name_size();
356 char port_name
[port_name_size
];
357 bool needs_cin
= false;
358 bool needs_cout
= false;
360 for (unsigned long i
=0; i
<PortCount
; i
++)
362 LADSPA_PortDescriptor PortType
= ldescriptor
->PortDescriptors
[i
];
363 LADSPA_PortRangeHint PortHint
= ldescriptor
->PortRangeHints
[i
];
365 if (LADSPA_IS_PORT_AUDIO(PortType
))
367 if (carla_options
.global_jack_client
)
369 strncpy(port_name
, name
, (port_name_size
/2)-2);
370 strcat(port_name
, ":");
371 strncat(port_name
, ldescriptor
->PortNames
[i
], port_name_size
/2);
374 strncpy(port_name
, ldescriptor
->PortNames
[i
], port_name_size
);
376 if (LADSPA_IS_PORT_INPUT(PortType
))
379 ain
.ports
[j
] = jack_port_register(jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
382 else if (LADSPA_IS_PORT_OUTPUT(PortType
))
385 aout
.ports
[j
] = jack_port_register(jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
386 aout
.rindexes
[j
] = i
;
390 qWarning("WARNING - Got a broken Port (Audio, but not input or output)");
392 else if (LADSPA_IS_PORT_CONTROL(PortType
))
395 param
.data
[j
].index
= j
;
396 param
.data
[j
].rindex
= i
;
397 param
.data
[j
].hints
= 0;
398 param
.data
[j
].midi_channel
= 0;
399 param
.data
[j
].midi_cc
= -1;
401 double min
, max
, def
, step
, step_small
, step_large
;
404 if (LADSPA_IS_HINT_BOUNDED_BELOW(PortHint
.HintDescriptor
))
405 min
= PortHint
.LowerBound
;
410 if (LADSPA_IS_HINT_BOUNDED_ABOVE(PortHint
.HintDescriptor
))
411 max
= PortHint
.UpperBound
;
421 if (LADSPA_IS_HINT_HAS_DEFAULT(PortHint
.HintDescriptor
))
423 switch (PortHint
.HintDescriptor
& LADSPA_HINT_DEFAULT_MASK
)
425 case LADSPA_HINT_DEFAULT_MINIMUM
:
428 case LADSPA_HINT_DEFAULT_MAXIMUM
:
431 case LADSPA_HINT_DEFAULT_0
:
434 case LADSPA_HINT_DEFAULT_1
:
437 case LADSPA_HINT_DEFAULT_100
:
440 case LADSPA_HINT_DEFAULT_440
:
443 case LADSPA_HINT_DEFAULT_LOW
:
444 if (LADSPA_IS_HINT_LOGARITHMIC(PortHint
.HintDescriptor
))
445 def
= exp((log(min
)*0.75) + (log(max
)*0.25));
447 def
= (min
*0.75) + (max
*0.25);
449 case LADSPA_HINT_DEFAULT_MIDDLE
:
450 if (LADSPA_IS_HINT_LOGARITHMIC(PortHint
.HintDescriptor
))
455 case LADSPA_HINT_DEFAULT_HIGH
:
456 if (LADSPA_IS_HINT_LOGARITHMIC(PortHint
.HintDescriptor
))
457 def
= exp((log(min
)*0.25) + (log(max
)*0.75));
459 def
= (min
*0.25) + (max
*0.75);
462 if (min
< 0.0 && max
> 0.0)
472 if (min
< 0.0 && max
> 0.0)
483 if (max
- min
== 0.0)
485 qWarning("Broken plugin parameter -> max - min == 0");
489 if (LADSPA_IS_HINT_SAMPLE_RATE(PortHint
.HintDescriptor
))
491 double sample_rate
= get_sample_rate();
495 param
.data
[j
].hints
|= PARAMETER_USES_SAMPLERATE
;
498 if (LADSPA_IS_HINT_INTEGER(PortHint
.HintDescriptor
))
504 else if (LADSPA_IS_HINT_TOGGLED(PortHint
.HintDescriptor
))
512 double range
= max
- min
;
514 step_small
= range
/1000.0;
515 step_large
= range
/10.0;
518 if (LADSPA_IS_PORT_INPUT(PortType
))
520 param
.data
[j
].type
= PARAMETER_INPUT
;
521 param
.data
[j
].hints
|= PARAMETER_IS_ENABLED
;
522 param
.data
[j
].hints
|= PARAMETER_IS_AUTOMABLE
;
526 if (descriptor
->get_midi_controller_for_port
)
528 int cc
= descriptor
->get_midi_controller_for_port(handle
, i
);
529 if (DSSI_CONTROLLER_IS_SET(cc
) && DSSI_IS_CC(cc
))
530 param
.data
[j
].midi_cc
= DSSI_CC_NUMBER(cc
);
533 else if (LADSPA_IS_PORT_OUTPUT(PortType
))
535 param
.data
[j
].type
= PARAMETER_OUTPUT
;
536 param
.data
[j
].hints
|= PARAMETER_IS_ENABLED
;
538 if (strcmp(ldescriptor
->PortNames
[i
], "latency") != 0 && strcmp(ldescriptor
->PortNames
[i
], "_latency") != 0)
540 param
.data
[j
].hints
|= PARAMETER_IS_AUTOMABLE
;
547 max
= get_sample_rate();
556 param
.data
[j
].type
= PARAMETER_UNKNOWN
;
557 qWarning("WARNING - Got a broken Port (Control, but not input or output)");
560 param
.ranges
[j
].min
= min
;
561 param
.ranges
[j
].max
= max
;
562 param
.ranges
[j
].def
= def
;
563 param
.ranges
[j
].step
= step
;
564 param
.ranges
[j
].step_small
= step_small
;
565 param
.ranges
[j
].step_large
= step_large
;
567 // Start parameters in their default values
568 param
.buffers
[j
] = def
;
570 ldescriptor
->connect_port(handle
, i
, ¶m
.buffers
[j
]);
574 // Not Audio or Control
575 qCritical("ERROR - Got a broken Port (neither Audio or Control)");
576 ldescriptor
->connect_port(handle
, i
, nullptr);
582 if (carla_options
.global_jack_client
)
584 strncpy(port_name
, name
, (port_name_size
/2)-2);
585 strcat(port_name
, ":control-in");
588 strcpy(port_name
, "control-in");
590 param
.port_in
= jack_port_register(jack_client
, port_name
, JACK_DEFAULT_MIDI_TYPE
, JackPortIsInput
, 0);
595 if (carla_options
.global_jack_client
)
597 strncpy(port_name
, name
, (port_name_size
/2)-2);
598 strcat(port_name
, ":control-out");
601 strcpy(port_name
, "control-out");
603 param
.port_out
= jack_port_register(jack_client
, port_name
, JACK_DEFAULT_MIDI_TYPE
, JackPortIsOutput
, 0);
608 if (carla_options
.global_jack_client
)
610 strncpy(port_name
, name
, (port_name_size
/2)-2);
611 strcat(port_name
, ":midi-in");
614 strcpy(port_name
, "midi-in");
616 min
.ports
[0] = jack_port_register(jack_client
, port_name
, JACK_DEFAULT_MIDI_TYPE
, JackPortIsInput
, 0);
622 param
.count
= params
;
624 reload_programs(true);
626 // Check if plugin is synth
627 if (min
.count
> 0 && aout
.count
> 0)
628 hints
|= PLUGIN_IS_SYNTH
;
630 // Check if plugin uses chunks -> hack by name check ["some-name VST"]
631 if (QString(ldescriptor
->Name
).endsWith(" VST", Qt::CaseSensitive
))
633 if (descriptor
->getCustomData
&& descriptor
->setCustomData
)
634 hints
|= PLUGIN_USES_CHUNKS
;
637 // Other plugin checks
638 if (aouts
> 0 && (ains
== aouts
|| ains
== 1))
639 hints
|= PLUGIN_CAN_DRYWET
;
642 hints
|= PLUGIN_CAN_VOL
;
644 if (aouts
>= 2 && aouts
%2 == 0)
645 hints
|= PLUGIN_CAN_BALANCE
;
651 if (carla_options
.global_jack_client
== false)
652 jack_activate(jack_client
);
655 void reload_programs(bool init
)
657 qDebug("DssiAudioPlugin::reload_programs(%s)", bool2str(init
));
658 uint32_t i
, old_count
= midiprog
.count
;
660 // Delete old programs
661 if (midiprog
.count
> 0)
663 for (uint32_t i
=0; i
< midiprog
.count
; i
++)
664 free((void*)midiprog
.names
[i
]);
666 delete[] midiprog
.data
;
667 delete[] midiprog
.names
;
671 // Query new programs
672 if (descriptor
->get_program
&& descriptor
->select_program
)
674 while (descriptor
->get_program(handle
, midiprog
.count
))
678 if (midiprog
.count
> 0)
680 midiprog
.data
= new midi_program_t
[midiprog
.count
];
681 midiprog
.names
= new const char* [midiprog
.count
];
685 for (i
=0; i
< midiprog
.count
; i
++)
687 const DSSI_Program_Descriptor
* pdesc
= descriptor
->get_program(handle
, i
);
690 midiprog
.data
[i
].bank
= pdesc
->Bank
;
691 midiprog
.data
[i
].program
= pdesc
->Program
;
692 midiprog
.names
[i
] = strdup(pdesc
->Name
);
696 midiprog
.data
[i
].bank
= 0;
697 midiprog
.data
[i
].program
= 0;
698 midiprog
.names
[i
] = strdup("(error)");
703 osc_send_set_midi_program_count(&global_osc_data
, id
, midiprog
.count
);
705 if (midiprog
.count
> 0)
707 for (i
=0; i
< midiprog
.count
; i
++)
708 osc_send_set_midi_program_data(&global_osc_data
, id
, i
, midiprog
.data
[i
].bank
, midiprog
.data
[i
].program
, midiprog
.names
[i
]);
711 callback_action(CALLBACK_RELOAD_PROGRAMS
, id
, 0, 0, 0.0);
715 if (midiprog
.count
> 0)
716 set_midi_program(0, false, false, false, true);
720 callback_action(CALLBACK_UPDATE
, id
, 0, 0, 0.0);
722 // Check if current program is invalid
723 bool program_changed
= false;
725 if (midiprog
.count
== old_count
+1)
727 // one midi program added, probably created by user
728 midiprog
.current
= old_count
;
729 program_changed
= true;
731 else if (midiprog
.current
>= (int32_t)midiprog
.count
)
733 // current midi program > count
734 midiprog
.current
= 0;
735 program_changed
= true;
737 else if (midiprog
.current
< 0 && midiprog
.count
> 0)
739 // programs exist now, but not before
740 midiprog
.current
= 0;
741 program_changed
= true;
743 else if (midiprog
.current
>= 0 && midiprog
.count
== 0)
745 // programs existed before, but not anymore
746 midiprog
.current
= -1;
747 program_changed
= true;
751 set_midi_program(midiprog
.current
, true, true, true, true);
755 void prepare_for_save()
759 void process(jack_nframes_t nframes
)
762 unsigned short plugin_id
= id
;
763 unsigned long midi_event_count
= 0;
765 double ains_peak_tmp
[2] = { 0.0 };
766 double aouts_peak_tmp
[2] = { 0.0 };
768 jack_default_audio_sample_t
* ains_buffer
[ain
.count
];
769 jack_default_audio_sample_t
* aouts_buffer
[aout
.count
];
770 jack_default_audio_sample_t
* mins_buffer
[min
.count
];
772 for (i
=0; i
< ain
.count
; i
++)
773 ains_buffer
[i
] = (jack_default_audio_sample_t
*)jack_port_get_buffer(ain
.ports
[i
], nframes
);
775 for (i
=0; i
< aout
.count
; i
++)
776 aouts_buffer
[i
] = (jack_default_audio_sample_t
*)jack_port_get_buffer(aout
.ports
[i
], nframes
);
778 for (i
=0; i
< min
.count
; i
++)
779 mins_buffer
[i
] = (jack_default_audio_sample_t
*)jack_port_get_buffer(min
.ports
[i
], nframes
);
781 // --------------------------------------------------------------------------------------------------------
786 short j2
= (ain
.count
== 1) ? 0 : 1;
788 for (k
=0; k
<nframes
; k
++)
790 if (ains_buffer
[0][k
] > ains_peak_tmp
[0])
791 ains_peak_tmp
[0] = ains_buffer
[0][k
];
792 if (ains_buffer
[j2
][k
] > ains_peak_tmp
[1])
793 ains_peak_tmp
[1] = ains_buffer
[j2
][k
];
797 CARLA_PROCESS_CONTINUE_CHECK
;
799 // --------------------------------------------------------------------------------------------------------
800 // Parameters Input [Automation]
804 jack_default_audio_sample_t
* pin_buffer
= (jack_default_audio_sample_t
*)jack_port_get_buffer(param
.port_in
, nframes
);
806 jack_midi_event_t pin_event
;
807 uint32_t n_pin_events
= jack_midi_get_event_count(pin_buffer
);
809 for (i
=0; i
<n_pin_events
; i
++)
811 if (jack_midi_event_get(&pin_event
, pin_buffer
, i
) != 0)
814 unsigned char channel
= pin_event
.buffer
[0] & 0x0F;
815 unsigned char mode
= pin_event
.buffer
[0] & 0xF0;
820 unsigned char status
= pin_event
.buffer
[1] & 0x7F;
821 unsigned char velo
= pin_event
.buffer
[2] & 0x7F;
822 double value
, velo_per
= double(velo
)/127;
824 // Control GUI stuff (channel 0 only)
830 set_active(false, false, false);
831 postpone_event(PostEventParameter
, PARAMETER_ACTIVE
, 0.0);
834 else if (status
== 0x09 && hints
& PLUGIN_CAN_DRYWET
)
836 // Dry/Wet (using '0x09', undefined)
837 set_drywet(velo_per
, false, false);
838 postpone_event(PostEventParameter
, PARAMETER_DRYWET
, velo_per
);
840 else if (status
== 0x07 && hints
& PLUGIN_CAN_VOL
)
843 value
= double(velo
)/100;
844 set_vol(value
, false, false);
845 postpone_event(PostEventParameter
, PARAMETER_VOLUME
, value
);
847 else if (status
== 0x08 && hints
& PLUGIN_CAN_BALANCE
)
851 value
= (double(velo
)-63.5)/63.5;
856 right
= (value
*2)+1.0;
860 left
= (value
*2)-1.0;
869 set_balance_left(left
, false, false);
870 set_balance_right(right
, false, false);
871 postpone_event(PostEventParameter
, PARAMETER_BALANCE_LEFT
, left
);
872 postpone_event(PostEventParameter
, PARAMETER_BALANCE_RIGHT
, right
);
876 // Control plugin parameters
877 for (k
=0; k
< param
.count
; k
++)
879 if (param
.data
[k
].type
== PARAMETER_INPUT
&& (param
.data
[k
].hints
& PARAMETER_IS_AUTOMABLE
) > 0 &&
880 param
.data
[k
].midi_channel
== channel
&& param
.data
[k
].midi_cc
== status
)
882 value
= (velo_per
* (param
.ranges
[k
].max
- param
.ranges
[k
].min
)) + param
.ranges
[k
].min
;
883 set_parameter_value(k
, value
, false, false, false);
884 postpone_event(PostEventParameter
, k
, value
);
889 else if (mode
== 0xC0)
891 unsigned char midi_program
= pin_event
.buffer
[1] & 0x7F;
893 if (midi_program
< midiprog
.count
)
895 set_midi_program(midi_program
, false, false, false, false);
896 postpone_event(PostEventMidiProgram
, midi_program
, 0.0);
900 } // End of Parameters Input
902 CARLA_PROCESS_CONTINUE_CHECK
;
904 // --------------------------------------------------------------------------------------------------------
905 // MIDI Input (External)
911 for (i
=0; i
<MAX_MIDI_EVENTS
&& midi_event_count
< MAX_MIDI_EVENTS
; i
++)
913 if (ExternalMidiNotes
[i
].valid
)
915 if (ExternalMidiNotes
[i
].plugin_id
== plugin_id
)
917 ExternalMidiNote
* enote
= &ExternalMidiNotes
[i
];
918 enote
->valid
= false;
920 snd_seq_event_t
* midi_event
= &midi_events
[midi_event_count
];
921 memset(midi_event
, 0, sizeof(snd_seq_event_t
));
923 midi_event
->type
= enote
->onoff
? SND_SEQ_EVENT_NOTEON
: SND_SEQ_EVENT_NOTEOFF
;
924 midi_event
->data
.note
.channel
= 0;
925 midi_event
->data
.note
.note
= enote
->note
;
926 midi_event
->data
.note
.velocity
= enote
->velo
;
928 midi_event_count
+= 1;
937 } // End of MIDI Input (External)
939 CARLA_PROCESS_CONTINUE_CHECK
;
941 // --------------------------------------------------------------------------------------------------------
946 jack_midi_event_t min_event
;
947 uint32_t n_min_events
= jack_midi_get_event_count(mins_buffer
[0]);
949 for (k
=0; k
<n_min_events
&& midi_event_count
< MAX_MIDI_EVENTS
; k
++)
951 if (jack_midi_event_get(&min_event
, mins_buffer
[0], k
) != 0)
954 unsigned char channel
= min_event
.buffer
[0] & 0x0F;
955 unsigned char mode
= min_event
.buffer
[0] & 0xF0;
956 unsigned char note
= min_event
.buffer
[1] & 0x7F;
957 unsigned char velo
= min_event
.buffer
[2] & 0x7F;
960 if (mode
== 0x90 && velo
== 0)
966 snd_seq_event_t
* midi_event
= &midi_events
[midi_event_count
];
967 memset(midi_event
, 0, sizeof(snd_seq_event_t
));
971 midi_event
->type
= SND_SEQ_EVENT_NOTEOFF
;
972 midi_event
->data
.note
.channel
= channel
;
973 midi_event
->data
.note
.note
= note
;
974 midi_event
->data
.note
.velocity
= velo
;
975 midi_event
->time
.tick
= min_event
.time
;
976 postpone_event(PostEventNoteOff
, note
, velo
);
978 else if (mode
== 0x90)
980 midi_event
->type
= SND_SEQ_EVENT_NOTEON
;
981 midi_event
->data
.note
.channel
= channel
;
982 midi_event
->data
.note
.note
= note
;
983 midi_event
->data
.note
.velocity
= velo
;
984 midi_event
->time
.tick
= min_event
.time
;
985 postpone_event(PostEventNoteOn
, note
, velo
);
987 else if (mode
== 0xB0)
989 midi_event
->type
= SND_SEQ_EVENT_CONTROLLER
;
990 midi_event
->data
.control
.channel
= channel
;
991 midi_event
->data
.control
.param
= note
;
992 midi_event
->data
.control
.value
= velo
;
993 midi_event
->time
.tick
= min_event
.time
;
995 else if (mode
== 0xE0)
998 //midi_event->type = SND_SEQ_EVENT_PITCHBEND;
999 //midi_event->data.control.channel = channel;
1000 //midi_event->data.control.param = note;
1001 //midi_event->data.control.value = (min_event.buffer[2] << 7) | min_event.buffer[1];
1002 //midi_event->time.tick = min_event.time;
1005 midi_event_count
+= 1;
1007 } // End of MIDI Input (JACK)
1009 CARLA_PROCESS_CONTINUE_CHECK
;
1011 // --------------------------------------------------------------------------------------------------------
1012 // Plugin processing
1018 if (ldescriptor
->activate
)
1019 ldescriptor
->activate(handle
);
1022 for (i
=0; i
< ain
.count
; i
++)
1023 ldescriptor
->connect_port(handle
, ain
.rindexes
[i
], ains_buffer
[i
]);
1025 for (i
=0; i
< aout
.count
; i
++)
1026 ldescriptor
->connect_port(handle
, aout
.rindexes
[i
], aouts_buffer
[i
]);
1028 if (descriptor
->run_synth
)
1030 descriptor
->run_synth(handle
, nframes
, midi_events
, midi_event_count
);
1032 else if (descriptor
->run_multiple_synths
)
1034 snd_seq_event_t
* dssi_events_ptr
[] = { midi_events
, nullptr };
1035 descriptor
->run_multiple_synths(1, &handle
, nframes
, dssi_events_ptr
, &midi_event_count
);
1037 else if (ldescriptor
->run
)
1038 ldescriptor
->run(handle
, nframes
);
1044 if (ldescriptor
->deactivate
)
1045 ldescriptor
->deactivate(handle
);
1049 CARLA_PROCESS_CONTINUE_CHECK
;
1051 // --------------------------------------------------------------------------------------------------------
1052 // Post-processing (dry/wet, volume and balance)
1056 double bal_rangeL
, bal_rangeR
;
1057 jack_default_audio_sample_t old_bal_left
[nframes
];
1059 for (i
=0; i
< aout
.count
; i
++)
1061 // Dry/Wet and Volume
1062 for (k
=0; k
<nframes
; k
++)
1064 if (hints
& PLUGIN_CAN_DRYWET
&& x_drywet
!= 1.0)
1066 if (aout
.count
== 1)
1067 aouts_buffer
[i
][k
] = (aouts_buffer
[i
][k
]*x_drywet
)+(ains_buffer
[0][k
]*(1.0-x_drywet
));
1069 aouts_buffer
[i
][k
] = (aouts_buffer
[i
][k
]*x_drywet
)+(ains_buffer
[i
][k
]*(1.0-x_drywet
));
1072 if (hints
& PLUGIN_CAN_VOL
)
1073 aouts_buffer
[i
][k
] *= x_vol
;
1077 if (hints
& PLUGIN_CAN_BALANCE
)
1080 memcpy(&old_bal_left
, aouts_buffer
[i
], sizeof(jack_default_audio_sample_t
)*nframes
);
1082 bal_rangeL
= (x_bal_left
+1.0)/2;
1083 bal_rangeR
= (x_bal_right
+1.0)/2;
1085 for (k
=0; k
<nframes
; k
++)
1090 aouts_buffer
[i
][k
] = old_bal_left
[k
]*(1.0-bal_rangeL
);
1091 aouts_buffer
[i
][k
] += aouts_buffer
[i
+1][k
]*(1.0-bal_rangeR
);
1096 aouts_buffer
[i
][k
] = aouts_buffer
[i
][k
]*bal_rangeR
;
1097 aouts_buffer
[i
][k
] += old_bal_left
[k
]*bal_rangeL
;
1105 for (k
=0; k
<nframes
; k
++)
1107 if (aouts_buffer
[i
][k
] > aouts_peak_tmp
[i
])
1108 aouts_peak_tmp
[i
] = aouts_buffer
[i
][k
];
1115 // disable any output sound if not active
1116 for (i
=0; i
< aout
.count
; i
++)
1117 memset(aouts_buffer
[i
], 0.0f
, sizeof(jack_default_audio_sample_t
)*nframes
);
1119 aouts_peak_tmp
[0] = 0.0;
1120 aouts_peak_tmp
[1] = 0.0;
1122 } // End of Post-processing
1124 CARLA_PROCESS_CONTINUE_CHECK
;
1126 // --------------------------------------------------------------------------------------------------------
1131 jack_default_audio_sample_t
* cout_buffer
= (jack_default_audio_sample_t
*)jack_port_get_buffer(param
.port_out
, nframes
);
1132 jack_midi_clear_buffer(cout_buffer
);
1136 for (k
=0; k
< param
.count
; k
++)
1138 if (param
.data
[k
].type
== PARAMETER_OUTPUT
&& param
.data
[k
].midi_cc
>= 0)
1140 value_per
= (param
.buffers
[k
] - param
.ranges
[k
].min
)/(param
.ranges
[k
].max
- param
.ranges
[k
].min
);
1142 jack_midi_data_t
* event_buffer
= jack_midi_event_reserve(cout_buffer
, 0, 3);
1143 event_buffer
[0] = 0xB0 + param
.data
[k
].midi_channel
;
1144 event_buffer
[1] = param
.data
[k
].midi_cc
;
1145 event_buffer
[2] = 127*value_per
;
1148 } // End of Control Output
1150 CARLA_PROCESS_CONTINUE_CHECK
;
1152 // --------------------------------------------------------------------------------------------------------
1155 ains_peak
[(plugin_id
*2)+0] = ains_peak_tmp
[0];
1156 ains_peak
[(plugin_id
*2)+1] = ains_peak_tmp
[1];
1157 aouts_peak
[(plugin_id
*2)+0] = aouts_peak_tmp
[0];
1158 aouts_peak
[(plugin_id
*2)+1] = aouts_peak_tmp
[1];
1160 active_before
= active
;
1163 void buffer_size_changed(jack_nframes_t
)
1168 DssiOscGuiThread::DssiOscGuiThread(QObject
* parent
) :
1174 void DssiOscGuiThread::set_plugin_id(short plugin_id_
)
1176 plugin_id
= plugin_id_
;
1179 void DssiOscGuiThread::run()
1181 if (plugin_id
< 0 || plugin_id
> MAX_PLUGINS
)
1183 qCritical("DssiOscGuiThread::run() - invalid plugin id '%i'", plugin_id
);
1187 DssiAudioPlugin
* plugin
= (DssiAudioPlugin
*)AudioPlugins
[plugin_id
];
1191 qCritical("DssiOscGuiThread::run() - invalid plugin");
1195 if (plugin
->id
!= plugin_id
)
1197 qCritical("DssiOscGuiThread::run() - plugin mismatch '%i'' != '%i'", plugin
->id
, plugin_id
);
1201 QString cmd
= QString("%1 '%2/%3' '%4' '%5' '%6 (GUI)'").arg(plugin
->gui
.name
).arg(get_host_osc_url()).arg(plugin_id
).arg(plugin
->filename
).arg(plugin
->ldescriptor
->Label
).arg(plugin
->name
);
1203 qDebug("DssiOscGuiThread::run() - command: %s", cmd
.toStdString().data());
1207 while (plugin
&& plugin
->id
== plugin_id
&& carla_is_engine_running())
1209 if (plugin
->gui
.show_now
)
1211 ret
= system(cmd
.toStdString().data());
1212 plugin
->gui
.visible
= false;
1213 plugin
->gui
.show_now
= false;
1218 callback_action(CALLBACK_SHOW_GUI
, plugin_id
, 0, 0, 0.0f
);
1223 callback_action(CALLBACK_SHOW_GUI
, plugin_id
, -1, 0, 0.0f
);
1224 qWarning("DssiOscGuiThread::run() - DSSI GUI crashed");
1232 if (plugin
->gui
.name
)
1233 free((void*)plugin
->gui
.name
);
1235 plugin
->gui
.name
= nullptr;
1236 plugin
->gui
.visible
= false;
1239 short add_plugin_dssi(const char* filename
, const char* label
, void* extra_stuff
)
1241 qDebug("add_plugin_dssi(%s, %s, %p)", filename
, label
, extra_stuff
);
1243 short id
= get_new_plugin_id();
1247 DssiAudioPlugin
* plugin
= new DssiAudioPlugin
;
1249 if ((plugin
->lib_open(filename
)))
1251 DSSI_Descriptor_Function descfn
= (DSSI_Descriptor_Function
)plugin
->lib_symbol("dssi_descriptor");
1255 unsigned long i
= 0;
1256 while ((plugin
->descriptor
= descfn(i
++)))
1258 plugin
->ldescriptor
= plugin
->descriptor
->LADSPA_Plugin
;
1259 if (strcmp(plugin
->ldescriptor
->Label
, label
) == 0)
1263 if (plugin
->descriptor
&& plugin
->ldescriptor
)
1265 plugin
->handle
= plugin
->ldescriptor
->instantiate(plugin
->ldescriptor
, get_sample_rate());
1269 plugin
->name
= get_unique_name(plugin
->ldescriptor
->Name
);
1270 plugin
->filename
= strdup(filename
);
1272 if (carla_options
.global_jack_client
)
1273 plugin
->jack_client
= carla_jack_client
;
1275 carla_jack_register_plugin(plugin
);
1277 if (plugin
->jack_client
)
1282 unique_names
[id
] = plugin
->name
;
1283 AudioPlugins
[id
] = plugin
;
1286 const char* gui_filename
= (char*)extra_stuff
;
1290 DssiOscGuiThread
* dssi_thread
= new DssiOscGuiThread();
1291 dssi_thread
->set_plugin_id(plugin
->id
);
1292 dssi_thread
->start();
1294 plugin
->gui
.name
= strdup(gui_filename
);
1295 plugin
->gui
.type
= GUI_EXTERNAL_OSC
;
1296 plugin
->osc
.thread
= dssi_thread
;
1299 if (plugin
->gui
.type
!= GUI_NONE
)
1300 plugin
->hints
|= PLUGIN_HAS_GUI
;
1302 osc_new_plugin(plugin
);
1306 set_last_error("Failed to register plugin in JACK");
1313 set_last_error("Plugin failed to initialize");
1320 set_last_error("Could not find the requested plugin Label in the plugin library");
1327 set_last_error("Could not find the DSSI Descriptor in the plugin library");
1334 set_last_error(plugin
->lib_error());
1340 set_last_error("Maximum number of plugins reached");