├── README.md ├── example ├── data │ ├── annotations.json │ └── drum-loop.wav ├── index.html └── js │ ├── drumLoop.wav │ ├── jquery-1.11.2.js │ └── waves.min.js └── jquery-waveform-visualizer.js /README.md: -------------------------------------------------------------------------------- 1 | jQuery waveform visualizer 2 | =================================================== 3 | 4 | _A simple jQuery plugin build around [`waves.js`](https://github.com/Ircam-RnD/waves), to visualize waveform and segments along with an HTML5 audio player._ 5 | 6 | __warning__: this plugin relies on the `webAudioAPI` (IE not supported) 7 | 8 | ## Install 9 | 10 | first load `jquery`, `waves.js` and the plugin files in your page, before closing the `body` tag is considered as a good practice. 11 | 12 | ```html 13 | 14 | 15 | 16 | ``` 17 | 18 | ## Plugin initialization 19 | 20 | In order to display the visualization, you need to insert the following tag: 21 | 22 | ```html 23 |
24 | ``` 25 | 26 | - `data-audio` _(mandatory)_ 27 | value must be set to the path to your audio file 28 | _for cross browser compatibility, use a `.wav` file, multiple sources are not yet supported_ 29 | - `data-annotations` _(optionnal)_ 30 | attribute value must be the path to your annotation data file 31 | 32 | The data file is used to configure the segments on the visualization and must follow this convention: 33 | 34 | ```javascript 35 | [ 36 | { 37 | "start": 1.2, // in seconds 38 | "duration": 1 // in seconds 39 | }, { 40 | // ... 41 | } 42 | ] 43 | ``` 44 | 45 | You can also set the size of the module in css (defaults: `width: 100%, height: 200px`): 46 | 47 | ```css 48 | #timeline { 49 | width: 800px; 50 | height: 240px; 51 | } 52 | ``` 53 | 54 | Finally, initialize the plugin: 55 | 56 | ```javascript 57 | $(document).ready(function() { 58 | $('#timeline').waveformVisualizer(); 59 | }); 60 | ``` 61 | 62 | ## Configuration options 63 | 64 | Here are the default values: 65 | 66 | ```javascript 67 | var defaults = { 68 | waveformColor: '#586e75', 69 | segmentColor: '#cb4b16', 70 | anchorColor: '#657b83', 71 | cursorColor: '#dc322f', 72 | segmentOpacity: 0.3 73 | }; 74 | ``` 75 | 76 | These values can be overriden in the plugin initialization: 77 | 78 | ```javascript 79 | $('#timeline').waveformVisualizer({ 80 | waveformColor: 'steelblue', 81 | // ... 82 | }) 83 | ``` 84 | 85 | ## Example 86 | 87 | To see a live example of the plugin, launch a file server in this directory and go to the `/example` folder 88 | 89 | 90 | -------------------------------------------------------------------------------- /example/data/annotations.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "start": 1.2, 4 | "duration": 1 5 | }, { 6 | "start": 3, 7 | "duration": 1 8 | } 9 | ] 10 | -------------------------------------------------------------------------------- /example/data/drum-loop.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ircam-RnD/jquery-waveform-visualizer/7b564f32d266c76ded34e455ed3cd460f6f9d84c/example/data/drum-loop.wav -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | waves.js - Jquery waveform visualizer 6 | 39 | 40 | 41 |

jQuery waveform visualizer

42 | with annotations 43 |
44 | without annotations 45 |
46 | 47 | 48 | 49 | 50 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /example/js/drumLoop.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ircam-RnD/jquery-waveform-visualizer/7b564f32d266c76ded34e455ed3cd460f6f9d84c/example/js/drumLoop.wav -------------------------------------------------------------------------------- /jquery-waveform-visualizer.js: -------------------------------------------------------------------------------- 1 | (function($, waves, window, document) { 2 | 3 | var pluginName = 'waveformVisualizer'; 4 | 5 | var defaults = { 6 | waveformColor: '#586e75', 7 | segmentColor: '#cb4b16', 8 | anchorColor: '#657b83', 9 | cursorColor: '#dc322f', 10 | segmentInteractions: { selectable: true }, 11 | segmentOpacity: 0.3 12 | }; 13 | 14 | var defaultHeight = 200; 15 | var d3 = waves.ui.timeline.d3; 16 | 17 | function Plugin($el, options) { 18 | this.config = $.extend({}, defaults, options); 19 | this.$el = $el; 20 | this.$audio = null; 21 | this.$player = null; 22 | this.$axis = null; 23 | this.$graph = null; 24 | 25 | this.graph = null; // wave timeline 26 | // this.getSize(); 27 | this.width = this.$el.width(); 28 | this.height = this.$el.height() || defaultHeight; 29 | this.audioHeight = null; 30 | this.axisHeight = 24; 31 | this.graphHeight = null; 32 | 33 | this.$el.height(this.height); 34 | // get data and audio 35 | this.audioPath = this.$el.attr('data-audio'); 36 | this.annotationsPath = this.$el.attr('data-annotations'); 37 | this.audioBuffer = null; 38 | this.annotations = null; 39 | 40 | // load audio file and annotation file 41 | var that = this; 42 | var audioBufferLoader = new waves.loaders.AudioBufferLoader(); 43 | 44 | var promises = [audioBufferLoader.load(this.audioPath)]; 45 | if (this.annotationsPath) { 46 | promises.push($.getJSON(this.annotationsPath)); 47 | } 48 | 49 | $.when.apply($, promises).done(function(audioPromise, annotationsRes) { 50 | that.annotations = annotationsRes ? annotationsRes[0] : undefined; 51 | 52 | audioPromise.then(function(audioBuffer) { 53 | try { 54 | that.audioBuffer = audioBuffer; 55 | that.initialize(); 56 | } catch (err) { 57 | console.log(err.stack); 58 | } 59 | }, function(err) { conole.log(err); }); 60 | }); 61 | } 62 | 63 | // ----------------------------------- 64 | // create DOM 65 | // ----------------------------------- 66 | Plugin.prototype.initialize = function() { 67 | var parts = this.audioPath.split('.'); 68 | var extention = parts[parts.length - 1]; 69 | // create audio player 70 | this.$player = $('