audio_stream_ogg_vorbis.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**************************************************************************/
  2. /* audio_stream_ogg_vorbis.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_OGG_VORBIS_H
  31. #define AUDIO_STREAM_OGG_VORBIS_H
  32. #include "core/variant/variant.h"
  33. #include "modules/ogg/ogg_packet_sequence.h"
  34. #include "servers/audio/audio_stream.h"
  35. #include <vorbis/codec.h>
  36. class AudioStreamOggVorbis;
  37. class AudioStreamPlaybackOggVorbis : public AudioStreamPlaybackResampled {
  38. GDCLASS(AudioStreamPlaybackOggVorbis, AudioStreamPlaybackResampled);
  39. uint32_t frames_mixed = 0;
  40. bool active = false;
  41. int loops = 0;
  42. enum {
  43. FADE_SIZE = 256
  44. };
  45. AudioFrame loop_fade[FADE_SIZE];
  46. int loop_fade_remaining = FADE_SIZE;
  47. vorbis_info info;
  48. vorbis_comment comment;
  49. vorbis_dsp_state dsp_state;
  50. vorbis_block block;
  51. bool info_is_allocated = false;
  52. bool comment_is_allocated = false;
  53. bool dsp_state_is_allocated = false;
  54. bool block_is_allocated = false;
  55. bool ready = false;
  56. bool have_samples_left = false;
  57. bool have_packets_left = false;
  58. friend class AudioStreamOggVorbis;
  59. Ref<OggPacketSequence> vorbis_data;
  60. Ref<OggPacketSequencePlayback> vorbis_data_playback;
  61. Ref<AudioStreamOggVorbis> vorbis_stream;
  62. int _mix_frames(AudioFrame *p_buffer, int p_frames);
  63. int _mix_frames_vorbis(AudioFrame *p_buffer, int p_frames);
  64. // Allocates vorbis data structures. Returns true upon success, false on failure.
  65. bool _alloc_vorbis();
  66. protected:
  67. virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
  68. virtual float get_stream_sampling_rate() override;
  69. public:
  70. virtual void start(double p_from_pos = 0.0) override;
  71. virtual void stop() override;
  72. virtual bool is_playing() const override;
  73. virtual int get_loop_count() const override; //times it looped
  74. virtual double get_playback_position() const override;
  75. virtual void seek(double p_time) override;
  76. virtual void tag_used_streams() override;
  77. AudioStreamPlaybackOggVorbis() {}
  78. ~AudioStreamPlaybackOggVorbis();
  79. };
  80. class AudioStreamOggVorbis : public AudioStream {
  81. GDCLASS(AudioStreamOggVorbis, AudioStream);
  82. OBJ_SAVE_TYPE(AudioStream); // Saves derived classes with common type so they can be interchanged.
  83. RES_BASE_EXTENSION("oggvorbisstr");
  84. friend class AudioStreamPlaybackOggVorbis;
  85. int channels = 1;
  86. double length = 0.0;
  87. bool loop = false;
  88. double loop_offset = 0.0;
  89. // Performs a seek to the beginning of the stream, should not be called during playback!
  90. // Also causes allocation and deallocation.
  91. void maybe_update_info();
  92. Ref<OggPacketSequence> packet_sequence;
  93. double bpm = 0;
  94. int beat_count = 0;
  95. int bar_beats = 4;
  96. protected:
  97. static void _bind_methods();
  98. public:
  99. static Ref<AudioStreamOggVorbis> load_from_file(const String &p_path);
  100. static Ref<AudioStreamOggVorbis> load_from_buffer(const Vector<uint8_t> &file_data);
  101. void set_loop(bool p_enable);
  102. virtual bool has_loop() const override;
  103. void set_loop_offset(double p_seconds);
  104. double get_loop_offset() const;
  105. void set_bpm(double p_bpm);
  106. virtual double get_bpm() const override;
  107. void set_beat_count(int p_beat_count);
  108. virtual int get_beat_count() const override;
  109. void set_bar_beats(int p_bar_beats);
  110. virtual int get_bar_beats() const override;
  111. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  112. virtual String get_stream_name() const override;
  113. void set_packet_sequence(Ref<OggPacketSequence> p_packet_sequence);
  114. Ref<OggPacketSequence> get_packet_sequence() const;
  115. virtual double get_length() const override; //if supported, otherwise return 0
  116. virtual bool is_monophonic() const override;
  117. AudioStreamOggVorbis();
  118. virtual ~AudioStreamOggVorbis();
  119. };
  120. #endif // AUDIO_STREAM_OGG_VORBIS_H