audio_stream_sample.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*************************************************************************/
  2. /* audio_stream_sample.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 AUDIOSTREAMSAMPLE_H
  31. #define AUDIOSTREAMSAMPLE_H
  32. #include "servers/audio/audio_stream.h"
  33. class AudioStreamSample;
  34. class AudioStreamPlaybackSample : public AudioStreamPlayback {
  35. GDCLASS(AudioStreamPlaybackSample, AudioStreamPlayback)
  36. enum {
  37. MIX_FRAC_BITS = 13,
  38. MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
  39. MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
  40. };
  41. struct IMA_ADPCM_State {
  42. int16_t step_index;
  43. int32_t predictor;
  44. /* values at loop point */
  45. int16_t loop_step_index;
  46. int32_t loop_predictor;
  47. int32_t last_nibble;
  48. int32_t loop_pos;
  49. int32_t window_ofs;
  50. } ima_adpcm[2];
  51. int64_t offset;
  52. int sign;
  53. bool active;
  54. friend class AudioStreamSample;
  55. Ref<AudioStreamSample> base;
  56. template <class Depth, bool is_stereo, bool is_ima_adpcm>
  57. void do_resample(const Depth *p_src, AudioFrame *p_dst, int64_t &offset, int32_t &increment, uint32_t amount, IMA_ADPCM_State *ima_adpcm);
  58. public:
  59. virtual void start(float p_from_pos = 0.0);
  60. virtual void stop();
  61. virtual bool is_playing() const;
  62. virtual int get_loop_count() const; //times it looped
  63. virtual float get_playback_position() const;
  64. virtual void seek(float p_time);
  65. virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
  66. AudioStreamPlaybackSample();
  67. };
  68. class AudioStreamSample : public AudioStream {
  69. GDCLASS(AudioStreamSample, AudioStream)
  70. RES_BASE_EXTENSION("sample")
  71. public:
  72. enum Format {
  73. FORMAT_8_BITS,
  74. FORMAT_16_BITS,
  75. FORMAT_IMA_ADPCM
  76. };
  77. enum LoopMode {
  78. LOOP_DISABLED,
  79. LOOP_FORWARD,
  80. LOOP_PING_PONG,
  81. LOOP_BACKWARD
  82. };
  83. private:
  84. friend class AudioStreamPlaybackSample;
  85. enum {
  86. DATA_PAD = 16 //padding for interpolation
  87. };
  88. Format format;
  89. LoopMode loop_mode;
  90. bool stereo;
  91. int loop_begin;
  92. int loop_end;
  93. int mix_rate;
  94. void *data;
  95. uint32_t data_bytes;
  96. protected:
  97. static void _bind_methods();
  98. public:
  99. void set_format(Format p_format);
  100. Format get_format() const;
  101. void set_loop_mode(LoopMode p_loop_mode);
  102. LoopMode get_loop_mode() const;
  103. void set_loop_begin(int p_frame);
  104. int get_loop_begin() const;
  105. void set_loop_end(int p_frame);
  106. int get_loop_end() const;
  107. void set_mix_rate(int p_hz);
  108. int get_mix_rate() const;
  109. void set_stereo(bool p_enable);
  110. bool is_stereo() const;
  111. virtual float get_length() const; //if supported, otherwise return 0
  112. void set_data(const PoolVector<uint8_t> &p_data);
  113. PoolVector<uint8_t> get_data() const;
  114. void save_to_wav(String p_path);
  115. virtual Ref<AudioStreamPlayback> instance_playback();
  116. virtual String get_stream_name() const;
  117. AudioStreamSample();
  118. ~AudioStreamSample();
  119. };
  120. VARIANT_ENUM_CAST(AudioStreamSample::Format)
  121. VARIANT_ENUM_CAST(AudioStreamSample::LoopMode)
  122. #endif // AUDIOSTREAMSample_H