obs-vst.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*****************************************************************************
  2. Copyright (C) 2016-2017 by Colin Edwards.
  3. Additional Code Copyright (C) 2016-2017 by c3r1c3 <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *****************************************************************************/
  15. #include "headers/VSTPlugin.h"
  16. #include <QCryptographicHash>
  17. #define OPEN_VST_SETTINGS "open_vst_settings"
  18. #define CLOSE_VST_SETTINGS "close_vst_settings"
  19. #define OPEN_WHEN_ACTIVE_VST_SETTINGS "open_when_active_vst_settings"
  20. #define PLUG_IN_NAME obs_module_text("VstPlugin")
  21. #define OPEN_VST_TEXT obs_module_text("OpenPluginInterface")
  22. #define CLOSE_VST_TEXT obs_module_text("ClosePluginInterface")
  23. #define OPEN_WHEN_ACTIVE_VST_TEXT obs_module_text("OpenInterfaceWhenActive")
  24. OBS_DECLARE_MODULE()
  25. OBS_MODULE_USE_DEFAULT_LOCALE("obs-vst", "en-US")
  26. MODULE_EXPORT const char *obs_module_description(void)
  27. {
  28. return "VST 2.x Plug-in filter";
  29. }
  30. static bool open_editor_button_clicked(obs_properties_t *props,
  31. obs_property_t *property, void *data)
  32. {
  33. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  34. if (vstPlugin && vstPlugin->vstLoaded()) {
  35. QMetaObject::invokeMethod(vstPlugin, "openEditor");
  36. obs_property_set_visible(
  37. obs_properties_get(props, OPEN_VST_SETTINGS), false);
  38. obs_property_set_visible(
  39. obs_properties_get(props, CLOSE_VST_SETTINGS), true);
  40. }
  41. UNUSED_PARAMETER(props);
  42. UNUSED_PARAMETER(property);
  43. UNUSED_PARAMETER(data);
  44. return true;
  45. }
  46. static bool close_editor_button_clicked(obs_properties_t *props,
  47. obs_property_t *property, void *data)
  48. {
  49. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  50. if (vstPlugin && vstPlugin->vstLoaded() && vstPlugin->isEditorOpen()) {
  51. QMetaObject::invokeMethod(vstPlugin, "closeEditor");
  52. obs_property_set_visible(
  53. obs_properties_get(props, OPEN_VST_SETTINGS), true);
  54. obs_property_set_visible(
  55. obs_properties_get(props, CLOSE_VST_SETTINGS), false);
  56. }
  57. UNUSED_PARAMETER(property);
  58. return true;
  59. }
  60. std::string getFileMD5(const char *file)
  61. {
  62. QFile f(file);
  63. if (f.open(QFile::ReadOnly)) {
  64. QCryptographicHash hash(QCryptographicHash::Md5);
  65. if (hash.addData(&f))
  66. return std::string(hash.result().toHex());
  67. }
  68. return std::string();
  69. }
  70. static const char *vst_name(void *unused)
  71. {
  72. UNUSED_PARAMETER(unused);
  73. return PLUG_IN_NAME;
  74. }
  75. static void vst_destroy(void *data)
  76. {
  77. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  78. QMetaObject::invokeMethod(vstPlugin, "closeEditor");
  79. vstPlugin->deleteLater();
  80. }
  81. static void vst_update(void *data, obs_data_t *settings)
  82. {
  83. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  84. vstPlugin->openInterfaceWhenActive =
  85. obs_data_get_bool(settings, OPEN_WHEN_ACTIVE_VST_SETTINGS);
  86. const char *path = obs_data_get_string(settings, "plugin_path");
  87. if (strcmp(path, "") == 0) {
  88. vstPlugin->unloadEffect();
  89. return;
  90. }
  91. vstPlugin->loadEffectFromPath(std::string(path));
  92. std::string hash = getFileMD5(path);
  93. const char *chunkHash = obs_data_get_string(settings, "chunk_hash");
  94. const char *chunkData = obs_data_get_string(settings, "chunk_data");
  95. bool chunkHashesMatch = chunkHash && strlen(chunkHash) > 0 &&
  96. hash.compare(chunkHash) == 0;
  97. if (chunkData && strlen(chunkData) > 0 &&
  98. (chunkHashesMatch || !chunkHash || strlen(chunkHash) == 0)) {
  99. vstPlugin->setChunk(std::string(chunkData));
  100. }
  101. }
  102. static void *vst_create(obs_data_t *settings, obs_source_t *filter)
  103. {
  104. VSTPlugin *vstPlugin = new VSTPlugin(filter);
  105. vst_update(vstPlugin, settings);
  106. return vstPlugin;
  107. }
  108. static void vst_save(void *data, obs_data_t *settings)
  109. {
  110. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  111. obs_data_set_string(settings, "chunk_data",
  112. vstPlugin->getChunk().c_str());
  113. obs_data_set_string(
  114. settings, "chunk_hash",
  115. getFileMD5(vstPlugin->getEffectPath().c_str()).c_str());
  116. }
  117. static struct obs_audio_data *vst_filter_audio(void *data,
  118. struct obs_audio_data *audio)
  119. {
  120. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  121. vstPlugin->process(audio);
  122. /*
  123. * OBS can only guarantee getting the filter source's parent and own name
  124. * in this call, so we grab it and return the results for processing
  125. * by the EditorWidget.
  126. */
  127. vstPlugin->getSourceNames();
  128. return audio;
  129. }
  130. static void fill_out_plugins(obs_property_t *list)
  131. {
  132. QStringList dir_list;
  133. #ifdef __APPLE__
  134. dir_list << "/Library/Audio/Plug-Ins/VST/"
  135. << "~/Library/Audio/Plug-ins/VST/";
  136. #elif WIN32
  137. #ifndef _WIN64
  138. HANDLE hProcess = GetCurrentProcess();
  139. BOOL isWow64;
  140. IsWow64Process(hProcess, &isWow64);
  141. if (!isWow64) {
  142. #endif
  143. dir_list << qEnvironmentVariable("ProgramFiles") +
  144. "/Steinberg/VstPlugins/"
  145. << qEnvironmentVariable("CommonProgramFiles") +
  146. "/Steinberg/Shared Components/"
  147. << qEnvironmentVariable("CommonProgramFiles") + "/VST2"
  148. << qEnvironmentVariable("CommonProgramFiles") +
  149. "/Steinberg/VST2"
  150. << qEnvironmentVariable("CommonProgramFiles") +
  151. "/VSTPlugins/"
  152. << qEnvironmentVariable("ProgramFiles") +
  153. "/VSTPlugins/";
  154. #ifndef _WIN64
  155. } else {
  156. dir_list << qEnvironmentVariable("ProgramFiles(x86)") +
  157. "/Steinberg/VstPlugins/"
  158. << qEnvironmentVariable("CommonProgramFiles(x86)") +
  159. "/Steinberg/Shared Components/"
  160. << qEnvironmentVariable("CommonProgramFiles(x86)") +
  161. "/VST2"
  162. << qEnvironmentVariable("CommonProgramFiles(x86)") +
  163. "/VSTPlugins/"
  164. << qEnvironmentVariable("ProgramFiles(x86)") +
  165. "/VSTPlugins/";
  166. }
  167. #endif
  168. #elif __linux__
  169. // If the user has set the VST_PATH environmental
  170. // variable, then use it. Else default to a list
  171. // of common locations.
  172. QString vstPathEnv(getenv("VST_PATH"));
  173. if (!vstPathEnv.isNull()) {
  174. dir_list.append(vstPathEnv.split(":"));
  175. } else {
  176. QString home(getenv("HOME"));
  177. // Choose the most common locations
  178. // clang-format off
  179. dir_list << "/usr/lib/vst/"
  180. << "/usr/lib/lxvst/"
  181. << "/usr/lib/linux_vst/"
  182. << "/usr/lib64/vst/"
  183. << "/usr/lib64/lxvst/"
  184. << "/usr/lib64/linux_vst/"
  185. << "/usr/local/lib/vst/"
  186. << "/usr/local/lib/lxvst/"
  187. << "/usr/local/lib/linux_vst/"
  188. << "/usr/local/lib64/vst/"
  189. << "/usr/local/lib64/lxvst/"
  190. << "/usr/local/lib64/linux_vst/"
  191. << home + "/.vst/"
  192. << home + "/.lxvst/";
  193. // clang-format on
  194. }
  195. #endif
  196. QStringList filters;
  197. #ifdef __APPLE__
  198. filters << "*.vst";
  199. #elif WIN32
  200. filters << "*.dll";
  201. #elif __linux__
  202. filters << "*.so"
  203. << "*.o";
  204. #endif
  205. QStringList vst_list;
  206. // Read all plugins into a list...
  207. for (int a = 0; a < dir_list.size(); ++a) {
  208. QDir search_dir(dir_list[a]);
  209. search_dir.setNameFilters(filters);
  210. QDirIterator it(search_dir, QDirIterator::Subdirectories);
  211. while (it.hasNext()) {
  212. QString path = it.next();
  213. QString name = it.fileName();
  214. #ifdef __APPLE__
  215. name.remove(".vst", Qt::CaseInsensitive);
  216. #elif WIN32
  217. name.remove(".dll", Qt::CaseInsensitive);
  218. #elif __linux__
  219. name.remove(".so", Qt::CaseInsensitive);
  220. name.remove(".o", Qt::CaseInsensitive);
  221. #endif
  222. name.append("=").append(path);
  223. vst_list << name;
  224. }
  225. }
  226. // Now sort list alphabetically (still case-sensitive though).
  227. std::stable_sort(vst_list.begin(), vst_list.end(),
  228. std::less<QString>());
  229. // Now add said list to the plug-in list of OBS
  230. obs_property_list_add_string(list, "{Please select a plug-in}",
  231. nullptr);
  232. for (int b = 0; b < vst_list.size(); ++b) {
  233. QString vst_sorted = vst_list[b];
  234. obs_property_list_add_string(
  235. list,
  236. vst_sorted.left(vst_sorted.indexOf('='))
  237. .toStdString()
  238. .c_str(),
  239. vst_sorted.mid(vst_sorted.indexOf('=') + 1)
  240. .toStdString()
  241. .c_str());
  242. }
  243. }
  244. static obs_properties_t *vst_properties(void *data)
  245. {
  246. obs_properties_t *props = obs_properties_create();
  247. obs_property_t *list = obs_properties_add_list(props, "plugin_path",
  248. PLUG_IN_NAME,
  249. OBS_COMBO_TYPE_LIST,
  250. OBS_COMBO_FORMAT_STRING);
  251. fill_out_plugins(list);
  252. obs_properties_add_button(props, OPEN_VST_SETTINGS, OPEN_VST_TEXT,
  253. open_editor_button_clicked);
  254. obs_properties_add_button(props, CLOSE_VST_SETTINGS, CLOSE_VST_TEXT,
  255. close_editor_button_clicked);
  256. bool open_settings_vis = true;
  257. bool close_settings_vis = false;
  258. if (data) {
  259. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  260. if (!vstPlugin->vstLoaded()) {
  261. close_settings_vis = false;
  262. open_settings_vis = false;
  263. } else {
  264. if (vstPlugin->isEditorOpen()) {
  265. close_settings_vis = true;
  266. open_settings_vis = false;
  267. }
  268. }
  269. }
  270. obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS),
  271. open_settings_vis);
  272. obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS),
  273. close_settings_vis);
  274. obs_properties_add_bool(props, OPEN_WHEN_ACTIVE_VST_SETTINGS,
  275. OPEN_WHEN_ACTIVE_VST_TEXT);
  276. return props;
  277. }
  278. bool obs_module_load(void)
  279. {
  280. struct obs_source_info vst_filter = {};
  281. vst_filter.id = "vst_filter";
  282. vst_filter.type = OBS_SOURCE_TYPE_FILTER;
  283. vst_filter.output_flags = OBS_SOURCE_AUDIO;
  284. vst_filter.get_name = vst_name;
  285. vst_filter.create = vst_create;
  286. vst_filter.destroy = vst_destroy;
  287. vst_filter.update = vst_update;
  288. vst_filter.filter_audio = vst_filter_audio;
  289. vst_filter.get_properties = vst_properties;
  290. vst_filter.save = vst_save;
  291. obs_register_source(&vst_filter);
  292. return true;
  293. }