audio_stream_preview.cpp 7.7 KB

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