Skip to content

Commit

Permalink
Feat: Add setScrollTime method (katspaugh#3547)
Browse files Browse the repository at this point in the history
* Feat: Add setScrollTime method

This adds a `setScrollTime` method to `wavesurfer` that takes a time in
seconds. It also adds `setScroll` and `setScrollPercentage` methods to
`renderer` which are used by `setScrollTime`.

This allows the viwing window to be set indepenently of the play head.

Fixes: katspaugh#3411
Related: katspaugh#3361

* Add a test

* make this more accurate

* Update src/renderer.ts

Co-authored-by: katspaugh <381895+katspaugh@users.noreply.github.com>

* Update src/wavesurfer.ts

Co-authored-by: katspaugh <381895+katspaugh@users.noreply.github.com>

---------

Co-authored-by: katspaugh <381895+katspaugh@users.noreply.github.com>
  • Loading branch information
jagthedrummer and katspaugh committed Feb 11, 2024
1 parent a6756f2 commit 02afdf9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
17 changes: 17 additions & 0 deletions cypress/e2e/basic.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ describe('WaveSurfer basic tests', () => {
})
})

it('should scroll on setScrollTime if zoomed in', () => {
cy.window().then((win) => {
win.wavesurfer.zoom(300)
const zoomedWidth = win.wavesurfer.getWrapper().clientWidth
win.wavesurfer.zoom(600)
const newWidth = win.wavesurfer.getWrapper().clientWidth

expect(Math.round(newWidth / zoomedWidth)).to.equal(2)

win.wavesurfer.setScrollTime(20)

cy.wait(1000).then(() => {
expect(win.wavesurfer.getScroll()).to.be.greaterThan(100)
})
})
})

it('should export decoded audio data', () => {
cy.window().then((win) => {
const data = win.wavesurfer.getDecodedData()
Expand Down
2 changes: 1 addition & 1 deletion src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Player<T extends GeneralEventTypes> extends EventEmitter<T> {
return !this.media.paused && !this.media.ended
}

/** Jumpt to a specific time in the audio (in seconds) */
/** Jump to a specific time in the audio (in seconds) */
public setTime(time: number) {
this.media.currentTime = time
}
Expand Down
10 changes: 10 additions & 0 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ class Renderer extends EventEmitter<RendererEvents> {
return this.scrollContainer.scrollLeft
}

private setScroll(pixels: number) {
this.scrollContainer.scrollLeft = pixels
}

setScrollPercentage(percent: number) {
const { scrollWidth } = this.scrollContainer
const scrollStart = scrollWidth * percent
this.setScroll(scrollStart)
}

destroy() {
this.container.remove()
this.resizeObserver?.disconnect()
Expand Down
8 changes: 7 additions & 1 deletion src/wavesurfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ class WaveSurfer extends Player<WaveSurferEvents> {
return this.renderer.getScroll()
}

/** Move the start of the viewing window to a specific time in the audio (in seconds) */
public setScrollTime(time: number) {
const percentage = time / this.getDuration()
this.renderer.setScrollPercentage(percentage)
}

/** Get all registered plugins */
public getActivePlugins() {
return this.plugins
Expand Down Expand Up @@ -459,7 +465,7 @@ class WaveSurfer extends Player<WaveSurferEvents> {
this.options.interact = isInteractive
}

/** Jumpt to a specific time in the audio (in seconds) */
/** Jump to a specific time in the audio (in seconds) */
public setTime(time: number) {
super.setTime(time)
this.updateProgress(time)
Expand Down

0 comments on commit 02afdf9

Please sign in to comment.