audio_stream_wav.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**************************************************************************/
  2. /* audio_stream_wav.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_WAV_H
  31. #define AUDIO_STREAM_WAV_H
  32. #include "servers/audio/audio_stream.h"
  33. class AudioStreamWAV;
  34. class AudioStreamPlaybackWAV : public AudioStreamPlayback {
  35. GDCLASS(AudioStreamPlaybackWAV, 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 = 0;
  43. int32_t predictor = 0;
  44. /* values at loop point */
  45. int16_t loop_step_index = 0;
  46. int32_t loop_predictor = 0;
  47. int32_t last_nibble = 0;
  48. int32_t loop_pos = 0;
  49. int32_t window_ofs = 0;
  50. } ima_adpcm[2];
  51. int64_t offset = 0;
  52. int sign = 1;
  53. bool active = false;
  54. friend class AudioStreamWAV;
  55. Ref<AudioStreamWAV> base;
  56. template <typename Depth, bool is_stereo, bool is_ima_adpcm>
  57. void do_resample(const Depth *p_src, AudioFrame *p_dst, int64_t &p_offset, int32_t &p_increment, uint32_t p_amount, IMA_ADPCM_State *p_ima_adpcm);
  58. public:
  59. virtual void start(double p_from_pos = 0.0) override;
  60. virtual void stop() override;
  61. virtual bool is_playing() const override;
  62. virtual int get_loop_count() const override; //times it looped
  63. virtual double get_playback_position() const override;
  64. virtual void seek(double p_time) override;
  65. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
  66. virtual void tag_used_streams() override;
  67. AudioStreamPlaybackWAV();
  68. };
  69. class AudioStreamWAV : public AudioStream {
  70. GDCLASS(AudioStreamWAV, AudioStream);
  71. RES_BASE_EXTENSION("sample")
  72. public:
  73. enum Format {
  74. FORMAT_8_BITS,
  75. FORMAT_16_BITS,
  76. FORMAT_IMA_ADPCM
  77. };
  78. // Keep the ResourceImporterWAV `edit/loop_mode` enum hint in sync with these options.
  79. enum LoopMode {
  80. LOOP_DISABLED,
  81. LOOP_FORWARD,
  82. LOOP_PINGPONG,
  83. LOOP_BACKWARD
  84. };
  85. private:
  86. friend class AudioStreamPlaybackWAV;
  87. enum {
  88. DATA_PAD = 16 //padding for interpolation
  89. };
  90. Format format = FORMAT_8_BITS;
  91. LoopMode loop_mode = LOOP_DISABLED;
  92. bool stereo = false;
  93. int loop_begin = 0;
  94. int loop_end = 0;
  95. int mix_rate = 44100;
  96. void *data = nullptr;
  97. uint32_t data_bytes = 0;
  98. protected:
  99. static void _bind_methods();
  100. public:
  101. void set_format(Format p_format);
  102. Format get_format() const;
  103. void set_loop_mode(LoopMode p_loop_mode);
  104. LoopMode get_loop_mode() const;
  105. void set_loop_begin(int p_frame);
  106. int get_loop_begin() const;
  107. void set_loop_end(int p_frame);
  108. int get_loop_end() const;
  109. void set_mix_rate(int p_hz);
  110. int get_mix_rate() const;
  111. void set_stereo(bool p_enable);
  112. bool is_stereo() const;
  113. virtual double get_length() const override; //if supported, otherwise return 0
  114. virtual bool is_monophonic() const override;
  115. void set_data(const Vector<uint8_t> &p_data);
  116. Vector<uint8_t> get_data() const;
  117. Error save_to_wav(const String &p_path);
  118. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  119. virtual String get_stream_name() const override;
  120. AudioStreamWAV();
  121. ~AudioStreamWAV();
  122. };
  123. VARIANT_ENUM_CAST(AudioStreamWAV::Format)
  124. VARIANT_ENUM_CAST(AudioStreamWAV::LoopMode)
  125. #endif // AUDIO_STREAM_WAV_H