audio_stream_ogg_vorbis.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. bool looping_override = false;
  42. bool looping = false;
  43. int loops = 0;
  44. enum {
  45. FADE_SIZE = 256
  46. };
  47. AudioFrame loop_fade[FADE_SIZE];
  48. int loop_fade_remaining = FADE_SIZE;
  49. vorbis_info info;
  50. vorbis_comment comment;
  51. vorbis_dsp_state dsp_state;
  52. vorbis_block block;
  53. bool info_is_allocated = false;
  54. bool comment_is_allocated = false;
  55. bool dsp_state_is_allocated = false;
  56. bool block_is_allocated = false;
  57. bool ready = false;
  58. bool have_samples_left = false;
  59. bool have_packets_left = false;
  60. friend class AudioStreamOggVorbis;
  61. Ref<OggPacketSequence> vorbis_data;
  62. Ref<OggPacketSequencePlayback> vorbis_data_playback;
  63. Ref<AudioStreamOggVorbis> vorbis_stream;
  64. bool _is_sample = false;
  65. Ref<AudioSamplePlayback> sample_playback;
  66. int _mix_frames(AudioFrame *p_buffer, int p_frames);
  67. int _mix_frames_vorbis(AudioFrame *p_buffer, int p_frames);
  68. // Allocates vorbis data structures. Returns true upon success, false on failure.
  69. bool _alloc_vorbis();
  70. protected:
  71. virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
  72. virtual float get_stream_sampling_rate() override;
  73. public:
  74. virtual void start(double p_from_pos = 0.0) override;
  75. virtual void stop() override;
  76. virtual bool is_playing() const override;
  77. virtual int get_loop_count() const override; //times it looped
  78. virtual double get_playback_position() const override;
  79. virtual void seek(double p_time) override;
  80. virtual void tag_used_streams() override;
  81. virtual void set_parameter(const StringName &p_name, const Variant &p_value) override;
  82. virtual Variant get_parameter(const StringName &p_name) const override;
  83. virtual void set_is_sample(bool p_is_sample) override;
  84. virtual bool get_is_sample() const override;
  85. virtual Ref<AudioSamplePlayback> get_sample_playback() const override;
  86. virtual void set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;
  87. AudioStreamPlaybackOggVorbis() {}
  88. ~AudioStreamPlaybackOggVorbis();
  89. };
  90. class AudioStreamOggVorbis : public AudioStream {
  91. GDCLASS(AudioStreamOggVorbis, AudioStream);
  92. OBJ_SAVE_TYPE(AudioStream); // Saves derived classes with common type so they can be interchanged.
  93. RES_BASE_EXTENSION("oggvorbisstr");
  94. friend class AudioStreamPlaybackOggVorbis;
  95. int channels = 1;
  96. double length = 0.0;
  97. bool loop = false;
  98. double loop_offset = 0.0;
  99. // Performs a seek to the beginning of the stream, should not be called during playback!
  100. // Also causes allocation and deallocation.
  101. void maybe_update_info();
  102. Ref<OggPacketSequence> packet_sequence;
  103. double bpm = 0;
  104. int beat_count = 0;
  105. int bar_beats = 4;
  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> &file_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. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  122. virtual String get_stream_name() const override;
  123. void set_packet_sequence(Ref<OggPacketSequence> p_packet_sequence);
  124. Ref<OggPacketSequence> get_packet_sequence() const;
  125. virtual double get_length() const override; //if supported, otherwise return 0
  126. virtual bool is_monophonic() const override;
  127. virtual void get_parameter_list(List<Parameter> *r_parameters) override;
  128. virtual bool can_be_sampled() const override {
  129. return true;
  130. }
  131. virtual Ref<AudioSample> generate_sample() const override;
  132. AudioStreamOggVorbis();
  133. virtual ~AudioStreamOggVorbis();
  134. };
  135. #endif // AUDIO_STREAM_OGG_VORBIS_H