audio_effect_capture.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**************************************************************************/
  2. /* audio_effect_capture.cpp */
  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. #include "audio_effect_capture.h"
  31. bool AudioEffectCapture::can_get_buffer(int p_frames) const {
  32. return buffer.data_left() >= p_frames;
  33. }
  34. PackedVector2Array AudioEffectCapture::get_buffer(int p_frames) {
  35. ERR_FAIL_COND_V(!buffer_initialized, PackedVector2Array());
  36. ERR_FAIL_INDEX_V(p_frames, buffer.size(), PackedVector2Array());
  37. int data_left = buffer.data_left();
  38. if (data_left < p_frames || p_frames == 0) {
  39. return PackedVector2Array();
  40. }
  41. PackedVector2Array ret;
  42. ret.resize(p_frames);
  43. Vector<AudioFrame> streaming_data;
  44. streaming_data.resize(p_frames);
  45. buffer.read(streaming_data.ptrw(), p_frames);
  46. for (int32_t i = 0; i < p_frames; i++) {
  47. ret.write[i] = Vector2(streaming_data[i].left, streaming_data[i].right);
  48. }
  49. return ret;
  50. }
  51. void AudioEffectCapture::clear_buffer() {
  52. const int32_t data_left = buffer.data_left();
  53. buffer.advance_read(data_left);
  54. }
  55. void AudioEffectCapture::_bind_methods() {
  56. ClassDB::bind_method(D_METHOD("can_get_buffer", "frames"), &AudioEffectCapture::can_get_buffer);
  57. ClassDB::bind_method(D_METHOD("get_buffer", "frames"), &AudioEffectCapture::get_buffer);
  58. ClassDB::bind_method(D_METHOD("clear_buffer"), &AudioEffectCapture::clear_buffer);
  59. ClassDB::bind_method(D_METHOD("set_buffer_length", "buffer_length_seconds"), &AudioEffectCapture::set_buffer_length);
  60. ClassDB::bind_method(D_METHOD("get_buffer_length"), &AudioEffectCapture::get_buffer_length);
  61. ClassDB::bind_method(D_METHOD("get_frames_available"), &AudioEffectCapture::get_frames_available);
  62. ClassDB::bind_method(D_METHOD("get_discarded_frames"), &AudioEffectCapture::get_discarded_frames);
  63. ClassDB::bind_method(D_METHOD("get_buffer_length_frames"), &AudioEffectCapture::get_buffer_length_frames);
  64. ClassDB::bind_method(D_METHOD("get_pushed_frames"), &AudioEffectCapture::get_pushed_frames);
  65. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "buffer_length", PROPERTY_HINT_RANGE, "0.01,10,0.01,suffix:s"), "set_buffer_length", "get_buffer_length");
  66. }
  67. Ref<AudioEffectInstance> AudioEffectCapture::instantiate() {
  68. if (!buffer_initialized) {
  69. float target_buffer_size = AudioServer::get_singleton()->get_mix_rate() * buffer_length_seconds;
  70. ERR_FAIL_COND_V(target_buffer_size <= 0 || target_buffer_size >= (1 << 27), Ref<AudioEffectInstance>());
  71. buffer.resize(nearest_shift((int)target_buffer_size));
  72. buffer_initialized = true;
  73. }
  74. clear_buffer();
  75. Ref<AudioEffectCaptureInstance> ins;
  76. ins.instantiate();
  77. ins->base = Ref<AudioEffectCapture>(this);
  78. return ins;
  79. }
  80. void AudioEffectCapture::set_buffer_length(float p_buffer_length_seconds) {
  81. buffer_length_seconds = p_buffer_length_seconds;
  82. }
  83. float AudioEffectCapture::get_buffer_length() {
  84. return buffer_length_seconds;
  85. }
  86. int AudioEffectCapture::get_frames_available() const {
  87. ERR_FAIL_COND_V(!buffer_initialized, 0);
  88. return buffer.data_left();
  89. }
  90. int64_t AudioEffectCapture::get_discarded_frames() const {
  91. return discarded_frames.get();
  92. }
  93. int AudioEffectCapture::get_buffer_length_frames() const {
  94. ERR_FAIL_COND_V(!buffer_initialized, 0);
  95. return buffer.size();
  96. }
  97. int64_t AudioEffectCapture::get_pushed_frames() const {
  98. return pushed_frames.get();
  99. }
  100. void AudioEffectCaptureInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
  101. RingBuffer<AudioFrame> &buffer = base->buffer;
  102. for (int i = 0; i < p_frame_count; i++) {
  103. p_dst_frames[i] = p_src_frames[i];
  104. }
  105. if (buffer.space_left() >= p_frame_count) {
  106. // Add incoming audio frames to the IO ring buffer
  107. int32_t ret = buffer.write(p_src_frames, p_frame_count);
  108. ERR_FAIL_COND_MSG(ret != p_frame_count, "Failed to add data to effect capture ring buffer despite sufficient space.");
  109. base->pushed_frames.add(p_frame_count);
  110. } else {
  111. base->discarded_frames.add(p_frame_count);
  112. }
  113. }
  114. bool AudioEffectCaptureInstance::process_silence() const {
  115. return true;
  116. }