1
0

libopenmpt_ext.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * libopenmpt_ext.hpp
  3. * ------------------
  4. * Purpose: libopenmpt public c++ interface for libopenmpt extensions
  5. * Notes :
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #ifndef LIBOPENMPT_EXT_HPP
  10. #define LIBOPENMPT_EXT_HPP
  11. #include "libopenmpt_config.h"
  12. #include "libopenmpt.hpp"
  13. /*!
  14. * \page libopenmpt_ext_cpp_overview libopenmpt_ext C++ API
  15. *
  16. * libopenmpt_ext is now included in all builds by default.
  17. *
  18. * \section libopenmpt-ext-cpp-detailed Detailed documentation
  19. *
  20. * \ref libopenmpt_ext_cpp
  21. *
  22. */
  23. /*! \defgroup libopenmpt_ext_cpp libopenmpt_ext C++ */
  24. namespace openmpt {
  25. /*! \addtogroup libopenmpt_ext_cpp
  26. @{
  27. */
  28. class module_ext_impl;
  29. class LIBOPENMPT_CXX_API module_ext : public module {
  30. private:
  31. module_ext_impl * ext_impl;
  32. private:
  33. // non-copyable
  34. module_ext( const module_ext & );
  35. void operator = ( const module_ext & );
  36. public:
  37. module_ext( std::istream & stream, std::ostream & log = std::clog, const std::map< std::string, std::string > & ctls = detail::initial_ctls_map() );
  38. module_ext( const std::vector<std::byte> & data, std::ostream & log = std::clog, const std::map< std::string, std::string > & ctls = detail::initial_ctls_map() );
  39. module_ext( const std::vector<std::uint8_t> & data, std::ostream & log = std::clog, const std::map< std::string, std::string > & ctls = detail::initial_ctls_map() );
  40. module_ext( const std::vector<char> & data, std::ostream & log = std::clog, const std::map< std::string, std::string > & ctls = detail::initial_ctls_map() );
  41. module_ext( const std::byte * data, std::size_t size, std::ostream & log = std::clog, const std::map< std::string, std::string > & ctls = detail::initial_ctls_map() );
  42. module_ext( const std::uint8_t * data, std::size_t size, std::ostream & log = std::clog, const std::map< std::string, std::string > & ctls = detail::initial_ctls_map() );
  43. module_ext( const char * data, std::size_t size, std::ostream & log = std::clog, const std::map< std::string, std::string > & ctls = detail::initial_ctls_map() );
  44. module_ext( const void * data, std::size_t size, std::ostream & log = std::clog, const std::map< std::string, std::string > & ctls = detail::initial_ctls_map() );
  45. virtual ~module_ext();
  46. public:
  47. //! Retrieve a libopenmpt extension.
  48. /*! Example: Retrieving the interactive extension to change the tempo of a module:
  49. \code{.cpp}
  50. openmpt::module_ext *mod = new openmpt::module_ext( stream );
  51. #ifdef LIBOPENMPT_EXT_INTERFACE_INTERACTIVE
  52. openmpt::ext::interactive *interactive = static_cast<openmpt::ext::interactive *>( self->mod->get_interface( openmpt::ext::interactive_id ) );
  53. if ( interactive ) {
  54. interactive->set_tempo_factor( 2.0 ); // play module at double speed
  55. } else {
  56. // interface not available
  57. }
  58. #else
  59. // interfae not available
  60. #endif
  61. \endcode
  62. \param interface_id The name of the extension interface to retrieve.
  63. \return The interface object. This may be a nullptr if the extension was not found.
  64. */
  65. void * get_interface( const std::string & interface_id );
  66. }; // class module_ext
  67. /*!
  68. @}
  69. */
  70. namespace ext {
  71. /*! \addtogroup libopenmpt_ext_cpp
  72. @{
  73. */
  74. #define LIBOPENMPT_DECLARE_EXT_CXX_INTERFACE(name) \
  75. static const char name ## _id [] = # name ; \
  76. class name; \
  77. /**/
  78. #define LIBOPENMPT_EXT_CXX_INTERFACE(name) \
  79. protected: \
  80. name () {} \
  81. virtual ~ name () {} \
  82. public: \
  83. /**/
  84. #ifndef LIBOPENMPT_EXT_INTERFACE_PATTERN_VIS
  85. #define LIBOPENMPT_EXT_INTERFACE_PATTERN_VIS
  86. #endif
  87. LIBOPENMPT_DECLARE_EXT_CXX_INTERFACE(pattern_vis)
  88. class pattern_vis {
  89. LIBOPENMPT_EXT_CXX_INTERFACE(pattern_vis)
  90. //! Pattern command type
  91. enum effect_type {
  92. effect_unknown = 0,
  93. effect_general = 1,
  94. effect_global = 2,
  95. effect_volume = 3,
  96. effect_panning = 4,
  97. effect_pitch = 5
  98. }; // enum effect_type
  99. //! Get pattern command type for pattern highlighting
  100. /*!
  101. \param pattern The pattern whose data should be retrieved.
  102. \param row The row from which the data should be retrieved.
  103. \param channel The channel from which the data should be retrieved.
  104. \return The command type in the effect column at the given pattern position (see openmpt::ext::pattern_vis::effect_type)
  105. \sa openmpt::ext::pattern_vis::get_pattern_row_channel_effect_type
  106. */
  107. virtual effect_type get_pattern_row_channel_volume_effect_type( std::int32_t pattern, std::int32_t row, std::int32_t channel ) const = 0;
  108. //! Get pattern command type for pattern highlighting
  109. /*!
  110. \param pattern The pattern whose data should be retrieved.
  111. \param row The row from which the data should be retrieved.
  112. \param channel The channel from which the data should be retrieved.
  113. \return The command type in the volume column at the given pattern position (see openmpt::ext::pattern_vis::effect_type)
  114. \sa openmpt::ext::pattern_vis::get_pattern_row_channel_volume_effect_type
  115. */
  116. virtual effect_type get_pattern_row_channel_effect_type( std::int32_t pattern, std::int32_t row, std::int32_t channel ) const = 0;
  117. }; // class pattern_vis
  118. #ifndef LIBOPENMPT_EXT_INTERFACE_INTERACTIVE
  119. #define LIBOPENMPT_EXT_INTERFACE_INTERACTIVE
  120. #endif
  121. LIBOPENMPT_DECLARE_EXT_CXX_INTERFACE(interactive)
  122. class interactive {
  123. LIBOPENMPT_EXT_CXX_INTERFACE(interactive)
  124. //! Set the current ticks per row (speed)
  125. /*!
  126. \param speed The new tick count in range [1, 65535].
  127. \throws openmpt::exception Throws an exception derived from openmpt::exception if the speed is outside the specified range.
  128. \remarks The tick count may be reset by pattern commands at any time.
  129. \sa openmpt::module::get_current_speed
  130. */
  131. virtual void set_current_speed( std::int32_t speed ) = 0;
  132. //! Set the current module tempo
  133. /*!
  134. \param tempo The new tempo in range [32, 512]. The exact meaning of the value depends on the tempo mode used by the module.
  135. \throws openmpt::exception Throws an exception derived from openmpt::exception if the tempo is outside the specified range.
  136. \remarks The tempo may be reset by pattern commands at any time. Use openmpt::ext:interactive::set_tempo_factor to apply a tempo factor that is independent of pattern commands.
  137. \sa openmpt::module::get_current_tempo
  138. */
  139. virtual void set_current_tempo( std::int32_t tempo ) = 0;
  140. //! Set the current module tempo factor without affecting playback pitch
  141. /*!
  142. \param factor The new tempo factor in range ]0.0, 4.0] - 1.0 means unmodified tempo.
  143. \throws openmpt::exception Throws an exception derived from openmpt::exception if the factor is outside the specified range.
  144. \remarks Modifying the tempo without applying the same pitch factor using openmpt::ext::interactive::set_pitch_factor may cause rhythmic samples (e.g. drum loops) to go out of sync.
  145. \sa openmpt::ext::interactive::get_tempo_factor
  146. */
  147. virtual void set_tempo_factor( double factor ) = 0;
  148. //! Gets the current module tempo factor
  149. /*!
  150. \return The current tempo factor.
  151. \sa openmpt::ext::interactive::set_tempo_factor
  152. */
  153. virtual double get_tempo_factor( ) const = 0;
  154. //! Set the current module pitch factor without affecting playback speed
  155. /*!
  156. \param factor The new pitch factor in range ]0.0, 4.0] - 1.0 means unmodified pitch.
  157. \throws openmpt::exception Throws an exception derived from openmpt::exception if the factor is outside the specified range.
  158. \remarks Modifying the pitch without applying the the same tempo factor using openmpt::ext::interactive::set_tempo_factor may cause rhythmic samples (e.g. drum loops) to go out of sync.
  159. \remarks To shift the pich by `n` semitones, the parameter can be calculated as follows: `pow( 2.0, n / 12.0 )`
  160. \sa openmpt::ext::interactive::get_pitch_factor
  161. */
  162. virtual void set_pitch_factor( double factor ) = 0;
  163. //! Gets the current module pitch factor
  164. /*!
  165. \return The current pitch factor.
  166. \throws openmpt::exception Throws an exception derived from openmpt::exception if the pitch is outside the specified range.
  167. \sa openmpt::ext::interactive::set_pitch_factor
  168. */
  169. virtual double get_pitch_factor( ) const = 0;
  170. //! Set the current global volume
  171. /*!
  172. \param volume The new global volume in range [0.0, 1.0]
  173. \throws openmpt::exception Throws an exception derived from openmpt::exception if the volume is outside the specified range.
  174. \remarks The global volume may be reset by pattern commands at any time. Use openmpt::module::set_render_param to apply a global overall volume factor that is independent of pattern commands.
  175. \sa openmpt::ext::interactive::get_global_volume
  176. */
  177. virtual void set_global_volume( double volume ) = 0;
  178. //! Get the current global volume
  179. /*!
  180. \return The current global volume in range [0.0, 1.0]
  181. \sa openmpt::ext::interactive::set_global_volume
  182. */
  183. virtual double get_global_volume( ) const = 0;
  184. //! Set the current channel volume for a channel
  185. /*!
  186. \param channel The channel whose volume should be set, in range [0, openmpt::module::get_num_channels()[
  187. \param volume The new channel volume in range [0.0, 1.0]
  188. \throws openmpt::exception Throws an exception derived from openmpt::exception if the channel or volume is outside the specified range.
  189. \remarks The channel volume may be reset by pattern commands at any time.
  190. \sa openmpt::ext::interactive::get_channel_volume
  191. */
  192. virtual void set_channel_volume( std::int32_t channel, double volume ) = 0;
  193. //! Get the current channel volume for a channel
  194. /*!
  195. \param channel The channel whose volume should be retrieved, in range [0, openmpt::module::get_num_channels()[
  196. \return The current channel volume in range [0.0, 1.0]
  197. \throws openmpt::exception Throws an exception derived from openmpt::exception if the channel is outside the specified range.
  198. \sa openmpt::ext::interactive::set_channel_volume
  199. */
  200. virtual double get_channel_volume( std::int32_t channel ) const = 0;
  201. //! Set the current mute status for a channel
  202. /*!
  203. \param channel The channel whose mute status should be set, in range [0, openmpt::module::get_num_channels()[
  204. \param mute The new mute status. true is muted, false is unmuted.
  205. \throws openmpt::exception Throws an exception derived from openmpt::exception if the channel is outside the specified range.
  206. \sa openmpt::ext::interactive::get_channel_mute_status
  207. */
  208. virtual void set_channel_mute_status( std::int32_t channel, bool mute ) = 0;
  209. //! Get the current mute status for a channel
  210. /*!
  211. \param channel The channel whose mute status should be retrieved, in range [0, openmpt::module::get_num_channels()[
  212. \return The current channel mute status. true is muted, false is unmuted.
  213. \throws openmpt::exception Throws an exception derived from openmpt::exception if the channel is outside the specified range.
  214. \sa openmpt::ext::interactive::set_channel_mute_status
  215. */
  216. virtual bool get_channel_mute_status( std::int32_t channel ) const = 0;
  217. //! Set the current mute status for an instrument
  218. /*!
  219. \param instrument The instrument whose mute status should be set, in range [0, openmpt::module::get_num_instruments()[ if openmpt::module::get_num_instruments is not 0, otherwise in [0, openmpt::module::get_num_samples()[
  220. \param mute The new mute status. true is muted, false is unmuted.
  221. \throws openmpt::exception Throws an exception derived from openmpt::exception if the instrument is outside the specified range.
  222. \sa openmpt::ext::interactive::get_instrument_mute_status
  223. */
  224. virtual void set_instrument_mute_status( std::int32_t instrument, bool mute ) = 0;
  225. //! Get the current mute status for an instrument
  226. /*!
  227. \param instrument The instrument whose mute status should be retrieved, in range [0, openmpt::module::get_num_instruments()[ if openmpt::module::get_num_instruments is not 0, otherwise in [0, openmpt::module::get_num_samples()[
  228. \return The current instrument mute status. true is muted, false is unmuted.
  229. \throws openmpt::exception Throws an exception derived from openmpt::exception if the instrument is outside the specified range.
  230. \sa openmpt::ext::interactive::set_instrument_mute_status
  231. */
  232. virtual bool get_instrument_mute_status( std::int32_t instrument ) const = 0;
  233. //! Play a note using the specified instrument
  234. /*!
  235. \param instrument The instrument that should be played, in range [0, openmpt::module::get_num_instruments()[ if openmpt::module::get_num_instruments is not 0, otherwise in [0, openmpt::module::get_num_samples()[
  236. \param note The note to play, in rage [0, 119]. 60 is the middle C.
  237. \param volume The volume at which the note should be triggered, in range [0.0, 1.0]
  238. \param panning The panning position at which the note should be triggered, in range [-1.0, 1.0], 0.0 is center.
  239. \return The channel on which the note is played. This can pe be passed to openmpt::ext::interactive::stop_note to stop the note.
  240. \throws openmpt::exception Throws an exception derived from openmpt::exception if the instrument or note is outside the specified range.
  241. \sa openmpt::ext::interactive::stop_note
  242. \sa openmpt::ext::interactive2::note_off
  243. \sa openmpt::ext::interactive2::note_fade
  244. */
  245. virtual std::int32_t play_note( std::int32_t instrument, std::int32_t note, double volume, double panning ) = 0;
  246. //! Stop the note playing on the specified channel
  247. /*!
  248. \param channel The channel on which the note should be stopped. This is the value returned by a previous play_note call.
  249. \throws openmpt::exception Throws an exception derived from openmpt::exception if the channel index is invalid.
  250. \sa openmpt::ext::interactive::play_note
  251. \sa openmpt::ext::interactive2::note_off
  252. \sa openmpt::ext::interactive2::note_fade
  253. */
  254. virtual void stop_note( std::int32_t channel ) = 0;
  255. }; // class interactive
  256. #ifndef LIBOPENMPT_EXT_INTERFACE_INTERACTIVE2
  257. #define LIBOPENMPT_EXT_INTERFACE_INTERACTIVE2
  258. #endif
  259. LIBOPENMPT_DECLARE_EXT_CXX_INTERFACE(interactive2)
  260. class interactive2 {
  261. LIBOPENMPT_EXT_CXX_INTERFACE(interactive2)
  262. //! Sends a key-off command for the note playing on the specified channel
  263. /*!
  264. \param channel The channel on which the key-off event should be triggered. This is the value returned by a previous play_note call.
  265. \throws openmpt::exception Throws an exception derived from openmpt::exception if the channel index is invalid.
  266. \remarks This method releases envelopes and sample sustain loops. If the sample has no sustain loop, or if the module does not use instruments, it does nothing.
  267. \sa openmpt::ext::interactive::play_note
  268. \sa openmpt::ext::interactive::stop_note
  269. \sa openmpt::ext::interactive2::note_fade
  270. \since 0.6.0
  271. */
  272. virtual void note_off(int32_t channel ) = 0;
  273. //! Sends a note fade command for the note playing on the specified channel
  274. /*!
  275. \param channel The channel on which the note should be faded. This is the value returned by a previous play_note call.
  276. \throws openmpt::exception Throws an exception derived from openmpt::exception if the channel index is invalid.
  277. \remarks This method uses the instrument's fade-out value. If the module does not use instruments, or the instrument's fade-out value is 0, it does nothing.
  278. \sa openmpt::ext::interactive::play_note
  279. \sa openmpt::ext::interactive::stop_note
  280. \sa openmpt::ext::interactive2::note_off
  281. \since 0.6.0
  282. */
  283. virtual void note_fade(int32_t channel) = 0;
  284. //! Set the current panning position for a channel
  285. /*!
  286. \param channel The channel whose panning will be changed, in range [0, openmpt::module::get_num_channels()[
  287. \param panning The panning position to set on the channel, in range [-1.0, 1.0], 0.0 is center.
  288. \throws openmpt::exception Throws an exception derived from openmpt::exception if the channel index is invalid.
  289. \remarks This command affects subsequent notes played on the same channel, and may itself be overridden by subsequent panning commands encountered in the module itself.
  290. \sa openmpt::ext::interactive2::get_channel_panning
  291. \since 0.6.0
  292. */
  293. virtual void set_channel_panning(int32_t channel, double panning ) = 0;
  294. //! Get the current panning position for a channel
  295. /*!
  296. \param channel The channel whose panning should be retrieved, in range [0, openmpt::module::get_num_channels()[
  297. \return The current channel panning, in range [-1.0, 1.0], 0.0 is center.
  298. \throws openmpt::exception Throws an exception derived from openmpt::exception if the channel is outside the specified range.
  299. \sa openmpt::ext::interactive2::set_channel_panning
  300. \since 0.6.0
  301. */
  302. virtual double get_channel_panning( int32_t channel ) = 0;
  303. //! Set the finetune for the currently playing note on a channel
  304. /*!
  305. \param channel The channel whose finetune will be changed, in range [0, openmpt::module::get_num_channels()[
  306. \param finetune The finetune to set on the channel, in range [-1.0, 1.0], 0.0 is center.
  307. \throws openmpt::exception Throws an exception derived from openmpt::exception if the channel index is invalid.
  308. \remarks The finetune range depends on the pitch wheel depth of the instrument playing on the current channel; for sample-based modules, the depth of this command is fixed to +/-1 semitone.
  309. \remarks This command does not affect subsequent notes played on the same channel, but may itself be overridden by subsequent finetune commands encountered in the module itself.
  310. \sa openmpt::ext::interactive2::get_note_finetune
  311. \since 0.6.0
  312. */
  313. virtual void set_note_finetune(int32_t channel, double finetune ) = 0;
  314. //! Get the finetune for the currently playing note on a channel
  315. /*!
  316. \param channel The channel whose finetune should be retrieved, in range [0, openmpt::module::get_num_channels()[
  317. \return The current channel finetune, in range [-1.0, 1.0], 0.0 is center.
  318. \throws openmpt::exception Throws an exception derived from openmpt::exception if the channel is outside the specified range.
  319. \remarks The finetune range depends on the pitch wheel depth of the instrument playing on the current channel; for sample-based modules, the depth of this command is fixed to +/-1 semitone.
  320. \sa openmpt::ext::interactive2::set_note_finetune
  321. \since 0.6.0
  322. */
  323. virtual double get_note_finetune( int32_t channel ) = 0;
  324. }; // class interactive
  325. /* add stuff here */
  326. #undef LIBOPENMPT_DECLARE_EXT_CXX_INTERFACE
  327. #undef LIBOPENMPT_EXT_CXX_INTERFACE
  328. /*!
  329. @}
  330. */
  331. } // namespace ext
  332. } // namespace openmpt
  333. #endif // LIBOPENMPT_EXT_HPP