Skip to content

A self-hosted media server and file management system

feat: spectrograph ui and options

aminnausin79a5be56ffff
diff --git a/resources/js/components/cedar-ui/input/BasicInput.vue b/resources/js/components/cedar-ui/input/BasicInput.vue
new file mode 100644
index 00000000..e0a7390c
--- /dev/null
+++ b/resources/js/components/cedar-ui/input/BasicInput.vue
@@ -0,0 +1,36 @@
+<script setup lang="ts">
+import type { InputShellProps } from '@aminnausin/cedar-ui';
+import type { InputHTMLAttributes } from 'vue';
+
+import { InputShell } from '.';
+import { ref } from 'vue';
+import { cn } from '@aminnausin/cedar-ui';
+
+defineOptions({ inheritAttrs: false });
+
+const model = defineModel();
+const props = defineProps<InputHTMLAttributes & InputShellProps>();
+const el = ref<HTMLInputElement | null>(null);
+
+defineExpose({ el });
+</script>
+<template>
+ <InputShell :clamp-text="clampText">
+ <template #input="{ class: inputClass }">
+ <input
+ ref="el"
+ v-model="model"
+ v-bind="$attrs"
+ :class="cn(inputClass, props.class)"
+ :type="type"
+ :min="min"
+ :max="max"
+ :minlength="minlength"
+ :maxlength="maxlength"
+ :disabled="disabled"
+ :readonly="readonly"
+ :required="required"
+ />
+ </template>
+ </InputShell>
+</template>
diff --git a/resources/js/components/video/VideoPlayer.vue b/resources/js/components/video/VideoPlayer.vue
index 97cf3a98..78442dc9 100755
--- a/resources/js/components/video/VideoPlayer.vue
+++ b/resources/js/components/video/VideoPlayer.vue
@@ -24,9 +24,10 @@ import { MediaType } from '@/types/types';
import { onSeek } from '@/service/player/seekBus';
import { FLAGS } from '@/config/featureFlags';
+import AudioSpectrographPanel from '@/components/video/audio/AudioSpectrographPanel.vue';
import VideoControlWrapper from '@/components/video/VideoControlWrapper.vue';
import VideoPopoverSlider from '@/components/video/VideoPopoverSlider.vue';
-import AudioSpectrograph from '@/components/audio/AudioSpectrograph.vue';
+import AudioSpectrograph from '@/components/video/audio/AudioSpectrograph.vue';
import VideoPopoverItem from '@/components/video/VideoPopoverItem.vue';
import PlayerSubtitles from '@/components/video/subtitles/PlayerSubtitles.vue';
import VideoPartyPanel from '@/components/video/VideoPartyPanel.vue';
@@ -52,6 +53,7 @@ import ProiconsVolumeMute from '~icons/proicons/volume-mute';
import ProiconsVolumeLow from '~icons/proicons/volume-low';
import ProiconsCheckmark from '~icons/proicons/checkmark';
import TablerMicrophone2 from '~icons/tabler/microphone-2';
+import IconSpectrograph from '@/components/icons/IconSpectrograph.vue';
import ProiconsSparkle2 from '~icons/proicons/sparkle-2';
import ProiconsSettings from '~icons/proicons/settings';
import ProiconsSpinner from '~icons/proicons/spinner';
@@ -112,7 +114,7 @@ const emit = defineEmits(['loadedData', 'seeked', 'play', 'pause', 'ended', 'loa
// Global State
// So isAutoPlay determines if the video should auto start, and has no ui toggle
// But isPlaylist determines if should navigate to next video at the end of current video and has a ui toggle called Autoplay ????????????
-const { contextMenuItems, contextMenuStyle, contextMenuItemStyle, playbackHeatmap, ambientMode, lightMode, isAutoPlay, isPlaylist, usingPlayerModernUI } =
+const { contextMenuItems, contextMenuStyle, contextMenuItemStyle, playbackHeatmap, ambientMode, lightMode, isAutoPlay, isPlaylist, usingPlayerModernUI, isAudioGraphEnabled } =
storeToRefs(useAppStore());
const { updateViewCount } = useContentStore();
const { setContextMenu } = useAppStore();
@@ -156,6 +158,7 @@ const volumeChangeTimeout = ref<NodeJS.Timeout>();
const autoSeekTimeout = ref<NodeJS.Timeout>();
const timeDisplay = ref<'timeElapsed' | 'timeRemaining'>('timeElapsed');
+const isShowingAudioGraphSettings = ref(false);
const isPlayerSizeConstrained = computed(() => (isAudio.value || aspectRatio.value.isPortrait) && isNormalView.value); // Is size determined by album art or portrait video
const isThumbnailDismissed = ref(false);
const isPictureInPicture = ref(false);
@@ -253,9 +256,10 @@ const popover = useTemplateRef('player-popover');
const timeline = useTemplateRef('player-timeline');
const container = useTemplateRef('player-container');
-const playerContextMenu = useTemplateRef('contextMenu');
-const playerLyrics = useTemplateRef('playerLyrics');
+const playerContextMenu = useTemplateRef('context-menu');
+const playerLyrics = useTemplateRef('player-lyrics');
const playerSubtitles = useTemplateRef('player-subtitles');
+const playerSpectrograph = useTemplateRef('player-spectrograph');
const progressTooltip = computed(() => timeline.value?.progressTooltip);
@@ -271,14 +275,14 @@ const playerContextMenuItems = computed(() => {
},
},
{
- text: 'Show Stats',
+ text: 'Player Stats',
icon: isShowingStats.value ? ProiconsCheckmark : undefined,
action: () => {
isShowingStats.value = !isShowingStats.value;
},
},
{
- text: 'Show Party Demo',
+ text: 'Watch Party',
icon: isShowingParty.value ? ProiconsCheckmark : undefined,
selected: isShowingParty.value,
disabled: !userData.value?.id,
@@ -295,6 +299,15 @@ const playerContextMenuItems = computed(() => {
togglePictureInPicture();
},
},
+ {
+ text: 'Audio Graph Menu',
+ icon: isShowingAudioGraphSettings.value ? ProiconsCheckmark : undefined,
+ selected: isShowingAudioGraphSettings.value,
+ hidden: !isAudioGraphEnabled.value,
+ action: () => {
+ isShowingAudioGraphSettings.value = !isShowingAudioGraphSettings.value;
+ },
+ },
{
text: 'Save Frame',
hidden: isAudio.value,
@@ -362,6 +375,18 @@ const videoPopoverItems = computed(() => {
selectedIconStyle: 'text-primary',
action: handleToggleAutoplay,
},
+ {
+ text: 'Audio Graph',
+ title: 'Toggle Audio Visualiser',
+ icon: IconSpectrograph,
+ selectedIcon: ProiconsCheckmark,
+ selected: isAudioGraphEnabled.value,
+ selectedIconStyle: 'text-primary',
+ action: () => {
+ isAudioGraphEnabled.value = !isAudioGraphEnabled.value;
+ if (!isAudioGraphEnabled.value) isShowingAudioGraphSettings.value = false;
+ },
+ },
{
text: 'Modern UI',
title: `Toggle backgrounds on player controls`,
@@ -1372,6 +1397,19 @@ defineExpose({
style="z-index: 7"
/>
+ <div
+ class="layer pointer-events-none absolute inset-0 flex flex-col gap-4 p-2 sm:p-4"
+ style="z-index: 7"
+ v-if="playerSpectrograph"
+ v-show="isShowingAudioGraphSettings"
+ >
+ <AudioSpectrographPanel
+ :close-panel="() => (isShowingAudioGraphSettings = false)"
+ :toggle-scale="playerSpectrograph?.toggleScale"
+ :is-maximised="isFullScreen || isTheatreView"
+ />
+ </div>
+
<!-- Watch Party (Z-7) -->
<div class="pointer-events-auto absolute top-0 right-0 p-1 sm:p-4" v-show="isShowingParty" style="z-index: 7">
<VideoPartyPanel :player="player ?? undefined" />
@@ -1651,7 +1689,7 @@ defineExpose({
>
<div :class="`absolute top-0 flex h-full w-full opacity-0 transition-all`" style="z-index: 5" v-show="isShowingLyrics">
<VideoLyrics
- ref="playerLyrics"
+ ref="player-lyrics"
v-if="isAudio || stateFolder.is_majority_audio"
@seek="handleManualSeek"
:player="player"
@@ -1820,15 +1858,15 @@ defineExpose({
</Transition>
</div>
- <div style="z-index: 4" :class="['pointer-events-none absolute bottom-0 w-full transition-all duration-400', { '': isShowingControls }]">
- <AudioSpectrograph v-if="player" />
+ <div style="z-index: 4" :class="['pointer-events-none absolute bottom-0 w-full transition-all duration-400']">
+ <AudioSpectrograph v-if="player" :is-enabled="isAudioGraphEnabled" ref="player-spectrograph" />
</div>
<div class="pointer-events-none absolute top-0 left-0 h-full w-full" v-show="isFullScreen || isTheatreView">
<GlobalModal :teleport-disabled="true" v-if="isFullScreen || isTheatreView" />
<ToastController :teleport-disabled="true" :position="'bottom-left'" v-if="isFullScreen || isTheatreView" />
<ContextMenu
- ref="contextMenu"
+ ref="context-menu"
:items="contextMenuItems"
:style="contextMenuStyle"
:itemStyle="contextMenuItemStyle ?? 'hover:bg-primary hover:text-white'"
diff --git a/resources/js/components/video/audio/AudioSpectrograph.vue b/resources/js/components/video/audio/AudioSpectrograph.vue
index d15fa07a..795f3358 100644
--- a/resources/js/components/video/audio/AudioSpectrograph.vue
+++ b/resources/js/components/video/audio/AudioSpectrograph.vue
@@ -1,70 +1,77 @@
<script setup lang="ts">
-import { onMounted, onUnmounted, ref, useTemplateRef } from 'vue';
-import { AudioSpectrograph } from '@/service/AudioSpectrograph/AudioSpectrograph';
+import { inject, onMounted, onUnmounted, ref, useTemplateRef, watch, type Ref } from 'vue';
+import { AudioSpectrograph } from '@/service/audio/AudioSpectrograph';
+import { debounce } from 'lodash-es';
-const isDrawing = ref(false);
-const isEnabled = ref(true);
+const props = defineProps<{ isEnabled?: boolean }>();
const canvas = useTemplateRef('canvas');
-const player = ref<HTMLVideoElement | null>(null);
-const ctx = ref<CanvasRenderingContext2D | null>(null);
+const player = inject<Ref<HTMLVideoElement>>('player');
+
+const isDrawing = ref(false);
let spectrograph: AudioSpectrograph | null = null;
+let ro: ResizeObserver | null = null;
const handlePlay = () => {
- if (spectrograph) {
- isDrawing.value = true;
- spectrograph.start();
- }
+ if (!spectrograph || spectrograph.isDrawing || player?.value.paused) return;
+ spectrograph.start();
+ isDrawing.value = true;
};
const handlePause = () => {
if (spectrograph) spectrograph.stop();
isDrawing.value = false;
+ // This was my original fill style spectrograph.setFillStyle(solidColour('hsl(329.15 100% 45%)'));
};
-const setSpectrographColours = () => {
- if (!ctx.value || !canvas.value) return;
+function resizeSpectrograph() {
+ if (!spectrograph || !canvas.value || !player?.value) return;
+ spectrograph.resize(player.value.offsetWidth, Math.floor(player.value.offsetHeight / 4));
+}
- const gradient = ctx.value.createLinearGradient(0, canvas.value.height, 0, 0);
- gradient.addColorStop(0, '#9f7aeaff'); // purple-400
- gradient.addColorStop(1, '#ed64a6ff'); // pink-400
- ctx.value.fillStyle = gradient;
-};
+function toggleScale() {
+ if (spectrograph?.getUseLog()) spectrograph.setLinearScale();
+ else spectrograph?.setLogScale();
+}
onMounted(async () => {
- player.value = document.getElementById('video-source') as HTMLVideoElement;
- if (!player.value || !canvas.value) return;
+ if (!player?.value || !canvas.value) return;
spectrograph = new AudioSpectrograph(canvas.value, player.value);
-
- function resize() {
- if (!spectrograph || !canvas.value || !player.value) return;
- spectrograph.resize(player.value.offsetWidth, Math.floor(player.value.offsetHeight / 4));
- }
-
- resize();
+ resizeSpectrograph();
player.value.addEventListener('play', handlePlay);
player.value.addEventListener('pause', handlePause);
- window.addEventListener('resize', resize);
+ ro = new ResizeObserver(debounce(resizeSpectrograph, 150));
+ ro.observe(player.value);
});
onUnmounted(() => {
- isDrawing.value = false;
+ spectrograph?.destroy();
- if (player.value) {
+ if (player?.value) {
player.value.removeEventListener('play', handlePlay);
player.value.removeEventListener('pause', handlePause);
}
- spectrograph?.destroy();
- window.removeEventListener('resize', () => {});
+ ro?.disconnect();
});
+
+watch(
+ () => props.isEnabled,
+ (value) => {
+ if (spectrograph === null) return;
+ if (value) return handlePlay();
+ handlePause();
+ },
+);
+
+defineExpose({ toggleScale });
</script>
<template>
<Transition enter-to-class="opacity-100" enter-from-class="opacity-0" leave-from-class="opacity-100" leave-to-class="opacity-0">
- <canvas v-cloak v-show="isDrawing && isEnabled" aria-hidden="true" tabindex="-1" class="mx-auto mt-auto w-full opacity-0 transition-opacity duration-700" ref="canvas" />
+ <canvas v-cloak v-show="isEnabled && isDrawing" aria-hidden="true" tabindex="-1" class="mx-auto mt-auto w-full opacity-0 transition-opacity duration-700" ref="canvas" />
</Transition>
</template>
diff --git a/resources/js/components/video/audio/AudioSpectrographPanel.vue b/resources/js/components/video/audio/AudioSpectrographPanel.vue
new file mode 100644
index 00000000..91cf2bc4
--- /dev/null
+++ b/resources/js/components/video/audio/AudioSpectrographPanel.vue
@@ -0,0 +1,92 @@
+<script setup lang="ts">
+import { ButtonCorner, ButtonIcon, ButtonText } from '@/components/cedar-ui/button';
+import { ref } from 'vue';
+
+import BasicInput from '@/components/cedar-ui/input/BasicInput.vue';
+
+import ProiconsArrowSwap from '~icons/proicons/arrow-swap';
+import ProiconsCancel from '~icons/proicons/cancel';
+
+const props = defineProps<{
+ isMaximised: boolean;
+ closePanel: () => void;
+ toggleScale: () => void;
+}>();
+
+const minFreq = ref(100);
+const maxFreq = ref(16000);
+const minDb = ref(-60);
+const maxDb = ref(-24);
+const numBins = ref(128);
+const scale = ref<'log' | 'linear'>('log');
+</script>
+
+<template>
+ <div
+ class="w-fit rounded-md border border-neutral-700/10 bg-neutral-800/90 p-2 backdrop-blur-xs sm:min-w-52"
+ :class="['pointer-events-auto', { 'top-6 p-4': isMaximised }]"
+ style="z-index: 7"
+ >
+ <div class="scrollbar-minimal scrollbar-dark xs:h-16 flex h-12 max-h-64 w-full flex-col gap-1 overflow-y-auto pe-1 pb-1 *:ms-4 sm:h-auto sm:overflow-visible! sm:pe-0">
+ <div class="ms-0! flex items-center justify-between gap-2">
+ <h5>Audio Graph</h5>
+ <ButtonCorner
+ title="Close Options"
+ @click="closePanel"
+ colour-classes="hover:bg-transparent"
+ text-classes="hover:text-danger-2"
+ position-classes="size-4 sticky top-0 self-start mx-1"
+ >
+ <template #icon><ProiconsCancel /></template>
+ </ButtonCorner>
+ </div>
+ <div class="flex items-center justify-between gap-2">
+ <p title="File Buffer Health">
+ Scale: <span>{{ scale }}</span>
+ </p>
+
+ <ButtonIcon
+ class="hocus:ring-white size-6 h-full p-1 hover:bg-neutral-950"
+ :variant="'transparent'"
+ @click="
+ () => {
+ toggleScale();
+ scale = scale === 'log' ? 'linear' : 'log';
+ }
+ "
+ >
+ <ProiconsArrowSwap
+ /></ButtonIcon>
+ </div>
+ <template v-if="false">
+ <div class="flex items-center justify-between gap-2">
+ <label for="min-freq" title="File Buffer Health">Min Frequency:</label>
+ <BasicInput id="min-freq" type="number" min="100" max="15000" class="h-5.5 w-16" v-model="minFreq"></BasicInput>
+ </div>
+ <div class="flex items-center justify-between gap-2">
+ <label for="max-freq" title="File Buffer Health">Max Frequency:</label>
+ <BasicInput id="max-freq" type="number" min="200" max="16000" class="h-5.5 w-16" v-model="maxFreq"></BasicInput>
+ </div>
+ <div class="flex items-center justify-between gap-2">
+ <label for="min-db" title="File Buffer Health">Min Db:</label>
+ <BasicInput id="min-db" type="number" min="100" max="15000" class="h-5.5 w-16" v-model="minFreq"></BasicInput>
+ </div>
+ <div class="flex items-center justify-between gap-2">
+ <label for="max-db" title="File Buffer Health">Max Db:</label>
+ <BasicInput id="max-db" type="number" min="200" max="16000" class="h-5.5 w-16" v-model="maxFreq"></BasicInput>
+ </div>
+ <div class="flex items-center justify-between gap-2">
+ <label for="num-bins" title="File Buffer Health">Bin Count:</label>
+ <BasicInput id="num-bins" class="h-5.5 w-16" v-model="numBins" type="number" min="16" max="512"></BasicInput>
+ </div>
+
+ <ButtonText class="ml-auto! h-5.5 w-16">Apply</ButtonText>
+ </template>
+ </div>
+ </div>
+</template>
+<style lang="css" scoped>
+span {
+ color: var(--color-neutral-300); /* text-foreground-1 but dark only */
+}
+</style>
diff --git a/resources/js/service/audio/AudioSpectrograph.ts b/resources/js/service/audio/AudioSpectrograph.ts
index dcac31e3..4e498c26 100644
--- a/resources/js/service/audio/AudioSpectrograph.ts
+++ b/resources/js/service/audio/AudioSpectrograph.ts
@@ -1,8 +1,19 @@
-const FFT_SIZE = 2048;
-const DECIBELS_MIN = -70;
-const DECIBELS_MAX = -24;
+import type { FillStyleFactory } from './FillStyleFactory';
+
+import { purplePinkGradient } from './FillStyleFactory';
export class AudioSpectrograph {
+ private static readonly FFT_SIZE = 2048;
+ private static readonly DECIBELS_MIN = -66;
+ private static readonly DECIBELS_MAX = -24;
+ private static readonly BAR_GAP = 1;
+
+ private static readonly FREQ_MIN = 100;
+ private static readonly FREQ_MAX = 16000;
+ private static readonly BINS_PER_POINT = 2;
+ private static readonly LOG_BINS = 256;
+ private static readonly SMOOTHING_FACTOR = 0.95; // 0=instant, 0.98=very smooth, 1=frozen
+
private audioCtx: AudioContext | null = null;
private audioAnalyser: AnalyserNode | null = null;
private src: MediaElementAudioSourceNode | null = null;
@@ -12,16 +23,31 @@ export class AudioSpectrograph {
private ctx: CanvasRenderingContext2D;
private dataArray: Uint8Array<ArrayBuffer> | null = null;
+ private animFrameId: number | null = null;
+
public isDrawing = false;
+ private dpr = 1;
+
private useLog = false;
- private logBins = 32;
+ private logBins = AudioSpectrograph.LOG_BINS;
private logRanges: Array<[number, number]> = [];
+ private minFreq = AudioSpectrograph.FREQ_MIN;
+ private maxFreq = AudioSpectrograph.FREQ_MAX;
+ private binsPerPoint = AudioSpectrograph.BINS_PER_POINT;
+
+ private fillStyleFactory: FillStyleFactory = purplePinkGradient;
+ private cachedFillStyle: string | CanvasGradient | CanvasPattern | null = null;
+
+ private smoothedData: Float32Array | null = null;
+ private smoothingFactor = AudioSpectrograph.SMOOTHING_FACTOR;
+
constructor(canvas: HTMLCanvasElement, player: HTMLVideoElement) {
this.player = player;
this.canvas = canvas;
- this.ctx = canvas.getContext('2d', { willReadFrequently: true })!;
+ this.ctx = canvas.getContext('2d')!;
+ this.dpr = window.devicePixelRatio || 1;
}
async attach() {
@@ -30,147 +56,161 @@ export class AudioSpectrograph {
await this.audioCtx.resume();
}
+ if (this.src) return;
+
this.src = this.audioCtx.createMediaElementSource(this.player);
this.audioAnalyser = this.audioCtx.createAnalyser();
- this.audioAnalyser.fftSize = FFT_SIZE;
- this.audioAnalyser.minDecibels = DECIBELS_MIN;
- this.audioAnalyser.maxDecibels = DECIBELS_MAX;
+ this.audioAnalyser.fftSize = AudioSpectrograph.FFT_SIZE;
+ this.audioAnalyser.minDecibels = AudioSpectrograph.DECIBELS_MIN;
+ this.audioAnalyser.maxDecibels = AudioSpectrograph.DECIBELS_MAX;
this.src.connect(this.audioAnalyser);
this.audioAnalyser.connect(this.audioCtx.destination);
this.dataArray = new Uint8Array(this.audioAnalyser.frequencyBinCount);
+ this.smoothedData = new Float32Array(this.audioAnalyser.frequencyBinCount);
}
- setLogScale(bins: number = 32) {
- // This does not work
- if (!this.audioCtx || !this.audioAnalyser) {
- return;
- }
+ setLogScale(
+ bins: number = AudioSpectrograph.LOG_BINS,
+ options?: {
+ minFreq?: number;
+ maxFreq?: number;
+ binsPerPoint?: number;
+ },
+ ) {
+ if (!this.audioCtx || !this.audioAnalyser) return;
- // this.useLog = true;
this.logBins = bins;
+ this.minFreq = options?.minFreq ?? AudioSpectrograph.FREQ_MIN;
+ this.maxFreq = options?.maxFreq ?? AudioSpectrograph.FREQ_MAX;
+ this.binsPerPoint = options?.binsPerPoint ?? AudioSpectrograph.BINS_PER_POINT;
const nyquist = this.audioCtx.sampleRate / 2;
const binCount = this.audioAnalyser.frequencyBinCount;
+ const freqToBin = (f: number) => Math.round((f / nyquist) * binCount);
this.logRanges = [];
- let lastFreq = 20; // lowest useful audible freq
- const maxFreq = nyquist;
-
for (let i = 0; i < bins; i++) {
- const nextFreq = lastFreq * 2; // 1-octave doubling
+ // Evenly space bin edges on a log scale between minFreq and nyquist
+ const freqStart = this.minFreq * Math.pow(this.maxFreq / this.minFreq, i / bins);
+ const freqEnd = this.minFreq * Math.pow(this.maxFreq / this.minFreq, (i + 1) / bins);
+
+ const rawStart = freqToBin(freqStart);
+ const rawEnd = freqToBin(freqEnd);
- const start = Math.floor((lastFreq / maxFreq) * binCount);
- const end = Math.min(Math.floor((nextFreq / maxFreq) * binCount), binCount - 1);
+ const clampedEnd = Math.min(Math.max(rawStart + this.binsPerPoint - 1, rawEnd), binCount - 1);
- this.logRanges.push([start, end]);
- lastFreq = nextFreq;
- if (nextFreq > maxFreq) break;
+ this.logRanges.push([rawStart, clampedEnd]);
}
+
+ this.useLog = true;
}
setLinearScale() {
this.useLog = false;
}
- resize(width: number, height: number) {
- this.canvas.width = width;
- this.canvas.height = height;
+ setFillStyle(factory: FillStyleFactory) {
+ this.fillStyleFactory = factory;
+ this.cachedFillStyle = null;
+ }
+
+ getUseLog() {
+ return this.useLog;
+ }
+
+ private getFillStyle(): string | CanvasGradient | CanvasPattern {
+ if (!this.cachedFillStyle) {
+ const w = this.canvas.width / this.dpr;
+ const h = this.canvas.height / this.dpr;
+ this.cachedFillStyle = this.fillStyleFactory(this.ctx, w, h);
+ }
+ return this.cachedFillStyle;
+ }
+
+ resize(logicalWidth: number, logicalHeight: number) {
+ this.dpr = window.devicePixelRatio || 1;
+
+ this.canvas.width = Math.round(logicalWidth * this.dpr);
+ this.canvas.height = Math.round(logicalHeight * this.dpr);
+ this.canvas.style.width = `${logicalWidth}px`;
+ this.canvas.style.height = `${logicalHeight}px`;
+
+ this.ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0);
+ this.cachedFillStyle = null;
+ if (this.useLog) this.setLogScale(this.logBins);
}
async start() {
- if (this.isDrawing) return;
+ if (this.isDrawing) {
+ return;
+ }
if (!this.audioAnalyser || !this.dataArray) {
await this.attach();
}
this.isDrawing = true;
- this.draw();
+ this.scheduleFrame();
}
stop() {
this.isDrawing = false;
+ if (this.animFrameId !== null) {
+ cancelAnimationFrame(this.animFrameId);
+ this.animFrameId = null;
+ }
}
- private draw() {
- if (!this.isDrawing || !this.audioAnalyser || !this.dataArray) return;
-
- requestAnimationFrame(() => this.draw());
-
- // const WIDTH = this.canvas.width;
- // const HEIGHT = this.canvas.height;
-
- // this.audioAnalyser.getByteFrequencyData(this.dataArray);
-
- // this.ctx.clearRect(0, 0, WIDTH, HEIGHT);
-
- // const barWidth = (WIDTH / this.dataArray.length) * 2.5;
-
- // let x = 0;
-
- // const gradient = this.ctx.createLinearGradient(0, HEIGHT, 0, 0);
- // gradient.addColorStop(0, '#9f7aea');
- // gradient.addColorStop(1, '#ed64a6');
-
- // for (let i = 0; i < this.dataArray.length; i++) {
- // const barHeight = this.dataArray[i] / 2;
-
- // this.ctx.fillStyle = gradient;
- // this.ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);
+ private scheduleFrame() {
+ this.animFrameId = requestAnimationFrame(() => this.draw());
+ }
- // x += barWidth + 1;
- // }
+ private draw() {
+ if (!this.isDrawing || !this.audioAnalyser || !this.dataArray || !this.smoothedData) return;
- const HEIGHT = this.canvas.height;
- const WIDTH = this.canvas.width;
+ const WIDTH = this.canvas.width / this.dpr;
+ const HEIGHT = this.canvas.height / this.dpr;
+ const binCount = this.audioAnalyser.frequencyBinCount;
this.audioAnalyser.getByteFrequencyData(this.dataArray);
this.ctx.clearRect(0, 0, WIDTH, HEIGHT);
- this.ctx.fillStyle = this.ctx.fillStyle;
+ this.ctx.fillStyle = this.getFillStyle();
- const barWidth = (WIDTH / this.audioAnalyser.frequencyBinCount) * 2.5;
-
- let barHeight;
- let x = 0;
+ for (let i = 0; i < this.dataArray.length; i++) {
+ this.smoothedData[i] = this.smoothingFactor * this.smoothedData[i] + (1 - this.smoothingFactor) * this.dataArray[i];
+ }
if (this.useLog) {
- for (let i = 0; i < this.logRanges.length; i++) {
- const [start, end] = this.logRanges[i];
- if (i == 0) {
- console.log('draw log');
- }
- // max magnitude in that frequency range
+ const barWidth = Math.max(1, (WIDTH - this.logRanges.length * AudioSpectrograph.BAR_GAP) / this.logRanges.length);
+ let x = 0;
+
+ for (const [start, end] of this.logRanges) {
let max = 0;
for (let b = start; b <= end; b++) {
- if (this.dataArray[b] > max) max = this.dataArray[b];
+ if (this.smoothedData[b] > max) max = this.smoothedData[b];
}
-
- this.drawBar(x, max, barWidth);
- x += barWidth + 1;
+ const barHeight = (max / 255) * HEIGHT;
+ if (barHeight > 0) this.ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);
+ x += barWidth + AudioSpectrograph.BAR_GAP;
+ }
+ } else {
+ const visibleBins = Math.floor(binCount / 2);
+ const barWidth = Math.max(1, (WIDTH - visibleBins * AudioSpectrograph.BAR_GAP) / visibleBins);
+ let x = 0;
+ for (let i = 0; i < visibleBins; i++) {
+ const barHeight = (this.smoothedData[i] / 255) * HEIGHT;
+ if (barHeight > 0) {
+ this.ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);
+ }
+ x += barWidth + AudioSpectrograph.BAR_GAP;
}
- return;
- }
-
- for (let i = 0; i < this.audioAnalyser.frequencyBinCount; i++) {
- barHeight = this.dataArray[i];
-
- this.drawBar(x, barHeight, barWidth);
- x += barWidth + 1;
}
- }
-
- private drawBar(x: number, value: number, barWidth: number) {
- const HEIGHT = this.canvas.height;
- const barHeight = value * 0.75;
- // this.ctx.fillStyle = `rgb(${barHeight + 100} 50 50)`;
- this.ctx.fillStyle = `hsl(329.15 ${Math.min(value / HEIGHT / 3 + 70, 100)}% 45%)`;
- this.ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);
- // this.ctx.fillRect(x, HEIGHT - barHeight / 2, barWidth, barHeight / 2);
+ this.scheduleFrame();
}
destroy() {
@@ -179,5 +219,6 @@ export class AudioSpectrograph {
this.audioCtx = null;
this.audioAnalyser = null;
this.src = null;
+ this.dataArray = null;
}
}
diff --git a/resources/js/service/audio/FillStyleFactory.ts b/resources/js/service/audio/FillStyleFactory.ts
new file mode 100644
index 00000000..e315d5e0
--- /dev/null
+++ b/resources/js/service/audio/FillStyleFactory.ts
@@ -0,0 +1,29 @@
+export type FillStyleFactory = (ctx: CanvasRenderingContext2D, width: number, height: number) => string | CanvasGradient | CanvasPattern;
+
+export const purplePinkGradient: FillStyleFactory = (ctx, _w, h) => {
+ const g = ctx.createLinearGradient(0, h, 0, 0);
+ g.addColorStop(0, '#9f7aea');
+ g.addColorStop(1, '#E6005C');
+ return g;
+};
+
+export const solidColour =
+ (color: string): FillStyleFactory =>
+ () =>
+ color;
+
+export const verticalGradient =
+ (stops: [number, string][]): FillStyleFactory =>
+ (ctx, _w, h) => {
+ const g = ctx.createLinearGradient(0, h, 0, 0);
+ stops.forEach(([offset, colour]) => g.addColorStop(offset, colour));
+ return g;
+ };
+
+export const horizontalGradient =
+ (stops: [number, string][]): FillStyleFactory =>
+ (ctx, w, _) => {
+ const g = ctx.createLinearGradient(0, 0, w, 0);
+ stops.forEach(([offset, colour]) => g.addColorStop(offset, colour));
+ return g;
+ };