mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
switch to TS
This commit is contained in:
@@ -1,7 +1,21 @@
|
|||||||
// Override the startRecording method so we can pass the desired stream
|
// Source code: https://github.com/katspaugh/wavesurfer.js/blob/fa2bcfe/src/plugins/record.ts
|
||||||
// Checkout: https://github.com/katspaugh/wavesurfer.js/blob/fa2bcfe/src/plugins/record.ts
|
/**
|
||||||
|
* Record audio from the microphone, render a waveform and download the audio.
|
||||||
|
*/
|
||||||
|
|
||||||
import RecordPlugin from "wavesurfer.js/dist/plugins/record";
|
import BasePlugin, {
|
||||||
|
type BasePluginEvents,
|
||||||
|
} from "wavesurfer.js/dist/base-plugin";
|
||||||
|
|
||||||
|
export type RecordPluginOptions = {
|
||||||
|
mimeType?: MediaRecorderOptions["mimeType"];
|
||||||
|
audioBitsPerSecond?: MediaRecorderOptions["audioBitsPerSecond"];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RecordPluginEvents = BasePluginEvents & {
|
||||||
|
startRecording: [];
|
||||||
|
stopRecording: [];
|
||||||
|
};
|
||||||
|
|
||||||
const MIME_TYPES = [
|
const MIME_TYPES = [
|
||||||
"audio/webm",
|
"audio/webm",
|
||||||
@@ -13,11 +27,44 @@ const MIME_TYPES = [
|
|||||||
const findSupportedMimeType = () =>
|
const findSupportedMimeType = () =>
|
||||||
MIME_TYPES.find((mimeType) => MediaRecorder.isTypeSupported(mimeType));
|
MIME_TYPES.find((mimeType) => MediaRecorder.isTypeSupported(mimeType));
|
||||||
|
|
||||||
class CustomRecordPlugin extends RecordPlugin {
|
class RecordPlugin extends BasePlugin<RecordPluginEvents, RecordPluginOptions> {
|
||||||
static create(options) {
|
private mediaRecorder: MediaRecorder | null = null;
|
||||||
return new CustomRecordPlugin(options || {});
|
private recordedUrl = "";
|
||||||
|
private savedCursorWidth = 1;
|
||||||
|
private savedInteractive = true;
|
||||||
|
|
||||||
|
public static create(options?: RecordPluginOptions) {
|
||||||
|
return new RecordPlugin(options || {});
|
||||||
}
|
}
|
||||||
render(stream) {
|
|
||||||
|
private preventInteraction() {
|
||||||
|
if (this.wavesurfer) {
|
||||||
|
this.savedCursorWidth = this.wavesurfer.options.cursorWidth || 1;
|
||||||
|
this.savedInteractive = this.wavesurfer.options.interact || true;
|
||||||
|
this.wavesurfer.options.cursorWidth = 0;
|
||||||
|
this.wavesurfer.options.interact = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private restoreInteraction() {
|
||||||
|
if (this.wavesurfer) {
|
||||||
|
this.wavesurfer.options.cursorWidth = this.savedCursorWidth;
|
||||||
|
this.wavesurfer.options.interact = this.savedInteractive;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onInit() {
|
||||||
|
this.preventInteraction();
|
||||||
|
}
|
||||||
|
|
||||||
|
private loadBlob(data: Blob[], type: string) {
|
||||||
|
const blob = new Blob(data, { type });
|
||||||
|
this.recordedUrl = URL.createObjectURL(blob);
|
||||||
|
this.restoreInteraction();
|
||||||
|
this.wavesurfer?.load(this.recordedUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
render(stream: MediaStream): () => void {
|
||||||
if (!this.wavesurfer) return () => undefined;
|
if (!this.wavesurfer) return () => undefined;
|
||||||
|
|
||||||
const container = this.wavesurfer.getWrapper();
|
const container = this.wavesurfer.getWrapper();
|
||||||
@@ -36,11 +83,12 @@ class CustomRecordPlugin extends RecordPlugin {
|
|||||||
const bufferLength = analyser.frequencyBinCount;
|
const bufferLength = analyser.frequencyBinCount;
|
||||||
const dataArray = new Uint8Array(bufferLength);
|
const dataArray = new Uint8Array(bufferLength);
|
||||||
|
|
||||||
let animationId, previousTimeStamp;
|
let animationId: number, previousTimeStamp: number;
|
||||||
|
const DATA_SIZE = 128.0;
|
||||||
const BUFFER_SIZE = 2 ** 8;
|
const BUFFER_SIZE = 2 ** 8;
|
||||||
const dataBuffer = new Array(BUFFER_SIZE).fill(canvas.height);
|
const dataBuffer = new Array(BUFFER_SIZE).fill(DATA_SIZE);
|
||||||
|
|
||||||
const drawWaveform = (timeStamp) => {
|
const drawWaveform = (timeStamp: number) => {
|
||||||
if (!canvasCtx) return;
|
if (!canvasCtx) return;
|
||||||
|
|
||||||
analyser.getByteTimeDomainData(dataArray);
|
analyser.getByteTimeDomainData(dataArray);
|
||||||
@@ -64,9 +112,9 @@ class CustomRecordPlugin extends RecordPlugin {
|
|||||||
let x = 0;
|
let x = 0;
|
||||||
|
|
||||||
for (let i = 0; i < dataBuffer.length; i++) {
|
for (let i = 0; i < dataBuffer.length; i++) {
|
||||||
const valueNormalized = dataBuffer[i] / canvas.height;
|
const y = (canvas.height * dataBuffer[i]) / (2 * DATA_SIZE);
|
||||||
const y = (valueNormalized * canvas.height) / 2;
|
const sliceHeight =
|
||||||
const sliceHeight = canvas.height + 1 - y * 2;
|
((1 - canvas.height) * dataBuffer[i]) / DATA_SIZE + canvas.height;
|
||||||
|
|
||||||
canvasCtx.fillRect(x, y, (sliceWidth * 2) / 3, sliceHeight);
|
canvasCtx.fillRect(x, y, (sliceWidth * 2) / 3, sliceHeight);
|
||||||
x += sliceWidth;
|
x += sliceWidth;
|
||||||
@@ -75,7 +123,7 @@ class CustomRecordPlugin extends RecordPlugin {
|
|||||||
animationId = requestAnimationFrame(drawWaveform);
|
animationId = requestAnimationFrame(drawWaveform);
|
||||||
};
|
};
|
||||||
|
|
||||||
drawWaveform();
|
drawWaveform(0);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
if (animationId) {
|
if (animationId) {
|
||||||
@@ -94,7 +142,17 @@ class CustomRecordPlugin extends RecordPlugin {
|
|||||||
canvas?.remove();
|
canvas?.remove();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
startRecording(stream) {
|
|
||||||
|
private cleanUp() {
|
||||||
|
this.stopRecording();
|
||||||
|
this.wavesurfer?.empty();
|
||||||
|
if (this.recordedUrl) {
|
||||||
|
URL.revokeObjectURL(this.recordedUrl);
|
||||||
|
this.recordedUrl = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async startRecording(stream: MediaStream) {
|
||||||
this.preventInteraction();
|
this.preventInteraction();
|
||||||
this.cleanUp();
|
this.cleanUp();
|
||||||
|
|
||||||
@@ -103,7 +161,7 @@ class CustomRecordPlugin extends RecordPlugin {
|
|||||||
mimeType: this.options.mimeType || findSupportedMimeType(),
|
mimeType: this.options.mimeType || findSupportedMimeType(),
|
||||||
audioBitsPerSecond: this.options.audioBitsPerSecond,
|
audioBitsPerSecond: this.options.audioBitsPerSecond,
|
||||||
});
|
});
|
||||||
const recordedChunks = [];
|
const recordedChunks: Blob[] = [];
|
||||||
|
|
||||||
mediaRecorder.addEventListener("dataavailable", (event) => {
|
mediaRecorder.addEventListener("dataavailable", (event) => {
|
||||||
if (event.data.size > 0) {
|
if (event.data.size > 0) {
|
||||||
@@ -123,6 +181,25 @@ class CustomRecordPlugin extends RecordPlugin {
|
|||||||
|
|
||||||
this.mediaRecorder = mediaRecorder;
|
this.mediaRecorder = mediaRecorder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public isRecording(): boolean {
|
||||||
|
return this.mediaRecorder?.state === "recording";
|
||||||
|
}
|
||||||
|
|
||||||
|
public stopRecording() {
|
||||||
|
if (this.isRecording()) {
|
||||||
|
this.mediaRecorder?.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getRecordedUrl(): string {
|
||||||
|
return this.recordedUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public destroy() {
|
||||||
|
super.destroy();
|
||||||
|
this.cleanUp();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default CustomRecordPlugin;
|
export default RecordPlugin;
|
||||||
@@ -36,12 +36,7 @@ const App = () => {
|
|||||||
} = useAudioDevice();
|
} = useAudioDevice();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col items-center h-[100svh] bg-gradient-to-r from-[#8ec5fc30] to-[#e0c3fc42]">
|
<div className="w-full flex flex-col items-center h-[100svh]">
|
||||||
<div className="h-[13svh] flex flex-col justify-center items-center">
|
|
||||||
<h1 className="text-5xl font-bold text-blue-500">Reflector</h1>
|
|
||||||
<p className="text-gray-500">Capture The Signal, Not The Noise</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{permissionOk ? (
|
{permissionOk ? (
|
||||||
<>
|
<>
|
||||||
<Recorder
|
<Recorder
|
||||||
|
|||||||
@@ -1,18 +1,22 @@
|
|||||||
import React, { useRef, useEffect, useState } from "react";
|
import React, { useRef, useEffect, useState } from "react";
|
||||||
|
|
||||||
import WaveSurfer from "wavesurfer.js";
|
import WaveSurfer from "wavesurfer.js";
|
||||||
|
import RecordPlugin from "../lib/custom-plugins/record";
|
||||||
|
|
||||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
import { faDownload } from "@fortawesome/free-solid-svg-icons";
|
import { faDownload } from "@fortawesome/free-solid-svg-icons";
|
||||||
|
|
||||||
import Dropdown from "react-dropdown";
|
import Dropdown, { Option } from "react-dropdown";
|
||||||
import "react-dropdown/style.css";
|
import "react-dropdown/style.css";
|
||||||
|
|
||||||
import CustomRecordPlugin from "../lib/CustomRecordPlugin";
|
|
||||||
import { formatTime } from "../lib/time";
|
import { formatTime } from "../lib/time";
|
||||||
|
|
||||||
const AudioInputsDropdown = (props) => {
|
const AudioInputsDropdown = (props: {
|
||||||
const [ddOptions, setDdOptions] = useState([]);
|
audioDevices: Option[];
|
||||||
|
setDeviceId: React.Dispatch<React.SetStateAction<string | null>>;
|
||||||
|
disabled: boolean;
|
||||||
|
}) => {
|
||||||
|
const [ddOptions, setDdOptions] = useState<Option[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setDdOptions(props.audioDevices);
|
setDdOptions(props.audioDevices);
|
||||||
@@ -21,8 +25,8 @@ const AudioInputsDropdown = (props) => {
|
|||||||
);
|
);
|
||||||
}, [props.audioDevices]);
|
}, [props.audioDevices]);
|
||||||
|
|
||||||
const handleDropdownChange = (e) => {
|
const handleDropdownChange = (option: Option) => {
|
||||||
props.setDeviceId(e.value);
|
props.setDeviceId(option.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -36,18 +40,19 @@ const AudioInputsDropdown = (props) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default function Recorder(props) {
|
export default function Recorder(props) {
|
||||||
const waveformRef = useRef();
|
const waveformRef = useRef<HTMLDivElement>(null);
|
||||||
const [wavesurfer, setWavesurfer] = useState(null);
|
const [wavesurfer, setWavesurfer] = useState<WaveSurfer | null>(null);
|
||||||
const [record, setRecord] = useState(null);
|
const [record, setRecord] = useState<RecordPlugin | null>(null);
|
||||||
const [isRecording, setIsRecording] = useState(false);
|
const [isRecording, setIsRecording] = useState<boolean>(false);
|
||||||
const [isPlaying, setIsPlaying] = useState(false);
|
const [isPlaying, setIsPlaying] = useState<boolean>(false);
|
||||||
const [deviceId, setDeviceId] = useState(null);
|
const [deviceId, setDeviceId] = useState<string | null>(null);
|
||||||
const [currentTime, setCurrentTime] = useState(0);
|
const [currentTime, setCurrentTime] = useState<number>(0);
|
||||||
const [timeInterval, setTimeInterval] = useState(null);
|
const [timeInterval, setTimeInterval] = useState<number | null>(null);
|
||||||
const [duration, setDuration] = useState(0);
|
const [duration, setDuration] = useState<number>(0);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
document.getElementById("play-btn").disabled = true;
|
const playBtn = document.getElementById("play-btn");
|
||||||
|
if (playBtn) playBtn.setAttribute("disabled", "true");
|
||||||
|
|
||||||
if (waveformRef.current) {
|
if (waveformRef.current) {
|
||||||
const _wavesurfer = WaveSurfer.create({
|
const _wavesurfer = WaveSurfer.create({
|
||||||
@@ -72,7 +77,7 @@ export default function Recorder(props) {
|
|||||||
});
|
});
|
||||||
_wavesurfer.on("timeupdate", setCurrentTime);
|
_wavesurfer.on("timeupdate", setCurrentTime);
|
||||||
|
|
||||||
setRecord(_wavesurfer.registerPlugin(CustomRecordPlugin.create()));
|
setRecord(_wavesurfer.registerPlugin(RecordPlugin.create()));
|
||||||
setWavesurfer(_wavesurfer);
|
setWavesurfer(_wavesurfer);
|
||||||
return () => {
|
return () => {
|
||||||
_wavesurfer.destroy();
|
_wavesurfer.destroy();
|
||||||
@@ -86,8 +91,10 @@ export default function Recorder(props) {
|
|||||||
if (record) {
|
if (record) {
|
||||||
return record.on("stopRecording", () => {
|
return record.on("stopRecording", () => {
|
||||||
const link = document.getElementById("download-recording");
|
const link = document.getElementById("download-recording");
|
||||||
link.href = record.getRecordedUrl();
|
if (!link) return;
|
||||||
link.download = "reflector-recording.webm";
|
|
||||||
|
link.setAttribute("href", record.getRecordedUrl());
|
||||||
|
link.setAttribute("download", "reflector-recording.webm");
|
||||||
link.style.visibility = "visible";
|
link.style.visibility = "visible";
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -95,13 +102,13 @@ export default function Recorder(props) {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isRecording) {
|
if (isRecording) {
|
||||||
const interval = setInterval(() => {
|
const interval = window.setInterval(() => {
|
||||||
setCurrentTime((prev) => prev + 1);
|
setCurrentTime((prev) => prev + 1);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
setTimeInterval(interval);
|
setTimeInterval(interval);
|
||||||
return () => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
} else {
|
} else {
|
||||||
clearInterval(timeInterval);
|
clearInterval(timeInterval as number);
|
||||||
setCurrentTime((prev) => {
|
setCurrentTime((prev) => {
|
||||||
setDuration(prev);
|
setDuration(prev);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -116,7 +123,8 @@ export default function Recorder(props) {
|
|||||||
props.onStop();
|
props.onStop();
|
||||||
record.stopRecording();
|
record.stopRecording();
|
||||||
setIsRecording(false);
|
setIsRecording(false);
|
||||||
document.getElementById("play-btn").disabled = false;
|
const playBtn = document.getElementById("play-btn");
|
||||||
|
if (playBtn) playBtn.removeAttribute("disabled");
|
||||||
} else {
|
} else {
|
||||||
const stream = await props.getAudioStream(deviceId);
|
const stream = await props.getAudioStream(deviceId);
|
||||||
props.setStream(stream);
|
props.setStream(stream);
|
||||||
@@ -133,9 +141,8 @@ export default function Recorder(props) {
|
|||||||
|
|
||||||
const timeLabel = () => {
|
const timeLabel = () => {
|
||||||
if (isRecording) return formatTime(currentTime);
|
if (isRecording) return formatTime(currentTime);
|
||||||
else if (duration)
|
if (duration) return `${formatTime(currentTime)}/${formatTime(duration)}`;
|
||||||
return `${formatTime(currentTime)}/${formatTime(duration)}`;
|
return "";
|
||||||
else "";
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useState } from "react";
|
||||||
|
|
||||||
|
import { Option } from "react-dropdown";
|
||||||
|
|
||||||
const useAudioDevice = () => {
|
const useAudioDevice = () => {
|
||||||
const [permissionOk, setPermissionOk] = useState(false);
|
const [permissionOk, setPermissionOk] = useState(false);
|
||||||
const [audioDevices, setAudioDevices] = useState([]);
|
const [audioDevices, setAudioDevices] = useState<Option[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
const requestPermission = () => {
|
const requestPermission = () => {
|
||||||
@@ -22,7 +24,9 @@ const useAudioDevice = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAudioStream = async (deviceId) => {
|
const getAudioStream = async (
|
||||||
|
deviceId: string,
|
||||||
|
): Promise<MediaStream | null> => {
|
||||||
try {
|
try {
|
||||||
const stream = await navigator.mediaDevices.getUserMedia({
|
const stream = await navigator.mediaDevices.getUserMedia({
|
||||||
audio: {
|
audio: {
|
||||||
@@ -39,7 +43,7 @@ const useAudioDevice = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateDevices = async () => {
|
const updateDevices = async (): Promise<void> => {
|
||||||
const devices = await navigator.mediaDevices.enumerateDevices();
|
const devices = await navigator.mediaDevices.enumerateDevices();
|
||||||
const _audioDevices = devices
|
const _audioDevices = devices
|
||||||
.filter(
|
.filter(
|
||||||
@@ -51,10 +55,6 @@ const useAudioDevice = () => {
|
|||||||
setAudioDevices(_audioDevices);
|
setAudioDevices(_audioDevices);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
requestPermission();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
permissionOk,
|
permissionOk,
|
||||||
audioDevices,
|
audioDevices,
|
||||||
Reference in New Issue
Block a user