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