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!

Scratching the web
Authors

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:

  1. The record: making it spin and able to drag cq. scratch
  2. The audio: loading, reversing and controlling the playback speed
  3. 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 rotation
  • isDragging: whether the user is dragging the record (eg. when I'm "scratching")
  • pointerStartPosition: a position (x, y) of the starting drag position
  • center: a x, y position indicating the centre of the UI element

Some functions and listeners:

  • pointerup, pointerdown and pointermove to track the dragging/scratching.
  • A loop function called in a requestAnimationFrame to update the UI.

Within the pointerdown listener I'll indicate that the record should not rotate automatically and set the starting drag position:

Record.js
 onPointerDown(e: PointerEvent) {
  isDragging = true;

  pointerStartPosition = {
    x: e.clientX,
    y: e.clientY,
  };
}
Record.js
onPointerUp() {
  isDragging = false;
}

Now a little math is required to determine the angle that was dragged. Take given example image:

Calculating the angle

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:

Record.js
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:

Record.js
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?
Canculating playback speed

Altering the loop function, it should look something like this:

Record.js
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:

Record.js
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.

Audio.js
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:

Audio.js
// ✅ 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:

Audio.js
// ❌ 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:

Audio.js
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:

Audio.js
// 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:

Audio.js
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:

Callback and update

For example:

Record.js
loop() {
  this.callbacks.onLoop({ speed, reversed, offset });
}
App.js
record.callbacks.onLoop = (updatePayload) => {
  sampler.updateSpeed(updatePayload);
};
Audio.js
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:

The 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

  • Mastering 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
  • The Leadership Meetup

    PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED What distinguishes a software developer from a software team lead? As a team leader, you are responsible for people, their performance, and motivation. Your output is the output of your team. Whether you are a front-end or back-end developer, or any other discipline that wants to grow into the role of a tech lead, RSVP for an evening of learning, delicious food, and the fusion of leadership and tech! 🚀 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: Pixel Perfect and Perfectly Insane: About That Time My Brain Just Switched Off Remy Parzinski, Design System Lead at Logius Learn from Remy how you can care for yourself because we all need to. Second Round of Talks: Becoming a LeadDev at your client; How to Fail at Large (or How to Do Slightly Better) Arno Koehler Engineering Manager @ iO What are the things that will help you become a lead engineer? Building Team Culture (Tales of trust and positivity) Michel Blankenstein Engineering Manager @ iO & Head of Technology @ Zorggenoot How do you create a culture at your company or team? RSVP now to secure your spot, and let's explore the fascinating world of design systems together!

    | Coven of Wisdom - Amsterdam

    Go to page for The Leadership Meetup
  • Coven of Wisdom - Herentals - Spring `24 edition

    Join us for an exciting web technology meetup where you’ll get a chance to gain valuable insights and knowledge about the latest trends in the field. Don’t miss out on this opportunity to expand your knowledge, network with fellow developers, and discover new and exciting possibilities. And the best part? Food and drinks are on us! Johan Vervloet - Event sourced wiezen; an introduction to Event Sourcing and CQRS Join me on a journey into the world of CQRS and Event Sourcing! Together we will unravel the misteries behind these powerful concepts, by exploring a real-life application: a score app for the 'Wiezen' card game.Using examples straight from the card table, we will delve into the depths of event sourcing and CQRS, comparing them to more traditional approaches that rely on an ORM.We will uncover the signs in your own database that indicate where event sourcing can bring added value. I will also provide you with some tips and pointers, should you decide to embark on your own event sourcing adventure. Filip Van Reeth - WordPress API; "Are you talking to me?" What if the WordPress API could be one of your best friends? What kind of light-hearted or profound requests would it share with you? In this talk, I would like to introduce you to it and ensure that you become best friends so that together you can have many more pleasant conversations (calls). Wanna be friends? Please note that the event or talks will be conducted in Dutch. Want to give a talk? Send us your proposal at meetup.herentals@iodigital.com 18:00 - 19:00: Food/Drinks/Networking 19:00 - 21:00: Talks 21:00 - 22:00: Networking Thursday 30th of May, 18h00 - 22h00 CET iO Campus Herentals, Zavelheide 15, Herentals

    | Coven of Wisdom Herentals

    Go to page for Coven of Wisdom - Herentals - Spring `24 edition

Share