Fix build with gcc 4.7
[cadence.git] / src / carla / misc.cpp
blob3cb09d7f7511362d3d0f6ba10ec8f4008aa22446
1 /* JACK Backend code for Carla */
3 #include "misc.h"
4 #include "audio_plugin.h"
6 #include <QtCore/QProcess>
8 // Global variables
9 extern AudioPlugin* AudioPlugins[MAX_PLUGINS];
11 extern volatile double ains_peak[MAX_PLUGINS*2];
12 extern volatile double aouts_peak[MAX_PLUGINS*2];
14 // Global OSC stuff
15 extern OscData global_osc_data;
17 // --------------------------------------------------------------------------------------------------------
18 // Helper functions
20 const char* bool2str(bool yesno)
22 if (yesno)
23 return "true";
24 else
25 return "false";
28 // --------------------------------------------------------------------------------------------------------
29 // PluginOscThread
31 PluginOscThread::PluginOscThread(QObject *parent) :
32 QThread(parent)
34 qDebug("PluginOscThread::PluginOscThread(%p)", parent);
35 m_process = new QProcess(parent);
38 PluginOscThread::~PluginOscThread()
40 // TODO - kill process
41 delete m_process;
44 void PluginOscThread::set_plugin(AudioPlugin* plugin)
46 m_plugin = plugin;
49 void PluginOscThread::run()
53 // --------------------------------------------------------------------------------------------------------
54 // CarlaCheckThread
56 CarlaCheckThread::CarlaCheckThread(QObject *parent) :
57 QThread(parent)
59 qDebug("CarlaCheckThread::CarlaCheckThread(%p)", parent);
62 void CarlaCheckThread::run()
64 qDebug("CarlaCheckThread::run()");
66 uint32_t j;
67 PluginPostEvent post_events[MAX_POSTEVENTS];
69 while (carla_is_engine_running())
71 for (unsigned short i=0; i<MAX_PLUGINS; i++)
73 AudioPlugin* plugin = AudioPlugins[i];
74 if (plugin && plugin->id >= 0)
76 // --------------------------------------------------------------------------------------------------------
77 // Process postponed events
79 // Make a safe copy of events, and clear them
80 plugin->post_events_lock();
81 memcpy(&post_events, &plugin->post_events.events, sizeof(PluginPostEvent)*MAX_POSTEVENTS);
83 for (j=0; j < MAX_POSTEVENTS; j++)
84 plugin->post_events.events[j].valid = false;
86 plugin->post_events_unlock();
88 // Process events now
89 for (j=0; j < MAX_POSTEVENTS; j++)
91 if (post_events[j].valid)
93 switch (post_events[j].type)
95 case PostEventDebug:
96 callback_action(CALLBACK_DEBUG, plugin->id, post_events[j].index, 0, post_events[j].value);
97 break;
99 case PostEventParameter:
100 osc_send_set_parameter_value(&global_osc_data, plugin->id, post_events[j].index, post_events[j].value);
101 callback_action(CALLBACK_PARAMETER_CHANGED, plugin->id, post_events[j].index, 0, post_events[j].value);
103 if (plugin->hints & PLUGIN_IS_BRIDGE)
104 osc_send_control(&plugin->osc.data, post_events[j].index, post_events[j].value);
105 break;
107 case PostEventProgram:
108 osc_send_set_program(&global_osc_data, plugin->id, post_events[j].index);
109 callback_action(CALLBACK_PROGRAM_CHANGED, plugin->id, post_events[j].index, 0, 0.0f);
111 if (plugin->hints & PLUGIN_IS_BRIDGE)
112 osc_send_program(&plugin->osc.data, post_events[j].index);
114 for (uint32_t k=0; k < plugin->param.count; k++)
115 osc_send_set_default_value(&global_osc_data, plugin->id, k, plugin->param.ranges[k].def);
116 break;
118 case PostEventMidiProgram:
119 osc_send_set_midi_program(&global_osc_data, plugin->id, post_events[j].index);
120 callback_action(CALLBACK_MIDI_PROGRAM_CHANGED, plugin->id, post_events[j].index, 0, 0.0f);
122 if (plugin->type == PLUGIN_DSSI)
123 osc_send_program_as_midi(&plugin->osc.data, plugin->midiprog.data[post_events[j].index].bank, plugin->midiprog.data[post_events[j].index].program);
125 if (plugin->hints & PLUGIN_IS_BRIDGE)
126 osc_send_midi_program(&plugin->osc.data, plugin->midiprog.data[post_events[j].index].bank, plugin->midiprog.data[post_events[j].index].program);
128 for (uint32_t k=0; k < plugin->param.count; k++)
129 osc_send_set_default_value(&global_osc_data, plugin->id, k, plugin->param.ranges[k].def);
130 break;
132 case PostEventNoteOn:
133 osc_send_note_on(&global_osc_data, plugin->id, post_events[j].index, post_events[j].value);
134 callback_action(CALLBACK_NOTE_ON, plugin->id, post_events[j].index, post_events[j].value, 0.0f);
136 if (plugin->hints & PLUGIN_IS_BRIDGE)
137 osc_send_note_on(&plugin->osc.data, plugin->id, post_events[j].index, post_events[j].value);
138 break;
140 case PostEventNoteOff:
141 osc_send_note_off(&global_osc_data, plugin->id, post_events[j].index, post_events[j].value);
142 callback_action(CALLBACK_NOTE_OFF, plugin->id, post_events[j].index, post_events[j].value, 0.0f);
144 if (plugin->hints & PLUGIN_IS_BRIDGE)
145 osc_send_note_off(&plugin->osc.data, plugin->id, post_events[j].index, post_events[j].value);
146 break;
148 default:
149 break;
154 // --------------------------------------------------------------------------------------------------------
155 // Idle plugin
156 if (plugin->gui.visible && plugin->gui.type != GUI_EXTERNAL_OSC)
157 plugin->idle_gui();
159 // --------------------------------------------------------------------------------------------------------
160 // Update ports
162 // Check if it needs update
163 bool update_ports_gui = (plugin->gui.visible && plugin->gui.type == GUI_EXTERNAL_OSC && plugin->osc.data.target);
165 if (!global_osc_data.target && !update_ports_gui)
166 continue;
168 // Update
169 for (j=0; j < plugin->param.count; j++)
171 if (plugin->param.data[j].type == PARAMETER_OUTPUT && (plugin->param.data[j].hints & PARAMETER_IS_AUTOMABLE) > 0)
173 if (update_ports_gui)
174 osc_send_control(&plugin->osc.data, plugin->param.data[j].rindex, plugin->param.buffers[j]);
176 osc_send_set_parameter_value(&global_osc_data, plugin->id, j, plugin->param.buffers[j]);
180 // --------------------------------------------------------------------------------------------------------
181 // Send peak values (OSC)
182 if (global_osc_data.target)
184 if (plugin->ain.count > 0)
186 osc_send_set_input_peak_value(&global_osc_data, plugin->id, 1, ains_peak[(plugin->id*2)+0]);
187 osc_send_set_input_peak_value(&global_osc_data, plugin->id, 2, ains_peak[(plugin->id*2)+1]);
189 if (plugin->aout.count > 0)
191 osc_send_set_output_peak_value(&global_osc_data, plugin->id, 1, aouts_peak[(plugin->id*2)+0]);
192 osc_send_set_output_peak_value(&global_osc_data, plugin->id, 2, aouts_peak[(plugin->id*2)+1]);
197 usleep(50000); // 50 ms