audio_stream_mp3.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. mp3dec_ex_t *mp3d = nullptr;
  44. uint32_t frames_mixed = 0;
  45. bool active = false;
  46. int loops = 0;
  47. friend class AudioStreamMP3;
  48. Ref<AudioStreamMP3> mp3_stream;
  49. protected:
  50. virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
  51. virtual float get_stream_sampling_rate() override;
  52. public:
  53. virtual void start(double p_from_pos = 0.0) override;
  54. virtual void stop() override;
  55. virtual bool is_playing() const override;
  56. virtual int get_loop_count() const override; //times it looped
  57. virtual double get_playback_position() const override;
  58. virtual void seek(double p_time) override;
  59. virtual void tag_used_streams() override;
  60. AudioStreamPlaybackMP3() {}
  61. ~AudioStreamPlaybackMP3();
  62. };
  63. class AudioStreamMP3 : public AudioStream {
  64. GDCLASS(AudioStreamMP3, AudioStream);
  65. OBJ_SAVE_TYPE(AudioStream) //children are all saved as AudioStream, so they can be exchanged
  66. RES_BASE_EXTENSION("mp3str");
  67. friend class AudioStreamPlaybackMP3;
  68. PackedByteArray data;
  69. uint32_t data_len = 0;
  70. float sample_rate = 1.0;
  71. int channels = 1;
  72. float length = 0.0;
  73. bool loop = false;
  74. float loop_offset = 0.0;
  75. void clear_data();
  76. double bpm = 0;
  77. int beat_count = 0;
  78. int bar_beats = 4;
  79. protected:
  80. static void _bind_methods();
  81. public:
  82. void set_loop(bool p_enable);
  83. virtual bool has_loop() const override;
  84. void set_loop_offset(double p_seconds);
  85. double get_loop_offset() const;
  86. void set_bpm(double p_bpm);
  87. virtual double get_bpm() const override;
  88. void set_beat_count(int p_beat_count);
  89. virtual int get_beat_count() const override;
  90. void set_bar_beats(int p_bar_beats);
  91. virtual int get_bar_beats() const override;
  92. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  93. virtual String get_stream_name() const override;
  94. void set_data(const Vector<uint8_t> &p_data);
  95. Vector<uint8_t> get_data() const;
  96. virtual double get_length() const override;
  97. virtual bool is_monophonic() const override;
  98. AudioStreamMP3();
  99. virtual ~AudioStreamMP3();
  100. };
  101. #endif // AUDIO_STREAM_MP3_H