1
0

libopenmpt_impl.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * libopenmpt_impl.hpp
  3. * -------------------
  4. * Purpose: libopenmpt private interface
  5. * Notes : This is not a public header. Do NOT ship in distributions dev packages.
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #ifndef LIBOPENMPT_IMPL_HPP
  10. #define LIBOPENMPT_IMPL_HPP
  11. #include "libopenmpt_internal.h"
  12. #include "libopenmpt.hpp"
  13. #include <iosfwd>
  14. #include <memory>
  15. #include <utility>
  16. #if defined(_MSC_VER)
  17. #pragma warning(push)
  18. #pragma warning(disable:4512) // assignment operator could not be generated
  19. #endif
  20. // forward declarations
  21. namespace mpt {
  22. inline namespace mpt_libopenmpt {
  23. namespace IO {
  24. class FileCursorTraitsFileData;
  25. template <typename Tpath>
  26. class FileCursorFilenameTraits;
  27. template <typename Ttraits, typename Tfilenametraits>
  28. class FileCursor;
  29. } // namespace IO
  30. } // namespace mpt_libopenmpt
  31. } // namespace mpt
  32. namespace OpenMPT {
  33. namespace detail {
  34. template <typename Ttraits, typename Tfilenametraits>
  35. using FileCursor = mpt::IO::FileCursor<Ttraits, Tfilenametraits>;
  36. } // namespace detail
  37. namespace mpt {
  38. class PathString;
  39. } // namespace mpt
  40. using FileCursor = detail::FileCursor<mpt::IO::FileCursorTraitsFileData, mpt::IO::FileCursorFilenameTraits<mpt::PathString>>;
  41. class CSoundFile;
  42. struct DithersWrapperOpenMPT;
  43. } // namespace OpenMPT
  44. namespace openmpt {
  45. namespace version {
  46. std::uint32_t get_library_version();
  47. std::uint32_t get_core_version();
  48. std::string get_string( const std::string & key );
  49. } // namespace version
  50. class log_interface {
  51. protected:
  52. log_interface();
  53. public:
  54. virtual ~log_interface();
  55. virtual void log( const std::string & message ) const = 0;
  56. }; // class log_interface
  57. class std_ostream_log : public log_interface {
  58. private:
  59. std::ostream & destination;
  60. public:
  61. std_ostream_log( std::ostream & dst );
  62. virtual ~std_ostream_log();
  63. void log( const std::string & message ) const override;
  64. }; // class CSoundFileLog_std_ostream
  65. class log_forwarder;
  66. struct callback_stream_wrapper {
  67. void * stream;
  68. std::size_t (*read)( void * stream, void * dst, std::size_t bytes );
  69. int (*seek)( void * stream, std::int64_t offset, int whence );
  70. std::int64_t (*tell)( void * stream );
  71. }; // struct callback_stream_wrapper
  72. class module_impl {
  73. public:
  74. enum class amiga_filter_type {
  75. a500,
  76. a1200,
  77. unfiltered,
  78. auto_filter,
  79. };
  80. protected:
  81. struct subsong_data {
  82. double duration;
  83. std::int32_t start_row;
  84. std::int32_t start_order;
  85. std::int32_t sequence;
  86. subsong_data( double duration, std::int32_t start_row, std::int32_t start_order, std::int32_t sequence );
  87. }; // struct subsong_data
  88. typedef std::vector<subsong_data> subsongs_type;
  89. enum class song_end_action {
  90. fadeout_song,
  91. continue_song,
  92. stop_song,
  93. };
  94. static constexpr std::int32_t all_subsongs = -1;
  95. enum class ctl_type {
  96. boolean,
  97. integer,
  98. floatingpoint,
  99. text,
  100. };
  101. struct ctl_info {
  102. const char * name;
  103. ctl_type type;
  104. };
  105. std::unique_ptr<log_interface> m_Log;
  106. std::unique_ptr<log_forwarder> m_LogForwarder;
  107. std::int32_t m_current_subsong;
  108. double m_currentPositionSeconds;
  109. std::unique_ptr<OpenMPT::CSoundFile> m_sndFile;
  110. bool m_loaded;
  111. bool m_mixer_initialized;
  112. std::unique_ptr<OpenMPT::DithersWrapperOpenMPT> m_Dithers;
  113. subsongs_type m_subsongs;
  114. float m_Gain;
  115. song_end_action m_ctl_play_at_end;
  116. amiga_filter_type m_ctl_render_resampler_emulate_amiga_type = amiga_filter_type::auto_filter;
  117. bool m_ctl_load_skip_samples;
  118. bool m_ctl_load_skip_patterns;
  119. bool m_ctl_load_skip_plugins;
  120. bool m_ctl_load_skip_subsongs_init;
  121. bool m_ctl_seek_sync_samples;
  122. std::vector<std::string> m_loaderMessages;
  123. public:
  124. void PushToCSoundFileLog( const std::string & text ) const;
  125. void PushToCSoundFileLog( int loglevel, const std::string & text ) const;
  126. protected:
  127. std::string mod_string_to_utf8( const std::string & encoded ) const;
  128. void apply_mixer_settings( std::int32_t samplerate, int channels );
  129. void apply_libopenmpt_defaults();
  130. subsongs_type get_subsongs() const;
  131. void init_subsongs( subsongs_type & subsongs ) const;
  132. bool has_subsongs_inited() const;
  133. void ctor( const std::map< std::string, std::string > & ctls );
  134. void load( const OpenMPT::FileCursor & file, const std::map< std::string, std::string > & ctls );
  135. bool is_loaded() const;
  136. std::size_t read_wrapper( std::size_t count, std::int16_t * left, std::int16_t * right, std::int16_t * rear_left, std::int16_t * rear_right );
  137. std::size_t read_wrapper( std::size_t count, float * left, float * right, float * rear_left, float * rear_right );
  138. std::size_t read_interleaved_wrapper( std::size_t count, std::size_t channels, std::int16_t * interleaved );
  139. std::size_t read_interleaved_wrapper( std::size_t count, std::size_t channels, float * interleaved );
  140. std::string get_message_instruments() const;
  141. std::string get_message_samples() const;
  142. std::pair< std::string, std::string > format_and_highlight_pattern_row_channel_command( std::int32_t p, std::int32_t r, std::int32_t c, int command ) const;
  143. std::pair< std::string, std::string > format_and_highlight_pattern_row_channel( std::int32_t p, std::int32_t r, std::int32_t c, std::size_t width, bool pad ) const;
  144. static double could_open_probability( const OpenMPT::FileCursor & file, double effort, std::unique_ptr<log_interface> log );
  145. public:
  146. static std::vector<std::string> get_supported_extensions();
  147. /// <summary>
  148. /// From version 0.7.0
  149. /// Hakan DANISIK
  150. /// </summary>
  151. /// <param name="extension"></param>
  152. /// <returns></returns>
  153. static std::string get_tracker_name( const std::string & extension );
  154. static bool is_extension_supported( std::string_view extension );
  155. static double could_open_probability( callback_stream_wrapper stream, double effort, std::unique_ptr<log_interface> log );
  156. static double could_open_probability( std::istream & stream, double effort, std::unique_ptr<log_interface> log );
  157. static std::size_t probe_file_header_get_recommended_size();
  158. static int probe_file_header( std::uint64_t flags, const std::byte * data, std::size_t size, std::uint64_t filesize );
  159. static int probe_file_header( std::uint64_t flags, const std::uint8_t * data, std::size_t size, std::uint64_t filesize );
  160. static int probe_file_header( std::uint64_t flags, const void * data, std::size_t size, std::uint64_t filesize );
  161. static int probe_file_header( std::uint64_t flags, const std::byte * data, std::size_t size );
  162. static int probe_file_header( std::uint64_t flags, const std::uint8_t * data, std::size_t size );
  163. static int probe_file_header( std::uint64_t flags, const void * data, std::size_t size );
  164. static int probe_file_header( std::uint64_t flags, std::istream & stream );
  165. static int probe_file_header( std::uint64_t flags, callback_stream_wrapper stream );
  166. module_impl( callback_stream_wrapper stream, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls );
  167. module_impl( std::istream & stream, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls );
  168. module_impl( const std::vector<std::byte> & data, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls );
  169. module_impl( const std::vector<std::uint8_t> & data, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls );
  170. module_impl( const std::vector<char> & data, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls );
  171. module_impl( const std::byte * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls );
  172. module_impl( const std::uint8_t * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls );
  173. module_impl( const char * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls );
  174. module_impl( const void * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls );
  175. ~module_impl();
  176. public:
  177. void select_subsong( std::int32_t subsong );
  178. std::int32_t get_selected_subsong() const;
  179. void set_repeat_count( std::int32_t repeat_count );
  180. std::int32_t get_repeat_count() const;
  181. double get_duration_seconds() const;
  182. double set_position_seconds( double seconds );
  183. double get_position_seconds() const;
  184. double set_position_order_row( std::int32_t order, std::int32_t row );
  185. std::int32_t get_render_param( int param ) const;
  186. void set_render_param( int param, std::int32_t value );
  187. std::size_t read( std::int32_t samplerate, std::size_t count, std::int16_t * mono );
  188. std::size_t read( std::int32_t samplerate, std::size_t count, std::int16_t * left, std::int16_t * right );
  189. std::size_t read( std::int32_t samplerate, std::size_t count, std::int16_t * left, std::int16_t * right, std::int16_t * rear_left, std::int16_t * rear_right );
  190. std::size_t read( std::int32_t samplerate, std::size_t count, float * mono );
  191. std::size_t read( std::int32_t samplerate, std::size_t count, float * left, float * right );
  192. std::size_t read( std::int32_t samplerate, std::size_t count, float * left, float * right, float * rear_left, float * rear_right );
  193. std::size_t read_interleaved_stereo( std::int32_t samplerate, std::size_t count, std::int16_t * interleaved_stereo );
  194. std::size_t read_interleaved_quad( std::int32_t samplerate, std::size_t count, std::int16_t * interleaved_quad );
  195. std::size_t read_interleaved_stereo( std::int32_t samplerate, std::size_t count, float * interleaved_stereo );
  196. std::size_t read_interleaved_quad( std::int32_t samplerate, std::size_t count, float * interleaved_quad );
  197. std::vector<std::string> get_metadata_keys() const;
  198. std::string get_metadata( const std::string & key ) const;
  199. double get_current_estimated_bpm() const;
  200. std::int32_t get_current_speed() const;
  201. std::int32_t get_current_tempo() const;
  202. std::int32_t get_current_order() const;
  203. std::int32_t get_current_pattern() const;
  204. std::int32_t get_current_row() const;
  205. std::int32_t get_current_playing_channels() const;
  206. float get_current_channel_vu_mono( std::int32_t channel ) const;
  207. float get_current_channel_vu_left( std::int32_t channel ) const;
  208. float get_current_channel_vu_right( std::int32_t channel ) const;
  209. float get_current_channel_vu_rear_left( std::int32_t channel ) const;
  210. float get_current_channel_vu_rear_right( std::int32_t channel ) const;
  211. std::int32_t get_num_subsongs() const;
  212. std::int32_t get_num_channels() const;
  213. std::int32_t get_num_orders() const;
  214. std::int32_t get_num_patterns() const;
  215. std::int32_t get_num_instruments() const;
  216. std::int32_t get_num_samples() const;
  217. std::vector<std::string> get_subsong_names() const;
  218. std::vector<std::string> get_channel_names() const;
  219. std::vector<std::string> get_order_names() const;
  220. std::vector<std::string> get_pattern_names() const;
  221. std::vector<std::string> get_instrument_names() const;
  222. std::vector<std::string> get_sample_names() const;
  223. std::int32_t get_order_pattern( std::int32_t o ) const;
  224. std::int32_t get_pattern_num_rows( std::int32_t p ) const;
  225. std::uint8_t get_pattern_row_channel_command( std::int32_t p, std::int32_t r, std::int32_t c, int cmd ) const;
  226. std::string format_pattern_row_channel_command( std::int32_t p, std::int32_t r, std::int32_t c, int cmd ) const;
  227. std::string highlight_pattern_row_channel_command( std::int32_t p, std::int32_t r, std::int32_t c, int cmd ) const;
  228. std::string format_pattern_row_channel( std::int32_t p, std::int32_t r, std::int32_t c, std::size_t width, bool pad ) const;
  229. std::string highlight_pattern_row_channel( std::int32_t p, std::int32_t r, std::int32_t c, std::size_t width, bool pad ) const;
  230. std::pair<const module_impl::ctl_info *, const module_impl::ctl_info *> get_ctl_infos() const;
  231. std::vector<std::string> get_ctls() const;
  232. std::string ctl_get( std::string ctl, bool throw_if_unknown = true ) const;
  233. bool ctl_get_boolean( std::string_view ctl, bool throw_if_unknown = true ) const;
  234. std::int64_t ctl_get_integer( std::string_view ctl, bool throw_if_unknown = true ) const;
  235. double ctl_get_floatingpoint( std::string_view ctl, bool throw_if_unknown = true ) const;
  236. std::string ctl_get_text( std::string_view ctl, bool throw_if_unknown = true ) const;
  237. void ctl_set( std::string ctl, const std::string & value, bool throw_if_unknown = true );
  238. void ctl_set_boolean( std::string_view ctl, bool value, bool throw_if_unknown = true );
  239. void ctl_set_integer( std::string_view ctl, std::int64_t value, bool throw_if_unknown = true );
  240. void ctl_set_floatingpoint( std::string_view ctl, double value, bool throw_if_unknown = true );
  241. void ctl_set_text( std::string_view ctl, std::string_view value, bool throw_if_unknown = true );
  242. }; // class module_impl
  243. namespace helper {
  244. template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) {
  245. return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  246. }
  247. } // namespace helper
  248. } // namespace openmpt
  249. #if defined(_MSC_VER)
  250. #pragma warning(pop)
  251. #endif
  252. #endif // LIBOPENMPT_IMPL_HPP