audio_effect_record.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. return;
  34. }
  35. //Add incoming audio frames to the IO ring buffer
  36. const AudioFrame *src = p_src_frames;
  37. AudioFrame *rb_buf = ring_buffer.ptrw();
  38. for (int i = 0; i < p_frame_count; i++) {
  39. rb_buf[ring_buffer_pos & ring_buffer_mask] = src[i];
  40. ring_buffer_pos++;
  41. }
  42. }
  43. bool AudioEffectRecordInstance::process_silence() {
  44. return true;
  45. }
  46. void AudioEffectRecordInstance::_io_thread_process() {
  47. //Reset recorder status
  48. thread_active = true;
  49. ring_buffer_pos = 0;
  50. ring_buffer_read_pos = 0;
  51. //We start a new recording
  52. recording_data.resize(0); //Clear data completely and reset length
  53. is_recording = true;
  54. while (is_recording) {
  55. //Check: The current recording has been requested to stop
  56. if (is_recording && !base->recording_active) {
  57. is_recording = false;
  58. }
  59. //Case: Frames are remaining in the buffer
  60. if (ring_buffer_read_pos < ring_buffer_pos) {
  61. //Read from the buffer into recording_data
  62. _io_store_buffer();
  63. }
  64. //Case: The buffer is empty
  65. else if (is_recording) {
  66. //Wait to avoid too much busy-wait
  67. OS::get_singleton()->delay_usec(500);
  68. }
  69. }
  70. thread_active = false;
  71. }
  72. void AudioEffectRecordInstance::_io_store_buffer() {
  73. int to_read = ring_buffer_pos - ring_buffer_read_pos;
  74. AudioFrame *rb_buf = ring_buffer.ptrw();
  75. while (to_read) {
  76. AudioFrame buffered_frame = rb_buf[ring_buffer_read_pos & ring_buffer_mask];
  77. recording_data.push_back(buffered_frame.l);
  78. recording_data.push_back(buffered_frame.r);
  79. ring_buffer_read_pos++;
  80. to_read--;
  81. }
  82. }
  83. void AudioEffectRecordInstance::_thread_callback(void *_instance) {
  84. AudioEffectRecordInstance *aeri = reinterpret_cast<AudioEffectRecordInstance *>(_instance);
  85. aeri->_io_thread_process();
  86. }
  87. void AudioEffectRecordInstance::init() {
  88. io_thread = Thread::create(_thread_callback, this);
  89. }
  90. Ref<AudioEffectInstance> AudioEffectRecord::instance() {
  91. Ref<AudioEffectRecordInstance> ins;
  92. ins.instance();
  93. ins->base = Ref<AudioEffectRecord>(this);
  94. ins->is_recording = false;
  95. //Re-using the buffer size calculations from audio_effect_delay.cpp
  96. float ring_buffer_max_size = IO_BUFFER_SIZE_MS;
  97. ring_buffer_max_size /= 1000.0; //convert to seconds
  98. ring_buffer_max_size *= AudioServer::get_singleton()->get_mix_rate();
  99. int ringbuff_size = ring_buffer_max_size;
  100. int bits = 0;
  101. while (ringbuff_size > 0) {
  102. bits++;
  103. ringbuff_size /= 2;
  104. }
  105. ringbuff_size = 1 << bits;
  106. ins->ring_buffer_mask = ringbuff_size - 1;
  107. ins->ring_buffer_pos = 0;
  108. ins->ring_buffer.resize(ringbuff_size);
  109. ins->ring_buffer_read_pos = 0;
  110. ensure_thread_stopped();
  111. current_instance = ins;
  112. if (recording_active) {
  113. ins->init();
  114. }
  115. return ins;
  116. }
  117. void AudioEffectRecord::ensure_thread_stopped() {
  118. recording_active = false;
  119. if (current_instance != 0 && current_instance->thread_active) {
  120. Thread::wait_to_finish(current_instance->io_thread);
  121. }
  122. }
  123. void AudioEffectRecord::set_recording_active(bool p_record) {
  124. if (p_record) {
  125. if (current_instance == 0) {
  126. WARN_PRINTS("Recording should not be set as active before Godot has initialized.");
  127. recording_active = false;
  128. return;
  129. }
  130. ensure_thread_stopped();
  131. current_instance->init();
  132. }
  133. recording_active = p_record;
  134. }
  135. bool AudioEffectRecord::is_recording_active() const {
  136. return recording_active;
  137. }
  138. void AudioEffectRecord::set_format(AudioStreamSample::Format p_format) {
  139. format = p_format;
  140. }
  141. AudioStreamSample::Format AudioEffectRecord::get_format() const {
  142. return format;
  143. }
  144. Ref<AudioStreamSample> AudioEffectRecord::get_recording() const {
  145. AudioStreamSample::Format dst_format = format;
  146. bool stereo = true; //forcing mono is not implemented
  147. PoolVector<uint8_t> dst_data;
  148. if (dst_format == AudioStreamSample::FORMAT_8_BITS) {
  149. int data_size = current_instance->recording_data.size();
  150. dst_data.resize(data_size);
  151. PoolVector<uint8_t>::Write w = dst_data.write();
  152. for (int i = 0; i < data_size; i++) {
  153. int8_t v = CLAMP(current_instance->recording_data[i] * 128, -128, 127);
  154. w[i] = v;
  155. }
  156. } else if (dst_format == AudioStreamSample::FORMAT_16_BITS) {
  157. int data_size = current_instance->recording_data.size();
  158. dst_data.resize(data_size * 2);
  159. PoolVector<uint8_t>::Write w = dst_data.write();
  160. for (int i = 0; i < data_size; i++) {
  161. int16_t v = CLAMP(current_instance->recording_data[i] * 32768, -32768, 32767);
  162. encode_uint16(v, &w[i * 2]);
  163. }
  164. } else if (dst_format == AudioStreamSample::FORMAT_IMA_ADPCM) {
  165. //byte interleave
  166. Vector<float> left;
  167. Vector<float> right;
  168. int tframes = current_instance->recording_data.size() / 2;
  169. left.resize(tframes);
  170. right.resize(tframes);
  171. for (int i = 0; i < tframes; i++) {
  172. left.set(i, current_instance->recording_data[i * 2 + 0]);
  173. right.set(i, current_instance->recording_data[i * 2 + 1]);
  174. }
  175. PoolVector<uint8_t> bleft;
  176. PoolVector<uint8_t> bright;
  177. ResourceImporterWAV::_compress_ima_adpcm(left, bleft);
  178. ResourceImporterWAV::_compress_ima_adpcm(right, bright);
  179. int dl = bleft.size();
  180. dst_data.resize(dl * 2);
  181. PoolVector<uint8_t>::Write w = dst_data.write();
  182. PoolVector<uint8_t>::Read rl = bleft.read();
  183. PoolVector<uint8_t>::Read rr = bright.read();
  184. for (int i = 0; i < dl; i++) {
  185. w[i * 2 + 0] = rl[i];
  186. w[i * 2 + 1] = rr[i];
  187. }
  188. } else {
  189. ERR_PRINT("Format not implemented.");
  190. }
  191. Ref<AudioStreamSample> sample;
  192. sample.instance();
  193. sample->set_data(dst_data);
  194. sample->set_format(dst_format);
  195. sample->set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
  196. sample->set_loop_mode(AudioStreamSample::LOOP_DISABLED);
  197. sample->set_loop_begin(0);
  198. sample->set_loop_end(0);
  199. sample->set_stereo(stereo);
  200. return sample;
  201. }
  202. void AudioEffectRecord::_bind_methods() {
  203. ClassDB::bind_method(D_METHOD("set_recording_active", "record"), &AudioEffectRecord::set_recording_active);
  204. ClassDB::bind_method(D_METHOD("is_recording_active"), &AudioEffectRecord::is_recording_active);
  205. ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioEffectRecord::set_format);
  206. ClassDB::bind_method(D_METHOD("get_format"), &AudioEffectRecord::get_format);
  207. ClassDB::bind_method(D_METHOD("get_recording"), &AudioEffectRecord::get_recording);
  208. ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA-ADPCM"), "set_format", "get_format");
  209. }
  210. AudioEffectRecord::AudioEffectRecord() {
  211. format = AudioStreamSample::FORMAT_16_BITS;
  212. }