audio_effect_record.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*************************************************************************/
  2. /* audio_effect_record.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_record.h"
  31. #ifdef TOOLS_ENABLED
  32. // FIXME: This file shouldn't depend on editor stuff.
  33. #include "editor/import/resource_importer_wav.h"
  34. #endif
  35. void AudioEffectRecordInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
  36. if (!is_recording) {
  37. for (int i = 0; i < p_frame_count; i++) {
  38. p_dst_frames[i] = p_src_frames[i];
  39. }
  40. return;
  41. }
  42. //Add incoming audio frames to the IO ring buffer
  43. const AudioFrame *src = p_src_frames;
  44. AudioFrame *rb_buf = ring_buffer.ptrw();
  45. for (int i = 0; i < p_frame_count; i++) {
  46. p_dst_frames[i] = p_src_frames[i];
  47. rb_buf[ring_buffer_pos & ring_buffer_mask] = src[i];
  48. ring_buffer_pos++;
  49. }
  50. }
  51. void AudioEffectRecordInstance::_update_buffer() {
  52. //Case: Frames are remaining in the buffer
  53. while (ring_buffer_read_pos < ring_buffer_pos) {
  54. //Read from the buffer into recording_data
  55. _io_store_buffer();
  56. }
  57. }
  58. void AudioEffectRecordInstance::_update(void *userdata) {
  59. AudioEffectRecordInstance *ins = static_cast<AudioEffectRecordInstance *>(userdata);
  60. ins->_update_buffer();
  61. }
  62. bool AudioEffectRecordInstance::process_silence() const {
  63. return true;
  64. }
  65. void AudioEffectRecordInstance::_io_thread_process() {
  66. while (is_recording) {
  67. //Check: The current recording has been requested to stop
  68. if (!base->recording_active) {
  69. is_recording = false;
  70. }
  71. _update_buffer();
  72. if (is_recording) {
  73. //Wait to avoid too much busy-wait
  74. OS::get_singleton()->delay_usec(500);
  75. }
  76. }
  77. }
  78. void AudioEffectRecordInstance::_io_store_buffer() {
  79. int to_read = ring_buffer_pos - ring_buffer_read_pos;
  80. AudioFrame *rb_buf = ring_buffer.ptrw();
  81. while (to_read) {
  82. AudioFrame buffered_frame = rb_buf[ring_buffer_read_pos & ring_buffer_mask];
  83. recording_data.push_back(buffered_frame.l);
  84. recording_data.push_back(buffered_frame.r);
  85. ring_buffer_read_pos++;
  86. to_read--;
  87. }
  88. }
  89. void AudioEffectRecordInstance::_thread_callback(void *_instance) {
  90. AudioEffectRecordInstance *aeri = reinterpret_cast<AudioEffectRecordInstance *>(_instance);
  91. aeri->_io_thread_process();
  92. }
  93. void AudioEffectRecordInstance::init() {
  94. //Reset recorder status
  95. ring_buffer_pos = 0;
  96. ring_buffer_read_pos = 0;
  97. //We start a new recording
  98. recording_data.clear(); //Clear data completely and reset length
  99. is_recording = true;
  100. #ifdef NO_THREADS
  101. AudioServer::get_singleton()->add_update_callback(&AudioEffectRecordInstance::_update, this);
  102. #else
  103. io_thread.start(_thread_callback, this);
  104. #endif
  105. }
  106. void AudioEffectRecordInstance::finish() {
  107. #ifdef NO_THREADS
  108. AudioServer::get_singleton()->remove_update_callback(&AudioEffectRecordInstance::_update, this);
  109. #else
  110. io_thread.wait_to_finish();
  111. #endif
  112. }
  113. AudioEffectRecordInstance::~AudioEffectRecordInstance() {
  114. finish();
  115. }
  116. Ref<AudioEffectInstance> AudioEffectRecord::instantiate() {
  117. Ref<AudioEffectRecordInstance> ins;
  118. ins.instantiate();
  119. ins->base = Ref<AudioEffectRecord>(this);
  120. ins->is_recording = false;
  121. //Re-using the buffer size calculations from audio_effect_delay.cpp
  122. float ring_buffer_max_size = IO_BUFFER_SIZE_MS;
  123. ring_buffer_max_size /= 1000.0; //convert to seconds
  124. ring_buffer_max_size *= AudioServer::get_singleton()->get_mix_rate();
  125. int ringbuff_size = ring_buffer_max_size;
  126. int bits = 0;
  127. while (ringbuff_size > 0) {
  128. bits++;
  129. ringbuff_size /= 2;
  130. }
  131. ringbuff_size = 1 << bits;
  132. ins->ring_buffer_mask = ringbuff_size - 1;
  133. ins->ring_buffer_pos = 0;
  134. ins->ring_buffer.resize(ringbuff_size);
  135. ins->ring_buffer_read_pos = 0;
  136. ensure_thread_stopped();
  137. current_instance = ins;
  138. if (recording_active) {
  139. ins->init();
  140. }
  141. return ins;
  142. }
  143. void AudioEffectRecord::ensure_thread_stopped() {
  144. recording_active = false;
  145. if (current_instance != nullptr) {
  146. current_instance->finish();
  147. }
  148. }
  149. void AudioEffectRecord::set_recording_active(bool p_record) {
  150. if (p_record) {
  151. if (current_instance == nullptr) {
  152. WARN_PRINT("Recording should not be set as active before Godot has initialized.");
  153. recording_active = false;
  154. return;
  155. }
  156. ensure_thread_stopped();
  157. recording_active = true;
  158. current_instance->init();
  159. } else {
  160. recording_active = false;
  161. }
  162. }
  163. bool AudioEffectRecord::is_recording_active() const {
  164. return recording_active;
  165. }
  166. void AudioEffectRecord::set_format(AudioStreamWAV::Format p_format) {
  167. format = p_format;
  168. }
  169. AudioStreamWAV::Format AudioEffectRecord::get_format() const {
  170. return format;
  171. }
  172. Ref<AudioStreamWAV> AudioEffectRecord::get_recording() const {
  173. AudioStreamWAV::Format dst_format = format;
  174. bool stereo = true; //forcing mono is not implemented
  175. Vector<uint8_t> dst_data;
  176. ERR_FAIL_COND_V(current_instance.is_null(), nullptr);
  177. ERR_FAIL_COND_V(current_instance->recording_data.size() == 0, nullptr);
  178. if (dst_format == AudioStreamWAV::FORMAT_8_BITS) {
  179. int data_size = current_instance->recording_data.size();
  180. dst_data.resize(data_size);
  181. uint8_t *w = dst_data.ptrw();
  182. for (int i = 0; i < data_size; i++) {
  183. int8_t v = CLAMP(current_instance->recording_data[i] * 128, -128, 127);
  184. w[i] = v;
  185. }
  186. } else if (dst_format == AudioStreamWAV::FORMAT_16_BITS) {
  187. int data_size = current_instance->recording_data.size();
  188. dst_data.resize(data_size * 2);
  189. uint8_t *w = dst_data.ptrw();
  190. for (int i = 0; i < data_size; i++) {
  191. int16_t v = CLAMP(current_instance->recording_data[i] * 32768, -32768, 32767);
  192. encode_uint16(v, &w[i * 2]);
  193. }
  194. } else if (dst_format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  195. //byte interleave
  196. Vector<float> left;
  197. Vector<float> right;
  198. int tframes = current_instance->recording_data.size() / 2;
  199. left.resize(tframes);
  200. right.resize(tframes);
  201. for (int i = 0; i < tframes; i++) {
  202. left.set(i, current_instance->recording_data[i * 2 + 0]);
  203. right.set(i, current_instance->recording_data[i * 2 + 1]);
  204. }
  205. Vector<uint8_t> bleft;
  206. Vector<uint8_t> bright;
  207. #ifdef TOOLS_ENABLED
  208. ResourceImporterWAV::_compress_ima_adpcm(left, bleft);
  209. ResourceImporterWAV::_compress_ima_adpcm(right, bright);
  210. #else
  211. ERR_PRINT("AudioEffectRecord cannot do IMA ADPCM compression at runtime.");
  212. #endif
  213. int dl = bleft.size();
  214. dst_data.resize(dl * 2);
  215. uint8_t *w = dst_data.ptrw();
  216. const uint8_t *rl = bleft.ptr();
  217. const uint8_t *rr = bright.ptr();
  218. for (int i = 0; i < dl; i++) {
  219. w[i * 2 + 0] = rl[i];
  220. w[i * 2 + 1] = rr[i];
  221. }
  222. } else {
  223. ERR_PRINT("Format not implemented.");
  224. }
  225. Ref<AudioStreamWAV> sample;
  226. sample.instantiate();
  227. sample->set_data(dst_data);
  228. sample->set_format(dst_format);
  229. sample->set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
  230. sample->set_loop_mode(AudioStreamWAV::LOOP_DISABLED);
  231. sample->set_loop_begin(0);
  232. sample->set_loop_end(0);
  233. sample->set_stereo(stereo);
  234. return sample;
  235. }
  236. void AudioEffectRecord::_bind_methods() {
  237. ClassDB::bind_method(D_METHOD("set_recording_active", "record"), &AudioEffectRecord::set_recording_active);
  238. ClassDB::bind_method(D_METHOD("is_recording_active"), &AudioEffectRecord::is_recording_active);
  239. ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioEffectRecord::set_format);
  240. ClassDB::bind_method(D_METHOD("get_format"), &AudioEffectRecord::get_format);
  241. ClassDB::bind_method(D_METHOD("get_recording"), &AudioEffectRecord::get_recording);
  242. ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA-ADPCM"), "set_format", "get_format");
  243. }
  244. AudioEffectRecord::AudioEffectRecord() {
  245. format = AudioStreamWAV::FORMAT_16_BITS;
  246. recording_active = false;
  247. }