obs-vst.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. QMetaObject::invokeMethod(vstPlugin, "openEditor");
  35. obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS),
  36. false);
  37. obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS),
  38. true);
  39. UNUSED_PARAMETER(props);
  40. UNUSED_PARAMETER(property);
  41. UNUSED_PARAMETER(data);
  42. return true;
  43. }
  44. static bool close_editor_button_clicked(obs_properties_t *props,
  45. obs_property_t *property, void *data)
  46. {
  47. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  48. QMetaObject::invokeMethod(vstPlugin, "closeEditor");
  49. obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS),
  50. true);
  51. obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS),
  52. false);
  53. UNUSED_PARAMETER(property);
  54. return true;
  55. }
  56. std::string getFileMD5(const char *file)
  57. {
  58. QFile f(file);
  59. if (f.open(QFile::ReadOnly)) {
  60. QCryptographicHash hash(QCryptographicHash::Md5);
  61. if (hash.addData(&f))
  62. return std::string(hash.result().toHex());
  63. }
  64. return std::string();
  65. }
  66. static const char *vst_name(void *unused)
  67. {
  68. UNUSED_PARAMETER(unused);
  69. return PLUG_IN_NAME;
  70. }
  71. static void vst_destroy(void *data)
  72. {
  73. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  74. QMetaObject::invokeMethod(vstPlugin, "closeEditor");
  75. vstPlugin->deleteLater();
  76. }
  77. static void vst_update(void *data, obs_data_t *settings)
  78. {
  79. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  80. vstPlugin->openInterfaceWhenActive =
  81. obs_data_get_bool(settings, OPEN_WHEN_ACTIVE_VST_SETTINGS);
  82. const char *path = obs_data_get_string(settings, "plugin_path");
  83. if (strcmp(path, "") == 0) {
  84. vstPlugin->unloadEffect();
  85. return;
  86. }
  87. vstPlugin->loadEffectFromPath(std::string(path));
  88. std::string hash = getFileMD5(path);
  89. const char *chunkHash = obs_data_get_string(settings, "chunk_hash");
  90. const char *chunkData = obs_data_get_string(settings, "chunk_data");
  91. bool chunkHashesMatch = chunkHash && strlen(chunkHash) > 0 &&
  92. hash.compare(chunkHash) == 0;
  93. if (chunkData && strlen(chunkData) > 0 &&
  94. (chunkHashesMatch || !chunkHash || strlen(chunkHash) == 0)) {
  95. vstPlugin->setChunk(std::string(chunkData));
  96. }
  97. }
  98. static void *vst_create(obs_data_t *settings, obs_source_t *filter)
  99. {
  100. VSTPlugin *vstPlugin = new VSTPlugin(filter);
  101. vst_update(vstPlugin, settings);
  102. return vstPlugin;
  103. }
  104. static void vst_save(void *data, obs_data_t *settings)
  105. {
  106. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  107. obs_data_set_string(settings, "chunk_data",
  108. vstPlugin->getChunk().c_str());
  109. obs_data_set_string(
  110. settings, "chunk_hash",
  111. getFileMD5(vstPlugin->getEffectPath().c_str()).c_str());
  112. }
  113. static struct obs_audio_data *vst_filter_audio(void *data,
  114. struct obs_audio_data *audio)
  115. {
  116. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  117. vstPlugin->process(audio);
  118. /*
  119. * OBS can only guarantee getting the filter source's parent and own name
  120. * in this call, so we grab it and return the results for processing
  121. * by the EditorWidget.
  122. */
  123. vstPlugin->getSourceNames();
  124. return audio;
  125. }
  126. static void fill_out_plugins(obs_property_t *list)
  127. {
  128. QStringList dir_list;
  129. #ifdef __APPLE__
  130. dir_list << "/Library/Audio/Plug-Ins/VST/"
  131. << "~/Library/Audio/Plug-ins/VST/";
  132. #elif WIN32
  133. #ifndef _WIN64
  134. HANDLE hProcess = GetCurrentProcess();
  135. BOOL isWow64;
  136. IsWow64Process(hProcess, &isWow64);
  137. if (!isWow64) {
  138. #endif
  139. dir_list << qEnvironmentVariable("ProgramFiles") +
  140. "/Steinberg/VstPlugins/"
  141. << qEnvironmentVariable("CommonProgramFiles") +
  142. "/Steinberg/Shared Components/"
  143. << qEnvironmentVariable("CommonProgramFiles") + "/VST2"
  144. << qEnvironmentVariable("CommonProgramFiles") +
  145. "/Steinberg/VST2"
  146. << qEnvironmentVariable("CommonProgramFiles") +
  147. "/VSTPlugins/"
  148. << qEnvironmentVariable("ProgramFiles") +
  149. "/VSTPlugins/";
  150. #ifndef _WIN64
  151. } else {
  152. dir_list << qEnvironmentVariable("ProgramFiles(x86)") +
  153. "/Steinberg/VstPlugins/"
  154. << qEnvironmentVariable("CommonProgramFiles(x86)") +
  155. "/Steinberg/Shared Components/"
  156. << qEnvironmentVariable("CommonProgramFiles(x86)") +
  157. "/VST2"
  158. << qEnvironmentVariable("CommonProgramFiles(x86)") +
  159. "/VSTPlugins/"
  160. << qEnvironmentVariable("ProgramFiles(x86)") +
  161. "/VSTPlugins/";
  162. }
  163. #endif
  164. #elif __linux__
  165. // If the user has set the VST_PATH environmental
  166. // variable, then use it. Else default to a list
  167. // of common locations.
  168. QString vstPathEnv(getenv("VST_PATH"));
  169. if (!vstPathEnv.isNull()) {
  170. dir_list.append(vstPathEnv.split(":"));
  171. } else {
  172. QString home(getenv("HOME"));
  173. // Choose the most common locations
  174. // clang-format off
  175. dir_list << "/usr/lib/vst/"
  176. << "/usr/lib/lxvst/"
  177. << "/usr/lib/linux_vst/"
  178. << "/usr/lib64/vst/"
  179. << "/usr/lib64/lxvst/"
  180. << "/usr/lib64/linux_vst/"
  181. << "/usr/local/lib/vst/"
  182. << "/usr/local/lib/lxvst/"
  183. << "/usr/local/lib/linux_vst/"
  184. << "/usr/local/lib64/vst/"
  185. << "/usr/local/lib64/lxvst/"
  186. << "/usr/local/lib64/linux_vst/"
  187. << home + "/.vst/"
  188. << home + "/.lxvst/";
  189. // clang-format on
  190. }
  191. #endif
  192. QStringList filters;
  193. #ifdef __APPLE__
  194. filters << "*.vst";
  195. #elif WIN32
  196. filters << "*.dll";
  197. #elif __linux__
  198. filters << "*.so"
  199. << "*.o";
  200. #endif
  201. QStringList vst_list;
  202. // Read all plugins into a list...
  203. for (int a = 0; a < dir_list.size(); ++a) {
  204. QDir search_dir(dir_list[a]);
  205. search_dir.setNameFilters(filters);
  206. QDirIterator it(search_dir, QDirIterator::Subdirectories);
  207. while (it.hasNext()) {
  208. QString path = it.next();
  209. QString name = it.fileName();
  210. #ifdef __APPLE__
  211. name.remove(".vst", Qt::CaseInsensitive);
  212. #elif WIN32
  213. name.remove(".dll", Qt::CaseInsensitive);
  214. #elif __linux__
  215. name.remove(".so", Qt::CaseInsensitive);
  216. name.remove(".o", Qt::CaseInsensitive);
  217. #endif
  218. name.append("=").append(path);
  219. vst_list << name;
  220. }
  221. }
  222. // Now sort list alphabetically (still case-sensitive though).
  223. std::stable_sort(vst_list.begin(), vst_list.end(),
  224. std::less<QString>());
  225. // Now add said list to the plug-in list of OBS
  226. obs_property_list_add_string(list, "{Please select a plug-in}",
  227. nullptr);
  228. for (int b = 0; b < vst_list.size(); ++b) {
  229. QString vst_sorted = vst_list[b];
  230. obs_property_list_add_string(
  231. list,
  232. vst_sorted.left(vst_sorted.indexOf('='))
  233. .toStdString()
  234. .c_str(),
  235. vst_sorted.mid(vst_sorted.indexOf('=') + 1)
  236. .toStdString()
  237. .c_str());
  238. }
  239. }
  240. static obs_properties_t *vst_properties(void *data)
  241. {
  242. obs_properties_t *props = obs_properties_create();
  243. obs_property_t *list = obs_properties_add_list(props, "plugin_path",
  244. PLUG_IN_NAME,
  245. OBS_COMBO_TYPE_LIST,
  246. OBS_COMBO_FORMAT_STRING);
  247. fill_out_plugins(list);
  248. obs_properties_add_button(props, OPEN_VST_SETTINGS, OPEN_VST_TEXT,
  249. open_editor_button_clicked);
  250. obs_properties_add_button(props, CLOSE_VST_SETTINGS, CLOSE_VST_TEXT,
  251. close_editor_button_clicked);
  252. bool open_settings_vis = true;
  253. bool close_settings_vis = false;
  254. if (data) {
  255. VSTPlugin *vstPlugin = (VSTPlugin *)data;
  256. if (vstPlugin->isEditorOpen()) {
  257. close_settings_vis = true;
  258. open_settings_vis = false;
  259. }
  260. }
  261. obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS),
  262. open_settings_vis);
  263. obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS),
  264. close_settings_vis);
  265. obs_properties_add_bool(props, OPEN_WHEN_ACTIVE_VST_SETTINGS,
  266. OPEN_WHEN_ACTIVE_VST_TEXT);
  267. return props;
  268. }
  269. bool obs_module_load(void)
  270. {
  271. struct obs_source_info vst_filter = {};
  272. vst_filter.id = "vst_filter";
  273. vst_filter.type = OBS_SOURCE_TYPE_FILTER;
  274. vst_filter.output_flags = OBS_SOURCE_AUDIO;
  275. vst_filter.get_name = vst_name;
  276. vst_filter.create = vst_create;
  277. vst_filter.destroy = vst_destroy;
  278. vst_filter.update = vst_update;
  279. vst_filter.filter_audio = vst_filter_audio;
  280. vst_filter.get_properties = vst_properties;
  281. vst_filter.save = vst_save;
  282. obs_register_source(&vst_filter);
  283. return true;
  284. }