InstrumentTrack.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * InstrumentTrack.h - declaration of class InstrumentTrack, a track + window
  3. * which holds an instrument-plugin
  4. *
  5. * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #ifndef INSTRUMENT_TRACK_H
  26. #define INSTRUMENT_TRACK_H
  27. #include "AudioPort.h"
  28. #include "Groove.h"
  29. #include "GroupBox.h"
  30. #include "InstrumentFunctions.h"
  31. #include "InstrumentSoundShaping.h"
  32. #include "MidiEventProcessor.h"
  33. #include "MidiPort.h"
  34. #include "NotePlayHandle.h"
  35. #include "Piano.h"
  36. #include "PianoView.h"
  37. #include "Pitch.h"
  38. #include "Plugin.h"
  39. #include "Track.h"
  40. class QLineEdit;
  41. template<class T> class QQueue;
  42. class InstrumentFunctionArpeggioView;
  43. class InstrumentFunctionNoteStackingView;
  44. class EffectRackView;
  45. class InstrumentSoundShapingView;
  46. class FadeButton;
  47. class Instrument;
  48. class InstrumentTrackWindow;
  49. class InstrumentMidiIOView;
  50. class InstrumentMiscView;
  51. class Knob;
  52. class FxLineLcdSpinBox;
  53. class LcdSpinBox;
  54. class LeftRightNav;
  55. class midiPortMenu;
  56. class DataFile;
  57. class PluginView;
  58. class TabWidget;
  59. class TrackLabelButton;
  60. class LedCheckBox;
  61. class QLabel;
  62. class LMMS_EXPORT InstrumentTrack : public Track, public MidiEventProcessor
  63. {
  64. Q_OBJECT
  65. MM_OPERATORS
  66. mapPropertyFromModel(int,getVolume,setVolume,m_volumeModel);
  67. public:
  68. InstrumentTrack( TrackContainer* tc );
  69. virtual ~InstrumentTrack();
  70. // used by instrument
  71. void processAudioBuffer( sampleFrame * _buf, const fpp_t _frames,
  72. NotePlayHandle * _n );
  73. MidiEvent applyMasterKey( const MidiEvent& event );
  74. void processInEvent( const MidiEvent& event, const MidiTime& time = MidiTime(), f_cnt_t offset = 0 ) override;
  75. void processOutEvent( const MidiEvent& event, const MidiTime& time = MidiTime(), f_cnt_t offset = 0 ) override;
  76. // silence all running notes played by this track
  77. void silenceAllNotes( bool removeIPH = false );
  78. bool isSustainPedalPressed() const
  79. {
  80. return m_sustainPedalPressed;
  81. }
  82. f_cnt_t beatLen( NotePlayHandle * _n ) const;
  83. void disableGroove();
  84. void enableGroove();
  85. // for capturing note-play-events -> need that for arpeggio,
  86. // filter and so on
  87. void playNote( NotePlayHandle * _n, sampleFrame * _working_buffer );
  88. QString instrumentName() const;
  89. const Instrument *instrument() const
  90. {
  91. return m_instrument;
  92. }
  93. Instrument *instrument()
  94. {
  95. return m_instrument;
  96. }
  97. void deleteNotePluginData( NotePlayHandle * _n );
  98. // name-stuff
  99. void setName( const QString & _new_name ) override;
  100. // translate given key of a note-event to absolute key (i.e.
  101. // add global master-pitch and base-note of this instrument track)
  102. int masterKey( int _midi_key ) const;
  103. // translate pitch to midi-pitch [0,16383]
  104. int midiPitch() const
  105. {
  106. return static_cast<int>( ( ( m_pitchModel.value() + m_pitchModel.range()/2 ) * MidiMaxPitchBend ) / m_pitchModel.range() );
  107. }
  108. /*! \brief Returns current range for pitch bend in semitones */
  109. int midiPitchRange() const
  110. {
  111. return m_pitchRangeModel.value();
  112. }
  113. // play everything in given frame-range - creates note-play-handles
  114. virtual bool play( const MidiTime & _start, const fpp_t _frames,
  115. const f_cnt_t _frame_base, int _tco_num = -1 ) override;
  116. // create new view for me
  117. TrackView * createView( TrackContainerView* tcv ) override;
  118. // create new track-content-object = pattern
  119. TrackContentObject * createTCO( const MidiTime & _pos ) override;
  120. // called by track
  121. virtual void saveTrackSpecificSettings( QDomDocument & _doc,
  122. QDomElement & _parent ) override;
  123. void loadTrackSpecificSettings( const QDomElement & _this ) override;
  124. using Track::setJournalling;
  125. // load instrument whose name matches given one
  126. Instrument * loadInstrument(const QString & _instrument_name,
  127. const Plugin::Descriptor::SubPluginFeatures::Key* key = nullptr,
  128. bool keyFromDnd = false);
  129. AudioPort * audioPort()
  130. {
  131. return &m_audioPort;
  132. }
  133. Groove * groove();
  134. MidiPort * midiPort()
  135. {
  136. return &m_midiPort;
  137. }
  138. const IntModel *baseNoteModel() const
  139. {
  140. return &m_baseNoteModel;
  141. }
  142. IntModel *baseNoteModel()
  143. {
  144. return &m_baseNoteModel;
  145. }
  146. int baseNote() const;
  147. Piano *pianoModel()
  148. {
  149. return &m_piano;
  150. }
  151. bool isArpeggioEnabled() const
  152. {
  153. return m_arpeggio.m_arpEnabledModel.value();
  154. }
  155. // simple helper for removing midiport-XML-node when loading presets
  156. static void removeMidiPortNode( DataFile& dataFile );
  157. FloatModel * pitchModel()
  158. {
  159. return &m_pitchModel;
  160. }
  161. FloatModel * volumeModel()
  162. {
  163. return &m_volumeModel;
  164. }
  165. FloatModel * panningModel()
  166. {
  167. return &m_panningModel;
  168. }
  169. IntModel* pitchRangeModel()
  170. {
  171. return &m_pitchRangeModel;
  172. }
  173. IntModel * effectChannelModel()
  174. {
  175. return &m_effectChannelModel;
  176. }
  177. void setPreviewMode( const bool );
  178. signals:
  179. void instrumentChanged();
  180. void midiNoteOn( const Note& );
  181. void midiNoteOff( const Note& );
  182. void nameChanged();
  183. void newNote();
  184. void endNote();
  185. protected:
  186. QString nodeName() const override
  187. {
  188. return "instrumenttrack";
  189. }
  190. protected slots:
  191. void updateBaseNote();
  192. void updatePitch();
  193. void updatePitchRange();
  194. void updateEffectChannel();
  195. private:
  196. MidiPort m_midiPort;
  197. NotePlayHandle* m_notes[NumKeys];
  198. NotePlayHandleList m_sustainedNotes;
  199. int m_runningMidiNotes[NumKeys];
  200. QMutex m_midiNotesMutex;
  201. bool m_sustainPedalPressed;
  202. bool m_silentBuffersProcessed;
  203. bool m_previewMode;
  204. IntModel m_baseNoteModel;
  205. NotePlayHandleList m_processHandles;
  206. FloatModel m_volumeModel;
  207. FloatModel m_panningModel;
  208. AudioPort m_audioPort;
  209. // Track specific groove or NULL
  210. Groove * m_groove;
  211. Groove * m_noGroove;
  212. bool m_grooveOn; //if true temporarily return nooop Groove for the groove
  213. FloatModel m_pitchModel;
  214. IntModel m_pitchRangeModel;
  215. IntModel m_effectChannelModel;
  216. BoolModel m_useMasterPitchModel;
  217. BoolModel m_useGrooveModel;
  218. Instrument * m_instrument;
  219. InstrumentSoundShaping m_soundShaping;
  220. InstrumentFunctionArpeggio m_arpeggio;
  221. InstrumentFunctionNoteStacking m_noteStacking;
  222. Piano m_piano;
  223. friend class InstrumentTrackView;
  224. friend class InstrumentTrackWindow;
  225. friend class NotePlayHandle;
  226. friend class InstrumentMiscView;
  227. private slots:
  228. void updateGroove();
  229. };
  230. class InstrumentTrackView : public TrackView
  231. {
  232. Q_OBJECT
  233. public:
  234. InstrumentTrackView( InstrumentTrack * _it, TrackContainerView* tc );
  235. virtual ~InstrumentTrackView();
  236. InstrumentTrackWindow * getInstrumentTrackWindow();
  237. InstrumentTrack * model()
  238. {
  239. return castModel<InstrumentTrack>();
  240. }
  241. const InstrumentTrack * model() const
  242. {
  243. return castModel<InstrumentTrack>();
  244. }
  245. static InstrumentTrackWindow * topLevelInstrumentTrackWindow();
  246. QMenu * midiMenu()
  247. {
  248. return m_midiMenu;
  249. }
  250. void freeInstrumentTrackWindow();
  251. static void cleanupWindowCache();
  252. // Create a menu for assigning/creating channels for this track
  253. QMenu * createFxMenu( QString title, QString newFxLabel ) override;
  254. protected:
  255. void dragEnterEvent( QDragEnterEvent * _dee ) override;
  256. void dropEvent( QDropEvent * _de ) override;
  257. private slots:
  258. void toggleInstrumentWindow( bool _on );
  259. void activityIndicatorPressed();
  260. void activityIndicatorReleased();
  261. void midiInSelected();
  262. void midiOutSelected();
  263. void midiConfigChanged();
  264. void muteChanged();
  265. void assignFxLine( int channelIndex );
  266. void createFxLine();
  267. private:
  268. InstrumentTrackWindow * m_window;
  269. static QQueue<InstrumentTrackWindow *> s_windowCache;
  270. // widgets in track-settings-widget
  271. TrackLabelButton * m_tlb;
  272. Knob * m_volumeKnob;
  273. Knob * m_panningKnob;
  274. FadeButton * m_activityIndicator;
  275. QMenu * m_midiMenu;
  276. QAction * m_midiInputAction;
  277. QAction * m_midiOutputAction;
  278. QPoint m_lastPos;
  279. friend class InstrumentTrackWindow;
  280. } ;
  281. class InstrumentTrackWindow : public QWidget, public ModelView,
  282. public SerializingObjectHook
  283. {
  284. Q_OBJECT
  285. public:
  286. InstrumentTrackWindow( InstrumentTrackView * _tv );
  287. virtual ~InstrumentTrackWindow();
  288. // parent for all internal tab-widgets
  289. TabWidget * tabWidgetParent()
  290. {
  291. return m_tabWidget;
  292. }
  293. InstrumentTrack * model()
  294. {
  295. return castModel<InstrumentTrack>();
  296. }
  297. const InstrumentTrack * model() const
  298. {
  299. return castModel<InstrumentTrack>();
  300. }
  301. void setInstrumentTrackView( InstrumentTrackView * _tv );
  302. InstrumentTrackView *instrumentTrackView()
  303. {
  304. return m_itv;
  305. }
  306. PianoView * pianoView()
  307. {
  308. return m_pianoView;
  309. }
  310. static void dragEnterEventGeneric( QDragEnterEvent * _dee );
  311. void dragEnterEvent( QDragEnterEvent * _dee ) override;
  312. void dropEvent( QDropEvent * _de ) override;
  313. public slots:
  314. void textChanged( const QString & _new_name );
  315. void toggleVisibility( bool _on );
  316. void updateName();
  317. void updateInstrumentView();
  318. protected:
  319. // capture close-events for toggling instrument-track-button
  320. void closeEvent( QCloseEvent * _ce ) override;
  321. void focusInEvent( QFocusEvent * _fe ) override;
  322. void saveSettings( QDomDocument & _doc, QDomElement & _this ) override;
  323. void loadSettings( const QDomElement & _this ) override;
  324. protected slots:
  325. void saveSettingsBtnClicked();
  326. void viewNextInstrument();
  327. void viewPrevInstrument();
  328. private:
  329. void modelChanged() override;
  330. void viewInstrumentInDirection(int d);
  331. //! adjust size of any child widget of the main tab
  332. //! required to keep the old look when using a variable sized tab widget
  333. void adjustTabSize(QWidget *w);
  334. InstrumentTrack * m_track;
  335. InstrumentTrackView * m_itv;
  336. // widgets on the top of an instrument-track-window
  337. QLineEdit * m_nameLineEdit;
  338. LeftRightNav * m_leftRightNav;
  339. Knob * m_volumeKnob;
  340. Knob * m_panningKnob;
  341. Knob * m_pitchKnob;
  342. QLabel * m_pitchLabel;
  343. LcdSpinBox* m_pitchRangeSpinBox;
  344. QLabel * m_pitchRangeLabel;
  345. FxLineLcdSpinBox * m_effectChannelNumber;
  346. // tab-widget with all children
  347. TabWidget * m_tabWidget;
  348. PluginView * m_instrumentView;
  349. InstrumentSoundShapingView * m_ssView;
  350. InstrumentFunctionNoteStackingView* m_noteStackingView;
  351. InstrumentFunctionArpeggioView* m_arpeggioView;
  352. InstrumentMidiIOView * m_midiView;
  353. EffectRackView * m_effectView;
  354. InstrumentMiscView *m_miscView;
  355. // test-piano at the bottom of every instrument-settings-window
  356. PianoView * m_pianoView;
  357. friend class InstrumentView;
  358. } ;
  359. #endif