Turn audio into a shareable video. forked from nypublicradio/audiogram

index.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. var d3 = require("d3"),
  2. $ = require("jquery"),
  3. themes = require("../settings/themes.json"),
  4. preview = require("./preview.js"),
  5. video = require("./video.js"),
  6. audio = require("./audio.js");
  7. preloadImages();
  8. function submitted() {
  9. d3.event.preventDefault();
  10. var theme = preview.theme(),
  11. caption = preview.caption(),
  12. selection = preview.selection(),
  13. file = preview.file();
  14. if (!file) {
  15. d3.select("#row-audio").classed("error", true);
  16. return setClass("error", "No audio file selected.");
  17. }
  18. if (theme.maxDuration && selection.duration > theme.maxDuration) {
  19. return setClass("error", "Your Audiogram must be under " + theme.maxDuration + " seconds.");
  20. }
  21. if (!theme || !theme.width || !theme.height) {
  22. return setClass("error", "No valid theme detected.");
  23. }
  24. video.kill();
  25. audio.pause();
  26. var formData = new FormData();
  27. var settings = $.extend({}, theme, {
  28. caption: caption,
  29. start: selection.start,
  30. end: selection.end
  31. });
  32. delete settings.backgroundImageFile;
  33. formData.append("audio", file);
  34. formData.append("settings", JSON.stringify(settings));
  35. setClass("loading");
  36. d3.select("#loading-message").text("Uploading audio...");
  37. $.ajax({
  38. url: "/submit/",
  39. type: "POST",
  40. data: formData,
  41. contentType: false,
  42. dataType: "json",
  43. cache: false,
  44. processData: false,
  45. success: function(data){
  46. poll(data.id, 0);
  47. },
  48. error: error
  49. });
  50. }
  51. function poll(id) {
  52. setTimeout(function(){
  53. $.ajax({
  54. url: "/status/" + id + "/",
  55. error: error,
  56. dataType: "json",
  57. success: function(result){
  58. if (result && result.status && result.status === "ready" && result.url) {
  59. video.update(result.url, preview.theme().name);
  60. setClass("rendered");
  61. } else {
  62. d3.select("#loading-message").text(statusMessage(result));
  63. poll(id);
  64. }
  65. }
  66. });
  67. }, 2500);
  68. }
  69. function error(msg) {
  70. if (msg.responseText) {
  71. msg = msg.responseText;
  72. }
  73. if (typeof msg !== "string") {
  74. msg = JSON.stringify(msg);
  75. }
  76. d3.select("#loading-message").text("Loading...");
  77. setClass("error", msg);
  78. }
  79. // Once images are downloaded, set up listeners
  80. function initialize(err, themesWithImages) {
  81. // Populate dropdown menu
  82. d3.select("#input-theme")
  83. .on("change", updateTheme)
  84. .selectAll("option")
  85. .data(themesWithImages)
  86. .enter()
  87. .append("option")
  88. .text(function(d){
  89. return d.name;
  90. });
  91. // Get initial theme
  92. d3.select("#input-theme").each(updateTheme);
  93. // Get initial caption (e.g. back button)
  94. d3.select("#input-caption").on("change keyup", updateCaption).each(updateCaption);
  95. // Space bar listener for audio play/pause
  96. d3.select(document).on("keypress", function(){
  97. if (!d3.select("body").classed("rendered") && d3.event.key === " " && !d3.matcher("input, textarea, button, select").call(d3.event.target)) {
  98. audio.toggle();
  99. }
  100. });
  101. // Button listeners
  102. d3.selectAll("#play, #pause").on("click", function(){
  103. d3.event.preventDefault();
  104. audio.toggle();
  105. });
  106. d3.select("#restart").on("click", function(){
  107. d3.event.preventDefault();
  108. audio.restart();
  109. });
  110. // If there's an initial piece of audio (e.g. back button) load it
  111. d3.select("#input-audio").on("change", updateAudioFile).each(updateAudioFile);
  112. d3.select("#return").on("click", function(){
  113. d3.event.preventDefault();
  114. video.kill();
  115. setClass(null);
  116. });
  117. d3.select("#submit").on("click", submitted);
  118. }
  119. function updateAudioFile() {
  120. d3.select("#row-audio").classed("error", false);
  121. audio.pause();
  122. video.kill();
  123. // Skip if empty
  124. if (!this.files || !this.files[0]) {
  125. d3.select("#minimap").classed("hidden", true);
  126. preview.file(null);
  127. setClass(null);
  128. return true;
  129. }
  130. d3.select("#loading-message").text("Analyzing...");
  131. setClass("loading");
  132. preview.loadAudio(this.files[0], function(err){
  133. if (err) {
  134. d3.select("#row-audio").classed("error", true);
  135. setClass("error", "Error decoding audio file");
  136. } else {
  137. setClass(null);
  138. }
  139. d3.selectAll("#minimap, #submit").classed("hidden", !!err);
  140. });
  141. }
  142. function updateCaption() {
  143. preview.caption(this.value);
  144. }
  145. function updateTheme() {
  146. var extended = $.extend({}, themes.default, d3.select(this.options[this.selectedIndex]).datum());
  147. preview.theme(extended);
  148. }
  149. function preloadImages() {
  150. // preload images
  151. var imageQueue = d3.queue();
  152. d3.entries(themes).forEach(function(theme){
  153. if (!theme.value.name) {
  154. theme.value.name = theme.key;
  155. }
  156. if (theme.key !== "default") {
  157. imageQueue.defer(getImage, theme.value);
  158. }
  159. });
  160. imageQueue.awaitAll(initialize);
  161. function getImage(theme,cb) {
  162. if (!theme.backgroundImage) {
  163. return cb(null,theme);
  164. }
  165. theme.backgroundImageFile = new Image();
  166. theme.backgroundImageFile.onload = function(){
  167. return cb(null,theme);
  168. };
  169. theme.backgroundImageFile.onerror = function(e){
  170. console.warn(e);
  171. return cb(null,theme);
  172. };
  173. theme.backgroundImageFile.src = "img/" + theme.backgroundImage;
  174. }
  175. }
  176. function setClass(cl, msg) {
  177. d3.select("body").attr("class", cl || null);
  178. d3.select("#error").text(msg || "");
  179. }
  180. function statusMessage(result) {
  181. switch (result.status) {
  182. case "queued":
  183. return "Waiting for other jobs to finish, #" + (result.position + 1) + " in queue";
  184. case "audio-download":
  185. return "Downloading audio for processing";
  186. case "trim":
  187. return "Trimming audio";
  188. case "duration":
  189. return "Checking duration";
  190. case "waveform":
  191. return "Analyzing waveform";
  192. case "renderer":
  193. return "Initializing renderer";
  194. case "frames":
  195. var msg = "Generating frames";
  196. if (result.numFrames) {
  197. msg += ", " + Math.round(100 * (result.framesComplete || 0) / result.numFrames) + "% complete";
  198. }
  199. return msg;
  200. case "combine":
  201. return "Combining frames with audio";
  202. case "ready":
  203. return "Cleaning up";
  204. default:
  205. return JSON.stringify(result);
  206. }
  207. }