VstEffectControls.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * VstEffectControls.cpp - controls for VST effect plugins
  3. *
  4. * Copyright (c) 2008-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #include <QDomElement>
  25. #include "VstEffectControls.h"
  26. #include "VstEffect.h"
  27. #include "LocaleHelper.h"
  28. #include "MainWindow.h"
  29. #include "GuiApplication.h"
  30. #include <QMdiArea>
  31. #include <QApplication>
  32. VstEffectControls::VstEffectControls( VstEffect * _eff ) :
  33. EffectControls( _eff ),
  34. m_effect( _eff ),
  35. m_subWindow( NULL ),
  36. knobFModel( NULL ),
  37. ctrHandle( NULL ),
  38. lastPosInMenu (0),
  39. m_vstGuiVisible ( true )
  40. // m_presetLabel ( NULL )
  41. {
  42. }
  43. VstEffectControls::~VstEffectControls()
  44. {
  45. delete ctrHandle;
  46. ctrHandle = NULL;
  47. }
  48. void VstEffectControls::loadSettings( const QDomElement & _this )
  49. {
  50. //m_effect->closePlugin();
  51. //m_effect->openPlugin( _this.attribute( "plugin" ) );
  52. m_effect->m_pluginMutex.lock();
  53. if( m_effect->m_plugin != NULL )
  54. {
  55. m_vstGuiVisible = _this.attribute( "guivisible" ).toInt();
  56. m_effect->m_plugin->loadSettings( _this );
  57. const QMap<QString, QString> & dump = m_effect->m_plugin->parameterDump();
  58. paramCount = dump.size();
  59. char paramStr[35];
  60. knobFModel = new FloatModel *[ paramCount ];
  61. QStringList s_dumpValues;
  62. for( int i = 0; i < paramCount; i++ )
  63. {
  64. sprintf( paramStr, "param%d", i );
  65. s_dumpValues = dump[ paramStr ].split( ":" );
  66. knobFModel[i] = new FloatModel( 0.0f, 0.0f, 1.0f, 0.01f, this, QString::number(i) );
  67. knobFModel[i]->loadSettings( _this, paramStr );
  68. if( !( knobFModel[ i ]->isAutomated() ||
  69. knobFModel[ i ]->controllerConnection() ) )
  70. {
  71. knobFModel[ i ]->setValue(LocaleHelper::toFloat(s_dumpValues.at(2)));
  72. knobFModel[ i ]->setInitValue(LocaleHelper::toFloat(s_dumpValues.at(2)));
  73. }
  74. #if QT_VERSION < 0x050000
  75. connect( knobFModel[i], SIGNAL( dataChanged( Model * ) ),
  76. this, SLOT( setParameter( Model * ) ), Qt::DirectConnection );
  77. #else
  78. connect( knobFModel[i], &FloatModel::dataChanged, this,
  79. [this, i]() { setParameter( knobFModel[i] ); }, Qt::DirectConnection);
  80. #endif
  81. }
  82. }
  83. m_effect->m_pluginMutex.unlock();
  84. }
  85. void VstEffectControls::setParameter( Model * action )
  86. {
  87. int knobUNID = action->displayName().toInt();
  88. if ( m_effect->m_plugin != NULL ) {
  89. m_effect->m_plugin->setParam( knobUNID, knobFModel[knobUNID]->value() );
  90. }
  91. }
  92. void VstEffectControls::saveSettings( QDomDocument & _doc, QDomElement & _this )
  93. {
  94. _this.setAttribute( "plugin", m_effect->m_key.attributes["file"] );
  95. m_effect->m_pluginMutex.lock();
  96. if( m_effect->m_plugin != NULL )
  97. {
  98. m_effect->m_plugin->saveSettings( _doc, _this );
  99. if (knobFModel != NULL) {
  100. const QMap<QString, QString> & dump = m_effect->m_plugin->parameterDump();
  101. paramCount = dump.size();
  102. char paramStr[35];
  103. for( int i = 0; i < paramCount; i++ )
  104. {
  105. if (knobFModel[i]->isAutomated() || knobFModel[i]->controllerConnection()) {
  106. sprintf( paramStr, "param%d", i);
  107. knobFModel[i]->saveSettings( _doc, _this, paramStr );
  108. }
  109. }
  110. }
  111. }
  112. m_effect->m_pluginMutex.unlock();
  113. }
  114. int VstEffectControls::controlCount()
  115. {
  116. return m_effect->m_plugin != NULL ? 1 : 0;
  117. }
  118. EffectControlDialog *VstEffectControls::createView()
  119. {
  120. auto dialog = new VstEffectControlDialog( this );
  121. dialog->togglePluginUI( m_vstGuiVisible );
  122. return dialog;
  123. }
  124. void VstEffectControls::managePlugin( void )
  125. {
  126. if ( m_effect->m_plugin != NULL && m_subWindow == NULL ) {
  127. manageVSTEffectView * tt = new manageVSTEffectView( m_effect, this);
  128. ctrHandle = (QObject *)tt;
  129. } else if (m_subWindow != NULL) {
  130. if (m_subWindow->widget()->isVisible() == false ) {
  131. m_scrollArea->show();
  132. m_subWindow->show();
  133. } else {
  134. m_scrollArea->hide();
  135. m_subWindow->hide();
  136. }
  137. }
  138. }
  139. void VstEffectControls::savePreset( void )
  140. {
  141. if ( m_effect->m_plugin != NULL ) {
  142. m_effect->m_plugin->savePreset( );
  143. /* bool converted;
  144. QString str = m_vi->m_plugin->currentProgramName().section("/", 0, 0);
  145. if (str != "")
  146. lastPosInMenu = str.toInt(&converted, 10) - 1;
  147. QWidget::update();*/
  148. }
  149. }
  150. void VstEffectControls::updateMenu( void )
  151. {
  152. // get all presets -
  153. if ( m_effect->m_plugin != NULL )
  154. {
  155. m_effect->m_plugin->loadProgramNames();
  156. ///QWidget::update();
  157. QString str = m_effect->m_plugin->allProgramNames();
  158. QStringList list1 = str.split("|");
  159. QMenu * to_menu = m_selPresetButton->menu();
  160. to_menu->clear();
  161. QAction *presetActions[list1.size()];
  162. for (int i = 0; i < list1.size(); i++) {
  163. presetActions[i] = new QAction(this);
  164. connect(presetActions[i], SIGNAL(triggered()), this, SLOT(selPreset()));
  165. presetActions[i]->setText(QString("%1. %2").arg(QString::number(i+1), list1.at(i)));
  166. presetActions[i]->setData(i);
  167. if (i == lastPosInMenu) {
  168. presetActions[i]->setIcon(embed::getIconPixmap( "sample_file", 16, 16 ));
  169. } else presetActions[i]->setIcon(embed::getIconPixmap( "edit_copy", 16, 16 ));
  170. to_menu->addAction( presetActions[i] );
  171. }
  172. }
  173. }
  174. void VstEffectControls::openPreset( void )
  175. {
  176. if ( m_effect->m_plugin != NULL ) {
  177. m_effect->m_plugin->openPreset( );
  178. bool converted;
  179. QString str = m_effect->m_plugin->currentProgramName().section("/", 0, 0);
  180. if (str != "")
  181. lastPosInMenu = str.toInt(&converted, 10) - 1;
  182. //QWidget::update();
  183. }
  184. }
  185. void VstEffectControls::rollPreset( void )
  186. {
  187. if ( m_effect->m_plugin != NULL ) {
  188. m_effect->m_plugin->rotateProgram( 1 );
  189. bool converted;
  190. QString str = m_effect->m_plugin->currentProgramName().section("/", 0, 0);
  191. if (str != "")
  192. lastPosInMenu = str.toInt(&converted, 10) - 1;
  193. //QWidget::update();
  194. }
  195. }
  196. void VstEffectControls::rolrPreset( void )
  197. {
  198. if ( m_effect->m_plugin != NULL ) {
  199. m_effect->m_plugin->rotateProgram( -1 );
  200. bool converted;
  201. QString str = m_effect->m_plugin->currentProgramName().section("/", 0, 0);
  202. if (str != "")
  203. lastPosInMenu = str.toInt(&converted, 10) - 1;
  204. //QWidget::update();
  205. }
  206. }
  207. void VstEffectControls::selPreset( void )
  208. {
  209. QAction *action = qobject_cast<QAction *>(sender());
  210. if (action)
  211. if ( m_effect->m_plugin != NULL ) {
  212. lastPosInMenu = action->data().toInt();
  213. m_effect->m_plugin->setProgram( lastPosInMenu );
  214. //QWidget::update();
  215. }
  216. }
  217. void VstEffectControls::paintEvent( QPaintEvent * )
  218. {
  219. }
  220. manageVSTEffectView::manageVSTEffectView( VstEffect * _eff, VstEffectControls * m_vi ) :
  221. m_effect( _eff )
  222. {
  223. m_vi2 = m_vi;
  224. widget = new QWidget();
  225. m_vi->m_scrollArea = new QScrollArea( widget );
  226. l = new QGridLayout( widget );
  227. m_vi->m_subWindow = gui->mainWindow()->addWindowedWidget(NULL, Qt::SubWindow |
  228. Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
  229. m_vi->m_subWindow->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
  230. m_vi->m_subWindow->setFixedSize( 960, 300);
  231. m_vi->m_subWindow->setWidget(m_vi->m_scrollArea);
  232. m_vi->m_subWindow->setWindowTitle( _eff->m_plugin->name() + tr( " - VST parameter control" ) );
  233. m_vi->m_subWindow->setWindowIcon( PLUGIN_NAME::getIconPixmap( "logo" ) );
  234. m_vi->m_subWindow->setAttribute(Qt::WA_DeleteOnClose, false);
  235. l->setContentsMargins( 20, 10, 10, 10 );
  236. l->setVerticalSpacing( 10 );
  237. l->setHorizontalSpacing( 23 );
  238. m_syncButton = new QPushButton( tr( "VST Sync" ), widget );
  239. connect( m_syncButton, SIGNAL( clicked() ), this,
  240. SLOT( syncPlugin() ) );
  241. m_syncButton->setWhatsThis(
  242. tr( "Click here if you want to synchronize all parameters with VST plugin." ) );
  243. l->addWidget( m_syncButton, 0, 0, 1, 2, Qt::AlignLeft );
  244. m_displayAutomatedOnly = new QPushButton( tr( "Automated" ), widget );
  245. connect( m_displayAutomatedOnly, SIGNAL( clicked() ), this,
  246. SLOT( displayAutomatedOnly() ) );
  247. m_displayAutomatedOnly->setWhatsThis(
  248. tr( "Click here if you want to display automated parameters only." ) );
  249. l->addWidget( m_displayAutomatedOnly, 0, 1, 1, 2, Qt::AlignLeft );
  250. m_closeButton = new QPushButton( tr( " Close " ), widget );
  251. connect( m_closeButton, SIGNAL( clicked() ), this,
  252. SLOT( closeWindow() ) );
  253. m_closeButton->setWhatsThis(
  254. tr( "Close VST effect knob-controller window." ) );
  255. l->addWidget( m_closeButton, 0, 2, 1, 7, Qt::AlignLeft );
  256. for( int i = 0; i < 10; i++ )
  257. {
  258. l->addItem( new QSpacerItem( 68, 45, QSizePolicy::Fixed, QSizePolicy::Fixed ), 0, i );
  259. }
  260. const QMap<QString, QString> & dump = m_effect->m_plugin->parameterDump();
  261. m_vi->paramCount = dump.size();
  262. vstKnobs = new Knob *[ m_vi->paramCount ];
  263. bool hasKnobModel = true;
  264. if (m_vi->knobFModel == NULL) {
  265. m_vi->knobFModel = new FloatModel *[ m_vi->paramCount ];
  266. hasKnobModel = false;
  267. }
  268. char paramStr[35];
  269. QStringList s_dumpValues;
  270. for( int i = 0; i < m_vi->paramCount; i++ )
  271. {
  272. sprintf( paramStr, "param%d", i);
  273. s_dumpValues = dump[ paramStr ].split( ":" );
  274. vstKnobs[ i ] = new Knob( knobBright_26, widget, s_dumpValues.at( 1 ) );
  275. vstKnobs[ i ]->setHintText( s_dumpValues.at( 1 ) + ":", "" );
  276. vstKnobs[ i ]->setLabel( s_dumpValues.at( 1 ).left( 15 ) );
  277. if( !hasKnobModel )
  278. {
  279. sprintf( paramStr, "%d", i);
  280. m_vi->knobFModel[ i ] = new FloatModel( LocaleHelper::toFloat(s_dumpValues.at(2)),
  281. 0.0f, 1.0f, 0.01f, _eff, tr( paramStr ) );
  282. }
  283. FloatModel * model = m_vi->knobFModel[i];
  284. #if QT_VERSION < 0x050000
  285. connect( model, SIGNAL( dataChanged( Model * ) ), this,
  286. SLOT( setParameter( Model * ) ), Qt::DirectConnection );
  287. #else
  288. connect( model, &FloatModel::dataChanged, this,
  289. [this, model]() { setParameter( model ); }, Qt::DirectConnection);
  290. #endif
  291. vstKnobs[ i ] ->setModel( model );
  292. }
  293. int i = 0;
  294. for( int lrow = 1; lrow < ( int( m_vi->paramCount / 10 ) + 1 ) + 1; lrow++ )
  295. {
  296. for( int lcolumn = 0; lcolumn < 10; lcolumn++ )
  297. {
  298. if( i < m_vi->paramCount )
  299. {
  300. l->addWidget( vstKnobs[i], lrow, lcolumn, Qt::AlignCenter );
  301. }
  302. i++;
  303. }
  304. }
  305. l->setRowStretch( ( int( m_vi->paramCount / 10 ) + 1 ), 1 );
  306. l->setColumnStretch( 10, 1 );
  307. widget->setLayout(l);
  308. widget->setAutoFillBackground(true);
  309. m_vi->m_scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
  310. m_vi->m_scrollArea->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
  311. m_vi->m_scrollArea->setPalette( QApplication::palette( m_vi->m_scrollArea ) );
  312. m_vi->m_scrollArea->setMinimumHeight( 64 );
  313. m_vi->m_scrollArea->setWidget( widget );
  314. m_vi->m_subWindow->show();
  315. }
  316. void manageVSTEffectView::closeWindow()
  317. {
  318. m_vi2->m_subWindow->hide();
  319. }
  320. void manageVSTEffectView::syncPlugin( void )
  321. {
  322. char paramStr[35];
  323. QStringList s_dumpValues;
  324. const QMap<QString, QString> & dump = m_effect->m_plugin->parameterDump();
  325. float f_value;
  326. for( int i = 0; i < m_vi2->paramCount; i++ )
  327. {
  328. // only not automated knobs are synced from VST
  329. // those auto-setted values are not jurnaled, tracked for undo / redo
  330. if( !( m_vi2->knobFModel[ i ]->isAutomated() ||
  331. m_vi2->knobFModel[ i ]->controllerConnection() ) )
  332. {
  333. sprintf( paramStr, "param%d", i );
  334. s_dumpValues = dump[ paramStr ].split( ":" );
  335. f_value = LocaleHelper::toFloat(s_dumpValues.at(2));
  336. m_vi2->knobFModel[ i ]->setAutomatedValue( f_value );
  337. m_vi2->knobFModel[ i ]->setInitValue( f_value );
  338. }
  339. }
  340. }
  341. void manageVSTEffectView::displayAutomatedOnly( void )
  342. {
  343. bool isAuto = QString::compare( m_displayAutomatedOnly->text(), tr( "Automated" ) ) == 0;
  344. for( int i = 0; i< m_vi2->paramCount; i++ )
  345. {
  346. if( !( m_vi2->knobFModel[ i ]->isAutomated() ||
  347. m_vi2->knobFModel[ i ]->controllerConnection() ) )
  348. {
  349. if( vstKnobs[ i ]->isVisible() == true && isAuto )
  350. {
  351. vstKnobs[ i ]->hide();
  352. m_displayAutomatedOnly->setText( "All" );
  353. } else {
  354. vstKnobs[ i ]->show();
  355. m_displayAutomatedOnly->setText( "Automated" );
  356. }
  357. }
  358. }
  359. }
  360. void manageVSTEffectView::setParameter( Model * action )
  361. {
  362. int knobUNID = action->displayName().toInt();
  363. if ( m_effect->m_plugin != NULL ) {
  364. m_effect->m_plugin->setParam( knobUNID, m_vi2->knobFModel[knobUNID]->value() );
  365. }
  366. }
  367. manageVSTEffectView::~manageVSTEffectView()
  368. {
  369. if( m_vi2->knobFModel != NULL )
  370. {
  371. for( int i = 0; i < m_vi2->paramCount; i++ )
  372. {
  373. delete m_vi2->knobFModel[ i ];
  374. delete vstKnobs[ i ];
  375. }
  376. }
  377. if( vstKnobs != NULL )
  378. {
  379. delete [] vstKnobs;
  380. vstKnobs = NULL;
  381. }
  382. if( m_vi2->knobFModel != NULL )
  383. {
  384. delete [] m_vi2->knobFModel;
  385. m_vi2->knobFModel = NULL;
  386. }
  387. if( m_vi2->m_scrollArea != NULL )
  388. {
  389. delete m_vi2->m_scrollArea;
  390. m_vi2->m_scrollArea = NULL;
  391. }
  392. if( m_vi2->m_subWindow != NULL )
  393. {
  394. m_vi2->m_subWindow->setAttribute( Qt::WA_DeleteOnClose );
  395. m_vi2->m_subWindow->close();
  396. if( m_vi2->m_subWindow != NULL )
  397. {
  398. delete m_vi2->m_subWindow;
  399. }
  400. m_vi2->m_subWindow = NULL;
  401. }
  402. //delete m_vi2->m_subWindow;
  403. //m_vi2->m_subWindow = NULL;
  404. }