vestige.cpp 28 KB

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