audio_stream_preview.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. 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("preview_updated", p_id);
  83. }
  84. void AudioStreamPreviewGenerator::_preview_thread(void *p_preview) {
  85. Preview *preview = (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("_update_emit", preview->id);
  122. }
  123. preview->playback->stop();
  124. preview->generating.clear();
  125. }
  126. Ref<AudioStreamPreview> AudioStreamPreviewGenerator::generate_preview(const Ref<AudioStream> &p_stream) {
  127. ERR_FAIL_COND_V(p_stream.is_null(), Ref<AudioStreamPreview>());
  128. if (previews.has(p_stream->get_instance_id())) {
  129. return previews[p_stream->get_instance_id()].preview;
  130. }
  131. //no preview exists
  132. previews[p_stream->get_instance_id()] = Preview();
  133. Preview *preview = &previews[p_stream->get_instance_id()];
  134. preview->base_stream = p_stream;
  135. preview->playback = preview->base_stream->instance_playback();
  136. preview->generating.set();
  137. preview->id = p_stream->get_instance_id();
  138. float len_s = preview->base_stream->get_length();
  139. if (len_s == 0) {
  140. len_s = 60 * 5; //five minutes
  141. }
  142. int frames = AudioServer::get_singleton()->get_mix_rate() * len_s;
  143. Vector<uint8_t> maxmin;
  144. int pw = frames / 20;
  145. maxmin.resize(pw * 2);
  146. {
  147. uint8_t *ptr = maxmin.ptrw();
  148. for (int i = 0; i < pw * 2; i++) {
  149. ptr[i] = 127;
  150. }
  151. }
  152. preview->preview.instance();
  153. preview->preview->preview = maxmin;
  154. preview->preview->length = len_s;
  155. if (preview->playback.is_valid()) {
  156. preview->thread = memnew(Thread);
  157. preview->thread->start(_preview_thread, preview);
  158. }
  159. return preview->preview;
  160. }
  161. void AudioStreamPreviewGenerator::_bind_methods() {
  162. ClassDB::bind_method("_update_emit", &AudioStreamPreviewGenerator::_update_emit);
  163. ClassDB::bind_method(D_METHOD("generate_preview", "stream"), &AudioStreamPreviewGenerator::generate_preview);
  164. ADD_SIGNAL(MethodInfo("preview_updated", PropertyInfo(Variant::INT, "obj_id")));
  165. }
  166. AudioStreamPreviewGenerator *AudioStreamPreviewGenerator::singleton = nullptr;
  167. void AudioStreamPreviewGenerator::_notification(int p_what) {
  168. if (p_what == NOTIFICATION_PROCESS) {
  169. List<ObjectID> to_erase;
  170. for (Map<ObjectID, Preview>::Element *E = previews.front(); E; E = E->next()) {
  171. if (!E->get().generating.is_set()) {
  172. if (E->get().thread) {
  173. E->get().thread->wait_to_finish();
  174. memdelete(E->get().thread);
  175. E->get().thread = nullptr;
  176. }
  177. if (!ObjectDB::get_instance(E->key())) { //no longer in use, get rid of preview
  178. to_erase.push_back(E->key());
  179. }
  180. }
  181. }
  182. while (to_erase.front()) {
  183. previews.erase(to_erase.front()->get());
  184. to_erase.pop_front();
  185. }
  186. }
  187. }
  188. AudioStreamPreviewGenerator::AudioStreamPreviewGenerator() {
  189. singleton = this;
  190. set_process(true);
  191. }