Debian Codegraph @

servers/audio/effects/audio_effect_capture.cpp

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