audio_effect_record.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. 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 = (AudioEffectRecordInstance *)userdata;
  56. ins->_update_buffer();
  57. }
  58. bool AudioEffectRecordInstance::process_silence() const {
  59. return true;
  60. }
  61. void AudioEffectRecordInstance::_io_thread_process() {
  62. thread_active = true;
  63. while (is_recording) {
  64. //Check: The current recording has been requested to stop
  65. if (!base->recording_active) {
  66. is_recording = false;
  67. }
  68. _update_buffer();
  69. if (is_recording) {
  70. //Wait to avoid too much busy-wait
  71. OS::get_singleton()->delay_usec(500);
  72. }
  73. }
  74. thread_active = false;
  75. }
  76. void AudioEffectRecordInstance::_io_store_buffer() {
  77. int to_read = ring_buffer_pos - ring_buffer_read_pos;
  78. AudioFrame *rb_buf = ring_buffer.ptrw();
  79. while (to_read) {
  80. AudioFrame buffered_frame = rb_buf[ring_buffer_read_pos & ring_buffer_mask];
  81. recording_data.push_back(buffered_frame.l);
  82. recording_data.push_back(buffered_frame.r);
  83. ring_buffer_read_pos++;
  84. to_read--;
  85. }
  86. }
  87. void AudioEffectRecordInstance::_thread_callback(void *_instance) {
  88. AudioEffectRecordInstance *aeri = reinterpret_cast<AudioEffectRecordInstance *>(_instance);
  89. aeri->_io_thread_process();
  90. }
  91. void AudioEffectRecordInstance::init() {
  92. //Reset recorder status
  93. ring_buffer_pos = 0;
  94. ring_buffer_read_pos = 0;
  95. //We start a new recording
  96. recording_data.resize(0); //Clear data completely and reset length
  97. is_recording = true;
  98. #ifdef NO_THREADS
  99. AudioServer::get_singleton()->add_update_callback(&AudioEffectRecordInstance::_update, this);
  100. #else
  101. io_thread = Thread::create(_thread_callback, this);
  102. #endif
  103. }
  104. void AudioEffectRecordInstance::finish() {
  105. #ifdef NO_THREADS
  106. AudioServer::get_singleton()->remove_update_callback(&AudioEffectRecordInstance::_update, this);
  107. #else
  108. if (thread_active) {
  109. Thread::wait_to_finish(io_thread);
  110. }
  111. #endif
  112. }
  113. AudioEffectRecordInstance::~AudioEffectRecordInstance() {
  114. finish();
  115. }
  116. Ref<AudioEffectInstance> AudioEffectRecord::instance() {
  117. Ref<AudioEffectRecordInstance> ins;
  118. ins.instance();
  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 != 0) {
  146. current_instance->finish();
  147. }
  148. }
  149. void AudioEffectRecord::set_recording_active(bool p_record) {
  150. if (p_record) {
  151. if (current_instance == 0) {
  152. WARN_PRINTS("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(AudioStreamSample::Format p_format) {
  167. format = p_format;
  168. }
  169. AudioStreamSample::Format AudioEffectRecord::get_format() const {
  170. return format;
  171. }
  172. Ref<AudioStreamSample> AudioEffectRecord::get_recording() const {
  173. AudioStreamSample::Format dst_format = format;
  174. bool stereo = true; //forcing mono is not implemented
  175. PoolVector<uint8_t> dst_data;
  176. ERR_FAIL_COND_V(current_instance.is_null(), NULL);
  177. ERR_FAIL_COND_V(current_instance->recording_data.size() == 0, NULL);
  178. if (dst_format == AudioStreamSample::FORMAT_8_BITS) {
  179. int data_size = current_instance->recording_data.size();
  180. dst_data.resize(data_size);
  181. PoolVector<uint8_t>::Write w = dst_data.write();
  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 == AudioStreamSample::FORMAT_16_BITS) {
  187. int data_size = current_instance->recording_data.size();
  188. dst_data.resize(data_size * 2);
  189. PoolVector<uint8_t>::Write w = dst_data.write();
  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 == AudioStreamSample::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. PoolVector<uint8_t> bleft;
  206. PoolVector<uint8_t> bright;
  207. ResourceImporterWAV::_compress_ima_adpcm(left, bleft);
  208. ResourceImporterWAV::_compress_ima_adpcm(right, bright);
  209. int dl = bleft.size();
  210. dst_data.resize(dl * 2);
  211. PoolVector<uint8_t>::Write w = dst_data.write();
  212. PoolVector<uint8_t>::Read rl = bleft.read();
  213. PoolVector<uint8_t>::Read rr = bright.read();
  214. for (int i = 0; i < dl; i++) {
  215. w[i * 2 + 0] = rl[i];
  216. w[i * 2 + 1] = rr[i];
  217. }
  218. } else {
  219. ERR_PRINT("Format not implemented.");
  220. }
  221. Ref<AudioStreamSample> sample;
  222. sample.instance();
  223. sample->set_data(dst_data);
  224. sample->set_format(dst_format);
  225. sample->set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
  226. sample->set_loop_mode(AudioStreamSample::LOOP_DISABLED);
  227. sample->set_loop_begin(0);
  228. sample->set_loop_end(0);
  229. sample->set_stereo(stereo);
  230. return sample;
  231. }
  232. void AudioEffectRecord::_bind_methods() {
  233. ClassDB::bind_method(D_METHOD("set_recording_active", "record"), &AudioEffectRecord::set_recording_active);
  234. ClassDB::bind_method(D_METHOD("is_recording_active"), &AudioEffectRecord::is_recording_active);
  235. ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioEffectRecord::set_format);
  236. ClassDB::bind_method(D_METHOD("get_format"), &AudioEffectRecord::get_format);
  237. ClassDB::bind_method(D_METHOD("get_recording"), &AudioEffectRecord::get_recording);
  238. ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA-ADPCM"), "set_format", "get_format");
  239. }
  240. AudioEffectRecord::AudioEffectRecord() {
  241. format = AudioStreamSample::FORMAT_16_BITS;
  242. recording_active = false;
  243. }