audio_stream_ogg_vorbis.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*************************************************************************/
  2. /* audio_stream_ogg_vorbis.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef AUDIO_STREAM_OGG_VORBIS_H
  30. #define AUDIO_STREAM_OGG_VORBIS_H
  31. #include "io/resource_loader.h"
  32. #include "os/file_access.h"
  33. #include "os/thread_safe.h"
  34. #include "scene/resources/audio_stream.h"
  35. #include <vorbis/vorbisfile.h>
  36. class AudioStreamPlaybackOGGVorbis : public AudioStreamPlayback {
  37. OBJ_TYPE(AudioStreamPlaybackOGGVorbis,AudioStreamPlayback);
  38. enum {
  39. MIN_MIX=1024
  40. };
  41. FileAccess *f;
  42. ov_callbacks _ov_callbacks;
  43. float length;
  44. static size_t _ov_read_func(void *p_dst,size_t p_data, size_t p_count, void *_f);
  45. static int _ov_seek_func(void *_f,ogg_int64_t offs, int whence);
  46. static int _ov_close_func(void *_f);
  47. static long _ov_tell_func(void *_f);
  48. String file;
  49. int64_t frames_mixed;
  50. bool stream_loaded;
  51. volatile bool playing;
  52. OggVorbis_File vf;
  53. int stream_channels;
  54. int stream_srate;
  55. int current_section;
  56. bool paused;
  57. bool loops;
  58. int repeats;
  59. Error _load_stream();
  60. void _clear_stream();
  61. void _close_file();
  62. bool stream_valid;
  63. float loop_restart_time;
  64. public:
  65. Error set_file(const String& p_file);
  66. virtual void play(float p_from=0);
  67. virtual void stop();
  68. virtual bool is_playing() const;
  69. virtual void set_loop_restart_time(float p_time) { loop_restart_time=p_time; }
  70. virtual void set_paused(bool p_paused);
  71. virtual bool is_paused(bool p_paused) const;
  72. virtual void set_loop(bool p_enable);
  73. virtual bool has_loop() const;
  74. virtual float get_length() const;
  75. virtual String get_stream_name() const;
  76. virtual int get_loop_count() const;
  77. virtual float get_pos() const;
  78. virtual void seek_pos(float p_time);
  79. virtual int get_channels() const { return stream_channels; }
  80. virtual int get_mix_rate() const { return stream_srate; }
  81. virtual int get_minimum_buffer_size() const { return 0; }
  82. virtual int mix(int16_t* p_bufer,int p_frames);
  83. AudioStreamPlaybackOGGVorbis();
  84. ~AudioStreamPlaybackOGGVorbis();
  85. };
  86. class AudioStreamOGGVorbis : public AudioStream {
  87. OBJ_TYPE(AudioStreamOGGVorbis,AudioStream);
  88. String file;
  89. public:
  90. Ref<AudioStreamPlayback> instance_playback() {
  91. Ref<AudioStreamPlaybackOGGVorbis> pb = memnew( AudioStreamPlaybackOGGVorbis );
  92. pb->set_file(file);
  93. return pb;
  94. }
  95. void set_file(const String& p_file) { file=p_file; }
  96. };
  97. class ResourceFormatLoaderAudioStreamOGGVorbis : public ResourceFormatLoader {
  98. public:
  99. virtual RES load(const String &p_path,const String& p_original_path="",Error *r_error=NULL);
  100. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  101. virtual bool handles_type(const String& p_type) const;
  102. virtual String get_resource_type(const String &p_path) const;
  103. };
  104. #endif // AUDIO_STREAM_OGG_H