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

index.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. // Skip if empty
  122. if (!this.files || !this.files[0]) {
  123. d3.select("#minimap").classed("hidden", true);
  124. preview.file(null);
  125. setClass(null);
  126. return true;
  127. }
  128. d3.select("#loading-message").text("Analyzing...");
  129. setClass("loading");
  130. preview.loadAudio(this.files[0], function(err){
  131. if (err) {
  132. d3.select("#row-audio").classed("error", true);
  133. setClass("error", "Error decoding audio file");
  134. } else {
  135. setClass(null);
  136. }
  137. d3.selectAll("#minimap, #submit").classed("hidden", !!err);
  138. });
  139. }
  140. function updateCaption() {
  141. preview.caption(this.value);
  142. }
  143. function updateTheme() {
  144. var extended = $.extend({}, themes.default, d3.select(this.options[this.selectedIndex]).datum());
  145. preview.theme(extended);
  146. }
  147. function preloadImages() {
  148. // preload images
  149. var imageQueue = d3.queue();
  150. d3.entries(themes).forEach(function(theme){
  151. if (!theme.value.name) {
  152. theme.value.name = theme.key;
  153. }
  154. if (theme.key !== "default") {
  155. imageQueue.defer(getImage, theme.value);
  156. }
  157. });
  158. imageQueue.awaitAll(initialize);
  159. function getImage(theme,cb) {
  160. if (!theme.backgroundImage) {
  161. return cb(null,theme);
  162. }
  163. theme.backgroundImageFile = new Image();
  164. theme.backgroundImageFile.onload = function(){
  165. return cb(null,theme);
  166. };
  167. theme.backgroundImageFile.onerror = function(e){
  168. console.warn(e);
  169. return cb(null,theme);
  170. };
  171. theme.backgroundImageFile.src = "img/" + theme.backgroundImage;
  172. }
  173. }
  174. function setClass(cl, msg) {
  175. d3.select("body").attr("class", cl || null);
  176. d3.select("#error").text(msg || "");
  177. }
  178. function statusMessage(result) {
  179. switch (result.status) {
  180. case "queued":
  181. return "Waiting for other jobs to finish, #" + (result.position + 1) + " in queue";
  182. case "audio-download":
  183. return "Downloading audio for processing";
  184. case "trim":
  185. return "Trimming audio";
  186. case "duration":
  187. return "Checking duration";
  188. case "waveform":
  189. return "Analyzing waveform";
  190. case "renderer":
  191. return "Initializing renderer";
  192. case "frames":
  193. var msg = "Generating frames";
  194. if (result.numFrames) {
  195. msg += ", " + Math.round(100 * (result.framesComplete || 0) / result.numFrames) + "% complete";
  196. }
  197. return msg;
  198. case "combine":
  199. return "Combining frames with audio";
  200. case "ready":
  201. return "Cleaning up";
  202. default:
  203. return JSON.stringify(result);
  204. }
  205. }