app.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. // Create an instance
  3. var wavesurfer;
  4. // Init & load
  5. document.addEventListener('DOMContentLoaded', function () {
  6. var options = {
  7. container : '#waveform',
  8. waveColor : 'violet',
  9. progressColor : 'purple',
  10. loaderColor : 'purple',
  11. cursorColor : 'navy',
  12. selectionColor: '#d0e9c6',
  13. loopSelection : false,
  14. plugins: [
  15. WaveSurfer.elan.create({
  16. url: 'transcripts/001z.xml',
  17. container: '#annotations',
  18. tiers: {
  19. Text: true,
  20. Comments: true
  21. }
  22. }),
  23. WaveSurfer.regions.create()
  24. ]
  25. };
  26. if (location.search.match('scroll')) {
  27. options.minPxPerSec = 100;
  28. options.scrollParent = true;
  29. }
  30. if (location.search.match('normalize')) {
  31. options.normalize = true;
  32. }
  33. // Init wavesurfer
  34. wavesurfer = WaveSurfer.create(options);
  35. /* Progress bar */
  36. (function () {
  37. var progressDiv = document.querySelector('#progress-bar');
  38. var progressBar = progressDiv.querySelector('.progress-bar');
  39. var showProgress = function (percent) {
  40. progressDiv.style.display = 'block';
  41. progressBar.style.width = percent + '%';
  42. };
  43. var hideProgress = function () {
  44. progressDiv.style.display = 'none';
  45. };
  46. wavesurfer.on('loading', showProgress);
  47. wavesurfer.on('ready', hideProgress);
  48. wavesurfer.on('destroy', hideProgress);
  49. wavesurfer.on('error', hideProgress);
  50. }());
  51. wavesurfer.elan.on('ready', function (data) {
  52. wavesurfer.load('transcripts/' + data.media.url);
  53. });
  54. wavesurfer.elan.on('select', function (start, end) {
  55. wavesurfer.backend.play(start, end);
  56. });
  57. wavesurfer.elan.on('ready', function () {
  58. var classList = wavesurfer.elan.container.querySelector('table').classList;
  59. [ 'table', 'table-striped', 'table-hover' ].forEach(function (cl) {
  60. classList.add(cl);
  61. });
  62. });
  63. var prevAnnotation, prevRow, region;
  64. var onProgress = function (time) {
  65. var annotation = wavesurfer.elan.getRenderedAnnotation(time);
  66. if (prevAnnotation != annotation) {
  67. prevAnnotation = annotation;
  68. region && region.remove();
  69. region = null;
  70. if (annotation) {
  71. // Highlight annotation table row
  72. var row = wavesurfer.elan.getAnnotationNode(annotation);
  73. prevRow && prevRow.classList.remove('success');
  74. prevRow = row;
  75. row.classList.add('success');
  76. var before = row.previousSibling;
  77. if (before) {
  78. wavesurfer.elan.container.scrollTop = before.offsetTop;
  79. }
  80. // Region
  81. region = wavesurfer.addRegion({
  82. start: annotation.start,
  83. end: annotation.end,
  84. resize: false,
  85. color: 'rgba(223, 240, 216, 0.7)'
  86. });
  87. }
  88. }
  89. };
  90. wavesurfer.on('audioprocess', onProgress);
  91. });