Instrument.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Instrument.h - declaration of class Instrument, which provides a
  3. * standard interface for all instrument plugins
  4. *
  5. * Copyright (c) 2005-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_H
  26. #define INSTRUMENT_H
  27. #include <QString>
  28. #include "lmms_export.h"
  29. #include "lmms_basics.h"
  30. #include "MemoryManager.h"
  31. #include "Plugin.h"
  32. #include "TimePos.h"
  33. // forward-declarations
  34. class InstrumentTrack;
  35. class MidiEvent;
  36. class NotePlayHandle;
  37. class Track;
  38. class LMMS_EXPORT Instrument : public Plugin
  39. {
  40. MM_OPERATORS
  41. public:
  42. enum Flag
  43. {
  44. NoFlags = 0x00,
  45. IsSingleStreamed = 0x01, /*! Instrument provides a single audio stream for all notes */
  46. IsMidiBased = 0x02, /*! Instrument is controlled by MIDI events rather than NotePlayHandles */
  47. IsNotBendable = 0x04, /*! Instrument can't react to pitch bend changes */
  48. };
  49. Q_DECLARE_FLAGS(Flags, Flag);
  50. Instrument(InstrumentTrack * _instrument_track,
  51. const Descriptor * _descriptor,
  52. const Descriptor::SubPluginFeatures::Key * key = nullptr);
  53. virtual ~Instrument() = default;
  54. // --------------------------------------------------------------------
  55. // functions that can/should be re-implemented:
  56. // --------------------------------------------------------------------
  57. virtual bool hasNoteInput() const { return true; }
  58. // if the plugin doesn't play each note, it can create an instrument-
  59. // play-handle and re-implement this method, so that it mixes its
  60. // output buffer only once per audio engine period
  61. virtual void play( sampleFrame * _working_buffer );
  62. // to be implemented by actual plugin
  63. virtual void playNote( NotePlayHandle * /* _note_to_play */,
  64. sampleFrame * /* _working_buf */ )
  65. {
  66. }
  67. // needed for deleting plugin-specific-data of a note - plugin has to
  68. // cast void-ptr so that the plugin-data is deleted properly
  69. // (call of dtor if it's a class etc.)
  70. virtual void deleteNotePluginData( NotePlayHandle * _note_to_play );
  71. // Get number of sample-frames that should be used when playing beat
  72. // (note with unspecified length)
  73. // Per default this function returns 0. In this case, channel is using
  74. // the length of the longest envelope (if one active).
  75. virtual f_cnt_t beatLen( NotePlayHandle * _n ) const;
  76. // some instruments need a certain number of release-frames even
  77. // if no envelope is active - such instruments can re-implement this
  78. // method for returning how many frames they at least like to have for
  79. // release
  80. virtual f_cnt_t desiredReleaseFrames() const
  81. {
  82. return 0;
  83. }
  84. virtual Flags flags() const
  85. {
  86. return NoFlags;
  87. }
  88. // sub-classes can re-implement this for receiving all incoming
  89. // MIDI-events
  90. inline virtual bool handleMidiEvent( const MidiEvent&, const TimePos& = TimePos(), f_cnt_t offset = 0 )
  91. {
  92. return true;
  93. }
  94. QString fullDisplayName() const override;
  95. // --------------------------------------------------------------------
  96. // provided functions:
  97. // --------------------------------------------------------------------
  98. //! instantiate instrument-plugin with given name or return NULL
  99. //! on failure
  100. static Instrument * instantiate(const QString & _plugin_name,
  101. InstrumentTrack * _instrument_track,
  102. const Plugin::Descriptor::SubPluginFeatures::Key* key,
  103. bool keyFromDnd = false);
  104. virtual bool isFromTrack( const Track * _track ) const;
  105. inline InstrumentTrack * instrumentTrack() const
  106. {
  107. return m_instrumentTrack;
  108. }
  109. protected:
  110. // fade in to prevent clicks
  111. void applyFadeIn(sampleFrame * buf, NotePlayHandle * n);
  112. // instruments may use this to apply a soft fade out at the end of
  113. // notes - method does this only if really less or equal
  114. // desiredReleaseFrames() frames are left
  115. void applyRelease( sampleFrame * buf, const NotePlayHandle * _n );
  116. private:
  117. InstrumentTrack * m_instrumentTrack;
  118. } ;
  119. Q_DECLARE_OPERATORS_FOR_FLAGS(Instrument::Flags)
  120. #endif