audio_stream_mpc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*************************************************************************/
  2. /* audio_stream_mpc.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef AUDIO_STREAM_MPC_H
  31. #define AUDIO_STREAM_MPC_H
  32. #include "io/resource_loader.h"
  33. #include "os/file_access.h"
  34. #include "os/thread_safe.h"
  35. #include "scene/resources/audio_stream.h"
  36. #include <mpc/mpcdec.h>
  37. class AudioStreamPlaybackMPC : public AudioStreamPlayback {
  38. OBJ_TYPE(AudioStreamPlaybackMPC, AudioStreamPlayback);
  39. bool preload;
  40. FileAccess *f;
  41. String file;
  42. DVector<uint8_t> data;
  43. int data_ofs;
  44. int streamlen;
  45. bool active;
  46. bool paused;
  47. bool loop;
  48. int loops;
  49. // mpc
  50. mpc_reader reader;
  51. mpc_demux *demux;
  52. mpc_streaminfo si;
  53. MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
  54. static mpc_int32_t _mpc_read(mpc_reader *p_reader, void *p_dst, mpc_int32_t p_bytes);
  55. static mpc_bool_t _mpc_seek(mpc_reader *p_reader, mpc_int32_t p_offset);
  56. static mpc_int32_t _mpc_tell(mpc_reader *p_reader);
  57. static mpc_int32_t _mpc_get_size(mpc_reader *p_reader);
  58. static mpc_bool_t _mpc_canseek(mpc_reader *p_reader);
  59. int stream_min_size;
  60. int stream_rate;
  61. int stream_channels;
  62. protected:
  63. Error _open_file();
  64. void _close_file();
  65. int _read_file(void *p_dst, int p_bytes);
  66. bool _seek_file(int p_pos);
  67. int _tell_file() const;
  68. int _sizeof_file() const;
  69. bool _canseek_file() const;
  70. Error _reload();
  71. static void _bind_methods();
  72. public:
  73. void set_file(const String &p_file);
  74. String get_file() const;
  75. virtual void play(float p_offset = 0);
  76. virtual void stop();
  77. virtual bool is_playing() const;
  78. virtual void set_loop(bool p_enable);
  79. virtual bool has_loop() const;
  80. virtual float get_length() const;
  81. virtual String get_stream_name() const;
  82. virtual int get_loop_count() const;
  83. virtual float get_pos() const;
  84. virtual void seek_pos(float p_time);
  85. virtual int get_channels() const { return stream_channels; }
  86. virtual int get_mix_rate() const { return stream_rate; }
  87. virtual int get_minimum_buffer_size() const { return stream_min_size; }
  88. virtual int mix(int16_t *p_bufer, int p_frames);
  89. virtual void set_loop_restart_time(float p_time) {}
  90. AudioStreamPlaybackMPC();
  91. ~AudioStreamPlaybackMPC();
  92. };
  93. class AudioStreamMPC : public AudioStream {
  94. OBJ_TYPE(AudioStreamMPC, AudioStream);
  95. String file;
  96. public:
  97. Ref<AudioStreamPlayback> instance_playback() {
  98. Ref<AudioStreamPlaybackMPC> pb = memnew(AudioStreamPlaybackMPC);
  99. pb->set_file(file);
  100. return pb;
  101. }
  102. void set_file(const String &p_file) { file = p_file; }
  103. };
  104. class ResourceFormatLoaderAudioStreamMPC : public ResourceFormatLoader {
  105. public:
  106. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  107. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  108. virtual bool handles_type(const String &p_type) const;
  109. virtual String get_resource_type(const String &p_path) const;
  110. };
  111. #endif // AUDIO_STREAM_MPC_H