audio_stream_preview.cpp 7.5 KB

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