Skip to content

Commit 05b44fb

Browse files
✨ Add a hook to reel people in.
1 parent 9ddb676 commit 05b44fb

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

blog/2023-04-14-integrating-react-flow-and-the-web-audio-api.mdx

+55-26
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,34 @@ state management, implementing custom nodes, and adding interactivity.
2121

2222
<!--truncate-->
2323

24-
This tutorial will go step-by-step. We may skip over some bits here and there,
25-
but for the most part if you're new to React Flow you should be able to follow
26-
along and have something working by the end. If you're already a React Flow wizard
27-
you might want to read the first section covering the Web Audio API and then jump
28-
to the third to see how things are tied together!
24+
<figure>
25+
<img
26+
src="/img/blog/webaudio/bleep-cafe.png"
27+
alt="A screenshot of bleep.cafe, a visual audio programming environment. In
28+
it, there are four nodes connected together: an xy pad, an oscillator node,
29+
a volume node, and a master output."
30+
/>
31+
<figcaption>
32+
This is <a href="https://bleep.cafe">bleep.cafe</a>. We're going to learn everything we need to
33+
know to build something just like it!
34+
</figcaption>
35+
</figure>
36+
37+
A while back I shared a project I was working on to the React Flow
38+
[discord server](https://discord.com/invite/RVmnytFmGW). It's called
39+
[bleep.cafe](https://bleep.cafe) and it's a little web app for learning digital
40+
synthesis all inside the browser. A lot of folks were interested to see how something
41+
like that was put together: most people don't even know **their browser has a whole
42+
synth engine built in!**
43+
44+
This tutorial will take us step-by-step to build something similar. We may skip
45+
over some bits here and there, but for the most part if you're new to React Flow
46+
_or_ the Web Audio API you should be able to follow along and have something
47+
working by the end.
48+
49+
If you're already a React Flow wizard you might want to read the first section
50+
covering the Web Audio API and then jump to the third to see how things are tied
51+
together!
2952

3053
But first...
3154

@@ -65,12 +88,12 @@ Here are the highlights you need to know:
6588

6689
### Hello, sound!
6790

68-
They say show don't tell, but doing is even better. Let's see some of this stuff
69-
in action and build our first Web Audio app! We won't be doing anything too wild:
70-
we'll make a simple mouse [theremin](http://www.thereminworld.com/Article/14232/what-s-a-theremin-).
71-
We'll use React for these examples and everything else moving forward (we're called
72-
React Flow after all!) and [`vite`](https://vitejs.dev) to handle bundling and
73-
hot reloading.
91+
Let's see some of this stuff in action and build our first Web Audio app! We won't
92+
be doing anything too wild: we'll make a simple mouse
93+
[theremin](http://www.thereminworld.com/Article/14232/what-s-a-theremin-). We'll
94+
use React for these examples and everything else moving forward (we're called React
95+
Flow after all!) and [`vite`](https://vitejs.dev) to handle bundling and hot
96+
reloading.
7497

7598
If you prefer another bundler like parcel or Create React App that's cool too, they
7699
all do largely the same thing. You could also choose to use TypeScript instead
@@ -81,10 +104,10 @@ fully typed (and written entirely in TypeScript) so it's a breeze to use!
81104
npm create vite@latest -- --template react
82105
```
83106

84-
Vite will helpfully scaffold out a simple React application for us. We can delete
85-
the assets and jump right into `App.jsx`. Remove the demo component generated for
86-
us and start by creating a new AudioContext and putting together the nodes we need.
87-
We want an OscillatorNode to generate some tones and a GainNode to control the volume.
107+
Vite will scaffold out a simple React application for us, but can delete the assets
108+
and jump right into `App.jsx`. Remove the demo component generated for us and start
109+
by creating a new AudioContext and putting together the nodes we need. We want
110+
an OscillatorNode to generate some tones and a GainNode to control the volume.
88111

89112
```js title="./src/App.jsx"
90113
// Create the brain of our audio-processing graph
@@ -186,16 +209,20 @@ Here's what we put together, in case you weren't following along at home:
186209
showEditor={false}
187210
/>
188211

212+
Now let's put this knowledge to one side and take a look at how to build a React
213+
Flow project from scratch.
214+
215+
:::info Already a React Flow pro?
189216
If you're already familiar with React Flow, you can comfortably skip over the
190217
next section and head straight on over to [making some sounds](#do-sound-to-it).
191218
For everyone else, let's take a look at how to build a React Flow project from
192219
scratch.
220+
:::
193221

194222
## Scaffolding a React Flow project
195223

196-
With that done, we can now turn our attention to React Flow and the rest of the
197-
tutorial. Later on we'll take what we've learned about the Web Audio API, oscillators,
198-
and gain nodes and use React Flow to interactively build audio-processing graphs.
224+
Later on we'll take what we've learned about the Web Audio API, oscillators, and
225+
gain nodes and use React Flow to interactively build audio-processing graphs.
199226
For now though, we need to put together an empty React Flow app.
200227

201228
We already have a React app set up with Vite, so we'll keep using that. If you
@@ -377,10 +404,10 @@ export default function App() {
377404
}
378405
```
379406

380-
OK that look's a bit weird, what's this `selector` thing all about? Zustand let's
381-
us supply a selector function to pluck out the exact bits of state we need from
382-
the store. Combined with the `shallow` equality function, this means we typically
383-
don't have re-renders when state we don't care about changes.
407+
So what's this `selector` thing all about? Zustand let's us supply a selector
408+
function to pluck out the exact bits of state we need from the store. Combined
409+
with the `shallow` equality function, this means we typically don't have re-renders
410+
when state we don't care about changes.
384411

385412
Right now, our store is small and we actually want everything from it to help
386413
render our React Flow graph, but as we expand on it this selector will make sure
@@ -415,9 +442,11 @@ const useStore = create((set, get) => ({
415442

416443
OK great, we have an interactive React Flow instance we can start playing with.
417444
We added some dummy nodes but they're just the default unstyled ones right now.
418-
In this step we'll add three custom nodes with interactive controls: an oscillator
419-
node and controls for the pitch and waveform type, a gain node and a control for
420-
the volume, and an output node and a button to toggle audio processing on and off.
445+
In this step we'll add three custom nodes with interactive controls:
446+
447+
1. An oscillator node and controls for the pitch and waveform type.
448+
2. A gain node and a control for the volume
449+
3. An output node and a button to toggle audio processing on and off.
421450

422451
Let's create a new folder, `nodes/`, and create a file for each custom node we
423452
want to create. Starting with the oscillator we need two controls and a source
@@ -653,7 +682,7 @@ To learn how to style your custom nodes, check out our docs on
653682
:::
654683

655684
Implementing a gain node is pretty much the same process, so we'll leave that one
656-
down to you. Instead, we'll turn our attention to the output node. This node will
685+
to you. Instead, we'll turn our attention to the output node. This node will
657686
have no parameters control, but we do want to toggle signal processing on and off.
658687
That's a bit difficult right now when we haven't implemented any audio code yet,
659688
so in the meantime we'll add just a flag to our store and an action to toggle it.

0 commit comments

Comments
 (0)