VstEffectControls.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. connect( knobFModel[i], &FloatModel::dataChanged, this,
  75. [this, i]() { setParameter( knobFModel[i] ); }, Qt::DirectConnection);
  76. }
  77. }
  78. m_effect->m_pluginMutex.unlock();
  79. }
  80. void VstEffectControls::setParameter( Model * action )
  81. {
  82. int knobUNID = action->displayName().toInt();
  83. if ( m_effect->m_plugin != NULL ) {
  84. m_effect->m_plugin->setParam( knobUNID, knobFModel[knobUNID]->value() );
  85. }
  86. }
  87. void VstEffectControls::saveSettings( QDomDocument & _doc, QDomElement & _this )
  88. {
  89. _this.setAttribute( "plugin", m_effect->m_key.attributes["file"] );
  90. m_effect->m_pluginMutex.lock();
  91. if( m_effect->m_plugin != NULL )
  92. {
  93. m_effect->m_plugin->saveSettings( _doc, _this );
  94. if (knobFModel != NULL) {
  95. const QMap<QString, QString> & dump = m_effect->m_plugin->parameterDump();
  96. paramCount = dump.size();
  97. char paramStr[35];
  98. for( int i = 0; i < paramCount; i++ )
  99. {
  100. if (knobFModel[i]->isAutomated() || knobFModel[i]->controllerConnection()) {
  101. sprintf( paramStr, "param%d", i);
  102. knobFModel[i]->saveSettings( _doc, _this, paramStr );
  103. }
  104. }
  105. }
  106. }
  107. m_effect->m_pluginMutex.unlock();
  108. }
  109. int VstEffectControls::controlCount()
  110. {
  111. return m_effect->m_plugin != NULL ? 1 : 0;
  112. }
  113. EffectControlDialog *VstEffectControls::createView()
  114. {
  115. auto dialog = new VstEffectControlDialog( this );
  116. dialog->togglePluginUI( m_vstGuiVisible );
  117. return dialog;
  118. }
  119. void VstEffectControls::managePlugin( void )
  120. {
  121. if ( m_effect->m_plugin != NULL && m_subWindow == NULL ) {
  122. manageVSTEffectView * tt = new manageVSTEffectView( m_effect, this);
  123. ctrHandle = (QObject *)tt;
  124. } else if (m_subWindow != NULL) {
  125. if (m_subWindow->widget()->isVisible() == false ) {
  126. m_scrollArea->show();
  127. m_subWindow->show();
  128. } else {
  129. m_scrollArea->hide();
  130. m_subWindow->hide();
  131. }
  132. }
  133. }
  134. void VstEffectControls::savePreset( void )
  135. {
  136. if ( m_effect->m_plugin != NULL ) {
  137. m_effect->m_plugin->savePreset( );
  138. /* bool converted;
  139. QString str = m_vi->m_plugin->currentProgramName().section("/", 0, 0);
  140. if (str != "")
  141. lastPosInMenu = str.toInt(&converted, 10) - 1;
  142. QWidget::update();*/
  143. }
  144. }
  145. void VstEffectControls::updateMenu( void )
  146. {
  147. // get all presets -
  148. if ( m_effect->m_plugin != NULL )
  149. {
  150. m_effect->m_plugin->loadProgramNames();
  151. ///QWidget::update();
  152. QString str = m_effect->m_plugin->allProgramNames();
  153. QStringList list1 = str.split("|");
  154. QMenu * to_menu = m_selPresetButton->menu();
  155. to_menu->clear();
  156. for (int i = 0; i < list1.size(); i++) {
  157. QAction* presetAction = new QAction(this);
  158. connect(presetAction, SIGNAL(triggered()), this, SLOT(selPreset()));
  159. presetAction->setText(QString("%1. %2").arg(QString::number(i+1), list1.at(i)));
  160. presetAction->setData(i);
  161. if (i == lastPosInMenu) {
  162. presetAction->setIcon(embed::getIconPixmap( "sample_file", 16, 16 ));
  163. } else presetAction->setIcon(embed::getIconPixmap( "edit_copy", 16, 16 ));
  164. to_menu->addAction( presetAction );
  165. }
  166. }
  167. }
  168. void VstEffectControls::openPreset( void )
  169. {
  170. if ( m_effect->m_plugin != NULL ) {
  171. m_effect->m_plugin->openPreset( );
  172. bool converted;
  173. QString str = m_effect->m_plugin->currentProgramName().section("/", 0, 0);
  174. if (str != "")
  175. lastPosInMenu = str.toInt(&converted, 10) - 1;
  176. //QWidget::update();
  177. }
  178. }
  179. void VstEffectControls::rollPreset( void )
  180. {
  181. if ( m_effect->m_plugin != NULL ) {
  182. m_effect->m_plugin->rotateProgram( 1 );
  183. bool converted;
  184. QString str = m_effect->m_plugin->currentProgramName().section("/", 0, 0);
  185. if (str != "")
  186. lastPosInMenu = str.toInt(&converted, 10) - 1;
  187. //QWidget::update();
  188. }
  189. }
  190. void VstEffectControls::rolrPreset( void )
  191. {
  192. if ( m_effect->m_plugin != NULL ) {
  193. m_effect->m_plugin->rotateProgram( -1 );
  194. bool converted;
  195. QString str = m_effect->m_plugin->currentProgramName().section("/", 0, 0);
  196. if (str != "")
  197. lastPosInMenu = str.toInt(&converted, 10) - 1;
  198. //QWidget::update();
  199. }
  200. }
  201. void VstEffectControls::selPreset( void )
  202. {
  203. QAction *action = qobject_cast<QAction *>(sender());
  204. if (action)
  205. if ( m_effect->m_plugin != NULL ) {
  206. lastPosInMenu = action->data().toInt();
  207. m_effect->m_plugin->setProgram( lastPosInMenu );
  208. //QWidget::update();
  209. }
  210. }
  211. void VstEffectControls::paintEvent( QPaintEvent * )
  212. {
  213. }
  214. manageVSTEffectView::manageVSTEffectView( VstEffect * _eff, VstEffectControls * m_vi ) :
  215. m_effect( _eff )
  216. {
  217. m_vi2 = m_vi;
  218. widget = new QWidget();
  219. m_vi->m_scrollArea = new QScrollArea( widget );
  220. l = new QGridLayout( widget );
  221. m_vi->m_subWindow = gui->mainWindow()->addWindowedWidget(NULL, Qt::SubWindow |
  222. Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
  223. m_vi->m_subWindow->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
  224. m_vi->m_subWindow->setFixedSize( 960, 300);
  225. m_vi->m_subWindow->setWidget(m_vi->m_scrollArea);
  226. m_vi->m_subWindow->setWindowTitle( _eff->m_plugin->name() + tr( " - VST parameter control" ) );
  227. m_vi->m_subWindow->setWindowIcon( PLUGIN_NAME::getIconPixmap( "logo" ) );
  228. m_vi->m_subWindow->setAttribute(Qt::WA_DeleteOnClose, false);
  229. l->setContentsMargins( 20, 10, 10, 10 );
  230. l->setVerticalSpacing( 10 );
  231. l->setHorizontalSpacing( 23 );
  232. m_syncButton = new QPushButton( tr( "VST sync" ), widget );
  233. connect( m_syncButton, SIGNAL( clicked() ), this,
  234. SLOT( syncPlugin() ) );
  235. l->addWidget( m_syncButton, 0, 0, 1, 2, Qt::AlignLeft );
  236. m_displayAutomatedOnly = new QPushButton( tr( "Automated" ), widget );
  237. connect( m_displayAutomatedOnly, SIGNAL( clicked() ), this,
  238. SLOT( displayAutomatedOnly() ) );
  239. l->addWidget( m_displayAutomatedOnly, 0, 1, 1, 2, Qt::AlignLeft );
  240. m_closeButton = new QPushButton( tr( " Close " ), widget );
  241. connect( m_closeButton, SIGNAL( clicked() ), this,
  242. SLOT( closeWindow() ) );
  243. l->addWidget( m_closeButton, 0, 2, 1, 7, Qt::AlignLeft );
  244. for( int i = 0; i < 10; i++ )
  245. {
  246. l->addItem( new QSpacerItem( 68, 45, QSizePolicy::Fixed, QSizePolicy::Fixed ), 0, i );
  247. }
  248. const QMap<QString, QString> & dump = m_effect->m_plugin->parameterDump();
  249. m_vi->paramCount = dump.size();
  250. vstKnobs = new CustomTextKnob *[ m_vi->paramCount ];
  251. bool hasKnobModel = true;
  252. if (m_vi->knobFModel == NULL) {
  253. m_vi->knobFModel = new FloatModel *[ m_vi->paramCount ];
  254. hasKnobModel = false;
  255. }
  256. char paramStr[35];
  257. QStringList s_dumpValues;
  258. for( int i = 0; i < m_vi->paramCount; i++ )
  259. {
  260. sprintf( paramStr, "param%d", i);
  261. s_dumpValues = dump[ paramStr ].split( ":" );
  262. vstKnobs[ i ] = new CustomTextKnob( knobBright_26, widget, s_dumpValues.at( 1 ) );
  263. vstKnobs[ i ]->setDescription( s_dumpValues.at( 1 ) + ":" );
  264. vstKnobs[ i ]->setLabel( s_dumpValues.at( 1 ).left( 15 ) );
  265. if( !hasKnobModel )
  266. {
  267. sprintf( paramStr, "%d", i);
  268. m_vi->knobFModel[ i ] = new FloatModel( LocaleHelper::toFloat(s_dumpValues.at(2)),
  269. 0.0f, 1.0f, 0.01f, _eff, paramStr );
  270. }
  271. FloatModel * model = m_vi->knobFModel[i];
  272. connect( model, &FloatModel::dataChanged, this,
  273. [this, model]() { setParameter( model ); }, Qt::DirectConnection);
  274. vstKnobs[ i ] ->setModel( model );
  275. }
  276. syncParameterText();
  277. int i = 0;
  278. for( int lrow = 1; lrow < ( int( m_vi->paramCount / 10 ) + 1 ) + 1; lrow++ )
  279. {
  280. for( int lcolumn = 0; lcolumn < 10; lcolumn++ )
  281. {
  282. if( i < m_vi->paramCount )
  283. {
  284. l->addWidget( vstKnobs[i], lrow, lcolumn, Qt::AlignCenter );
  285. }
  286. i++;
  287. }
  288. }
  289. l->setRowStretch( ( int( m_vi->paramCount / 10 ) + 1 ), 1 );
  290. l->setColumnStretch( 10, 1 );
  291. widget->setLayout(l);
  292. widget->setAutoFillBackground(true);
  293. m_vi->m_scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
  294. m_vi->m_scrollArea->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
  295. m_vi->m_scrollArea->setPalette( QApplication::palette( m_vi->m_scrollArea ) );
  296. m_vi->m_scrollArea->setMinimumHeight( 64 );
  297. m_vi->m_scrollArea->setWidget( widget );
  298. m_vi->m_subWindow->show();
  299. }
  300. void manageVSTEffectView::closeWindow()
  301. {
  302. m_vi2->m_subWindow->hide();
  303. }
  304. void manageVSTEffectView::syncPlugin( void )
  305. {
  306. char paramStr[35];
  307. QStringList s_dumpValues;
  308. const QMap<QString, QString> & dump = m_effect->m_plugin->parameterDump();
  309. float f_value;
  310. for( int i = 0; i < m_vi2->paramCount; i++ )
  311. {
  312. // only not automated knobs are synced from VST
  313. // those auto-setted values are not jurnaled, tracked for undo / redo
  314. if( !( m_vi2->knobFModel[ i ]->isAutomated() ||
  315. m_vi2->knobFModel[ i ]->controllerConnection() ) )
  316. {
  317. sprintf( paramStr, "param%d", i );
  318. s_dumpValues = dump[ paramStr ].split( ":" );
  319. f_value = LocaleHelper::toFloat(s_dumpValues.at(2));
  320. m_vi2->knobFModel[ i ]->setAutomatedValue( f_value );
  321. m_vi2->knobFModel[ i ]->setInitValue( f_value );
  322. }
  323. }
  324. syncParameterText();
  325. }
  326. void manageVSTEffectView::displayAutomatedOnly( void )
  327. {
  328. bool isAuto = QString::compare( m_displayAutomatedOnly->text(), tr( "Automated" ) ) == 0;
  329. for( int i = 0; i< m_vi2->paramCount; i++ )
  330. {
  331. if( !( m_vi2->knobFModel[ i ]->isAutomated() ||
  332. m_vi2->knobFModel[ i ]->controllerConnection() ) )
  333. {
  334. if( vstKnobs[ i ]->isVisible() == true && isAuto )
  335. {
  336. vstKnobs[ i ]->hide();
  337. m_displayAutomatedOnly->setText( "All" );
  338. } else {
  339. vstKnobs[ i ]->show();
  340. m_displayAutomatedOnly->setText( "Automated" );
  341. }
  342. }
  343. }
  344. }
  345. void manageVSTEffectView::setParameter( Model * action )
  346. {
  347. int knobUNID = action->displayName().toInt();
  348. if ( m_effect->m_plugin != NULL ) {
  349. m_effect->m_plugin->setParam( knobUNID, m_vi2->knobFModel[knobUNID]->value() );
  350. syncParameterText();
  351. }
  352. }
  353. void manageVSTEffectView::syncParameterText()
  354. {
  355. m_effect->m_plugin->loadParameterLabels();
  356. m_effect->m_plugin->loadParameterDisplays();
  357. QString paramLabelStr = m_effect->m_plugin->allParameterLabels();
  358. QString paramDisplayStr = m_effect->m_plugin->allParameterDisplays();
  359. QStringList paramLabelList;
  360. QStringList paramDisplayList;
  361. for( int i = 0; i < paramLabelStr.size(); )
  362. {
  363. const int length = paramLabelStr[i].digitValue();
  364. paramLabelList.append(paramLabelStr.mid(i + 1, length));
  365. i += length + 1;
  366. }
  367. for( int i = 0; i < paramDisplayStr.size(); )
  368. {
  369. const int length = paramDisplayStr[i].digitValue();
  370. paramDisplayList.append(paramDisplayStr.mid(i + 1, length));
  371. i += length + 1;
  372. }
  373. for( int i = 0; i < paramLabelList.size(); ++i )
  374. {
  375. vstKnobs[i]->setValueText(paramDisplayList[i] + ' ' + paramLabelList[i]);
  376. }
  377. }
  378. manageVSTEffectView::~manageVSTEffectView()
  379. {
  380. if( m_vi2->knobFModel != NULL )
  381. {
  382. for( int i = 0; i < m_vi2->paramCount; i++ )
  383. {
  384. delete m_vi2->knobFModel[ i ];
  385. delete vstKnobs[ i ];
  386. }
  387. }
  388. if( vstKnobs != NULL )
  389. {
  390. delete [] vstKnobs;
  391. vstKnobs = NULL;
  392. }
  393. if( m_vi2->knobFModel != NULL )
  394. {
  395. delete [] m_vi2->knobFModel;
  396. m_vi2->knobFModel = NULL;
  397. }
  398. if( m_vi2->m_scrollArea != NULL )
  399. {
  400. delete m_vi2->m_scrollArea;
  401. m_vi2->m_scrollArea = NULL;
  402. }
  403. if( m_vi2->m_subWindow != NULL )
  404. {
  405. m_vi2->m_subWindow->setAttribute( Qt::WA_DeleteOnClose );
  406. m_vi2->m_subWindow->close();
  407. if( m_vi2->m_subWindow != NULL )
  408. {
  409. delete m_vi2->m_subWindow;
  410. }
  411. m_vi2->m_subWindow = NULL;
  412. }
  413. //delete m_vi2->m_subWindow;
  414. //m_vi2->m_subWindow = NULL;
  415. }