audio_stream_mp3.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**************************************************************************/
  2. /* audio_stream_mp3.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_MP3_H
  31. #define AUDIO_STREAM_MP3_H
  32. #include "servers/audio/audio_stream.h"
  33. #include <minimp3_ex.h>
  34. class AudioStreamMP3;
  35. class AudioStreamPlaybackMP3 : public AudioStreamPlaybackResampled {
  36. GDCLASS(AudioStreamPlaybackMP3, AudioStreamPlaybackResampled);
  37. enum {
  38. FADE_SIZE = 256
  39. };
  40. AudioFrame loop_fade[FADE_SIZE];
  41. int loop_fade_remaining = FADE_SIZE;
  42. bool looping_override = false;
  43. bool looping = false;
  44. mp3dec_ex_t mp3d = {};
  45. uint32_t frames_mixed = 0;
  46. bool active = false;
  47. int loops = 0;
  48. friend class AudioStreamMP3;
  49. Ref<AudioStreamMP3> mp3_stream;
  50. bool _is_sample = false;
  51. Ref<AudioSamplePlayback> sample_playback;
  52. protected:
  53. virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
  54. virtual float get_stream_sampling_rate() override;
  55. public:
  56. virtual void start(double p_from_pos = 0.0) override;
  57. virtual void stop() override;
  58. virtual bool is_playing() const override;
  59. virtual int get_loop_count() const override; //times it looped
  60. virtual double get_playback_position() const override;
  61. virtual void seek(double p_time) override;
  62. virtual void tag_used_streams() override;
  63. virtual void set_is_sample(bool p_is_sample) override;
  64. virtual bool get_is_sample() const override;
  65. virtual Ref<AudioSamplePlayback> get_sample_playback() const override;
  66. virtual void set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;
  67. virtual void set_parameter(const StringName &p_name, const Variant &p_value) override;
  68. virtual Variant get_parameter(const StringName &p_name) const override;
  69. AudioStreamPlaybackMP3() {}
  70. ~AudioStreamPlaybackMP3();
  71. };
  72. class AudioStreamMP3 : public AudioStream {
  73. GDCLASS(AudioStreamMP3, AudioStream);
  74. OBJ_SAVE_TYPE(AudioStream) //children are all saved as AudioStream, so they can be exchanged
  75. RES_BASE_EXTENSION("mp3str");
  76. friend class AudioStreamPlaybackMP3;
  77. LocalVector<uint8_t> data;
  78. uint32_t data_len = 0;
  79. float sample_rate = 1.0;
  80. int channels = 1;
  81. float length = 0.0;
  82. bool loop = false;
  83. float loop_offset = 0.0;
  84. void clear_data();
  85. double bpm = 0;
  86. int beat_count = 0;
  87. int bar_beats = 4;
  88. protected:
  89. static void _bind_methods();
  90. public:
  91. static Ref<AudioStreamMP3> load_from_buffer(const Vector<uint8_t> &p_stream_data);
  92. static Ref<AudioStreamMP3> load_from_file(const String &p_path);
  93. void set_loop(bool p_enable);
  94. virtual bool has_loop() const override;
  95. void set_loop_offset(double p_seconds);
  96. double get_loop_offset() const;
  97. void set_bpm(double p_bpm);
  98. virtual double get_bpm() const override;
  99. void set_beat_count(int p_beat_count);
  100. virtual int get_beat_count() const override;
  101. void set_bar_beats(int p_bar_beats);
  102. virtual int get_bar_beats() const override;
  103. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  104. virtual String get_stream_name() const override;
  105. void set_data(const Vector<uint8_t> &p_data);
  106. Vector<uint8_t> get_data() const;
  107. virtual double get_length() const override;
  108. virtual bool is_monophonic() const override;
  109. virtual bool can_be_sampled() const override {
  110. return true;
  111. }
  112. virtual Ref<AudioSample> generate_sample() const override;
  113. virtual void get_parameter_list(List<Parameter> *r_parameters) override;
  114. AudioStreamMP3();
  115. virtual ~AudioStreamMP3();
  116. };
  117. #endif // AUDIO_STREAM_MP3_H