audio_effect_capture.cpp 6.0 KB

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