Skip to content

Commit

Permalink
Fixed waveform rescaling
Browse files Browse the repository at this point in the history
The WaveformZoomView _scale value must always match
the scale of the WaveformData object, which can
only be integer.

See #513
  • Loading branch information
chrisn committed Nov 29, 2023
1 parent 78a55d3 commit e66e88b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/waveform-zoomview.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ WaveformZoomView.prototype._syncPlayhead = function(time) {
};

WaveformZoomView.prototype._getScale = function(duration) {
return duration * this._data.sample_rate / this._width;
return Math.floor(duration * this._data.sample_rate / this._width);
};

function isAutoScale(options) {
Expand Down Expand Up @@ -348,7 +348,7 @@ WaveformZoomView.prototype.setZoom = function(options) {
else {
if (objectHasProperty(options, 'scale')) {
this._zoomLevelSeconds = null;
scale = options.scale;
scale = Math.floor(options.scale);
}
else if (objectHasProperty(options, 'seconds')) {
if (!isValidTime(options.seconds)) {
Expand Down
18 changes: 18 additions & 0 deletions test/api-view-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,24 @@ describe('WaveformView', function() {
});
});

context('with non-integer scale', function() {
it('should round the scale down to an integer value', function() {
const view = p.views.getView('zoomview');

const resampleData = sinon.spy(view, '_resampleData');

view.setZoom({ scale: 500.5 });

expect(resampleData.callCount).to.equal(1);

// width is here because WaveformData.resample modifies
// its options parameter
expect(resampleData).calledWithExactly({ scale: 500, width: null });

expect(view._scale).to.equal(500);
});
});

context('with auto option', function() {
it('should fit the waveform to the width of the view', function() {
const view = p.views.getView('zoomview');
Expand Down

0 comments on commit e66e88b

Please sign in to comment.