Skip to content

Commit

Permalink
fix: Sampler documentation (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
danigb authored Feb 18, 2024
1 parent 8abc8d3 commit 0e38c71
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,21 @@ const piano = new SplendidGrandPiano(context, { storage });

### Sampler

An audio buffer sampler.
An audio buffer sampler. Pass a `buffers` object with the files to be load:

```js
import { Sampler } from "smplr";

const samples = {
const buffers = {
kick: "https://danigb.github.io/samples/drum-machines/808-mini/kick.m4a",
snare: "https://danigb.github.io/samples/drum-machines/808-mini/snare-1.m4a",
};
const sampler = new Sampler(new AudioContext(), { samples });
const sampler = new Sampler(new AudioContext(), { buffers });
```

And then use the name of the buffer as note name:

```js
sampler.start({ note: "kick" });
```

Expand Down
23 changes: 5 additions & 18 deletions site/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"webmidi": "^3.1.5"
},
"devDependencies": {
"@next/font": "^13.4.2",
"@types/node": "^20.1.7",
"@types/react": "^18.2.6",
"@types/react-dom": "^18.2.4",
Expand Down
2 changes: 1 addition & 1 deletion site/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inter } from "@next/font/google";
import { Inter } from "next/font/google";
import Head from "next/head";
import { DrumMachineExample } from "src/DrumMachineExample";
import { ElectricPianoExample } from "src/ElectricPianoExample";
Expand Down
49 changes: 49 additions & 0 deletions site/pages/tests.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Inter } from "next/font/google";
import Head from "next/head";
import { useEffect, useRef } from "react";
import { Sampler } from "smplr";
import { getAudioContext } from "src/audio-context";

const inter = Inter({ subsets: ["latin"] });

export default function Tests() {
const samplerRef = useRef<Sampler | null>(null);

useEffect(() => {
const audioContext = getAudioContext();
const sampler = new Sampler(audioContext, {
buffers: { test: "test.mp3" },
});
samplerRef.current = sampler;
}, []);

return (
<>
<Head>
<title>smplr</title>
<meta name="description" content="Plug and play web instruments" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</Head>
<main className={"max-w-4xl mx-auto my-20 p-4" + inter.className}>
<div className="flex items-end mb-16">
<h1 className="text-6xl font-bold">smplr</h1>
</div>

<div>
<button
onClick={() => {
samplerRef.current?.start({
note: "test",
loop: true,
loopStart: 1.0,
loopEnd: 9.0,
});
}}
>
Play
</button>
</div>
</main>
</>
);
}
Binary file added site/public/test.mp3
Binary file not shown.

0 comments on commit 0e38c71

Please sign in to comment.