|
35 | 35 | #include "core/input/default_controller_mappings.h" |
36 | 36 | #include "core/input/input_map.h" |
37 | 37 | #include "core/os/os.h" |
| 38 | +#include "servers/audio_server.h" |
38 | 39 |
|
39 | 40 | #ifdef DEV_ENABLED |
40 | 41 | #include "core/os/thread.h" |
@@ -170,6 +171,10 @@ void Input::_bind_methods() { |
170 | 171 | ClassDB::bind_method(D_METHOD("is_emulating_mouse_from_touch"), &Input::is_emulating_mouse_from_touch); |
171 | 172 | ClassDB::bind_method(D_METHOD("set_emulate_touch_from_mouse", "enable"), &Input::set_emulate_touch_from_mouse); |
172 | 173 | ClassDB::bind_method(D_METHOD("is_emulating_touch_from_mouse"), &Input::is_emulating_touch_from_mouse); |
| 174 | + ClassDB::bind_method(D_METHOD("start_microphone"), &Input::start_microphone); |
| 175 | + ClassDB::bind_method(D_METHOD("stop_microphone"), &Input::stop_microphone); |
| 176 | + ClassDB::bind_method(D_METHOD("get_microphone_frames_available"), &Input::get_microphone_frames_available); |
| 177 | + ClassDB::bind_method(D_METHOD("get_microphone_buffer", "frames"), &Input::get_microphone_buffer); |
173 | 178 |
|
174 | 179 | ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_mode"), "set_mouse_mode", "get_mouse_mode"); |
175 | 180 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_accumulated_input"), "set_use_accumulated_input", "is_using_accumulated_input"); |
@@ -1837,6 +1842,53 @@ bool Input::is_input_disabled() const { |
1837 | 1842 | return disable_input; |
1838 | 1843 | } |
1839 | 1844 |
|
| 1845 | +Error Input::start_microphone() { |
| 1846 | + if (!GLOBAL_GET("audio/driver/enable_input")) { |
| 1847 | + WARN_PRINT("You must enable the project setting \"audio/driver/enable_input\" to use audio capture."); |
| 1848 | + return FAILED; |
| 1849 | + } |
| 1850 | + |
| 1851 | + microphone_buffer_ofs = 0; |
| 1852 | + return AudioDriver::get_singleton()->input_start(); |
| 1853 | +} |
| 1854 | + |
| 1855 | +Error Input::stop_microphone() { |
| 1856 | + return AudioDriver::get_singleton()->input_stop(); |
| 1857 | +} |
| 1858 | + |
| 1859 | +int Input::get_microphone_frames_available() { |
| 1860 | + unsigned int input_position = AudioDriver::get_singleton()->get_input_position(); |
| 1861 | + if (input_position < microphone_buffer_ofs) { |
| 1862 | + Vector<int32_t> &buf = AudioDriver::get_singleton()->get_input_buffer(); |
| 1863 | + input_position += buf.size(); |
| 1864 | + } |
| 1865 | + return (input_position - microphone_buffer_ofs) / 2; |
| 1866 | +} |
| 1867 | + |
| 1868 | +PackedVector2Array Input::get_microphone_buffer(int p_frames) { |
| 1869 | + PackedVector2Array ret; |
| 1870 | + unsigned int input_position = AudioDriver::get_singleton()->get_input_position(); |
| 1871 | + Vector<int32_t> &buf = AudioDriver::get_singleton()->get_input_buffer(); |
| 1872 | + if (input_position < microphone_buffer_ofs) { |
| 1873 | + input_position += buf.size(); |
| 1874 | + } |
| 1875 | + if ((microphone_buffer_ofs + p_frames * 2 <= input_position) && (p_frames >= 0)) { |
| 1876 | + ret.resize(p_frames); |
| 1877 | + for (int i = 0; i < p_frames; i++) { |
| 1878 | + float l = (buf[microphone_buffer_ofs++] >> 16) / 32768.f; |
| 1879 | + if (microphone_buffer_ofs >= buf.size()) { |
| 1880 | + microphone_buffer_ofs = 0; |
| 1881 | + } |
| 1882 | + float r = (buf[microphone_buffer_ofs++] >> 16) / 32768.f; |
| 1883 | + if (microphone_buffer_ofs >= buf.size()) { |
| 1884 | + microphone_buffer_ofs = 0; |
| 1885 | + } |
| 1886 | + ret.write[i] = Vector2(l, r); |
| 1887 | + } |
| 1888 | + } |
| 1889 | + return ret; |
| 1890 | +} |
| 1891 | + |
1840 | 1892 | Input::Input() { |
1841 | 1893 | singleton = this; |
1842 | 1894 |
|
|
0 commit comments