-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Yukiiro-Nite/feature/audio-waveform
Feature/audio waveform
- Loading branch information
Showing
5 changed files
with
207 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
let stopRender = false; | ||
|
||
function render(canvas: HTMLCanvasElement, audioData: Float32Array): ImageBitmap | undefined { | ||
const ctx = canvas.getContext('2d'); | ||
|
||
if (!ctx) return; | ||
|
||
ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
|
||
const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height); | ||
gradient.addColorStop(0, '#e08eff'); | ||
gradient.addColorStop(0.5, '#868aff'); | ||
gradient.addColorStop(1, '#e08eff'); | ||
|
||
ctx.strokeStyle = gradient; | ||
ctx.lineWidth = 1; | ||
ctx.beginPath(); | ||
ctx.moveTo(0, canvas.height / 2); | ||
|
||
let x = 0; | ||
let y = 0; | ||
for (let i = 0; (i < audioData.length || stopRender); i++) { | ||
y = (audioData[i] * (canvas.height / 2)) + (canvas.height / 2); | ||
x = (i / audioData.length) * canvas.width; | ||
ctx.lineTo(x, y); | ||
} | ||
|
||
if (stopRender) return; | ||
|
||
ctx.stroke(); | ||
|
||
// Need to create an image from the OffscreenCanvas to send it back to the main process. | ||
// https://developer.mozilla.org/en-US/docs/Web/API/OffScreenCanvas/transferToImageBitmap | ||
// @ts-ignore | ||
const img: ImageBitmap = canvas.transferToImageBitmap(); | ||
return img; | ||
} | ||
|
||
onmessage = function(e: MessageEvent<{ canvas: HTMLCanvasElement, audioData: Float32Array, stop: boolean }>) { | ||
const canvas = e.data.canvas; | ||
const audioData = e.data.audioData; | ||
stopRender = e.data.stop; | ||
|
||
if (stopRender) return; | ||
|
||
const img = render(canvas, audioData); | ||
|
||
if (img) { | ||
this.postMessage(img); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import Player from "./player"; | ||
|
||
export class WaveformRenderer { | ||
public canvas: HTMLCanvasElement; | ||
|
||
private player: Player; | ||
private audioBuffer: AudioBuffer | null; | ||
private audioCtx: AudioContext; | ||
private currentWorker: Worker | null; | ||
|
||
constructor (player: Player, canvasSelector: string) { | ||
this.player = player; | ||
this.canvas = document.querySelector(canvasSelector) || document.createElement("canvas"); | ||
this.audioCtx = new AudioContext(); | ||
this.audioBuffer = null; | ||
this.currentWorker = null; | ||
|
||
this.player.on('play', this.handlePlayAudio); | ||
} | ||
|
||
private handlePlayAudio = async () => { | ||
const currentSound = this.player.state.sound; | ||
await this.waitForLoadedData(currentSound); | ||
|
||
if (currentSound != this.player.state.sound) return; | ||
|
||
const channels = this.player.state.source?.channelCount ?? 1; | ||
const duration = this.player.state.currentlyPlayingMetadata?.format.duration ?? 1; | ||
const sampleRate = this.player.state.currentlyPlayingMetadata?.format.sampleRate ?? 1; | ||
|
||
const offlineAudioCtx = new OfflineAudioContext({ | ||
numberOfChannels: channels, | ||
length: duration * sampleRate, | ||
sampleRate | ||
}); | ||
|
||
|
||
const tempBuffer = await this.fetchAudioBuffer(this.player.state.sound.src, offlineAudioCtx); | ||
|
||
if (currentSound != this.player.state.sound) return; | ||
|
||
this.audioBuffer = tempBuffer; | ||
this.renderWaveform(); | ||
}; | ||
|
||
private setCanvasSize = () => { | ||
const parent = this.canvas.parentElement; | ||
if (parent) { | ||
const { width, height } = parent.getBoundingClientRect(); | ||
this.canvas.width = width; | ||
this.canvas.height = height; | ||
} | ||
}; | ||
|
||
private waitForLoadedData = async (audio: HTMLAudioElement) => { | ||
const loaded = new Promise<void>((resolve) => { | ||
const resolver = () => { | ||
audio.removeEventListener('loadeddata', resolver); | ||
resolve(); | ||
} | ||
audio.addEventListener('loadeddata', resolver); | ||
}); | ||
|
||
await loaded; | ||
}; | ||
|
||
private fetchAudioBuffer = (src: string, offlineAudioCtx: OfflineAudioContext): Promise<AudioBuffer> => { | ||
const source = offlineAudioCtx.createBufferSource(); | ||
const request = new XMLHttpRequest(); | ||
|
||
request.open('GET', src, true); | ||
|
||
request.responseType = 'arraybuffer'; | ||
|
||
return new Promise<AudioBuffer>((resolve, reject) => { | ||
request.onload = () => { | ||
var audioData = request.response; | ||
|
||
this.audioCtx.decodeAudioData(audioData) | ||
.then((buffer: AudioBuffer) => { | ||
const myBuffer = buffer; | ||
source.buffer = myBuffer; | ||
source.connect(offlineAudioCtx.destination); | ||
source.start(); | ||
|
||
offlineAudioCtx.startRendering() | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
} | ||
|
||
request.send(); | ||
}) | ||
}; | ||
|
||
private renderWaveform = () => { | ||
this.setCanvasSize(); | ||
const ctx = this.canvas.getContext('2d'); | ||
ctx?.clearRect(0, 0, this.canvas.width, this.canvas.height); | ||
const backCanvas = document.createElement('canvas'); | ||
backCanvas.width = this.canvas.width; | ||
backCanvas.height = this.canvas.height; | ||
|
||
// Electron 18.0.3 is using Chrome 100.0.4896.75 | ||
// https://www.electronjs.org/releases/stable?version=18&page=2#18.0.3 | ||
// | ||
// transferControlToOffscreen has been available since Chrome 69 | ||
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/transferControlToOffscreen#browser_compatibility | ||
// @ts-ignore | ||
const offscreen: OffscreenCanvas = backCanvas.transferControlToOffscreen(); | ||
|
||
if (this.audioBuffer === null) return; | ||
|
||
const audioData = this.audioBuffer.getChannelData(0); | ||
|
||
if (this.currentWorker !== null) { | ||
this.currentWorker.postMessage({ stop: true }); | ||
this.currentWorker.terminate(); | ||
} | ||
|
||
this.currentWorker = new Worker("waveformRenderWorker.ts"); | ||
this.currentWorker.onmessage = (msg) => { | ||
ctx?.clearRect(0, 0, this.canvas.width, this.canvas.height); | ||
ctx?.drawImage(msg.data, 0, 0); | ||
this.currentWorker = null; | ||
}; | ||
this.currentWorker.postMessage({ canvas: offscreen, audioData }, [offscreen]); | ||
}; | ||
|
||
public clean = () => { | ||
this.player.off('play', this.handlePlayAudio); | ||
}; | ||
} |