InstrumentTrack.h 10 KB

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