Skip to content

Commit

Permalink
fix: (Record) setTimeout instead of requestAnimationFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Aug 17, 2024
1 parent a984cde commit 5512242
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 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: Function) => {

Check failure on line 60 in src/plugins/record.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

src/plugins/record.ts#L60

[@typescript-eslint/ban-types] Don't use `Function` as a type. The `Function` type accepts any function-like value. It provides no type safety when calling the function, which can be a common source of bugs. It also accepts things like class declarations, which will throw at runtime as they will not be called with `new`. If you are expecting the function to accept certain arguments, you should explicitly define the function shape.
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,6 +106,9 @@ 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)

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 5512242

Please sign in to comment.