Skip to content

Commit

Permalink
Merge 05292bb into cbd7711
Browse files Browse the repository at this point in the history
  • Loading branch information
thijstriemstra committed Nov 24, 2019
2 parents cbd7711 + 05292bb commit 2bb6425
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ wavesurfer.js changelog
3.3.0 (unreleased)
------------------

- `wavesurfer.exportPCM` now accepts an optional `end` argument and returns
a Promise (#1728)
- Add `wavesurfer.setPlayEnd(position)` to set a point in seconds for
playback to stop at (#1795)
- Add `barMinHeight` option (#1693)
Expand Down
10 changes: 7 additions & 3 deletions spec/wavesurfer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,13 @@ describe('WaveSurfer/playback:', function() {
});

/** @test {WaveSurfer#exportPCM} */
it('return PCM data formatted using JSON.stringify', function() {
var pcmData = wavesurfer.exportPCM();
expect(pcmData).toBeNonEmptyString();
it('return Promise with PCM data formatted using JSON.stringify', function(done) {
manualDestroy = true;
wavesurfer.exportPCM().then(pcmData => {
expect(pcmData).toBeNonEmptyString();

done();
});
});

/** @test {WaveSurfer#getFilters} */
Expand Down
31 changes: 17 additions & 14 deletions src/wavesurfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1525,32 +1525,35 @@ export default class WaveSurfer extends util.Observer {
/**
* Exports PCM data into a JSON array and opens in a new window.
*
* @param {number} length=1024 The scale in which to export the peaks. (Integer)
* @param {number} accuracy=10000 (Integer)
* @param {number} length=1024 The scale in which to export the peaks
* @param {number} accuracy=10000
* @param {?boolean} noWindow Set to true to disable opening a new
* window with the JSON
* @param {number} start Start index
* @todo Update exportPCM to work with new getPeaks signature
* @return {string} JSON of peaks
* @param {number} end End index
* @return {Promise} Promise that resolves with array of peaks
*/
exportPCM(length, accuracy, noWindow, start) {
exportPCM(length, accuracy, noWindow, start, end) {
length = length || 1024;
start = start || 0;
accuracy = accuracy || 10000;
noWindow = noWindow || false;
const peaks = this.backend.getPeaks(length, start);
const peaks = this.backend.getPeaks(length, start, end);
const arr = [].map.call(
peaks,
val => Math.round(val * accuracy) / accuracy
);
const json = JSON.stringify(arr);
if (!noWindow) {
window.open(
'data:application/json;charset=utf-8,' +
encodeURIComponent(json)
);
}
return json;
return new Promise((resolve, reject) => {
const json = JSON.stringify(arr);

if (!noWindow) {
window.open(
'data:application/json;charset=utf-8,' +
encodeURIComponent(json)
);
}
resolve(json);
});
}

/**
Expand Down

0 comments on commit 2bb6425

Please sign in to comment.