Skip to content

Commit

Permalink
fix: (Record) setTimeout instead of requestAnimationFrame (#3827)
Browse files Browse the repository at this point in the history
* fix: (Record) setTimeout instead of requestAnimationFrame

* Lint
  • Loading branch information
katspaugh authored Aug 17, 2024
1 parent a984cde commit be50827
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/plugins/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ const FPS = 60
const MIME_TYPES = ['audio/webm', 'audio/wav', 'audio/mpeg', 'audio/mp4', 'audio/mp3']
const findSupportedMimeType = () => MIME_TYPES.find((mimeType) => MediaRecorder.isTypeSupported(mimeType))

const requestFrameTimeout = (callback: () => void) => {
return setTimeout(callback, 1000 / FPS)
}

class RecordPlugin extends BasePlugin<RecordPluginEvents, RecordPluginOptions> {
private stream: MediaStream | null = null
private mediaRecorder: MediaRecorder | null = null
Expand Down Expand Up @@ -102,10 +106,13 @@ class RecordPlugin extends BasePlugin<RecordPluginEvents, RecordPluginOptions> {
const analyser = audioContext.createAnalyser()
source.connect(analyser)

if (this.options.continuousWaveform) {
analyser.fftSize = 32
}
const bufferLength = analyser.frequencyBinCount
const dataArray = new Float32Array(bufferLength)

let animationId: number
let animationId: ReturnType<typeof requestFrameTimeout>
let sampleIdx = 0

if (this.wavesurfer) {
Expand All @@ -120,8 +127,10 @@ class RecordPlugin extends BasePlugin<RecordPluginEvents, RecordPluginOptions> {
}

const drawWaveform = () => {
animationId && clearTimeout(animationId)

if (this.isWaveformPaused) {
animationId = requestAnimationFrame(drawWaveform)
animationId = requestFrameTimeout(drawWaveform)
return
}

Expand All @@ -147,7 +156,13 @@ class RecordPlugin extends BasePlugin<RecordPluginEvents, RecordPluginOptions> {
this.dataWindow = new Float32Array(size)
}

const maxValue = Math.max(...dataArray)
let maxValue = 0
for (let i = 0; i < bufferLength; i++) {
const value = Math.abs(dataArray[i])
if (value > maxValue) {
maxValue = value
}
}

// Append the max value to the data window at the right position
if (sampleIdx + 1 > this.dataWindow.length) {
Expand All @@ -165,10 +180,6 @@ class RecordPlugin extends BasePlugin<RecordPluginEvents, RecordPluginOptions> {
// Render the waveform
if (this.wavesurfer) {
const totalDuration = (this.dataWindow?.length ?? 0) / FPS
let position = sampleIdx / this.dataWindow.length
if (this.wavesurfer.options.barWidth) {
position += this.wavesurfer.options.barWidth / this.wavesurfer.getWidth()
}

this.wavesurfer
.load(
Expand All @@ -178,7 +189,7 @@ class RecordPlugin extends BasePlugin<RecordPluginEvents, RecordPluginOptions> {
)
.then(() => {
if (this.wavesurfer && this.options.continuousWaveform) {
this.wavesurfer.seekTo(position)
this.wavesurfer.setTime(this.getDuration() / 1000)

if (!this.wavesurfer.options.minPxPerSec) {
this.wavesurfer.setOptions({
Expand All @@ -192,20 +203,20 @@ class RecordPlugin extends BasePlugin<RecordPluginEvents, RecordPluginOptions> {
})
}

animationId = requestAnimationFrame(drawWaveform)
animationId = requestFrameTimeout(drawWaveform)
}

drawWaveform()

return {
onDestroy: () => {
cancelAnimationFrame(animationId)
clearTimeout(animationId)
source?.disconnect()
audioContext?.close()
},
onEnd: () => {
this.isWaveformPaused = true
cancelAnimationFrame(animationId)
clearTimeout(animationId)
this.stopMic()
},
}
Expand Down

0 comments on commit be50827

Please sign in to comment.