audio_stream_speex.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*************************************************************************/
  2. /* audio_stream_speex.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_SPEEX_H
  31. #define AUDIO_STREAM_SPEEX_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 <speex/speex.h>
  37. #include <speex/speex_callbacks.h>
  38. #include <speex/speex_header.h>
  39. #include <speex/speex_stereo.h>
  40. // (akien) Prevents unbundling properly, but it's only for 2.1.x as speex
  41. // is dropped in 3.0+, so don't want to lose too much time on this.
  42. // Needed for speex_free (internal).
  43. #include "thirdparty/speex/os_support.h"
  44. #include <ogg/ogg.h>
  45. class AudioStreamPlaybackSpeex : public AudioStreamPlayback {
  46. OBJ_TYPE(AudioStreamPlaybackSpeex, AudioStreamPlayback);
  47. void *st;
  48. SpeexBits bits;
  49. Vector<uint8_t> data;
  50. int read_ofs;
  51. bool active;
  52. String filename;
  53. int loop_count;
  54. bool loops;
  55. int page_size;
  56. bool playing;
  57. bool packets_available;
  58. void unload();
  59. void reload();
  60. ogg_sync_state oy;
  61. ogg_page og;
  62. ogg_packet op;
  63. ogg_stream_state os;
  64. int nframes;
  65. int frame_size;
  66. int packet_no;
  67. ogg_int64_t page_granule, last_granule;
  68. int skip_samples, page_nb_packets;
  69. int stream_channels;
  70. int stream_srate;
  71. int stream_minbuff_size;
  72. void *process_header(ogg_packet *op, int *frame_size, int *rate, int *nframes, int *channels, int *extra_headers);
  73. static void _bind_methods();
  74. protected:
  75. //virtual bool _can_mix() const;
  76. Dictionary _get_bundled() const;
  77. void _set_bundled(const Dictionary &dict);
  78. public:
  79. void set_data(const Vector<uint8_t> &p_data);
  80. virtual void play(float p_from_pos = 0);
  81. virtual void stop();
  82. virtual bool is_playing() const;
  83. virtual void set_loop(bool p_enable);
  84. virtual bool has_loop() const;
  85. virtual float get_length() const;
  86. virtual String get_stream_name() const;
  87. virtual int get_loop_count() const;
  88. virtual float get_pos() const;
  89. virtual void seek_pos(float p_time);
  90. virtual int get_channels() const { return stream_channels; }
  91. virtual int get_mix_rate() const { return stream_srate; }
  92. virtual int get_minimum_buffer_size() const { return stream_minbuff_size; }
  93. virtual int mix(int16_t *p_bufer, int p_frames);
  94. virtual void set_loop_restart_time(float p_time) {} //no loop restart, ignore
  95. AudioStreamPlaybackSpeex();
  96. ~AudioStreamPlaybackSpeex();
  97. };
  98. class AudioStreamSpeex : public AudioStream {
  99. OBJ_TYPE(AudioStreamSpeex, AudioStream);
  100. Vector<uint8_t> data;
  101. String file;
  102. public:
  103. Ref<AudioStreamPlayback> instance_playback() {
  104. Ref<AudioStreamPlaybackSpeex> pb = memnew(AudioStreamPlaybackSpeex);
  105. pb->set_data(data);
  106. return pb;
  107. }
  108. void set_file(const String &p_file);
  109. };
  110. class ResourceFormatLoaderAudioStreamSpeex : public ResourceFormatLoader {
  111. public:
  112. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  113. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  114. virtual bool handles_type(const String &p_type) const;
  115. virtual String get_resource_type(const String &p_path) const;
  116. };
  117. #endif // AUDIO_STREAM_SPEEX_H