audio_stream_preview.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**************************************************************************/
  2. /* audio_stream_preview.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_stream_preview.h"
  31. /////////////////////
  32. float AudioStreamPreview::get_length() const {
  33. return length;
  34. }
  35. float AudioStreamPreview::get_max(float p_time, float p_time_next) const {
  36. if (length == 0) {
  37. return 0;
  38. }
  39. int max = preview.size() / 2;
  40. if (max == 0) {
  41. return 0;
  42. }
  43. int time_from = p_time / length * max;
  44. int time_to = p_time_next / length * max;
  45. time_from = CLAMP(time_from, 0, max - 1);
  46. time_to = CLAMP(time_to, 0, max - 1);
  47. if (time_to <= time_from) {
  48. time_to = time_from + 1;
  49. }
  50. uint8_t vmax = 0;
  51. for (int i = time_from; i < time_to; i++) {
  52. uint8_t v = preview[i * 2 + 1];
  53. if (i == 0 || v > vmax) {
  54. vmax = v;
  55. }
  56. }
  57. return (vmax / 255.0) * 2.0 - 1.0;
  58. }
  59. float AudioStreamPreview::get_min(float p_time, float p_time_next) const {
  60. if (length == 0) {
  61. return 0;
  62. }
  63. int max = preview.size() / 2;
  64. if (max == 0) {
  65. return 0;
  66. }
  67. int time_from = p_time / length * max;
  68. int time_to = p_time_next / length * max;
  69. time_from = CLAMP(time_from, 0, max - 1);
  70. time_to = CLAMP(time_to, 0, max - 1);
  71. if (time_to <= time_from) {
  72. time_to = time_from + 1;
  73. }
  74. uint8_t vmin = 255;
  75. for (int i = time_from; i < time_to; i++) {
  76. uint8_t v = preview[i * 2];
  77. if (i == 0 || v < vmin) {
  78. vmin = v;
  79. }
  80. }
  81. return (vmin / 255.0) * 2.0 - 1.0;
  82. }
  83. AudioStreamPreview::AudioStreamPreview() {
  84. length = 0;
  85. }
  86. ////
  87. void AudioStreamPreviewGenerator::_update_emit(ObjectID p_id) {
  88. emit_signal(SNAME("preview_updated"), p_id);
  89. }
  90. void AudioStreamPreviewGenerator::_preview_thread(void *p_preview) {
  91. Preview *preview = static_cast<Preview *>(p_preview);
  92. float muxbuff_chunk_s = 0.25;
  93. int mixbuff_chunk_frames = AudioServer::get_singleton()->get_mix_rate() * muxbuff_chunk_s;
  94. Vector<AudioFrame> mix_chunk;
  95. mix_chunk.resize(mixbuff_chunk_frames);
  96. int frames_total = AudioServer::get_singleton()->get_mix_rate() * preview->preview->length;
  97. int frames_todo = frames_total;
  98. preview->playback->start();
  99. while (frames_todo) {
  100. int ofs_write = uint64_t(frames_total - frames_todo) * uint64_t(preview->preview->preview.size() / 2) / uint64_t(frames_total);
  101. int to_read = MIN(frames_todo, mixbuff_chunk_frames);
  102. int to_write = uint64_t(to_read) * uint64_t(preview->preview->preview.size() / 2) / uint64_t(frames_total);
  103. to_write = MIN(to_write, (preview->preview->preview.size() / 2) - ofs_write);
  104. preview->playback->mix(mix_chunk.ptrw(), 1.0, to_read);
  105. for (int i = 0; i < to_write; i++) {
  106. float max = -1000;
  107. float min = 1000;
  108. int from = uint64_t(i) * to_read / to_write;
  109. int to = (uint64_t(i) + 1) * to_read / to_write;
  110. to = MIN(to, to_read);
  111. from = MIN(from, to_read - 1);
  112. if (to == from) {
  113. to = from + 1;
  114. }
  115. for (int j = from; j < to; j++) {
  116. max = MAX(max, mix_chunk[j].left);
  117. max = MAX(max, mix_chunk[j].right);
  118. min = MIN(min, mix_chunk[j].left);
  119. min = MIN(min, mix_chunk[j].right);
  120. }
  121. uint8_t pfrom = CLAMP((min * 0.5 + 0.5) * 255, 0, 255);
  122. uint8_t pto = CLAMP((max * 0.5 + 0.5) * 255, 0, 255);
  123. preview->preview->preview.write[(ofs_write + i) * 2 + 0] = pfrom;
  124. preview->preview->preview.write[(ofs_write + i) * 2 + 1] = pto;
  125. }
  126. frames_todo -= to_read;
  127. callable_mp(singleton, &AudioStreamPreviewGenerator::_update_emit).call_deferred(preview->id);
  128. }
  129. preview->preview->version++;
  130. preview->playback->stop();
  131. preview->generating.clear();
  132. }
  133. Ref<AudioStreamPreview> AudioStreamPreviewGenerator::generate_preview(const Ref<AudioStream> &p_stream) {
  134. ERR_FAIL_COND_V(p_stream.is_null(), Ref<AudioStreamPreview>());
  135. if (previews.has(p_stream->get_instance_id())) {
  136. return previews[p_stream->get_instance_id()].preview;
  137. }
  138. //no preview exists
  139. previews[p_stream->get_instance_id()] = Preview();
  140. Preview *preview = &previews[p_stream->get_instance_id()];
  141. preview->base_stream = p_stream;
  142. preview->playback = preview->base_stream->instantiate_playback();
  143. preview->generating.set();
  144. preview->id = p_stream->get_instance_id();
  145. float len_s = preview->base_stream->get_length();
  146. if (len_s == 0) {
  147. len_s = 60 * 5; //five minutes
  148. }
  149. int frames = AudioServer::get_singleton()->get_mix_rate() * len_s;
  150. Vector<uint8_t> maxmin;
  151. int pw = frames / 20;
  152. maxmin.resize(pw * 2);
  153. {
  154. uint8_t *ptr = maxmin.ptrw();
  155. for (int i = 0; i < pw * 2; i++) {
  156. ptr[i] = 127;
  157. }
  158. }
  159. preview->preview.instantiate();
  160. preview->preview->preview = maxmin;
  161. preview->preview->length = len_s;
  162. if (preview->playback.is_valid()) {
  163. preview->thread = memnew(Thread);
  164. preview->thread->set_name("AudioStreamPreviewGenerator");
  165. preview->thread->start(_preview_thread, preview);
  166. }
  167. return preview->preview;
  168. }
  169. void AudioStreamPreviewGenerator::_bind_methods() {
  170. ClassDB::bind_method(D_METHOD("generate_preview", "stream"), &AudioStreamPreviewGenerator::generate_preview);
  171. ADD_SIGNAL(MethodInfo("preview_updated", PropertyInfo(Variant::INT, "obj_id")));
  172. }
  173. AudioStreamPreviewGenerator *AudioStreamPreviewGenerator::singleton = nullptr;
  174. void AudioStreamPreviewGenerator::_notification(int p_what) {
  175. switch (p_what) {
  176. case NOTIFICATION_PROCESS: {
  177. List<ObjectID> to_erase;
  178. for (KeyValue<ObjectID, Preview> &E : previews) {
  179. if (!E.value.generating.is_set()) {
  180. if (E.value.thread) {
  181. E.value.thread->wait_to_finish();
  182. memdelete(E.value.thread);
  183. E.value.thread = nullptr;
  184. }
  185. if (!ObjectDB::get_instance(E.key)) { //no longer in use, get rid of preview
  186. to_erase.push_back(E.key);
  187. }
  188. }
  189. }
  190. while (to_erase.front()) {
  191. previews.erase(to_erase.front()->get());
  192. to_erase.pop_front();
  193. }
  194. } break;
  195. }
  196. }
  197. AudioStreamPreviewGenerator::AudioStreamPreviewGenerator() {
  198. singleton = this;
  199. set_process(true);
  200. }