Select the types of activity you want to include in your feed.
[READ-ONLY] Mirror of https://github.com/flo-bit/svelte-audio-visualizations. a few different simple audio visualizations (esp for voice input/output) for svelte
flo-bit.dev/svelte-audio-visualizations/
···10101111## Usage
12121313-All visualizations expect some props to be passed to them.
1313+There are two ways you can use the visualizations:
1414+1515+1. Using with a `WavRecorder`, `WavStreamPlayer` or `AudioFilePlayer` instance. For this use the components in the `lib/visualizations/audio` folder ending with `AudioVisualizer`.
1616+1717+```svelte
1818+<script lang="ts">
1919+ import { AudioFilePlayer } from '$lib/visualizations/wavtools';
14201515-The one required prop is `audioInput`, which can be either of type `WavRecorder`, `WavStreamPlayer` or `AudioFilePlayer` or a function that returns a normalized (between 0-1) Float32Array of the current frequency data. for other props, see the individual visualizations.
2121+ let audio: AudioFilePlayer | null = null;
16221717-see the `src/routes/+page.svelte` file for an example of how to use the visualizations.
2323+ function playMusic() {
2424+ audio = new AudioFilePlayer();
2525+ audio.loadFile('/svelte-audio-visualizations/music.mp3').then(() => audio.play());
2626+ }
2727+</script>
2828+2929+<button on:click={playMusic}>Play music</button>
3030+3131+{#if audio}
3232+ <CircleBarAudioVisualizer {audio} />
3333+{/if}
3434+```
3535+3636+2. Passing in values yourself. For this use the components in the `lib/visualizations/core` folder. Ending just in `Visualizer`.
3737+3838+```svelte
3939+<script lang="ts">
4040+ import DeformedCircleVisualizer from '$lib/visualizations/core/DeformedCircleVisualizer.svelte';
4141+</script>
4242+4343+<CircleBarVisualizer values={new Float32Array([0, 1, 0, 1, 0, 1])} />
4444+```
4545+4646+For this a normalized Float32Array is expected, where each value is between 0 and 1. Also note, that the length of the array influences the visualizations (e.g. number of bars in the `BarVisualizer`). To convert from any length array, to a specific length, you can use the `normalizeArray` function.
4747+4848+```ts
4949+import { normalizeArray } from '$lib/visualizations/core/utils';
5050+5151+const values = new Float32Array([0, 1, 0, 1, 0, 1]);
5252+5353+const normalizedValues = normalizeArray(values, 10);
5454+```
5555+18561957## Credits
2058
-18
src/lib/visualizations/AudioFrequency.svelte
···11-<script lang="ts">
22- import type { WavRecorder, AudioFilePlayer } from '$lib/visualizations/wavtools';
33- import { raf } from "./raf";
44- import { normalizeArray } from "./wav_helper";
55-66- export let audio:AudioFilePlayer | WavRecorder | null;
77-88- let values:Float32Array = new Float32Array([0]);
99-1010- //Update frequency values every render frame
1111- raf(() => values = audio ? audio.getFrequencies('music').values : new Float32Array([0]));
1212-1313- //Recreate the getValues function when values changes (to trigger reactivity)
1414- $:getValues = (detail:number)=>normalizeArray(values, detail)
1515-1616-</script>
1717-1818-<slot {values} {getValues} />
···11const dataMap = new WeakMap();
2233+import { onMount } from 'svelte';
44+55+export async function raf(callback: () => void) {
66+ let mounted = true;
77+88+ async function render() {
99+ if (!mounted) return;
1010+ callback();
1111+ requestAnimationFrame(render);
1212+ }
1313+1414+ onMount(() => {
1515+ render();
1616+ return () => (mounted = false);
1717+ });
1818+}
1919+320/**
421 * Normalizes a Float32Array to Array(m): We use this to draw amplitudes on a graph
522 * If we're rendering the same audio data, then we'll often be using