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

Feat: add redrawcomplete event #3480

Merged
merged 1 commit into from
Jan 11, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type RendererEvents = {
drag: [relativeX: number]
scroll: [relativeStart: number, relativeEnd: number]
render: []
rendered: []
}

class Renderer extends EventEmitter<RendererEvents> {
Expand Down Expand Up @@ -454,7 +455,7 @@ class Renderer extends EventEmitter<RendererEvents> {
}
}

private renderChannel(channelData: Array<Float32Array | number[]>, options: WaveSurferOptions, width: number) {
private renderChannel(channelData: Array<Float32Array | number[]>, options: WaveSurferOptions, width: number, done: () => void) {
// A container for canvases
const canvasContainer = document.createElement('div')
const height = this.getHeight(options.height)
Expand Down Expand Up @@ -501,6 +502,14 @@ class Renderer extends EventEmitter<RendererEvents> {
)
}

const status: { [k:string]: boolean } = { head: false, tail: end >= len }
const complete = (type: string) => {
status[type] = true
if (status.head && status.tail) {
done()
}
}

// Draw the waveform in viewport chunks, each with a delay
const headDelay = this.createDelay()
const tailDelay = this.createDelay()
Expand All @@ -510,6 +519,8 @@ class Renderer extends EventEmitter<RendererEvents> {
headDelay(() => {
renderHead(fromIndex - viewportLen, toIndex - viewportLen)
})
} else {
complete('head')
}
}
const renderTail = (fromIndex: number, toIndex: number) => {
Expand All @@ -518,6 +529,8 @@ class Renderer extends EventEmitter<RendererEvents> {
tailDelay(() => {
renderTail(fromIndex + viewportLen, toIndex + viewportLen)
})
} else {
complete('tail')
Copy link
Owner

@katspaugh katspaugh Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking, maybe this could be refactored into a promise, smth like:

    // Draw the waveform in viewport chunks, each with a delay
    return Promise.all([
      new Promise((resolve) => {
        const headDelay = this.createDelay()
        const renderHead = (fromIndex: number, toIndex: number) => {
          draw(fromIndex, toIndex)
          if (fromIndex > 0) {
            headDelay(() => {
              renderHead(fromIndex - viewportLen, toIndex - viewportLen)
            })
          } else {
            resolve(true)
          }
        }
        renderHead(start, end)
      }),

      new Promise((resolve) => {
        if (end >= len) {
          resolve(true)
          return
        }
        const tailDelay = this.createDelay()
        const renderTail = (fromIndex: number, toIndex: number) => {
          draw(fromIndex, toIndex)
          if (toIndex < len) {
            tailDelay(() => {
              renderTail(fromIndex + viewportLen, toIndex + viewportLen)
            })
          } else {
            resolve(true)
          }
        }
        renderTail(end, end + viewportLen)
      }),
    ])

I would also refactor those recursive timeouts into sequenced promises, but that's outside of the scope of this PR, I'll do it myself later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@katspaugh We thought about that, too; however, one downside to this is that createDelay could end up canceling the setTimeout, which would mean that resolve never gets called and ends up adding unresolved promises as memory leaks.

This is especially problematic if the rendercomplete gets interrupted by a new render.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, good point. I’ll approve the PR as is then.

}
}

Expand Down Expand Up @@ -564,16 +577,24 @@ class Renderer extends EventEmitter<RendererEvents> {

// Render the waveform
if (this.options.splitChannels) {
let counter = 0
const done = () => {
counter++
if (counter === audioData.numberOfChannels) {
this.emit('rendered')
}
}

// Render a waveform for each channel
for (let i = 0; i < audioData.numberOfChannels; i++) {
const options = { ...this.options, ...this.options.splitChannels[i] }
this.renderChannel([audioData.getChannelData(i)], options, width)
this.renderChannel([audioData.getChannelData(i)], options, width, done)
}
} else {
// Render a single waveform for the first two channels (left and right)
const channels = [audioData.getChannelData(0)]
if (audioData.numberOfChannels > 1) channels.push(audioData.getChannelData(1))
this.renderChannel(channels, this.options, width)
this.renderChannel(channels, this.options, width, () => this.emit('rendered'))
}

this.audioData = audioData
Expand Down
9 changes: 8 additions & 1 deletion src/wavesurfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ export type WaveSurferEvents = {
decode: [duration: number]
/** When the audio is both decoded and can play */
ready: [duration: number]
/** When a waveform is drawn */
/** When visible waveform is drawn */
redraw: []
/** When all audio channel chunks of the waveform have drawn */
redrawcomplete: []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally like the event name, it's clear!

/** When the audio starts playing */
play: []
/** When the audio pauses */
Expand Down Expand Up @@ -255,6 +257,11 @@ class WaveSurfer extends Player<WaveSurferEvents> {
this.renderer.on('render', () => {
this.emit('redraw')
}),

// RedrawComplete
this.renderer.on('rendered', () => {
this.emit('redrawcomplete')
}),
)

// Drag
Expand Down