audio_effect_record.cpp 9.3 KB

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