audio_stream_ogg_vorbis.h 6.4 KB

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