audio_stream_opus.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*************************************************************************/
  2. /* audio_stream_opus.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_OPUS_H
  31. #define AUDIO_STREAM_OPUS_H
  32. #include "core/io/resource_loader.h"
  33. #include "core/os/file_access.h"
  34. #include "scene/resources/audio_stream.h"
  35. #include <opus/opusfile.h>
  36. /**
  37. @author George Marques <george@gmarqu.es>
  38. */
  39. class AudioStreamPlaybackOpus : public AudioStreamPlayback {
  40. GDCLASS(AudioStreamPlaybackOpus, AudioStreamPlayback)
  41. enum {
  42. MIN_MIX = 1024
  43. };
  44. FileAccess *f;
  45. OpusFileCallbacks _op_callbacks;
  46. float length;
  47. static int _op_read_func(void *_stream, unsigned char *_ptr, int _nbytes);
  48. static int _op_seek_func(void *_stream, opus_int64 _offset, int _whence);
  49. static int _op_close_func(void *_stream);
  50. static opus_int64 _op_tell_func(void *_stream);
  51. static const float osrate;
  52. String file;
  53. int64_t frames_mixed;
  54. bool stream_loaded;
  55. volatile bool playing;
  56. OggOpusFile *opus_file;
  57. int stream_channels;
  58. int current_section;
  59. int pre_skip;
  60. bool paused;
  61. bool loops;
  62. int repeats;
  63. Error _load_stream();
  64. void _clear_stream();
  65. void _close_file();
  66. bool stream_valid;
  67. float loop_restart_time;
  68. public:
  69. Error set_file(const String &p_file);
  70. virtual void play(float p_from = 0);
  71. virtual void stop();
  72. virtual bool is_playing() const { return playing; }
  73. virtual void set_loop_restart_time(float p_time) { loop_restart_time = p_time; }
  74. virtual void set_paused(bool p_paused) { paused = p_paused; }
  75. virtual bool is_paused() const { return paused; }
  76. virtual void set_loop(bool p_enable) { loops = p_enable; }
  77. virtual bool has_loop() const { return loops; }
  78. virtual float get_length() const;
  79. virtual String get_stream_name() const { return ""; }
  80. virtual int get_loop_count() const { return repeats; }
  81. virtual float get_playback_position() const;
  82. virtual void seek(float p_time);
  83. virtual int get_channels() const { return stream_channels; }
  84. virtual int get_mix_rate() const { return osrate; }
  85. virtual int get_minimum_buffer_size() const;
  86. virtual int mix(int16_t *p_buffer, int p_frames);
  87. AudioStreamPlaybackOpus();
  88. ~AudioStreamPlaybackOpus();
  89. };
  90. class AudioStreamOpus : public AudioStream {
  91. GDCLASS(AudioStreamOpus, AudioStream)
  92. String file;
  93. public:
  94. Ref<AudioStreamPlayback> instance_playback() {
  95. Ref<AudioStreamPlaybackOpus> pb = memnew(AudioStreamPlaybackOpus);
  96. pb->set_file(file);
  97. return pb;
  98. }
  99. void set_file(const String &p_file) { file = p_file; }
  100. };
  101. class ResourceFormatLoaderAudioStreamOpus : public ResourceFormatLoader {
  102. GDCLASS(ResourceFormatLoaderAudioStreamOpus, ResourceFormatLoader)
  103. public:
  104. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  105. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  106. virtual bool handles_type(const String &p_type) const;
  107. virtual String get_resource_type(const String &p_path) const;
  108. };
  109. #endif // AUDIO_STREAM_OPUS_H