Instrument.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "export.h"
  29. #include "lmms_basics.h"
  30. #include "MemoryManager.h"
  31. #include "MidiTime.h"
  32. #include "Plugin.h"
  33. // forward-declarations
  34. class InstrumentTrack;
  35. class MidiEvent;
  36. class NotePlayHandle;
  37. class Track;
  38. class 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. virtual ~Instrument();
  53. // --------------------------------------------------------------------
  54. // functions that can/should be re-implemented:
  55. // --------------------------------------------------------------------
  56. // if the plugin doesn't play each note, it can create an instrument-
  57. // play-handle and re-implement this method, so that it mixes its
  58. // output buffer only once per mixer-period
  59. virtual void play( sampleFrame * _working_buffer );
  60. // to be implemented by actual plugin
  61. virtual void playNote( NotePlayHandle * /* _note_to_play */,
  62. sampleFrame * /* _working_buf */ )
  63. {
  64. }
  65. // needed for deleting plugin-specific-data of a note - plugin has to
  66. // cast void-ptr so that the plugin-data is deleted properly
  67. // (call of dtor if it's a class etc.)
  68. virtual void deleteNotePluginData( NotePlayHandle * _note_to_play );
  69. // Get number of sample-frames that should be used when playing beat
  70. // (note with unspecified length)
  71. // Per default this function returns 0. In this case, channel is using
  72. // the length of the longest envelope (if one active).
  73. virtual f_cnt_t beatLen( NotePlayHandle * _n ) const;
  74. // some instruments need a certain number of release-frames even
  75. // if no envelope is active - such instruments can re-implement this
  76. // method for returning how many frames they at least like to have for
  77. // release
  78. virtual f_cnt_t desiredReleaseFrames() const
  79. {
  80. return 0;
  81. }
  82. virtual Flags flags() const
  83. {
  84. return NoFlags;
  85. }
  86. // sub-classes can re-implement this for receiving all incoming
  87. // MIDI-events
  88. inline virtual bool handleMidiEvent( const MidiEvent&, const MidiTime& = MidiTime(), f_cnt_t offset = 0 )
  89. {
  90. return true;
  91. }
  92. virtual QString fullDisplayName() const;
  93. // --------------------------------------------------------------------
  94. // provided functions:
  95. // --------------------------------------------------------------------
  96. // instantiate instrument-plugin with given name or return NULL
  97. // on failure
  98. static Instrument * instantiate( const QString & _plugin_name,
  99. InstrumentTrack * _instrument_track );
  100. virtual bool isFromTrack( const Track * _track ) const;
  101. inline InstrumentTrack * instrumentTrack() const
  102. {
  103. return m_instrumentTrack;
  104. }
  105. protected:
  106. // instruments may use this to apply a soft fade out at the end of
  107. // notes - method does this only if really less or equal
  108. // desiredReleaseFrames() frames are left
  109. void applyRelease( sampleFrame * buf, const NotePlayHandle * _n );
  110. private:
  111. InstrumentTrack * m_instrumentTrack;
  112. } ;
  113. Q_DECLARE_OPERATORS_FOR_FLAGS(Instrument::Flags)
  114. #endif