123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- var d3 = require("d3"),
- audio = require("./audio.js"),
- video = require("./video.js"),
- minimap = require("./minimap.js"),
- sampleWave = require("./sample-wave.js"),
- getRenderer = require("../renderer/"),
- getWaveform = require("./waveform.js");
-
- var context = d3.select("canvas").node().getContext("2d");
-
- var theme,
- caption,
- citation,
- label,
- file,
- selection;
-
- function _file(_) {
- return arguments.length ? (file = _) : file;
- }
-
- function _theme(_) {
- return arguments.length ? (theme = _, redraw()) : theme;
- }
-
- function _caption(_) {
- return arguments.length ? (caption = _, redraw()) : caption;
- }
-
- function _citation(_) {
- return arguments.length ? (citation = _, redraw()) : citation;
- }
-
- function _label(_) {
- return arguments.length ? (label = _, redraw()) : label;
- }
-
- function _selection(_) {
- return arguments.length ? (selection = _) : selection;
- }
-
- minimap.onBrush(function(extent){
-
- var duration = audio.duration();
-
- selection = {
- duration: duration * (extent[1] - extent[0]),
- start: extent[0] ? extent[0] * duration : null,
- end: extent[1] < 1 ? extent[1] * duration : null
- };
-
- d3.select("#duration strong").text(Math.round(10 * selection.duration) / 10)
- .classed("red", theme && theme.maxDuration && theme.maxDuration < selection.duration);
-
- });
-
- // Resize video and preview canvas to maintain aspect ratio
- function resize(width, height) {
-
- var widthFactor = 640 / width,
- heightFactor = 360 / height,
- factor = Math.min(widthFactor, heightFactor);
-
- d3.select("canvas")
- .attr("width", factor * width)
- .attr("height", factor * height);
-
- d3.select("#canvas")
- .style("width", (factor * width) + "px");
-
- d3.select("video")
- .attr("height", widthFactor * height);
-
- d3.select("#video")
- .attr("height", (widthFactor * height) + "px");
-
- context.setTransform(factor, 0, 0, factor, 0, 0);
-
- }
-
- function redraw() {
-
- resize(theme.width, theme.height);
-
- video.kill();
-
- var renderer = getRenderer(theme);
-
- renderer.backgroundImage(theme.backgroundImageFile || null);
-
- renderer.drawFrame(context, {
- caption: caption,
- citation: citation,
- label: label,
- waveform: sampleWave,
- frame: 0
- });
-
- }
-
- function loadAudio(f, cb) {
-
- d3.queue()
- .defer(getWaveform, f)
- .defer(audio.src, f)
- .await(function(err, data){
-
- if (err) {
- return cb(err);
- }
-
- file = f;
- minimap.redraw(data.peaks);
-
- cb(err);
-
- });
-
- }
-
- module.exports = {
- caption: _caption,
- citation: _citation,
- label: _label,
- theme: _theme,
- file: _file,
- selection: _selection,
- loadAudio: loadAudio
- };
|