audio_effect_record.cpp 9.1 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) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. void AudioEffectRecordInstance::_update_buffer() {
  44. //Case: Frames are remaining in the buffer
  45. while (ring_buffer_read_pos < ring_buffer_pos) {
  46. //Read from the buffer into recording_data
  47. _io_store_buffer();
  48. }
  49. }
  50. void AudioEffectRecordInstance::_update(void *userdata) {
  51. AudioEffectRecordInstance *ins = (AudioEffectRecordInstance *)userdata;
  52. ins->_update_buffer();
  53. }
  54. bool AudioEffectRecordInstance::process_silence() const {
  55. return true;
  56. }
  57. void AudioEffectRecordInstance::_io_thread_process() {
  58. thread_active = true;
  59. while (is_recording) {
  60. //Check: The current recording has been requested to stop
  61. if (is_recording && !base->recording_active) {
  62. is_recording = false;
  63. }
  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. 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. //Reset recorder status
  89. ring_buffer_pos = 0;
  90. ring_buffer_read_pos = 0;
  91. //We start a new recording
  92. recording_data.resize(0); //Clear data completely and reset length
  93. is_recording = true;
  94. #ifdef NO_THREADS
  95. AudioServer::get_singleton()->add_update_callback(&AudioEffectRecordInstance::_update, this);
  96. #else
  97. io_thread = Thread::create(_thread_callback, this);
  98. #endif
  99. }
  100. void AudioEffectRecordInstance::finish() {
  101. #ifdef NO_THREADS
  102. AudioServer::get_singleton()->remove_update_callback(&AudioEffectRecordInstance::_update, this);
  103. #else
  104. if (thread_active) {
  105. Thread::wait_to_finish(io_thread);
  106. }
  107. #endif
  108. }
  109. AudioEffectRecordInstance::~AudioEffectRecordInstance() {
  110. finish();
  111. }
  112. Ref<AudioEffectInstance> AudioEffectRecord::instance() {
  113. Ref<AudioEffectRecordInstance> ins;
  114. ins.instance();
  115. ins->base = Ref<AudioEffectRecord>(this);
  116. ins->is_recording = false;
  117. //Re-using the buffer size calculations from audio_effect_delay.cpp
  118. float ring_buffer_max_size = IO_BUFFER_SIZE_MS;
  119. ring_buffer_max_size /= 1000.0; //convert to seconds
  120. ring_buffer_max_size *= AudioServer::get_singleton()->get_mix_rate();
  121. int ringbuff_size = ring_buffer_max_size;
  122. int bits = 0;
  123. while (ringbuff_size > 0) {
  124. bits++;
  125. ringbuff_size /= 2;
  126. }
  127. ringbuff_size = 1 << bits;
  128. ins->ring_buffer_mask = ringbuff_size - 1;
  129. ins->ring_buffer_pos = 0;
  130. ins->ring_buffer.resize(ringbuff_size);
  131. ins->ring_buffer_read_pos = 0;
  132. ensure_thread_stopped();
  133. current_instance = ins;
  134. if (recording_active) {
  135. ins->init();
  136. }
  137. return ins;
  138. }
  139. void AudioEffectRecord::ensure_thread_stopped() {
  140. recording_active = false;
  141. if (current_instance != 0) {
  142. current_instance->finish();
  143. }
  144. }
  145. void AudioEffectRecord::set_recording_active(bool p_record) {
  146. if (p_record) {
  147. if (current_instance == 0) {
  148. WARN_PRINTS("Recording should not be set as active before Godot has initialized.");
  149. recording_active = false;
  150. return;
  151. }
  152. ensure_thread_stopped();
  153. current_instance->init();
  154. }
  155. recording_active = p_record;
  156. }
  157. bool AudioEffectRecord::is_recording_active() const {
  158. return recording_active;
  159. }
  160. void AudioEffectRecord::set_format(AudioStreamSample::Format p_format) {
  161. format = p_format;
  162. }
  163. AudioStreamSample::Format AudioEffectRecord::get_format() const {
  164. return format;
  165. }
  166. Ref<AudioStreamSample> AudioEffectRecord::get_recording() const {
  167. AudioStreamSample::Format dst_format = format;
  168. bool stereo = true; //forcing mono is not implemented
  169. PoolVector<uint8_t> dst_data;
  170. if (dst_format == AudioStreamSample::FORMAT_8_BITS) {
  171. int data_size = current_instance->recording_data.size();
  172. dst_data.resize(data_size);
  173. PoolVector<uint8_t>::Write w = dst_data.write();
  174. for (int i = 0; i < data_size; i++) {
  175. int8_t v = CLAMP(current_instance->recording_data[i] * 128, -128, 127);
  176. w[i] = v;
  177. }
  178. } else if (dst_format == AudioStreamSample::FORMAT_16_BITS) {
  179. int data_size = current_instance->recording_data.size();
  180. dst_data.resize(data_size * 2);
  181. PoolVector<uint8_t>::Write w = dst_data.write();
  182. for (int i = 0; i < data_size; i++) {
  183. int16_t v = CLAMP(current_instance->recording_data[i] * 32768, -32768, 32767);
  184. encode_uint16(v, &w[i * 2]);
  185. }
  186. } else if (dst_format == AudioStreamSample::FORMAT_IMA_ADPCM) {
  187. //byte interleave
  188. Vector<float> left;
  189. Vector<float> right;
  190. int tframes = current_instance->recording_data.size() / 2;
  191. left.resize(tframes);
  192. right.resize(tframes);
  193. for (int i = 0; i < tframes; i++) {
  194. left.set(i, current_instance->recording_data[i * 2 + 0]);
  195. right.set(i, current_instance->recording_data[i * 2 + 1]);
  196. }
  197. PoolVector<uint8_t> bleft;
  198. PoolVector<uint8_t> bright;
  199. ResourceImporterWAV::_compress_ima_adpcm(left, bleft);
  200. ResourceImporterWAV::_compress_ima_adpcm(right, bright);
  201. int dl = bleft.size();
  202. dst_data.resize(dl * 2);
  203. PoolVector<uint8_t>::Write w = dst_data.write();
  204. PoolVector<uint8_t>::Read rl = bleft.read();
  205. PoolVector<uint8_t>::Read rr = bright.read();
  206. for (int i = 0; i < dl; i++) {
  207. w[i * 2 + 0] = rl[i];
  208. w[i * 2 + 1] = rr[i];
  209. }
  210. } else {
  211. ERR_PRINT("Format not implemented.");
  212. }
  213. Ref<AudioStreamSample> sample;
  214. sample.instance();
  215. sample->set_data(dst_data);
  216. sample->set_format(dst_format);
  217. sample->set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
  218. sample->set_loop_mode(AudioStreamSample::LOOP_DISABLED);
  219. sample->set_loop_begin(0);
  220. sample->set_loop_end(0);
  221. sample->set_stereo(stereo);
  222. return sample;
  223. }
  224. void AudioEffectRecord::_bind_methods() {
  225. ClassDB::bind_method(D_METHOD("set_recording_active", "record"), &AudioEffectRecord::set_recording_active);
  226. ClassDB::bind_method(D_METHOD("is_recording_active"), &AudioEffectRecord::is_recording_active);
  227. ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioEffectRecord::set_format);
  228. ClassDB::bind_method(D_METHOD("get_format"), &AudioEffectRecord::get_format);
  229. ClassDB::bind_method(D_METHOD("get_recording"), &AudioEffectRecord::get_recording);
  230. ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA-ADPCM"), "set_format", "get_format");
  231. }
  232. AudioEffectRecord::AudioEffectRecord() {
  233. format = AudioStreamSample::FORMAT_16_BITS;
  234. }