audio_stream_opus.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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-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_OPUS_H
  31. #define AUDIO_STREAM_OPUS_H
  32. #include "io/resource_loader.h"
  33. #include "os/file_access.h"
  34. #include "scene/resources/audio_stream.h"
  35. #include <opus/opusfile.h>
  36. class AudioStreamPlaybackOpus : public AudioStreamPlayback {
  37. OBJ_TYPE(AudioStreamPlaybackOpus, AudioStreamPlayback)
  38. enum {
  39. MIN_MIX = 1024
  40. };
  41. FileAccess *f;
  42. OpusFileCallbacks _op_callbacks;
  43. float length;
  44. static int _op_read_func(void *_stream, unsigned char *_ptr, int _nbytes);
  45. static int _op_seek_func(void *_stream, opus_int64 _offset, int _whence);
  46. static int _op_close_func(void *_stream);
  47. static opus_int64 _op_tell_func(void *_stream);
  48. static const float osrate;
  49. String file;
  50. int64_t frames_mixed;
  51. bool stream_loaded;
  52. volatile bool playing;
  53. OggOpusFile *opus_file;
  54. int stream_channels;
  55. int current_section;
  56. int pre_skip;
  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 { return playing; }
  70. virtual void set_loop_restart_time(float p_time) { loop_restart_time = p_time; }
  71. virtual void set_paused(bool p_paused) { paused = p_paused; }
  72. virtual bool is_paused() const { return paused; }
  73. virtual void set_loop(bool p_enable) { loops = p_enable; }
  74. virtual bool has_loop() const { return loops; }
  75. virtual float get_length() const;
  76. virtual String get_stream_name() const { return ""; }
  77. virtual int get_loop_count() const { return repeats; }
  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 osrate; }
  82. virtual int get_minimum_buffer_size() const;
  83. virtual int mix(int16_t *p_bufer, int p_frames);
  84. AudioStreamPlaybackOpus();
  85. ~AudioStreamPlaybackOpus();
  86. };
  87. class AudioStreamOpus : public AudioStream {
  88. OBJ_TYPE(AudioStreamOpus, AudioStream)
  89. String file;
  90. public:
  91. Ref<AudioStreamPlayback> instance_playback() {
  92. Ref<AudioStreamPlaybackOpus> pb = memnew(AudioStreamPlaybackOpus);
  93. pb->set_file(file);
  94. return pb;
  95. }
  96. void set_file(const String &p_file) { file = p_file; }
  97. };
  98. class ResourceFormatLoaderAudioStreamOpus : 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_OPUS_H