audio_stream_mp3.h 5.3 KB

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