The Live Demo
The plan:
- Camera: acquire, transform, measure motion, track a coloured object.
- Microphone: acquire, measure loudness, split the sound into frequencies.
- Combine: let the sound change the picture.
- Out to the world: read a sensor and light an LED through an Arduino, and send the room's numbers to other programs such as TouchDesigner.
- Shaders: put the picture on the GPU, warp it, build a music visualizer, and bend it with the keyboard or mouse.
Every workflow you see below can be copy-pasted directly onto your canvas, or even as text in an email or message.
Part 1: Camera
Bonsai sees the world as streams. A camera is a stream of images. Every operator we add turns one stream into another, and every stream can be watched while it runs.
Exercise 1: Acquire and publish the camera image
- Start from the workflow you built in Getting Started, a
CameraCapturesource feeding aGrayscaletransform. - Select
CameraCapture, add aPublishSubjectand set itsNametoImage. - Move
Grayscaleoff the camera: delete it, then click the empty canvas, search the toolbox forImageand add it. AddGrayscaleafter it. - Press Start and double-click
Grayscaleto open its visualizer.
Note
A subject is a named stream. If we publish the camera image to a subject called Image, it appears in the toolbox, and we can subscribe to it anywhere else in the workflow as many times as we like.
Exercise 2: Transform
- Add a
Cannytransform afterGrayscale. SetThreshold1to 50 andThreshold2to 150. Run, and open the visualizer onCanny: edges only. - There are many transforms available in the Vision package, searchable in the toolbox. Try some others:
Threshold,Smooth,Flip.
Exercise 3: Motion energy
We want one number that says "how much is moving right now".
- Search the toolbox for
Imageand add it, thenGrayscale, then aBackgroundSubtractiontransform. SetAdaptationRateto 0.1. Run: still parts fade to black, moving parts glow. - Add a
Sumtransform (the one from Dsp). It adds up every pixel. - Right-click
Sumand selectScalar>Val0so the output is a plain number. - Publish this as
Motion. LikeImage, any later branch can now pick it up. - Run and open the visualizer on
Motion. Hold still. Wave.
Exercise 4: Track a coloured object
- Add
Imagefrom the toolbox, thenConvertColor, and setConversiontoBgr2Hsv. - Add
HsvThreshold. For a blue object, setLowerto 100, 100, 100 andUpperto 130, 255, 255. Run, open its visualizer, and adjust the bounds until only your object is white. Hue is the first value. - Add
FindContours, thenBinaryRegionAnalysis, thenLargestBinaryRegion. - Right-click
LargestBinaryRegionand selectConnectedComponent>Centroid. This chain of threshold, contours and largest region is commonly known as blob tracking. - Select every node from
ConvertColortoCentroid, right-click and choose Group (Ctrl+G). Name the groupColourTracking. The whole recipe is now one node; double-click it to look inside. - Inside the group, right-click
HsvThresholdand choose Externalize Property forLower, then again forUpper. Now you can retune the colour from the group's properties without opening it. - Publish this as
Position. - Run: the visualizer on
Positiondraws the object's position.
Part 2: Microphone
Audio arrives as buffers: chunks of a few hundred samples at a time. Bonsai treats a buffer as a matrix, so the same maths operators that work on images work on sound.
Exercise 5: Acquire
- Add an
AudioCapturesource. LeaveDeviceNameempty for the default microphone. - Publish this as
Audio, the microphone's twin ofImage. - Run and open the visualizer on
AudioCapture. Talk. Clap. - Optional: add an
AudioWritersink and setFileNameto a name ending in.wavto save the sample, run for a few seconds, stop, and play the file.
Exercise 6: Loudness
We want one number for "how loud is it right now", the audio twin of Motion.
- Add
Audiofrom the toolbox, thenConvertScale, and setDepthtoF32. The microphone records 16-bit whole numbers, which top out at 32767, and squaring a loud sample in the next step would overflow them. 32-bit decimals have room; without this step the loudness never climbs past about 180. - Add
Pow(from Dsp) and setPowerto 2. Negative and positive swings now both count. - Add
RunningAveragewithAlpha0.2. This blends each buffer with the ones before it, so the number moves smoothly instead of jittering. A smallerAlphais smoother but slower to react. - Add
Average. One number per buffer: the mean power. - Right-click
Averageand selectScalar>Val0. - Add another
Powand setPowerto 0.5. This is the square root, so the number is back in the units of the waveform. This chain is called the root-mean-square, or RMS. - Select every node from
ConvertScaleto the secondPow, right-click and choose Group (Ctrl+G). Name the groupRMS. We will not need to open it again. - Publish this as
Loudness. - Run and open the visualizer on
Loudness. Whisper. Shout.
Note
The raw value depends on your microphone and its gain. Make a note of the range you see when quiet and when loud, you will map it in the next part.
Exercise 7: Spectrum
A Fourier transform splits each buffer of sound into its frequencies, from low to high.
- Add
Audiofrom the toolbox, thenDiscreteFourierTransform, thenMagnitude. - Run and open the visualizer on
Magnitude. Notice the peaks sit at both edges. The transform outputs every frequency twice, mirrored, with the low notes at each end. - Add a
SubmatrixafterMagnitudeand setEndColto 60. Each column is 100 Hz wide, so this keeps 0 to 6000 Hz, which covers voices and whistles, with low notes on the left. - Open the visualizer on
Submatrix. Whistle and watch the peak slide as the pitch changes.
Part 3: Combine
We can change an operator's properties in the property editor while the workflow is running, but they can also be driven by another sequence in the workflow.
Exercise 8: Volume decides the thresholds
The Canny edge detection operator we used in Exercise 2 has two thresholds, which you can modify by hand in the property editor, even while the workflow is running. These properties can also be externalized and driven from any other stream in the workflow in real time.
- Right-click the
Cannynode from Exercise 2 and choose Externalize Property >Threshold1. A property node appears, feedingCanny. Every number that flows into it now overwritesThreshold1. - Our
Loudnesssubject carries numbers far higher than we would want to set onCanny, whose thresholds do their useful work between about 10 and 400. So we need to rescale the data to match the range it expects. Search the toolbox forLoudnessand add it, then addRescaleafter it withMinandMaxset to the quiet and loud values you noted,RangeMin200,RangeMax10 andRescaleTypeClamp. BecauseRangeMinis bigger thanRangeMax,Rescalealso flips the direction, so loud means low thresholds and more lines. ConnectRescaleinto theThreshold1node. - From the same
Loudnessnode, add a secondRescalewithRangeMin400 andRangeMax30. ExternalizeThreshold2fromCannythe same way and connect thisRescaleinto it. - Run and open the visualizer on
Canny. Quiet: a few bold outlines. Loud: the picture fills with lines. - Question: can you drive the thresholds from the
Motionnumber you made in Exercise 3 instead? What does moving do to the picture?
Part 4: Out to the world
So far every sensor has been on your laptop, and every number has remained inside Bonsai. However, Bonsai can talk to a wide variety of hardware and devices: cameras, microcontrollers, game controllers. If Bonsai cannot speak to your device, please get in touch!
Exercise 9: A sensor and an LED
One of the most common microcontrollers is the Arduino, and with the Firmata sketch on it every pin becomes a stream Bonsai can read or write. The kit on the table has dials, light, sound, distance and touch sensors, and LEDs, buzzers and motors to drive. This exercise reads one sensor and lights one LED.
- Plug the Arduino into USB and a sensor (the rotation dial, the light sensor or the sound sensor) into the
A0socket on the shield. Pin 13 is the built-in LED on the Arduino itself, but you can drive any output pin on the board. - Add
CreateArduino. SetNametoBoardandPortNameto the board's port. - Add
AnalogInput. SetPortNametoBoardandPinto 0. Run and open its visualizer: a number from 0 to 1023. Activate your sensor and watch how far the number moves. Note where it sits at rest and where it goes when you activate it. - Add
GreaterThanafter it and setValueto a number between those two readings, so that activating the sensor crosses it. The number becomes true or false. - Add
DistinctUntilChanged. The sensor reports many times a second, so without it the board is senttrueover and over; now it only hears about it when the answer flips. - Add
DigitalOutput. SetPortNametoBoardandPinto 13, the LED built into the board. - Run the workflow, and flash your LED using your sensor.
- Into the demo: try connecting your sensor to the
Cannythresholds from Exercise 8 in place ofLoudness. SetMinandMaxon bothRescalenodes to your sensor's rest and active readings.
Tip
AnalogInput gives whole numbers, but Rescale expects a double. Add an ExpressionTransform with Convert.ToDouble(it) in between to convert the number to the type it expects.
Exercise 10: Send to other programs
Bonsai does not have to draw the picture itself. It is just as happy being the sensing layer for another tool: it reads the cameras, microphones and hardware, turns them into a few clean numbers, and hands them to TouchDesigner, Max, Processing, a DAW, or a second laptop. Here we send the two numbers from this demo to a TouchDesigner patch called Ember Pulse, and you can take it home: it is touchdesigner/ember-pulse.toe in the workshop folder, and it opens in the free version of TouchDesigner.
Ember Pulse has five dials, each a number from 0 to 1 with a name: /pulse for the heartbeat, /spin for the rotation, /zoom, /hue for the colour and /trail for how long the trails last. A dial nobody sends stays at rest. We will turn two of them.
- Add
CreateUdpClient(from Osc). SetNametoOut,RemoteHostNameto127.0.0.1andRemotePortto 8000. Use another machine's IP address to send across the network instead. - Add
Motionfrom the toolbox, thenRescale,ExpressionTransformandSendMessagewithConnectionOutandAddress/pulse. The example TouchDesigner sketch expects normalised float values between 0 and 1, so we rescale and convert the type to match. Set theRescaleMaxto a "lots of movement" value from yourMotionvisualizer andRescaleTypetoClamp, and theExpressionTransformtoConvert.ToSingle(it); the workflow above has the rest of the values. - Repeat for
Loudness, with the loud value you noted asMax, and send it to/hue. - Run. Anything listening on port 8000 now receives two named channels.
- If you have the free version of TouchDesigner installed, you can immediately open
touchdesigner/ember-pulse.toe. Move and the heartbeat quickens and the embers thicken. Make noise and the colour drifts. - Optional: the addresses are just names. Try sending
Motionto/hue, andLoudnessto/pulse. OrLoudnessto/spin.
Tip
Only one program can listen on a port. If nothing arrives, check that no other app (or a second copy of Bonsai) is already using port 8000.
Part 5: Shaders
TouchDesigner turns our numbers into pictures with shaders. Bonsai has a shaders package of its own, so we can build the picture right here too.
So far the CPU has done all the work. A shader is a small program that runs on the GPU, which runs it for every pixel of the window at the same time. We hand it the camera image as a texture, it decides the colour of each pixel, and the window is painted.
A shader is two text files, both in the shaders folder at the top of the workshop folder. quad.vert stretches a rectangle over the whole window and camera.frag decides the colour of every pixel in that rectangle. Before it can draw, Bonsai needs to know three things: which shader to use, which image to hand it, and what shape to draw it on.
Keep working in playground.bonsai, or another workflow saved in the workshop folder, because the shader files are found relative to it.
Exercise 11: Warp
A shader cannot see the rest of the workflow. Its way in is a uniform: a named number the shader declares and the workflow sets with UpdateUniform. camera.frag has two. amount drives both the ripple and the colour shift, so a whisper barely moves the picture and a shout bends it into bands of colour. time makes the ripple flow.
- Copy the workflow above onto your canvas and run it. Speak and the picture bends. Shout and the colours bleed into rainbow.
- The setup. The first three branches set up the shader, draw each frame, and upload
Imageas the texture. - The
amountbranch.Loudnessgoes throughRescale(Min60,Max600,RangeMin0,RangeMax0.3,RescaleTypeClamp), then anExpressionTransformwithConvert.ToSingle(it), because uniforms take floats, thenUpdateUniformwithShaderNameCameraandUniformNameamount. SettingMinto 60 instead of 0 means background chatter leaves the picture still; only real noise moves it. - The
timebranch. FromRenderFrame,TimeStep>ElapsedTimegives the time since the last frame.Accumulateadds these up into a running clock, which goes throughConvert.ToSingle(it)intoUpdateUniformwithUniformNametime. This is what makes the ripple flow down the picture; without it the bend grows and shrinks but does not move. - Optional: make it flow only while people move: in the
timebranch, replaceElapsedTimewithMotionfrom the toolbox and aRescale(Min0,Maxyour "lots of movement" value,RangeMin0,RangeMax0.1,RescaleTypeClamp), and keepAccumulateafter it. Every bit of movement nudges the wave along; hold still and it freezes. - Optional: open
camera.fragin a text editor, change the20.0in thesinline to5.0, save, and restart the workflow. Shaders reload on start, so restart after every edit.
Any stream can drive a uniform, so anything Bonsai can read becomes a control.
Exercise 12: Keyboard
- Delete the
Loudnessbranch that setsamountfirst, or the two will fight over the same uniform. - Add a
KeyDownsource from Shaders and setKeytoUp. It only hears keys while the shader window has focus, so click that window before pressing anything. After it add aFloatand setValueto 0.03. - Add a second
KeyDownwithKeyDown, and aFloatwithValue-0.03. - Join the two with
Merge, addAccumulate, thenUpdateUniformforamount. - Run, click the shader window, and press the arrow keys. Each press nudges the warp.
Exercise 13: Mouse
- Replace the keyboard with the mouse cursor: delete the two
KeyDownbranches from Exercise 12 first. - Add a
MouseMovesource from Shaders, thenNormalizedDeviceCoordinatesand selectX. - Add
ExpressionTransformwithConvert.ToDouble(it), soRescalecan take the number. - Add
Rescale(Min-1,Max1,RangeMin0,RangeMax0.3,RescaleTypeClamp),ExpressionTransformwithConvert.ToSingle(it), andUpdateUniformforamount. - Run and slide the mouse across the shader window.
Exercise 14: Winamp Visualizer
Winamp's MilkDrop visualizer works on one main trick. Every frame, the GPU reads back the picture it drew last frame, zooms and rotates it, dims it a little, and paints fresh colour on top. Sound decides how much zoom, how much spin and how much colour. A MilkDrop preset is a list of equations that map bass, mid and treble onto those dials. In this exercise, we build such a preset by hand to demonstrate how this would work in Bonsai. The shaders in shaders/warp.frag, shaders/copy.frag and shaders/screen.frag are the equations, and the workflow provides the plumbing between audio and visualization.
The workflow has three parts: a feedback loop on the GPU, an audio analysis pipeline that extracts bass, mid, and treble from the microphone, and then uniforms that connect these streams together.
It brings its own window and shaders, which clash with those of Exercise 11. Select the Exercise 11 branches, right-click and choose Disable, then paste this workflow beside the rest of your demo. It listens to the Audio subject from Exercise 5, so keep that running.
- The loop. The
Texturesoperator definesDefaultFeedbackandDefaultWorktextures to hold the last frame on the GPU (feedback) and the current frame being drawn (work). EachRenderFrameruns fourDrawMeshcalls in order.DefaultWarpsamplesDefaultFeedback, zooms, rotates and fades it, adds a wandering blob of colour, and draws intoDefaultWork.DefaultWaveformdraws the raw audio signal as a line on top, which is fed into the waveform shader directly as vertex buffer data.DefaultCopycopiesDefaultWorkback intoDefaultFeedback, ready for the next frame.DefaultScreendrawsDefaultWorkto the window with a mirrored echo, a hue drift and a vignette, none of which feed back. Each shader's target texture is set underFramebufferAttachmentsinShaderResources. With this pipeline, we get one shader that draws to the screen and one shader that remembers and warps what was previously drawn. - Audio in. The
Audiosubject from Exercise 5 delivers samples at 44100 Hz. They are rescaled to -2 to 2, andBufferwithCount1024 andSkip100 cuts the stream into overlapping windows of 1024 samples, a new one every 100 samples. One branch sends the buffered audio data straight toUpdateVertexBufferas the lineDefaultWaveformdraws. - Spectrum. As in Exercise 7, the audio is transformed into the frequency domain with
DiscreteFourierTransformthenMagnitude, keeping the first 512 columns asPower. With 1024 samples at 44.1 kHz, each column is about 43 Hz wide. - Slow average. A second branch from
Magnitudegoes throughIirFilterwith coefficients0,0.01and1,-0.99. Every column now creeps one percent of the way toward its new value each buffer, so it remembers what the room has sounded like for the last few seconds. This isAttenuated. - Three bands.
Submatrixcuts columns 0 to 6 (bass, below 250 Hz), 6 to 93 (mid, below 4 kHz) and 93 to 465 (treble, below 20 kHz) from bothPowerandAttenuated.Sum, thenVal0, thensingle(it)turn each slice into one float, andUpdateUniformhands it to the shaders asbass,mid,trebleand their_atttwins. - Louder than baseline. The shader never uses a band on its own. It divides each one by its slow average:
bass / bass_attis 1 when the room sounds as it usually does, 3 on a kick drum. That ratio sets the zoom,mid / mid_attsets the spin, andtreble / treble_attsets how bright the blob paints. Because it is a ratio, the picture reacts the same whatever the microphone gain, which is why there is noRescaleto tune this time. - Run. Silence: a slow spiral fading to black. Music: the picture breathes with the bass and sparkles with the hi-hats.
- Optional:
warp.fragdeclares its dials as uniforms with defaults, such asprev_scalefor the fade andzoom_scalefor how hard the bass pushes. How would you modify the Bonsai workflow to allow changing these dials dynamically? Hint: take a look at howbass,bass_attare fed in as uniforms withUpdateUniform. - Question: swap the
UniformNameon the mid and bass branches so the drums spin the picture and the voice zooms it. Which feels more musical?
Next: Fork It.