audio_effect_record.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**************************************************************************/
  2. /* audio_effect_record.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_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. _update_buffer();
  68. if (is_recording) {
  69. //Wait to avoid too much busy-wait
  70. OS::get_singleton()->delay_usec(500);
  71. }
  72. }
  73. }
  74. void AudioEffectRecordInstance::_io_store_buffer() {
  75. int to_read = ring_buffer_pos - ring_buffer_read_pos;
  76. AudioFrame *rb_buf = ring_buffer.ptrw();
  77. while (to_read) {
  78. AudioFrame buffered_frame = rb_buf[ring_buffer_read_pos & ring_buffer_mask];
  79. recording_data.push_back(buffered_frame.left);
  80. recording_data.push_back(buffered_frame.right);
  81. ring_buffer_read_pos++;
  82. to_read--;
  83. }
  84. }
  85. void AudioEffectRecordInstance::_thread_callback(void *_instance) {
  86. AudioEffectRecordInstance *aeri = reinterpret_cast<AudioEffectRecordInstance *>(_instance);
  87. aeri->_io_thread_process();
  88. }
  89. void AudioEffectRecordInstance::init() {
  90. //Reset recorder status
  91. ring_buffer_pos = 0;
  92. ring_buffer_read_pos = 0;
  93. //We start a new recording
  94. recording_data.clear(); //Clear data completely and reset length
  95. is_recording = true;
  96. io_thread.start(_thread_callback, this);
  97. }
  98. void AudioEffectRecordInstance::finish() {
  99. is_recording = false;
  100. if (io_thread.is_started()) {
  101. io_thread.wait_to_finish();
  102. }
  103. }
  104. Ref<AudioEffectInstance> AudioEffectRecord::instantiate() {
  105. Ref<AudioEffectRecordInstance> ins;
  106. ins.instantiate();
  107. ins->is_recording = false;
  108. // Reusing the buffer size calculations from audio_effect_delay.cpp.
  109. float ring_buffer_max_size = IO_BUFFER_SIZE_MS;
  110. ring_buffer_max_size /= 1000.0; //convert to seconds
  111. ring_buffer_max_size *= AudioServer::get_singleton()->get_mix_rate();
  112. int ringbuff_size = ring_buffer_max_size;
  113. int bits = 0;
  114. while (ringbuff_size > 0) {
  115. bits++;
  116. ringbuff_size /= 2;
  117. }
  118. ringbuff_size = 1 << bits;
  119. ins->ring_buffer_mask = ringbuff_size - 1;
  120. ins->ring_buffer_pos = 0;
  121. ins->ring_buffer.resize(ringbuff_size);
  122. ins->ring_buffer_read_pos = 0;
  123. ensure_thread_stopped();
  124. bool is_currently_recording = false;
  125. if (current_instance.is_valid()) {
  126. is_currently_recording = current_instance->is_recording;
  127. }
  128. if (is_currently_recording) {
  129. ins->init();
  130. }
  131. current_instance = ins;
  132. return ins;
  133. }
  134. void AudioEffectRecord::ensure_thread_stopped() {
  135. if (current_instance.is_valid()) {
  136. current_instance->finish();
  137. }
  138. }
  139. void AudioEffectRecord::set_recording_active(bool p_record) {
  140. if (p_record) {
  141. if (current_instance.is_null()) {
  142. WARN_PRINT("Recording should not be set as active before Godot has initialized.");
  143. return;
  144. }
  145. ensure_thread_stopped();
  146. current_instance->init();
  147. } else {
  148. if (current_instance.is_valid()) {
  149. current_instance->is_recording = false;
  150. }
  151. }
  152. }
  153. bool AudioEffectRecord::is_recording_active() const {
  154. if (current_instance.is_null()) {
  155. return false;
  156. } else {
  157. return current_instance->is_recording;
  158. }
  159. }
  160. void AudioEffectRecord::set_format(AudioStreamWAV::Format p_format) {
  161. format = p_format;
  162. }
  163. AudioStreamWAV::Format AudioEffectRecord::get_format() const {
  164. return format;
  165. }
  166. Ref<AudioStreamWAV> AudioEffectRecord::get_recording() const {
  167. AudioStreamWAV::Format dst_format = format;
  168. bool stereo = true; //forcing mono is not implemented
  169. Vector<uint8_t> dst_data;
  170. ERR_FAIL_COND_V(current_instance.is_null(), nullptr);
  171. ERR_FAIL_COND_V(current_instance->recording_data.is_empty(), nullptr);
  172. if (dst_format == AudioStreamWAV::FORMAT_8_BITS) {
  173. int data_size = current_instance->recording_data.size();
  174. dst_data.resize(data_size);
  175. uint8_t *w = dst_data.ptrw();
  176. for (int i = 0; i < data_size; i++) {
  177. int8_t v = CLAMP(current_instance->recording_data[i] * 128, -128, 127);
  178. w[i] = v;
  179. }
  180. } else if (dst_format == AudioStreamWAV::FORMAT_16_BITS) {
  181. int data_size = current_instance->recording_data.size();
  182. dst_data.resize(data_size * 2);
  183. uint8_t *w = dst_data.ptrw();
  184. for (int i = 0; i < data_size; i++) {
  185. int16_t v = CLAMP(current_instance->recording_data[i] * 32768, -32768, 32767);
  186. encode_uint16(v, &w[i * 2]);
  187. }
  188. } else if (dst_format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  189. //byte interleave
  190. Vector<float> left;
  191. Vector<float> right;
  192. int tframes = current_instance->recording_data.size() / 2;
  193. left.resize(tframes);
  194. right.resize(tframes);
  195. for (int i = 0; i < tframes; i++) {
  196. left.set(i, current_instance->recording_data[i * 2 + 0]);
  197. right.set(i, current_instance->recording_data[i * 2 + 1]);
  198. }
  199. Vector<uint8_t> bleft;
  200. Vector<uint8_t> bright;
  201. #ifdef TOOLS_ENABLED
  202. ResourceImporterWAV::_compress_ima_adpcm(left, bleft);
  203. ResourceImporterWAV::_compress_ima_adpcm(right, bright);
  204. #else
  205. ERR_PRINT("AudioEffectRecord cannot do IMA ADPCM compression at runtime.");
  206. #endif
  207. int dl = bleft.size();
  208. dst_data.resize(dl * 2);
  209. uint8_t *w = dst_data.ptrw();
  210. const uint8_t *rl = bleft.ptr();
  211. const uint8_t *rr = bright.ptr();
  212. for (int i = 0; i < dl; i++) {
  213. w[i * 2 + 0] = rl[i];
  214. w[i * 2 + 1] = rr[i];
  215. }
  216. } else {
  217. ERR_PRINT("Format not implemented.");
  218. }
  219. Ref<AudioStreamWAV> sample;
  220. sample.instantiate();
  221. sample->set_data(dst_data);
  222. sample->set_format(dst_format);
  223. sample->set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
  224. sample->set_loop_mode(AudioStreamWAV::LOOP_DISABLED);
  225. sample->set_loop_begin(0);
  226. sample->set_loop_end(0);
  227. sample->set_stereo(stereo);
  228. return sample;
  229. }
  230. void AudioEffectRecord::_bind_methods() {
  231. ClassDB::bind_method(D_METHOD("set_recording_active", "record"), &AudioEffectRecord::set_recording_active);
  232. ClassDB::bind_method(D_METHOD("is_recording_active"), &AudioEffectRecord::is_recording_active);
  233. ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioEffectRecord::set_format);
  234. ClassDB::bind_method(D_METHOD("get_format"), &AudioEffectRecord::get_format);
  235. ClassDB::bind_method(D_METHOD("get_recording"), &AudioEffectRecord::get_recording);
  236. ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA ADPCM,Quite OK Audio"), "set_format", "get_format");
  237. }
  238. AudioEffectRecord::AudioEffectRecord() {
  239. format = AudioStreamWAV::FORMAT_16_BITS;
  240. }
  241. AudioEffectRecord::~AudioEffectRecord() {
  242. ensure_thread_stopped();
  243. }