Skip to content

Commit

Permalink
add scalable, scrollable example
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkev committed Apr 15, 2024
1 parent 9f17880 commit 47d2621
Show file tree
Hide file tree
Showing 11 changed files with 419 additions and 273 deletions.
1 change: 1 addition & 0 deletions docs/assets/index-BXBGKmvD.css

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/assets/index-CEAGp2kb.css

This file was deleted.

246 changes: 0 additions & 246 deletions docs/assets/index-D6Wr2L7K.js

This file was deleted.

292 changes: 292 additions & 0 deletions docs/assets/index-DdBohDTO.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tufte-css/1.8.0/tufte.min.css"> -->
<!-- <link rel="stylesheet" href="https://www.w3.org/StyleSheets/Core/Modernist" type="text/css"> -->

<title>Simple Vite + React + TS Library Template</title>
<script type="module" crossorigin src="./assets/index-D6Wr2L7K.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-CEAGp2kb.css">
<title>webgpu-waveform</title>
<script type="module" crossorigin src="./assets/index-DdBohDTO.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-BXBGKmvD.css">
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tufte-css/1.8.0/tufte.min.css"> -->
<!-- <link rel="stylesheet" href="https://www.w3.org/StyleSheets/Core/Modernist" type="text/css"> -->

<title>Simple Vite + React + TS Library Template</title>
<title>webgpu-waveform</title>
</head>

<body>
Expand Down
9 changes: 9 additions & 0 deletions src/site/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ footer a:link {
footer a:visited {
color: #888;
}

details > pre {
margin: 0;
padding: 0;
}

details > summary {
padding: 0px 1ch;
}
57 changes: 54 additions & 3 deletions src/site/Examples.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useEffect, useRef } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { GPUWaveform } from "../lib/GPUWaveform";
import { audioContext, loadSound, usePromise } from "./utils";
import { useWaveformRenderer } from "../lib/useWaveformRenderer";
import { GPUWaveformRenderer } from "../lib/GPUWaveformRenderer";
import { nullthrows } from "../lib/useWebGPU";

export function Example({
render,
Expand All @@ -21,12 +22,62 @@ export function Example({
}
}

export function Main({ audioBuffer }: { audioBuffer: AudioBuffer }) {
const [offsetFr, setOffsetFr] = useState(0);
const [frPerPx, setFrPerPx] = useState(441);

return (
<div
style={{
display: "flex",
flexDirection: "column",
border: "4px solid black",
padding: "1ch",
}}
>
<div style={{ display: "flex", flexDirection: "row" }}>
Offset:{" "}
<input
type="range"
min={0}
max={audioBuffer.length}
value={offsetFr}
onChange={(v) => {
const value = parseInt(v.target.value);
setOffsetFr(value);
}}
></input>{" "}
{offsetFr} frames
</div>
<div style={{ display: "flex", flexDirection: "row" }}>
Scale:{" "}
<input
type="range"
min={1}
max={1764}
value={frPerPx}
onChange={(v) => {
const value = parseInt(v.target.value);
setFrPerPx(value);
}}
></input>{" "}
{frPerPx} frames / pixel
</div>

<GPUWaveform
audioBuffer={audioBuffer}
scale={frPerPx}
offset={offsetFr}
height={100}
/>
</div>
);
}

async function example1(canvas: HTMLCanvasElement, audioBuffer: AudioBuffer) {
const channelData = audioBuffer.getChannelData(0);
const renderer = await GPUWaveformRenderer.create(canvas, channelData);

console.log("result", renderer);

renderer?.render(800, 0, canvas.width, canvas.height);
}

Expand Down
61 changes: 56 additions & 5 deletions src/site/Readme.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GPUWaveform, useWaveformRenderer } from "../index";
import { audioContext, loadSound, usePromise } from "./utils";
import { Example, Example1, Example2, Example3 } from "./Examples"
import { Example, Main, Example1, Example2, Example3 } from "./Examples"
import { useRef, useEffect } from 'react'
import GitHubButton from 'react-github-btn'

Expand All @@ -17,7 +17,7 @@ export function H3ID({ id, children }) {
Render waveforms to `<canvas />` using [WebGPU](https://developer.mozilla.org/en-US/docs/Web/API/WebGPU_API)

<p>
<a href="#"><i className="ri-github-fill" ></i> View on github</a>
<a href="https://github.com/mrkev/webgpu-waveform"><i className="ri-github-fill" ></i> View on github</a>
</p>


Expand All @@ -30,6 +30,60 @@ There's three ways to use this library:

_Note: Examples here use a waveform that looks like a cow. Source: [Japhy Riddle](https://www.youtube.com/watch?v=qeUAHHPt-LY)_

<Example render={audioBuffer => <Main audioBuffer={audioBuffer} />} />

<p>
<details>
<summary style={{background: 'black'}}>Source</summary>
```tsx
export function Main({ audioBuffer }: { audioBuffer: AudioBuffer }) {
const [offsetFr, setOffsetFr] = useState(0);
const [frPerPx, setFrPerPx] = useState(441);

return (
<div style={{ display: "flex", flexDirection: "column" }}>
<div style={{ display: "flex", flexDirection: "row" }}>
Offset:{" "}
<input
type="range"
min={0}
max={audioBuffer.length}
value={offsetFr}
onChange={(v) => {
const value = parseInt(v.target.value);
setOffsetFr(value);
}}
></input>{" "}
{offsetFr} frames
</div>
<div style={{ display: "flex", flexDirection: "row" }}>
Scale:{" "}
<input
type="range"
min={1}
max={1764}
value={frPerPx}
onChange={(v) => {
const value = parseInt(v.target.value);
setFrPerPx(value);
}}
></input>{" "}
{frPerPx} frames / pixel
</div>

<GPUWaveform
audioBuffer={audioBuffer}
scale={frPerPx}
offset={offsetFr}
height={50}
/>
</div>
);
}
```
</details>
</p>

<br />

<H3ID id={'GPUWaveformRenderer'}>Using the `GPUWaveformRenderer` class</H3ID>
Expand Down Expand Up @@ -149,9 +203,6 @@ Result:
<Example render={audioBuffer => <Example3 audioBuffer={audioBuffer} />} />
<footer>
Built by Kevin Chavez · <a href="https://twitter.com/aykev"><i className="ri-twitter-fill"></i></a> <a href="https://github.com/mrkev"><i className="ri-github-fill" ></i></a>
</footer>
5 changes: 2 additions & 3 deletions src/site/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ h1 {
line-height: 1.1;
margin: 0;
}

button {
border-radius: 8px;
/* border-radius: 8px; */
border: 1px solid transparent;
padding: 0.4em 1.2em;
padding: 2px 12px;
font-size: 1em;
font-weight: 500;
font-family: inherit;
Expand Down
12 changes: 1 addition & 11 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,7 @@ import rehypeSlug from "rehype-slug";
// Builds the site
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react(),
mdx({ rehypePlugins: [highlight, rehypeSlug] }),
// externalizeDeps({
// deps: false,
// devDeps: false,
// except: [],
// optionalDeps: true,
// peerDeps: true,
// }),
],
plugins: [react(), mdx({ rehypePlugins: [highlight, rehypeSlug] })],
root: "src",
build: {
outDir: "../docs",
Expand Down

0 comments on commit 47d2621

Please sign in to comment.