vestige.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /*
  2. * vestige.cpp - instrument-plugin for hosting VST-instruments
  3. *
  4. * Copyright (c) 2005-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 <QtCore/QtGlobal>
  25. #include "VstPlugin.h"
  26. #include "vestige.h"
  27. #include <memory>
  28. #include <QDropEvent>
  29. #include <QMessageBox>
  30. #include <QPainter>
  31. #include <QPushButton>
  32. #include <QMdiArea>
  33. #include <QMenu>
  34. #include <QDomElement>
  35. #include <string>
  36. #include "BufferManager.h"
  37. #include "ConfigManager.h"
  38. #include "Engine.h"
  39. #include "FileDialog.h"
  40. #include "GuiApplication.h"
  41. #include "gui_templates.h"
  42. #include "InstrumentPlayHandle.h"
  43. #include "InstrumentTrack.h"
  44. #include "LocaleHelper.h"
  45. #include "MainWindow.h"
  46. #include "Mixer.h"
  47. #include "PathUtil.h"
  48. #include "PixmapButton.h"
  49. #include "SampleBuffer.h"
  50. #include "Song.h"
  51. #include "StringPairDrag.h"
  52. #include "TextFloat.h"
  53. #include "ToolTip.h"
  54. #include "Clipboard.h"
  55. #include "embed.h"
  56. extern "C"
  57. {
  58. Plugin::Descriptor Q_DECL_EXPORT vestige_plugin_descriptor =
  59. {
  60. STRINGIFY( PLUGIN_NAME ),
  61. "VeSTige",
  62. QT_TRANSLATE_NOOP( "PluginBrowser",
  63. "VST-host for using VST(i)-plugins within LMMS" ),
  64. "Tobias Doerffel <tobydox/at/users.sf.net>",
  65. 0x0100,
  66. Plugin::Instrument,
  67. new PluginPixmapLoader( "logo" ),
  68. "dll",
  69. NULL
  70. } ;
  71. }
  72. class vstSubWin : public SubWindow
  73. {
  74. public:
  75. vstSubWin( QWidget * _parent ) :
  76. SubWindow( _parent )
  77. {
  78. setAttribute( Qt::WA_DeleteOnClose, false );
  79. setWindowFlags( Qt::WindowCloseButtonHint );
  80. }
  81. virtual ~vstSubWin()
  82. {
  83. }
  84. virtual void closeEvent( QCloseEvent * e )
  85. {
  86. // ignore close-events - for some reason otherwise the VST GUI
  87. // remains hidden when re-opening
  88. hide();
  89. e->ignore();
  90. }
  91. };
  92. class VstInstrumentPlugin : public VstPlugin
  93. {
  94. public:
  95. using VstPlugin::VstPlugin;
  96. void createUI( QWidget *parent ) override
  97. {
  98. Q_UNUSED(parent);
  99. if ( !hasEditor() ) {
  100. return;
  101. }
  102. if ( embedMethod() != "none" ) {
  103. m_pluginSubWindow.reset(new vstSubWin( gui->mainWindow()->workspace() ));
  104. VstPlugin::createUI( m_pluginSubWindow.get() );
  105. m_pluginSubWindow->setWidget(pluginWidget());
  106. } else {
  107. VstPlugin::createUI( nullptr );
  108. }
  109. }
  110. /// Overwrite editor() to return the sub window instead of the embed widget
  111. /// itself. This makes toggleUI() and related functions toggle the
  112. /// sub window's visibility.
  113. QWidget* editor() override
  114. {
  115. return m_pluginSubWindow.get();
  116. }
  117. private:
  118. unique_ptr<QMdiSubWindow> m_pluginSubWindow;
  119. };
  120. QPixmap * VestigeInstrumentView::s_artwork = NULL;
  121. QPixmap * manageVestigeInstrumentView::s_artwork = NULL;
  122. vestigeInstrument::vestigeInstrument( InstrumentTrack * _instrument_track ) :
  123. Instrument( _instrument_track, &vestige_plugin_descriptor ),
  124. m_plugin( NULL ),
  125. m_pluginMutex(),
  126. m_subWindow( NULL ),
  127. m_scrollArea( NULL ),
  128. knobFModel( NULL ),
  129. p_subWindow( NULL )
  130. {
  131. // now we need a play-handle which cares for calling play()
  132. InstrumentPlayHandle * iph = new InstrumentPlayHandle( this, _instrument_track );
  133. Engine::mixer()->addPlayHandle( iph );
  134. connect( ConfigManager::inst(), SIGNAL( valueChanged(QString,QString,QString) ),
  135. this, SLOT( handleConfigChange(QString, QString, QString) ),
  136. Qt::QueuedConnection );
  137. }
  138. vestigeInstrument::~vestigeInstrument()
  139. {
  140. if (p_subWindow != NULL) {
  141. delete p_subWindow;
  142. p_subWindow = NULL;
  143. }
  144. if (knobFModel != NULL) {
  145. delete []knobFModel;
  146. knobFModel = NULL;
  147. }
  148. Engine::mixer()->removePlayHandlesOfTypes( instrumentTrack(),
  149. PlayHandle::TypeNotePlayHandle
  150. | PlayHandle::TypeInstrumentPlayHandle );
  151. closePlugin();
  152. }
  153. void vestigeInstrument::loadSettings( const QDomElement & _this )
  154. {
  155. QString plugin = _this.attribute( "plugin" );
  156. if( plugin.isEmpty() )
  157. {
  158. return;
  159. }
  160. loadFile( plugin );
  161. m_pluginMutex.lock();
  162. if( m_plugin != NULL )
  163. {
  164. m_plugin->loadSettings( _this );
  165. if (instrumentTrack() != NULL && instrumentTrack()->isPreviewMode())
  166. {
  167. m_plugin->hideUI();
  168. }
  169. else if (_this.attribute( "guivisible" ).toInt())
  170. {
  171. m_plugin->showUI();
  172. } else
  173. {
  174. m_plugin->hideUI();
  175. }
  176. const QMap<QString, QString> & dump = m_plugin->parameterDump();
  177. paramCount = dump.size();
  178. char paramStr[35];
  179. knobFModel = new FloatModel *[ paramCount ];
  180. QStringList s_dumpValues;
  181. for( int i = 0; i < paramCount; i++ )
  182. {
  183. sprintf( paramStr, "param%d", i );
  184. s_dumpValues = dump[ paramStr ].split( ":" );
  185. knobFModel[i] = new FloatModel( 0.0f, 0.0f, 1.0f, 0.01f, this, QString::number(i) );
  186. knobFModel[i]->loadSettings( _this, paramStr );
  187. if( !( knobFModel[ i ]->isAutomated() || knobFModel[ i ]->controllerConnection() ) )
  188. {
  189. knobFModel[ i ]->setValue(LocaleHelper::toFloat(s_dumpValues.at(2)));
  190. knobFModel[ i ]->setInitValue(LocaleHelper::toFloat(s_dumpValues.at(2)));
  191. }
  192. connect( knobFModel[i], &FloatModel::dataChanged, this,
  193. [this, i]() { setParameter( knobFModel[i] ); }, Qt::DirectConnection);
  194. }
  195. }
  196. m_pluginMutex.unlock();
  197. }
  198. void vestigeInstrument::setParameter( Model * action )
  199. {
  200. int knobUNID = action->displayName().toInt();
  201. if ( m_plugin != NULL ) {
  202. m_plugin->setParam( knobUNID, knobFModel[knobUNID]->value() );
  203. }
  204. }
  205. void vestigeInstrument::handleConfigChange(QString cls, QString attr, QString value)
  206. {
  207. Q_UNUSED(cls); Q_UNUSED(attr); Q_UNUSED(value);
  208. // Disabled for consistency with VST effects that don't implement this. (#3786)
  209. // if ( cls == "ui" && attr == "vstembedmethod" )
  210. // {
  211. // reloadPlugin();
  212. // }
  213. }
  214. void vestigeInstrument::reloadPlugin()
  215. {
  216. closePlugin();
  217. loadFile( m_pluginDLL );
  218. }
  219. void vestigeInstrument::saveSettings( QDomDocument & _doc, QDomElement & _this )
  220. {
  221. _this.setAttribute( "plugin", PathUtil::toShortestRelative(m_pluginDLL) );
  222. m_pluginMutex.lock();
  223. if( m_plugin != NULL )
  224. {
  225. m_plugin->saveSettings( _doc, _this );
  226. if (knobFModel != NULL) {
  227. const QMap<QString, QString> & dump = m_plugin->parameterDump();
  228. paramCount = dump.size();
  229. char paramStr[35];
  230. for( int i = 0; i < paramCount; i++ )
  231. {
  232. if (knobFModel[i]->isAutomated() || knobFModel[i]->controllerConnection()) {
  233. sprintf( paramStr, "param%d", i);
  234. knobFModel[i]->saveSettings( _doc, _this, paramStr );
  235. }
  236. /* QDomElement me = _doc.createElement( paramStr );
  237. me.setAttribute( "id", knobFModel[i]->id() );
  238. me.setAttribute( "value", knobFModel[i]->value() );
  239. _this.appendChild( me );
  240. ControllerConnection * m_controllerConnection = knobFModel[i]->controllerConnection();
  241. if (m_controllerConnection) {
  242. QDomElement controller_element;
  243. QDomNode node = _this.namedItem( "connection" );
  244. if( node.isElement() )
  245. {
  246. controller_element = node.toElement();
  247. }
  248. else
  249. {
  250. controller_element = _doc.createElement( "connection" );
  251. _this.appendChild( controller_element );
  252. }
  253. QDomElement element = _doc.createElement( paramStr );
  254. m_controllerConnection->saveSettings( _doc, element );
  255. controller_element.appendChild( element );
  256. }*/
  257. }
  258. }
  259. }
  260. m_pluginMutex.unlock();
  261. }
  262. QString vestigeInstrument::nodeName( void ) const
  263. {
  264. return( vestige_plugin_descriptor.name );
  265. }
  266. void vestigeInstrument::loadFile( const QString & _file )
  267. {
  268. m_pluginMutex.lock();
  269. const bool set_ch_name = ( m_plugin != NULL &&
  270. instrumentTrack()->name() == m_plugin->name() ) ||
  271. instrumentTrack()->name() == InstrumentTrack::tr( "Default preset" ) ||
  272. instrumentTrack()->name() == displayName();
  273. m_pluginMutex.unlock();
  274. // if the same is loaded don't load again (for preview)
  275. if (instrumentTrack() != NULL && instrumentTrack()->isPreviewMode() &&
  276. m_pluginDLL == PathUtil::toShortestRelative( _file ))
  277. return;
  278. if ( m_plugin != NULL )
  279. {
  280. closePlugin();
  281. }
  282. m_pluginDLL = PathUtil::toShortestRelative( _file );
  283. TextFloat * tf = NULL;
  284. if( gui )
  285. {
  286. tf = TextFloat::displayMessage(
  287. tr( "Loading plugin" ),
  288. tr( "Please wait while loading the VST plugin..." ),
  289. PLUGIN_NAME::getIconPixmap( "logo", 24, 24 ), 0 );
  290. }
  291. m_pluginMutex.lock();
  292. m_plugin = new VstInstrumentPlugin( m_pluginDLL );
  293. if( m_plugin->failed() )
  294. {
  295. m_pluginMutex.unlock();
  296. closePlugin();
  297. delete tf;
  298. collectErrorForUI( VstPlugin::tr( "The VST plugin %1 could not be loaded." ).arg( m_pluginDLL ) );
  299. m_pluginDLL = "";
  300. return;
  301. }
  302. if ( !(instrumentTrack() != NULL && instrumentTrack()->isPreviewMode()))
  303. {
  304. m_plugin->createUI(nullptr);
  305. m_plugin->showUI();
  306. }
  307. if( set_ch_name )
  308. {
  309. instrumentTrack()->setName( m_plugin->name() );
  310. }
  311. m_pluginMutex.unlock();
  312. emit dataChanged();
  313. delete tf;
  314. }
  315. void vestigeInstrument::play( sampleFrame * _buf )
  316. {
  317. if (!m_pluginMutex.tryLock(Engine::getSong()->isExporting() ? -1 : 0)) {return;}
  318. const fpp_t frames = Engine::mixer()->framesPerPeriod();
  319. if( m_plugin == NULL )
  320. {
  321. m_pluginMutex.unlock();
  322. return;
  323. }
  324. m_plugin->process( NULL, _buf );
  325. instrumentTrack()->processAudioBuffer( _buf, frames, NULL );
  326. m_pluginMutex.unlock();
  327. }
  328. bool vestigeInstrument::handleMidiEvent( const MidiEvent& event, const TimePos& time, f_cnt_t offset )
  329. {
  330. m_pluginMutex.lock();
  331. if( m_plugin != NULL )
  332. {
  333. m_plugin->processMidiEvent( event, offset );
  334. }
  335. m_pluginMutex.unlock();
  336. return true;
  337. }
  338. void vestigeInstrument::closePlugin( void )
  339. {
  340. // disconnect all signals
  341. if( knobFModel != NULL )
  342. {
  343. for( int i = 0; i < paramCount; i++ )
  344. {
  345. delete knobFModel[ i ];
  346. }
  347. }
  348. if( knobFModel != NULL )
  349. {
  350. delete [] knobFModel;
  351. knobFModel = NULL;
  352. }
  353. if( m_scrollArea != NULL )
  354. {
  355. // delete m_scrollArea;
  356. m_scrollArea = NULL;
  357. }
  358. if( m_subWindow != NULL )
  359. {
  360. m_subWindow->setAttribute( Qt::WA_DeleteOnClose );
  361. m_subWindow->close();
  362. if( m_subWindow != NULL )
  363. {
  364. delete m_subWindow;
  365. }
  366. m_subWindow = NULL;
  367. }
  368. if( p_subWindow != NULL )
  369. {
  370. p_subWindow = NULL;
  371. }
  372. m_pluginMutex.lock();
  373. delete m_plugin;
  374. m_plugin = NULL;
  375. m_pluginMutex.unlock();
  376. }
  377. PluginView * vestigeInstrument::instantiateView( QWidget * _parent )
  378. {
  379. return new VestigeInstrumentView( this, _parent );
  380. }
  381. VestigeInstrumentView::VestigeInstrumentView( Instrument * _instrument,
  382. QWidget * _parent ) :
  383. InstrumentViewFixedSize( _instrument, _parent ),
  384. lastPosInMenu (0)
  385. {
  386. if( s_artwork == NULL )
  387. {
  388. s_artwork = new QPixmap( PLUGIN_NAME::getIconPixmap(
  389. "artwork" ) );
  390. }
  391. m_openPluginButton = new PixmapButton( this, "" );
  392. m_openPluginButton->setCheckable( false );
  393. m_openPluginButton->setCursor( Qt::PointingHandCursor );
  394. m_openPluginButton->move( 216, 81 );
  395. m_openPluginButton->setActiveGraphic( PLUGIN_NAME::getIconPixmap(
  396. "select_file_active" ) );
  397. m_openPluginButton->setInactiveGraphic( PLUGIN_NAME::getIconPixmap(
  398. "select_file" ) );
  399. connect( m_openPluginButton, SIGNAL( clicked() ), this,
  400. SLOT( openPlugin() ) );
  401. ToolTip::add( m_openPluginButton, tr( "Open VST plugin" ) );
  402. m_managePluginButton = new PixmapButton( this, "" );
  403. m_managePluginButton->setCheckable( false );
  404. m_managePluginButton->setCursor( Qt::PointingHandCursor );
  405. m_managePluginButton->move( 216, 101 );
  406. m_managePluginButton->setActiveGraphic( PLUGIN_NAME::getIconPixmap(
  407. "controls_active" ) );
  408. m_managePluginButton->setInactiveGraphic( PLUGIN_NAME::getIconPixmap(
  409. "controls" ) );
  410. connect( m_managePluginButton, SIGNAL( clicked() ), this,
  411. SLOT( managePlugin() ) );
  412. ToolTip::add( m_managePluginButton, tr( "Control VST plugin from LMMS host" ) );
  413. m_openPresetButton = new PixmapButton( this, "" );
  414. m_openPresetButton->setCheckable( false );
  415. m_openPresetButton->setCursor( Qt::PointingHandCursor );
  416. m_openPresetButton->move( 200, 224 );
  417. m_openPresetButton->setActiveGraphic( embed::getIconPixmap(
  418. "project_open", 20, 20 ) );
  419. m_openPresetButton->setInactiveGraphic( embed::getIconPixmap(
  420. "project_open", 20, 20 ) );
  421. connect( m_openPresetButton, SIGNAL( clicked() ), this,
  422. SLOT( openPreset() ) );
  423. ToolTip::add( m_openPresetButton, tr( "Open VST plugin preset" ) );
  424. m_rolLPresetButton = new PixmapButton( this, "" );
  425. m_rolLPresetButton->setCheckable( false );
  426. m_rolLPresetButton->setCursor( Qt::PointingHandCursor );
  427. m_rolLPresetButton->move( 190, 201 );
  428. m_rolLPresetButton->setActiveGraphic( embed::getIconPixmap(
  429. "stepper-left-press" ) );
  430. m_rolLPresetButton->setInactiveGraphic( embed::getIconPixmap(
  431. "stepper-left" ) );
  432. connect( m_rolLPresetButton, SIGNAL( clicked() ), this,
  433. SLOT( previousProgram() ) );
  434. ToolTip::add( m_rolLPresetButton, tr( "Previous (-)" ) );
  435. m_rolLPresetButton->setShortcut( Qt::Key_Minus );
  436. m_savePresetButton = new PixmapButton( this, "" );
  437. m_savePresetButton->setCheckable( false );
  438. m_savePresetButton->setCursor( Qt::PointingHandCursor );
  439. m_savePresetButton->move( 224, 224 );
  440. m_savePresetButton->setActiveGraphic( embed::getIconPixmap(
  441. "project_save", 20, 20 ) );
  442. m_savePresetButton->setInactiveGraphic( embed::getIconPixmap(
  443. "project_save", 20, 20 ) );
  444. connect( m_savePresetButton, SIGNAL( clicked() ), this,
  445. SLOT( savePreset() ) );
  446. ToolTip::add( m_savePresetButton, tr( "Save preset" ) );
  447. m_rolRPresetButton = new PixmapButton( this, "" );
  448. m_rolRPresetButton->setCheckable( false );
  449. m_rolRPresetButton->setCursor( Qt::PointingHandCursor );
  450. m_rolRPresetButton->move( 209, 201 );
  451. m_rolRPresetButton->setActiveGraphic( embed::getIconPixmap(
  452. "stepper-right-press" ) );
  453. m_rolRPresetButton->setInactiveGraphic( embed::getIconPixmap(
  454. "stepper-right" ) );
  455. connect( m_rolRPresetButton, SIGNAL( clicked() ), this,
  456. SLOT( nextProgram() ) );
  457. ToolTip::add( m_rolRPresetButton, tr( "Next (+)" ) );
  458. m_rolRPresetButton->setShortcut( Qt::Key_Plus );
  459. m_selPresetButton = new QPushButton( tr( "" ), this );
  460. m_selPresetButton->setGeometry( 228, 201, 16, 16 );
  461. QMenu *menu = new QMenu;
  462. connect( menu, SIGNAL( aboutToShow() ), this, SLOT( updateMenu() ) );
  463. m_selPresetButton->setIcon( embed::getIconPixmap( "stepper-down" ) );
  464. m_selPresetButton->setMenu(menu);
  465. m_toggleGUIButton = new QPushButton( tr( "Show/hide GUI" ), this );
  466. m_toggleGUIButton->setGeometry( 20, 130, 200, 24 );
  467. m_toggleGUIButton->setIcon( embed::getIconPixmap( "zoom" ) );
  468. m_toggleGUIButton->setFont( pointSize<8>( m_toggleGUIButton->font() ) );
  469. connect( m_toggleGUIButton, SIGNAL( clicked() ), this,
  470. SLOT( toggleGUI() ) );
  471. QPushButton * note_off_all_btn = new QPushButton( tr( "Turn off all "
  472. "notes" ), this );
  473. note_off_all_btn->setGeometry( 20, 160, 200, 24 );
  474. note_off_all_btn->setIcon( embed::getIconPixmap( "stop" ) );
  475. note_off_all_btn->setFont( pointSize<8>( note_off_all_btn->font() ) );
  476. connect( note_off_all_btn, SIGNAL( clicked() ), this,
  477. SLOT( noteOffAll() ) );
  478. setAcceptDrops( true );
  479. _instrument2 = _instrument;
  480. _parent2 = _parent;
  481. }
  482. void VestigeInstrumentView::managePlugin( void )
  483. {
  484. if ( m_vi->m_plugin != NULL && m_vi->m_subWindow == NULL ) {
  485. m_vi->p_subWindow = new manageVestigeInstrumentView( _instrument2, _parent2, m_vi);
  486. } else if (m_vi->m_subWindow != NULL) {
  487. if (m_vi->m_subWindow->widget()->isVisible() == false ) {
  488. m_vi->m_scrollArea->show();
  489. m_vi->m_subWindow->show();
  490. } else {
  491. m_vi->m_scrollArea->hide();
  492. m_vi->m_subWindow->hide();
  493. }
  494. }
  495. }
  496. void VestigeInstrumentView::updateMenu( void )
  497. {
  498. // get all presets -
  499. if ( m_vi->m_plugin != NULL )
  500. {
  501. m_vi->m_plugin->loadProgramNames();
  502. QWidget::update();
  503. QString str = m_vi->m_plugin->allProgramNames();
  504. QStringList list1 = str.split("|");
  505. QMenu * to_menu = m_selPresetButton->menu();
  506. to_menu->clear();
  507. QVector<QAction*> presetActions(list1.size());
  508. for (int i = 0; i < list1.size(); i++) {
  509. presetActions[i] = new QAction(this);
  510. connect(presetActions[i], SIGNAL(triggered()), this, SLOT(selPreset()));
  511. presetActions[i]->setText(QString("%1. %2").arg(QString::number(i+1), list1.at(i)));
  512. presetActions[i]->setData(i);
  513. if (i == lastPosInMenu) {
  514. presetActions[i]->setIcon(embed::getIconPixmap( "sample_file", 16, 16 ));
  515. } else presetActions[i]->setIcon(embed::getIconPixmap( "edit_copy", 16, 16 ));
  516. to_menu->addAction( presetActions[i] );
  517. }
  518. }
  519. }
  520. VestigeInstrumentView::~VestigeInstrumentView()
  521. {
  522. }
  523. void VestigeInstrumentView::modelChanged()
  524. {
  525. m_vi = castModel<vestigeInstrument>();
  526. }
  527. void VestigeInstrumentView::openPlugin()
  528. {
  529. FileDialog ofd( NULL, tr( "Open VST plugin" ) );
  530. // set filters
  531. QStringList types;
  532. types << tr( "DLL-files (*.dll)" )
  533. << tr( "EXE-files (*.exe)" )
  534. ;
  535. ofd.setNameFilters( types );
  536. if( m_vi->m_pluginDLL != "" )
  537. {
  538. QString f = PathUtil::toAbsolute( m_vi->m_pluginDLL );
  539. ofd.setDirectory( QFileInfo( f ).absolutePath() );
  540. ofd.selectFile( QFileInfo( f ).fileName() );
  541. }
  542. else
  543. {
  544. ofd.setDirectory( ConfigManager::inst()->vstDir() );
  545. }
  546. if ( ofd.exec () == QDialog::Accepted )
  547. {
  548. if( ofd.selectedFiles().isEmpty() )
  549. {
  550. return;
  551. }
  552. Engine::mixer()->requestChangeInModel();
  553. if (m_vi->p_subWindow != NULL) {
  554. delete m_vi->p_subWindow;
  555. m_vi->p_subWindow = NULL;
  556. }
  557. m_vi->loadFile( ofd.selectedFiles()[0] );
  558. Engine::mixer()->doneChangeInModel();
  559. if( m_vi->m_plugin && m_vi->m_plugin->pluginWidget() )
  560. {
  561. m_vi->m_plugin->pluginWidget()->setWindowIcon(
  562. PLUGIN_NAME::getIconPixmap( "logo" ) );
  563. }
  564. }
  565. }
  566. void VestigeInstrumentView::openPreset()
  567. {
  568. if ( m_vi->m_plugin != NULL ) {
  569. m_vi->m_plugin->openPreset( );
  570. bool converted;
  571. QString str = m_vi->m_plugin->currentProgramName().section("/", 0, 0);
  572. if (str != "")
  573. lastPosInMenu = str.toInt(&converted, 10) - 1;
  574. QWidget::update();
  575. }
  576. }
  577. void VestigeInstrumentView::savePreset()
  578. {
  579. if ( m_vi->m_plugin != NULL )
  580. {
  581. m_vi->m_plugin->savePreset( );
  582. /* bool converted;
  583. QString str = m_vi->m_plugin->presetString().section("/", 0, 0);
  584. if (str != "")
  585. lastPosInMenu = str.toInt(&converted, 10) - 1;
  586. QWidget::update();*/
  587. }
  588. }
  589. void VestigeInstrumentView::nextProgram()
  590. {
  591. if ( m_vi->m_plugin != NULL ) {
  592. m_vi->m_plugin->rotateProgram( 1 );
  593. bool converted;
  594. QString str = m_vi->m_plugin->currentProgramName().section("/", 0, 0);
  595. if (str != "")
  596. lastPosInMenu = str.toInt(&converted, 10) - 1;
  597. QWidget::update();
  598. }
  599. }
  600. void VestigeInstrumentView::previousProgram()
  601. {
  602. if ( m_vi->m_plugin != NULL ) {
  603. m_vi->m_plugin->rotateProgram( -1 );
  604. bool converted;
  605. QString str = m_vi->m_plugin->currentProgramName().section("/", 0, 0);
  606. if (str != "")
  607. lastPosInMenu = str.toInt(&converted, 10) - 1;
  608. QWidget::update();
  609. }
  610. }
  611. void VestigeInstrumentView::selPreset( void )
  612. {
  613. QAction *action = qobject_cast<QAction *>(sender());
  614. if (action)
  615. if ( m_vi->m_plugin != NULL ) {
  616. lastPosInMenu = action->data().toInt();
  617. m_vi->m_plugin->setProgram( action->data().toInt() );
  618. QWidget::update();
  619. }
  620. }
  621. void VestigeInstrumentView::toggleGUI( void )
  622. {
  623. if( m_vi == NULL || m_vi->m_plugin == NULL )
  624. {
  625. return;
  626. }
  627. m_vi->m_plugin->toggleUI();
  628. }
  629. void VestigeInstrumentView::noteOffAll( void )
  630. {
  631. m_vi->m_pluginMutex.lock();
  632. if( m_vi->m_plugin != NULL )
  633. {
  634. for( int key = 0; key <= MidiMaxKey; ++key )
  635. {
  636. m_vi->m_plugin->processMidiEvent( MidiEvent( MidiNoteOff, 0, key, 0 ), 0 );
  637. }
  638. }
  639. m_vi->m_pluginMutex.unlock();
  640. }
  641. void VestigeInstrumentView::dragEnterEvent( QDragEnterEvent * _dee )
  642. {
  643. // For mimeType() and MimeType enum class
  644. using namespace Clipboard;
  645. if( _dee->mimeData()->hasFormat( mimeType( MimeType::StringPair ) ) )
  646. {
  647. QString txt = _dee->mimeData()->data(
  648. mimeType( MimeType::StringPair ) );
  649. if( txt.section( ':', 0, 0 ) == "vstplugin" )
  650. {
  651. _dee->acceptProposedAction();
  652. }
  653. else
  654. {
  655. _dee->ignore();
  656. }
  657. }
  658. else
  659. {
  660. _dee->ignore();
  661. }
  662. }
  663. void VestigeInstrumentView::dropEvent( QDropEvent * _de )
  664. {
  665. QString type = StringPairDrag::decodeKey( _de );
  666. QString value = StringPairDrag::decodeValue( _de );
  667. if( type == "vstplugin" )
  668. {
  669. m_vi->loadFile( value );
  670. _de->accept();
  671. return;
  672. }
  673. _de->ignore();
  674. }
  675. void VestigeInstrumentView::paintEvent( QPaintEvent * )
  676. {
  677. QPainter p( this );
  678. p.drawPixmap( 0, 0, *s_artwork );
  679. QString plugin_name = ( m_vi->m_plugin != NULL ) ?
  680. m_vi->m_plugin->name()/* + QString::number(
  681. m_plugin->version() )*/
  682. :
  683. tr( "No VST plugin loaded" );
  684. QFont f = p.font();
  685. f.setBold( true );
  686. p.setFont( pointSize<10>( f ) );
  687. p.setPen( QColor( 255, 255, 255 ) );
  688. p.drawText( 10, 100, plugin_name );
  689. p.setPen( QColor( 50, 50, 50 ) );
  690. p.drawText( 10, 211, tr( "Preset" ) );
  691. // m_pluginMutex.lock();
  692. if( m_vi->m_plugin != NULL )
  693. {
  694. p.setPen( QColor( 0, 0, 0 ) );
  695. f.setBold( false );
  696. p.setFont( pointSize<8>( f ) );
  697. p.drawText( 10, 114, tr( "by " ) +
  698. m_vi->m_plugin->vendorString() );
  699. p.setPen( QColor( 255, 255, 255 ) );
  700. p.drawText( 10, 225, m_vi->m_plugin->currentProgramName() );
  701. }
  702. if( m_vi->m_subWindow != NULL )
  703. {
  704. m_vi->m_subWindow->setWindowTitle( m_vi->instrumentTrack()->name()
  705. + tr( " - VST plugin control" ) );
  706. }
  707. // m_pluginMutex.unlock();
  708. }
  709. manageVestigeInstrumentView::manageVestigeInstrumentView( Instrument * _instrument,
  710. QWidget * _parent, vestigeInstrument * m_vi2 ) :
  711. InstrumentViewFixedSize( _instrument, _parent )
  712. {
  713. m_vi = m_vi2;
  714. m_vi->m_scrollArea = new QScrollArea( this );
  715. widget = new QWidget(this);
  716. l = new QGridLayout( this );
  717. m_vi->m_subWindow = gui->mainWindow()->addWindowedWidget(NULL, Qt::SubWindow |
  718. Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
  719. m_vi->m_subWindow->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
  720. m_vi->m_subWindow->setFixedWidth( 960 );
  721. m_vi->m_subWindow->setMinimumHeight( 300 );
  722. m_vi->m_subWindow->setWidget(m_vi->m_scrollArea);
  723. m_vi->m_subWindow->setWindowTitle( m_vi->instrumentTrack()->name()
  724. + tr( " - VST plugin control" ) );
  725. m_vi->m_subWindow->setWindowIcon( PLUGIN_NAME::getIconPixmap( "logo" ) );
  726. m_vi->m_subWindow->setAttribute( Qt::WA_DeleteOnClose, false );
  727. l->setContentsMargins( 20, 10, 10, 10 );
  728. l->setVerticalSpacing( 10 );
  729. l->setHorizontalSpacing( 23 );
  730. m_syncButton = new QPushButton( tr( "VST Sync" ), this );
  731. connect( m_syncButton, SIGNAL( clicked() ), this,
  732. SLOT( syncPlugin() ) );
  733. l->addWidget( m_syncButton, 0, 0, 1, 2, Qt::AlignLeft );
  734. m_displayAutomatedOnly = new QPushButton( tr( "Automated" ), this );
  735. connect( m_displayAutomatedOnly, SIGNAL( clicked() ), this,
  736. SLOT( displayAutomatedOnly() ) );
  737. l->addWidget( m_displayAutomatedOnly, 0, 1, 1, 2, Qt::AlignLeft );
  738. m_closeButton = new QPushButton( tr( " Close " ), widget );
  739. connect( m_closeButton, SIGNAL( clicked() ), this,
  740. SLOT( closeWindow() ) );
  741. l->addWidget( m_closeButton, 0, 2, 1, 7, Qt::AlignLeft );
  742. for( int i = 0; i < 10; i++ )
  743. {
  744. l->addItem( new QSpacerItem( 68, 45, QSizePolicy::Fixed, QSizePolicy::Fixed ), 0, i );
  745. }
  746. const QMap<QString, QString> & dump = m_vi->m_plugin->parameterDump();
  747. m_vi->paramCount = dump.size();
  748. vstKnobs = new CustomTextKnob *[ m_vi->paramCount ];
  749. bool hasKnobModel = true;
  750. if (m_vi->knobFModel == NULL) {
  751. m_vi->knobFModel = new FloatModel *[ m_vi->paramCount ];
  752. hasKnobModel = false;
  753. }
  754. char paramStr[35];
  755. QStringList s_dumpValues;
  756. for( int i = 0; i < m_vi->paramCount; i++ )
  757. {
  758. sprintf( paramStr, "param%d", i);
  759. s_dumpValues = dump[ paramStr ].split( ":" );
  760. vstKnobs[ i ] = new CustomTextKnob( knobBright_26, this, s_dumpValues.at( 1 ) );
  761. vstKnobs[ i ]->setDescription( s_dumpValues.at( 1 ) + ":" );
  762. vstKnobs[ i ]->setLabel( s_dumpValues.at( 1 ).left( 15 ) );
  763. if( !hasKnobModel )
  764. {
  765. sprintf( paramStr, "%d", i);
  766. m_vi->knobFModel[ i ] = new FloatModel( LocaleHelper::toFloat(s_dumpValues.at(2)),
  767. 0.0f, 1.0f, 0.01f, castModel<vestigeInstrument>(), paramStr );
  768. }
  769. FloatModel * model = m_vi->knobFModel[i];
  770. connect( model, &FloatModel::dataChanged, this,
  771. [this, model]() { setParameter( model ); }, Qt::DirectConnection);
  772. vstKnobs[i] ->setModel( model );
  773. }
  774. syncParameterText();
  775. int i = 0;
  776. for( int lrow = 1; lrow < ( int( m_vi->paramCount / 10 ) + 1 ) + 1; lrow++ )
  777. {
  778. for( int lcolumn = 0; lcolumn < 10; lcolumn++ )
  779. {
  780. if( i < m_vi->paramCount )
  781. {
  782. l->addWidget( vstKnobs[i], lrow, lcolumn, Qt::AlignCenter );
  783. }
  784. i++;
  785. }
  786. }
  787. l->setRowStretch( ( int( m_vi->paramCount / 10) + 1), 1 );
  788. l->setColumnStretch( 10, 1 );
  789. widget->setLayout(l);
  790. widget->setAutoFillBackground(true);
  791. m_vi->m_scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
  792. m_vi->m_scrollArea->setPalette( QApplication::palette( m_vi->m_scrollArea ) );
  793. m_vi->m_scrollArea->setMinimumHeight( 64 );
  794. m_vi->m_scrollArea->setWidget( widget );
  795. m_vi->m_subWindow->show();
  796. }
  797. void manageVestigeInstrumentView::closeWindow()
  798. {
  799. m_vi->m_subWindow->hide();
  800. }
  801. void manageVestigeInstrumentView::syncPlugin( void )
  802. {
  803. char paramStr[35];
  804. QStringList s_dumpValues;
  805. const QMap<QString, QString> & dump = m_vi->m_plugin->parameterDump();
  806. float f_value;
  807. for( int i = 0; i < m_vi->paramCount; i++ )
  808. {
  809. // only not automated knobs are synced from VST
  810. // those auto-setted values are not jurnaled, tracked for undo / redo
  811. if( !( m_vi->knobFModel[ i ]->isAutomated() || m_vi->knobFModel[ i ]->controllerConnection() ) )
  812. {
  813. sprintf( paramStr, "param%d", i );
  814. s_dumpValues = dump[ paramStr ].split( ":" );
  815. f_value = LocaleHelper::toFloat(s_dumpValues.at(2));
  816. m_vi->knobFModel[ i ]->setAutomatedValue( f_value );
  817. m_vi->knobFModel[ i ]->setInitValue( f_value );
  818. }
  819. }
  820. syncParameterText();
  821. }
  822. void manageVestigeInstrumentView::displayAutomatedOnly( void )
  823. {
  824. bool isAuto = QString::compare( m_displayAutomatedOnly->text(), tr( "Automated" ) ) == 0;
  825. for( int i = 0; i< m_vi->paramCount; i++ )
  826. {
  827. if( !( m_vi->knobFModel[ i ]->isAutomated() || m_vi->knobFModel[ i ]->controllerConnection() ) )
  828. {
  829. if( vstKnobs[ i ]->isVisible() == true && isAuto )
  830. {
  831. vstKnobs[ i ]->hide();
  832. m_displayAutomatedOnly->setText( "All" );
  833. } else {
  834. vstKnobs[ i ]->show();
  835. m_displayAutomatedOnly->setText( "Automated" );
  836. }
  837. }
  838. }
  839. }
  840. manageVestigeInstrumentView::~manageVestigeInstrumentView()
  841. {
  842. if( m_vi->knobFModel != NULL )
  843. {
  844. for( int i = 0; i < m_vi->paramCount; i++ )
  845. {
  846. delete m_vi->knobFModel[ i ];
  847. delete vstKnobs[ i ];
  848. }
  849. }
  850. if (vstKnobs != NULL) {
  851. delete []vstKnobs;
  852. vstKnobs = NULL;
  853. }
  854. if( m_vi->knobFModel != NULL )
  855. {
  856. delete [] m_vi->knobFModel;
  857. m_vi->knobFModel = NULL;
  858. }
  859. if (m_vi->m_scrollArea != NULL) {
  860. delete m_vi->m_scrollArea;
  861. m_vi->m_scrollArea = NULL;
  862. }
  863. if ( m_vi->m_subWindow != NULL ) {
  864. m_vi->m_subWindow->setAttribute(Qt::WA_DeleteOnClose);
  865. m_vi->m_subWindow->close();
  866. if ( m_vi->m_subWindow != NULL )
  867. delete m_vi->m_subWindow;
  868. m_vi->m_subWindow = NULL;
  869. }
  870. m_vi->p_subWindow = NULL;
  871. }
  872. void manageVestigeInstrumentView::setParameter( Model * action )
  873. {
  874. int knobUNID = action->displayName().toInt();
  875. if ( m_vi->m_plugin != NULL ) {
  876. m_vi->m_plugin->setParam( knobUNID, m_vi->knobFModel[knobUNID]->value() );
  877. syncParameterText();
  878. }
  879. }
  880. void manageVestigeInstrumentView::syncParameterText()
  881. {
  882. m_vi->m_plugin->loadParameterLabels();
  883. m_vi->m_plugin->loadParameterDisplays();
  884. QString paramLabelStr = m_vi->m_plugin->allParameterLabels();
  885. QString paramDisplayStr = m_vi->m_plugin->allParameterDisplays();
  886. QStringList paramLabelList;
  887. QStringList paramDisplayList;
  888. for( int i = 0; i < paramLabelStr.size(); )
  889. {
  890. const int length = paramLabelStr[i].digitValue();
  891. paramLabelList.append(paramLabelStr.mid(i + 1, length));
  892. i += length + 1;
  893. }
  894. for( int i = 0; i < paramDisplayStr.size(); )
  895. {
  896. const int length = paramDisplayStr[i].digitValue();
  897. paramDisplayList.append(paramDisplayStr.mid(i + 1, length));
  898. i += length + 1;
  899. }
  900. for( int i = 0; i < paramLabelList.size(); ++i )
  901. {
  902. vstKnobs[i]->setValueText(paramDisplayList[i] + ' ' + paramLabelList[i]);
  903. }
  904. }
  905. void manageVestigeInstrumentView::dragEnterEvent( QDragEnterEvent * _dee )
  906. {
  907. // For mimeType() and MimeType enum class
  908. using namespace Clipboard;
  909. if( _dee->mimeData()->hasFormat( mimeType( MimeType::StringPair ) ) )
  910. {
  911. QString txt = _dee->mimeData()->data(
  912. mimeType( MimeType::StringPair ) );
  913. if( txt.section( ':', 0, 0 ) == "vstplugin" )
  914. {
  915. _dee->acceptProposedAction();
  916. }
  917. else
  918. {
  919. _dee->ignore();
  920. }
  921. }
  922. else
  923. {
  924. _dee->ignore();
  925. }
  926. }
  927. void manageVestigeInstrumentView::dropEvent( QDropEvent * _de )
  928. {
  929. QString type = StringPairDrag::decodeKey( _de );
  930. QString value = StringPairDrag::decodeValue( _de );
  931. if( type == "vstplugin" )
  932. {
  933. m_vi->loadFile( value );
  934. _de->accept();
  935. return;
  936. }
  937. _de->ignore();
  938. }
  939. void manageVestigeInstrumentView::paintEvent( QPaintEvent * )
  940. {
  941. m_vi->m_subWindow->setWindowTitle( m_vi->instrumentTrack()->name()
  942. + tr( " - VST plugin control" ) );
  943. }
  944. extern "C"
  945. {
  946. // necessary for getting instance out of shared lib
  947. Q_DECL_EXPORT Plugin * lmms_plugin_main( Model *m, void * )
  948. {
  949. return new vestigeInstrument( static_cast<InstrumentTrack *>( m ) );
  950. }
  951. }