audio_stream_ogg_vorbis.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "io/resource_loader.h"
  33. #include "os/file_access.h"
  34. #include "os/thread_safe.h"
  35. #include "scene/resources/audio_stream.h"
  36. #include <vorbis/vorbisfile.h>
  37. class AudioStreamPlaybackOGGVorbis : public AudioStreamPlayback {
  38. OBJ_TYPE(AudioStreamPlaybackOGGVorbis, AudioStreamPlayback);
  39. enum {
  40. MIN_MIX = 1024
  41. };
  42. FileAccess *f;
  43. ov_callbacks _ov_callbacks;
  44. float length;
  45. static size_t _ov_read_func(void *p_dst, size_t p_data, size_t p_count, void *_f);
  46. static int _ov_seek_func(void *_f, ogg_int64_t offs, int whence);
  47. static int _ov_close_func(void *_f);
  48. static long _ov_tell_func(void *_f);
  49. String file;
  50. int64_t frames_mixed;
  51. bool stream_loaded;
  52. volatile bool playing;
  53. OggVorbis_File vf;
  54. int stream_channels;
  55. int stream_srate;
  56. int current_section;
  57. bool paused;
  58. bool loops;
  59. int repeats;
  60. Error _load_stream();
  61. void _clear_stream();
  62. void _close_file();
  63. bool stream_valid;
  64. float loop_restart_time;
  65. public:
  66. Error set_file(const String &p_file);
  67. virtual void play(float p_from = 0);
  68. virtual void stop();
  69. virtual bool is_playing() const;
  70. virtual void set_loop_restart_time(float p_time) { loop_restart_time = p_time; }
  71. virtual void set_paused(bool p_paused);
  72. virtual bool is_paused(bool p_paused) const;
  73. virtual void set_loop(bool p_enable);
  74. virtual bool has_loop() const;
  75. virtual float get_length() const;
  76. virtual String get_stream_name() const;
  77. virtual int get_loop_count() const;
  78. virtual float get_pos() const;
  79. virtual void seek_pos(float p_time);
  80. virtual int get_channels() const { return stream_channels; }
  81. virtual int get_mix_rate() const { return stream_srate; }
  82. virtual int get_minimum_buffer_size() const { return 0; }
  83. virtual int mix(int16_t *p_bufer, int p_frames);
  84. AudioStreamPlaybackOGGVorbis();
  85. ~AudioStreamPlaybackOGGVorbis();
  86. };
  87. class AudioStreamOGGVorbis : public AudioStream {
  88. OBJ_TYPE(AudioStreamOGGVorbis, AudioStream);
  89. String file;
  90. public:
  91. Ref<AudioStreamPlayback> instance_playback() {
  92. Ref<AudioStreamPlaybackOGGVorbis> pb = memnew(AudioStreamPlaybackOGGVorbis);
  93. pb->set_file(file);
  94. return pb;
  95. }
  96. void set_file(const String &p_file) { file = p_file; }
  97. };
  98. class ResourceFormatLoaderAudioStreamOGGVorbis : public ResourceFormatLoader {
  99. public:
  100. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  101. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  102. virtual bool handles_type(const String &p_type) const;
  103. virtual String get_resource_type(const String &p_path) const;
  104. };
  105. #endif // AUDIO_STREAM_OGG_H