Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ports from es5: Part two #1193

Merged
merged 9 commits into from
Aug 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ npm run doc
```

## Editing documentation
The homepage and the documentation are in the [`gh-pages` branch](https://github.com/katspaugh/wavesurfer.js/tree/gh-pages). Contributions to the documentation are especially welcome.
The homepage and documentation files are maintained in the [`gh-pages` branch](https://github.com/katspaugh/wavesurfer.js/tree/gh-pages). Contributions to the documentation are especially welcome.

## Credits

Expand Down
4 changes: 4 additions & 0 deletions example/zoom/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

<!-- wavesurfer.js -->
<script src="../../dist/wavesurfer.js"></script>
<script src="../../dist/plugin/wavesurfer.timeline.js"></script>
<script src="../../dist/plugin/wavesurfer.regions.js"></script>

<!-- Demo -->
<script src="main.js"></script>
Expand All @@ -35,6 +37,8 @@ <h1 itemprop="name">Zooming Feature Example</h1>
<!-- Here be the waveform -->
</div>

<div id="timeline"></div>

<div class="controls">
<div class="row">
<div class="col-sm-7">
Expand Down
20 changes: 19 additions & 1 deletion example/zoom/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,25 @@ document.addEventListener('DOMContentLoaded', function () {
container: document.querySelector('#waveform'),
waveColor: '#A8DBA8',
progressColor: '#3B8686',
backend: 'MediaElement'
backend: 'MediaElement',
plugins: [
WaveSurfer.regions.create({
regions: [
{
start: 0,
end: 5,
color: 'hsla(400, 100%, 30%, 0.1)'
}, {
start: 10,
end: 100,
color: 'hsla(200, 50%, 70%, 0.1)'
}
]
}),
WaveSurfer.timeline.create({
container: '#timeline'
})
]
});


Expand Down
8 changes: 4 additions & 4 deletions src/drawer.multicanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export default class MultiCanvas extends Drawer {
const gap = Math.max(this.params.pixelRatio, ~~(bar / 2));
const step = bar + gap;

let absmax = 1;
let absmax = 1 / this.params.barHeight;
if (this.params.normalize) {
const max = util.max(peaks);
const min = util.min(peaks);
Expand All @@ -275,7 +275,7 @@ export default class MultiCanvas extends Drawer {
this.fillRect(i + this.halfPixel, halfH - h + offsetY, bar + this.halfPixel, h * 2);
}

});
})(peaks, channelIndex, start, end);
}

/**
Expand Down Expand Up @@ -321,7 +321,7 @@ export default class MultiCanvas extends Drawer {
const offsetY = height * channelIndex || 0;
const halfH = height / 2;

let absmax = 1;
let absmax = 1 / this.params.barHeight;
if (this.params.normalize) {
const max = util.max(peaks);
const min = util.min(peaks);
Expand All @@ -332,7 +332,7 @@ export default class MultiCanvas extends Drawer {

// Always draw a median line
this.fillRect(0, halfH + offsetY - this.halfPixel, this.width, this.halfPixel);
});
})(peaks, channelIndex, start, end);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/mediaelement.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ export default class MediaElement extends WebAudio {
return (this.getCurrentTime() / this.getDuration()) || 0;
}

/**
* Get the audio source playback rate.
*
* @return {number}
*/
getPlaybackRate() {
return this.playbackRate || this.media.playbackRate;
}

/**
* Set the audio source playback rate.
*
Expand Down
3 changes: 3 additions & 0 deletions src/plugin/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export default class TimelinePlugin {

this.canvases = [];

this._onZoom = () => this.render();
this._onScroll = () => {
if (this.wrapper && this.drawer.wrapper) {
this.wrapper.scrollLeft = this.drawer.wrapper.scrollLeft;
Expand All @@ -142,6 +143,7 @@ export default class TimelinePlugin {
this.render();
ws.drawer.wrapper.addEventListener('scroll', this._onScroll);
ws.on('redraw', this._onRedraw);
ws.on('zoom', this._onZoom);
};
}

Expand All @@ -156,6 +158,7 @@ export default class TimelinePlugin {
destroy() {
this.unAll();
this.wavesurfer.un('redraw', this._onRedraw);
this.wavesurfer.un('zoom', this._onZoom);
this.wavesurfer.un('ready', this._onReady);
this.wavesurfer.drawer.wrapper.removeEventListener('scroll', this._onScroll);
if (this.wrapper && this.wrapper.parentNode) {
Expand Down
14 changes: 13 additions & 1 deletion src/wavesurfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import PeakCache from './peakcache';
* @property {string} backend='WebAudio' `'WebAudio'|'MediaElement'` In most cases
* you don't have to set this manually. MediaElement is a fallback for
* unsupported browsers.
* @property {number} barHeight=1 The height of the wave
* @property {boolean} closeAudioContext=false Close and nullify all audio
* contexts when the destroy method is called.
* @property {!string|HTMLElement} container CSS selector or HTML element where
Expand Down Expand Up @@ -172,6 +173,7 @@ export default class WaveSurfer extends util.Observer {
audioRate : 1,
autoCenter : true,
backend : 'WebAudio',
barHeight : 1,
container : null,
cursorColor : '#333',
cursorWidth : 1,
Expand Down Expand Up @@ -348,7 +350,6 @@ export default class WaveSurfer extends util.Observer {
this._onResize = util.debounce(() => {
if (prevWidth != this.drawer.wrapper.clientWidth) {
prevWidth = this.drawer.wrapper.clientWidth;
this.empty();
this.drawBuffer();
}
}, typeof this.params.responsive === 'number' ? this.params.responsive : 100);
Expand Down Expand Up @@ -838,6 +839,17 @@ export default class WaveSurfer extends util.Observer {
return this.isMuted;
}

/**
* Get the list of current set filters as an array.
*
* Filters must be set with setFilters method first
*
* @return {array}
*/
getFilters() {
return this.backend.filters || [];
}

/**
* Toggles `scrollParent` and redraws
*
Expand Down
2 changes: 1 addition & 1 deletion src/webaudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export default class WebAudio extends util.Observer {
// close the audioContext if closeAudioContext option is set to true
if (this.params.closeAudioContext) {
// check if browser supports AudioContext.close()
if (typeof this.ac.close === 'function') {
if (typeof this.ac.close === 'function' && this.ac.state != 'closed') {
this.ac.close();
}
// clear the reference to the audiocontext
Expand Down