Scratching the web
Scratching the web
By Pim van Die
5 min read
How can we use the Web Audio API to create a working realtime turntable? Read my approach and how you might make your own!
- Authors
- Name
- Pim van Die
- linkedinPim van Die
- Github
- githubpimskie
Browsers are awesome. The capabilities are almost endless with API's like WebRTC, WebGL, canvas, or the Web Audio API. This article will focus on the latter. Although the Web AUdio API can do a lot, I'm only going to cover the basics. I'll use it to load a track, control the playback speed, reverse it and eventually make a working turntable!
This article will consist of a few parts:
- The record: making it spin and able to drag cq. scratch
- The audio: loading, reversing and controlling the playback speed
- Tying the record to the audio
For the sake of the length of the article, the provided code blocks are simplified and are more like examples than actual implementations. Please refer to the Github repo here for the actual code.
An example of what can be made: The turntable
The record
A real turntable consists of a multitude of elements. Like pitch control, an arm with a needle of course and maybe a strobe light. But for this article I'll focus on the record only.
What should the record do?
So first we need to determine what functionality the record should have. In essence, it comes down to these:
- Rotate: Whether "by hand" or automatically.
- Stop: When it is clicked, it should stop. Simulating a finger pressed on the record.
- Dragging: Simulating the scratching effect, going back and forth.
The Record implementation
To implement the basic functionalities, a few properties need to be defined:
angle
: a number which tracks the current rotationisDragging
: whether the user is dragging the record (eg. when I'm "scratching")pointerStartPosition
: a position (x, y) of the starting drag positioncenter
: a x, y position indicating the centre of the UI element
Some functions and listeners:
pointerup
,pointerdown
andpointermove
to track the dragging/scratching.- A
loop
function called in arequestAnimationFrame
to update the UI.
Within the pointerdown
listener I'll indicate that the record should not rotate automatically and set the starting drag position:
onPointerDown(e: PointerEvent) {
isDragging = true;
pointerStartPosition = {
x: e.clientX,
y: e.clientY,
};
}
onPointerUp() {
isDragging = false;
}
Now a little math is required to determine the angle that was dragged. Take given example image:
The user clicks on spot "A", and moves the pointer towards "B". The center of the record is at "C".
The angle between "A" and "C", and "B" and "C" need to be calculated.
The difference between "AC" and "BC" is "D", the angle dragged.
The three built-in functions needed for the calculations are Math.atan2
, Math.sin
, and Math.cos
. With those 2 helper functions can be made:
const angleBetween = (vec1: Vector, vec2: Vector) = Math.atan2(vec2.y - vec1.y, vec2.x - vec1.x);
const angleDifference = ({ x, y }: Vector) => Math.atan2(Math.sin(x - y), Math.cos(x - y));
onPointerMove({ x, y }) {
const B: Vector = { x: clientX, y: clientY };
// A stored `onPointerDown`
const AC = angleBetween(center, A);
const BC = angleBetween(center, B);
const D = angleDifference({ x: AC, y: BC });
setAngle(angle - D);
}
Now the only thing needed is a looping function to update the record visual:
loop() {
if (!isDragging) {
setAngle(angle + 0.05);
}
element.style.transform = `rotate(${angle}rad)`;
requestAnimationFrame(loop);
}
Determine playback speed
The nitty gritty part: calculating the rotation speed which is also used for the playback speed. The following data is required:
- How many turns does the record make in 1 minute (RPM)?
- How many degrees should it rotate per MS, for a playback speed of 1?
- How many MS did elapse between 2 updates?
- How many degrees did the record actually rotate?
Altering the loop
function, it should look something like this:
loop() {
const currentTimestamp = performance.now();
const differenceTimestamp = currentTimestamp - previousTimestamp;
if (!isDragging) {
autoRotate(differenceTimestamp);
}
const differenceRotation = angle - previousAngle;
const normalRotation = RADIANS_PER_MS * differenceTimestamp;
const playbackSpeed = differenceRotation / normalRotation;
element.style.transform = `rotate(${angle}rad)`;
previousAngle = angle;
previousTimestamp = currentTimestamp;
requestAnimationFrame(loop);
}
Note that it calls the new function autoRotate
:
autoRotate(msElapsed: number) {
const rotationSpeed = RADIANS_PER_MS * msElapsed;
setAngle(angle + rotationSpeed);
}
So far the record can spin on its own, it can be dragged and the playback speed is calculated.
Time to look at the audio.
The Audio
Loading the audio is pretty straightforward using a simple fetch
.
The response, which will be an ArrayBuffer
, is used to create an AudioBuffer
. Once that is created, an AudioBufferSourceNode
can be made which will be the actual sound source that can be controlled.
const context = new AudioContext()
const response = await fetch(url)
const buffer = await response.arrayBuffer()
const audioBuffer = await context.decodeAudioData(buffer)
const source = new AudioBufferSourceNode(context)
source.buffer = audioBuffer
source.connect(context.destination)
source.start()
Setting the playback speed
Setting the playback speed is pretty easy. An AudioBufferSourceNode
as a playbackRate
property to control the speed:
// โ
play at double the speed
source.playbackRate.value = 2;
But the value has to be positive. So to play a track in reverse isn't as simple as:
// โ won't work
source.playbackRate.value = -2;
Reversing the audio
An audioBuffer has "channel data" which is a collection of "PCM data". Not interesting for now, but what is interesting, is the fact it is of type Float32Array
:
const channelData: Float32Array = audioBuffer.getChannelData(0)
And this is where the trick comes in. Like any array, a Float32Array
can be reversed. And a new AudioBuffer can be created with a given ChannelData:
// copy and reverse
const channelDataReversed = channelData.slice(0).reverse();
// create new audioBuffer with new channelData
const audioBuffer = new AudioBuffer(...)
audioBufferReversed.getChannelData(0).set(channelDataReversed);
Is it worth it? Let me work it
I put my thang down, flip it and reverse it
Ti esrever dna ti pilf nwod gnaht ym tup i - Missy Elliot
So when two audio buffers are available, they can easily be switched depending on the playback speed. For example:
const buffer = isReversed
? audioBufferReversed
: audioBuffer;
const cueTime = isReversed
? soundDuration - timePlayed
: timePlayed;
audioSource = new AudioBufferSourceNode(audioContext);
audioSource.buffer = buffer;
audioSource.start(0, cueTime);
Tying the record to the audio
One way to structure the project is as follows:
- App
- Record
- Audio
Where "Record" contains all the functionality from part 1, "Audio" all the functionality from part 2, and "App" orchestrates the working.
An easy implementation is when "Record" updates (the "loop" function), it calls a callback function in "App". "App" uses the payload from the callback to update the "Audio" accordingly. See this diagram:
For example:
loop() {
this.callbacks.onLoop({ speed, reversed, offset });
}
record.callbacks.onLoop = (updatePayload) => {
sampler.updateSpeed(updatePayload);
};
updateSpeed({ playbackSpeed, reversed, secondsPlayed }) {
if (reversed !== isReversed) {
changeDirection(reversed, secondsPlayed);
}
const speedAbsolute = Math.abs(playbackSpeed);
audioSource.playbackRate.cancelScheduledValues(currentTime);
audioSource.playbackRate.linearRampToValueAtTime(speedAbsolute, currentTime);
}
And there we have it! A working turntable, all with some good old plain javascript and the Web Audio API. Make sure to check out the final result:
Follow ups
This gives an example on how to create a turntable. But what's cooler than one turntable? Two!
The next iteration could be making two turntables and put a mixer in between. Maybe with some extra GainNode
?
See you in the next one!
Upcoming events
The Test Automation Meetup
PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED Test automation is a cornerstone of effective software development. It's about creating robust, predictable test suites that enhance quality and reliability. By diving into automation, you're architecting systems that ensure consistency and catch issues early. This expertise not only improves the development process but also broadens your skillset, making you a more versatile team member. Whether you're a developer looking to enhance your testing skills or a QA professional aiming to dive deeper into automation, RSVP for an evening of learning, delicious food, and the fusion of coding and quality assurance! ๐๐ 18:00 โ ๐ช Doors open to the public 18:15 โ ๐ Letโs eat 19:00 โ ๐ข First round of Talks 19:45 โ ๐น Small break 20:00 โ ๐ข Second round of Talks 20:45 โ ๐ป Drinks 21:00 โ ๐โโ๏ธ See you next time? First Round of Talks: The Power of Cross-browser Component Testing - Clarke Verdel, SR. Front-end Developer at iO How can you use Component Testing to ensure consistency cross-browser? Second Round of Talks: Omg who wrote this **** code!? - Erwin Heitzman, SR. Test Automation Engineer at Rabobank How can tests help you and your team? Beyond the Unit Test - Christian Wรผrthner, SR. Android Developer at iO How can you do advanced automated testing for, for instance, biometrics? RSVP now to secure your spot, and let's explore the fascinating world of test automation together!
| Coven of Wisdom - Amsterdam
Go to page for The Test Automation MeetupCoven of Wisdom - Herentals - Winter `24 edition
Worstelen jij en je team met automated testing en performance? Kom naar onze meetup waar ervaren sprekers hun inzichten en ervaringen delen over het bouwen van robuuste en efficiรซnte applicaties. Schrijf je in voor een avond vol kennis, heerlijk eten en een mix van creativiteit en technologie! ๐ 18:00 โ ๐ช Deuren open 18:15 โ ๐ Food & drinks 19:00 โ ๐ข Talk 1 20:00 โ ๐น Kleine pauze 20:15 โ ๐ข Talk 2 21:00 โ ๐โโ๏ธ Drinks 22:00 โ ๐ป Tot de volgende keer? Tijdens deze meetup gaan we dieper in op automated testing en performance. Onze sprekers delen heel wat praktische inzichten en ervaringen. Ze vertellen je hoe je effectieve geautomatiseerde tests kunt schrijven en onderhouden, en hoe je de prestaties van je applicatie kunt optimaliseren. Houd onze updates in de gaten voor meer informatie over de sprekers en hun specifieke onderwerpen. Over iO Wij zijn iO: een groeiend team van experts die end-to-end-diensten aanbieden voor communicatie en digitale transformatie. We denken groot en werken lokaal. Aan strategie, creatie, content, marketing en technologie. In nauwe samenwerking met onze klanten om hun merken te versterken, hun digitale systemen te verbeteren en hun toekomstbestendige groei veilig te stellen. We helpen klanten niet alleen hun zakelijke doelen te bereiken. Samen verkennen en benutten we de eindeloze mogelijkheden die markten in constante verandering bieden. De springplank voor die visie is talent. Onze campus is onze broedplaats voor innovatie, die een omgeving creรซert die talent de ruimte en stimulans geeft die het nodig heeft om te ontkiemen, te ontwikkelen en te floreren. Want werken aan de infinite opportunities van morgen, dat doen we vandaag.
| Coven of Wisdom Herentals
Go to page for Coven of Wisdom - Herentals - Winter `24 editionMastering Event-Driven Design
PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED Are you and your team struggling with event-driven microservices? Join us for a meetup with Mehmet Akif Tรผtรผncรผ, a senior software engineer, who has given multiple great talks so far and Allard Buijze founder of CTO and founder of AxonIQ, who built the fundaments of the Axon Framework. RSVP for an evening of learning, delicious food, and the fusion of creativity and tech! ๐ 18:00 โ ๐ช Doors open to the public 18:15 โ ๐ Letโs eat 19:00 โ ๐ข Getting Your Axe On Event Sourcing with Axon Framework 20:00 โ ๐น Small break 20:15 โ ๐ข Event-Driven Microservices - Beyond the Fairy Tale 21:00 โ ๐โโ๏ธ drinks 22:00 โ ๐ป See you next time? Details: Getting Your Axe On - Event Sourcing with Axon Framework In this presentation, we will explore the basics of event-driven architecture using Axon Framework. We'll start by explaining key concepts such as Event Sourcing and Command Query Responsibility Segregation (CQRS), and how they can improve the scalability and maintainability of modern applications. You will learn what Axon Framework is, how it simplifies implementing these patterns, and see hands-on examples of setting up a project with Axon Framework and Spring Boot. Whether you are new to these concepts or looking to understand them more, this session will provide practical insights and tools to help you build resilient and efficient applications. Event-Driven Microservices - Beyond the Fairy Tale Our applications need to be faster, better, bigger, smarter, and more enjoyable to meet our demanding end-users needs. In recent years, the way we build, run, and operate our software has changed significantly. We use scalable platforms to deploy and manage our applications. Instead of big monolithic deployment applications, we now deploy small, functionally consistent components as microservices. Problem. Solved. Right? Unfortunately, for most of us, microservices, and especially their event-driven variants, do not deliver on the beautiful, fairy-tale-like promises that surround them.In this session, Allard will share a different take on microservices. We will see that not much has changed in how we build software, which is why so many โmicroservices projectsโ fail nowadays. What lessons can we learn from concepts like DDD, CQRS, and Event Sourcing to help manage the complexity of our systems? He will also show how message-driven communication allows us to focus on finding the boundaries of functionally cohesive components, which we can evolve into microservices should the need arise.
| Coven of Wisdom - Utrecht
Go to page for Mastering Event-Driven Design