audio_effect_record.cpp 9.3 KB

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