@@ -21,11 +21,34 @@ state management, implementing custom nodes, and adding interactivity.
21
21
22
22
<!--truncate-->
23
23
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!
29
52
30
53
But first...
31
54
@@ -65,12 +88,12 @@ Here are the highlights you need to know:
65
88
66
89
### Hello, sound!
67
90
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.
74
97
75
98
If you prefer another bundler like parcel or Create React App that's cool too, they
76
99
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!
81
104
npm create vite@latest -- --template react
82
105
```
83
106
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.
88
111
89
112
``` js title="./src/App.jsx"
90
113
// 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:
186
209
showEditor = { false }
187
210
/>
188
211
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?
189
216
If you're already familiar with React Flow, you can comfortably skip over the
190
217
next section and head straight on over to [ making some sounds] ( #do-sound-to-it ) .
191
218
For everyone else, let's take a look at how to build a React Flow project from
192
219
scratch.
220
+ :::
193
221
194
222
## Scaffolding a React Flow project
195
223
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.
199
226
For now though, we need to put together an empty React Flow app.
200
227
201
228
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() {
377
404
}
378
405
```
379
406
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.
384
411
385
412
Right now, our store is small and we actually want everything from it to help
386
413
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) => ({
415
442
416
443
OK great, we have an interactive React Flow instance we can start playing with.
417
444
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.
421
450
422
451
Let's create a new folder, ` nodes/ ` , and create a file for each custom node we
423
452
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
653
682
:::
654
683
655
684
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
657
686
have no parameters control, but we do want to toggle signal processing on and off.
658
687
That's a bit difficult right now when we haven't implemented any audio code yet,
659
688
so in the meantime we'll add just a flag to our store and an action to toggle it.
0 commit comments