Thursday, May 8, 2025
News PouroverAI
Visit PourOver.AI
No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
News PouroverAI
No Result
View All Result

Creating Audio-Reactive Visuals with Dynamic Particles in Three.js

December 19, 2023
in Cloud & Programming
Reading Time: 4 mins read
0 0
A A
0
Share on FacebookShare on Twitter



In this tutorial, you will learn how we at ARKx crafted the audio-reactive visuals for Coala Music’s website. We’ll walk through the concepts and techniques used to synchronize audio frequencies and tempo, creating a dynamic visualizer with procedural particle animations.

Getting Started

We will initialize our Three.js scene only after the user interacts; this way, we can enable the audio to autoplay and avoid the block policy of the main browsers.


export default class App {
  constructor() {
    this.onClickBinder = () => this.init()
    document.addEventListener('click', this.onClickBinder)
  }

  init() {
    document.removeEventListener('click', this.onClickBinder)
    
    //BASIC THREEJS SCENE
    this.renderer = new THREE.WebGLRenderer()
    this.camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 10000)
    this.scene = new THREE.Scene()
  }
}

Analyzing Audio Data

Next, we initialize our Audio and BPM Managers. They are responsible for loading the audio, analyzing it, and synchronizing it with the visual elements.


async createManagers() {
	App.audioManager = new AudioManager()
  await App.audioManager.loadAudioBuffer()

  App.bpmManager = new BPMManager()
  App.bpmManager.addEventListener('beat', () => {
    this.particles.onBPMBeat()
  })
  await App.bpmManager.detectBPM(App.audioManager.audio.buffer)
}

The AudioManager class then loads the audio from a URL—we are using a Spotify Preview URL—and analyzes it to break down the audio signals into frequency bins in real-time.


const audioLoader = new THREE.AudioLoader();
audioLoader.load(this.song.url, buffer => {
	this.audio.setBuffer(buffer);
})

Frequency Data

We have to segregate the frequency spectrum into low, mid, and high bands to calculate the amplitudes.

To segment the bands, we need to define start and end points (e.g., the low band range starts at the lowFrequency value and ends at the midFrequency start value). To get the average amplitude, simply multiply the frequencies by the buffer length, then divide by the sample rate, and normalize it to a 0-1 scale.


this.lowFrequency = 10;
this.frequencyArray = this.audioAnalyser.getFrequencyData();
const lowFreqRangeStart = Math.floor((this.lowFrequency * this.bufferLength) / this.audioContext.sampleRate)
const lowFreqRangeEnd = Math.floor((this.midFrequency * this.bufferLength) / this.audioContext.sampleRate)
const lowAvg = this.normalizeValue(this.calculateAverage(this.frequencyArray, lowFreqRangeStart, lowFreqRangeEnd));

//THE SAME FOR MID AND HIGH

Detecting Tempo

The amplitude of the frequencies isn’t enough to align the music beat with the visual elements. Detecting the BPM (Beats Per Minute) is essential to make the elements react in sync with the pulse of the music. In Coala’s project, we feature many songs from their artists’ label, and we don’t know the tempo of each piece of music. Therefore, we are detecting the BPM asynchronously using the amazing web-audio-beat-detector module, by simply passing the audioBuffer.


const { bpm } = await guess(audioBuffer);

Dispatching the Signals

After detecting the BPM, we can dispatch the event signal using setInterval.


this.interval = 60000 / bpm; // Convert BPM to interval
this.intervalId = setInterval(() => {
	this.dispatchEvent({ type: 'beat' })
}, this.interval);

Procedural Reactive Particles (The fun part 😎)

Now, we’re going to create our dynamic particles that will soon be responsive to audio signals. Let’s start with two new functions that will create basic geometries (Box and Cylinder) with random segments and properties; this approach will result in a unique structure each time.

Next, we’ll add this geometry to a THREE.Points object with a simple ShaderMaterial.


const geometry = new THREE.BoxGeometry(1, 1, 1, widthSeg, heightSeg, depthSeg)
const material = new THREE.ShaderMaterial({
  side: THREE.DoubleSide,
  vertexShader: vertex,
  fragmentShader: fragment,
  transparent: true,
  uniforms: {
    size: { value: 2 },
  },
})
const pointsMesh = new THREE.Points(geometry, material)

Now, we can begin creating our meshes with random attributes in a specified interval:

Adding noise

We drew inspiration from Akella’s FBO Tutorial and incorporated the curl noise into the vertex shader to create organic, natural-looking movements and add fluid, swirling motions to the particles. I won’t delve deeply into the explanation of Curl Noise and FBO Particles, as Akella did an amazing job in his tutorial. You can check it out to learn more about it.

Animating the particles

To summarize, in the vertex shader, we animate the points to achieve dynamic effects that dictate the particles’ behavior and appearance. Starting with newpos, which is the original position of each point, we create a target. This target adds curl noise along its normal vector, varying based on frequency and amplitude uniforms. It is interpolated by the power of the distance d between them. This process creates a smooth transition, easing out as the point approaches the target.


vec3 newpos = position;
vec3 target = position + (normal * .1) + curl(newpos.x * frequency, newpos.y * frequency, newpos.z * frequency) * amplitude;
float d = length(newpos - target) / maxDistance;
newpos = mix(position, target, pow(d, 4.));

We also add a wave motion to newpos.z , adding an extra layer of liveliness to the animation.


newpos.z += sin(time) * (.1 * offsetGain);

Moreover, the size of each point adjusts dynamically based on how close the point is to its target and its depth in the scene, making the animation feel more three-dimensional.


gl_PointSize = size + (pow(d,3.) * offsetSize) * (1./-mvPosition.z);

Adding Colors

In the fragment shader, we are masking out the point with a circle shape function and interpolating the startColor and endColor uniforms according to the point’s vDistance defined in the vertex:


vec3 circ = vec3(circle(uv,1.));
vec3 color = mix(startColor,endColor,vDistance);
gl_FragColor=vec4(color,circ.r * vDistance);

Bringing Audio and Visuals Together

Now, we can use our creativity to assign the audio data and beat to all the properties, both in the vertex and fragment shader uniforms. We can also add some random animations to the scale, position and rotation using GSAP.


update() {
  // Dynamically update amplitude based on the high frequency data from the audio manager
  this.material.uniforms.amplitude.value = 0.8 + THREE.MathUtils.mapLinear(App.audioManager.frequencyData.high, 0, 0.6, -0.1, 0.2)

  // Update offset gain based on the low frequency data for subtle effect changes
  this.material.uniforms.offsetGain.value = App.audioManager.frequencyData.mid * 0.6

  // Map low frequency data to a range and use it to increment the time uniform
  const t = THREE.MathUtils.mapLinear(App.audioManager.frequencyData.low, 0.6, 1, 0.2, 0.5)
  this.time += THREE.MathUtils.clamp(t, 0.2, 0.5) // Clamp the value to ensure it stays within a desired range

  this.material.uniforms.time.value = this.time
}

Check the demo.

Conclusion

This tutorial has guided you on how to synchronize sound with engaging visual particle effects using Three.js. Hope you enjoyed it! If you have questions, let me know on Twitter.

Credits



Source link

Tags: 3D visual effectsARKxaudio visualizationaudio-reactive visualsAudioReactiveCoala MusicCreatingdynamicdynamic particlesJavascriptParticlessound synchronizationthree.jsTutorialvisualizerVisualsweb developmentwebgl
Previous Post

Anomaly detection in machine learning: Finding outliers for optimization of business functions

Next Post

Meet India’s CRYPTO Millionaire Sumit Gupta – EASY Explanation @CoinDCX | The Ranveer Show 97

Related Posts

Top 20 Javascript Libraries You Should Know in 2024
Cloud & Programming

Top 20 Javascript Libraries You Should Know in 2024

June 10, 2024
Simplify risk and compliance assessments with the new common control library in AWS Audit Manager
Cloud & Programming

Simplify risk and compliance assessments with the new common control library in AWS Audit Manager

June 6, 2024
Simplify Regular Expressions with RegExpBuilderJS
Cloud & Programming

Simplify Regular Expressions with RegExpBuilderJS

June 6, 2024
How to learn data visualization to accelerate your career
Cloud & Programming

How to learn data visualization to accelerate your career

June 6, 2024
BitTitan Announces Seasoned Tech Leader Aaron Wadsworth as General Manager
Cloud & Programming

BitTitan Announces Seasoned Tech Leader Aaron Wadsworth as General Manager

June 6, 2024
Copilot Studio turns to AI-powered workflows
Cloud & Programming

Copilot Studio turns to AI-powered workflows

June 6, 2024
Next Post
Meet India’s CRYPTO Millionaire Sumit Gupta – EASY Explanation @CoinDCX | The Ranveer Show 97

Meet India's CRYPTO Millionaire Sumit Gupta - EASY Explanation @CoinDCX | The Ranveer Show 97

Machine Learning (O que faz o ChatGPT funcionar) // Dicionário do Programador

Machine Learning (O que faz o ChatGPT funcionar) // Dicionário do Programador

AI-Automated Cybersecurity: What to Automate?

AI-Automated Cybersecurity: What to Automate?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
Is C.AI Down? Here Is What To Do Now

Is C.AI Down? Here Is What To Do Now

January 10, 2024
Porfo: Revolutionizing the Crypto Wallet Landscape

Porfo: Revolutionizing the Crypto Wallet Landscape

October 9, 2023
A Complete Guide to BERT with Code | by Bradney Smith | May, 2024

A Complete Guide to BERT with Code | by Bradney Smith | May, 2024

May 19, 2024
A faster, better way to prevent an AI chatbot from giving toxic responses | MIT News

A faster, better way to prevent an AI chatbot from giving toxic responses | MIT News

April 10, 2024
Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

November 20, 2023
Saginaw HMI Enclosures and Suspension Arm Systems from AutomationDirect – Library.Automationdirect.com

Saginaw HMI Enclosures and Suspension Arm Systems from AutomationDirect – Library.Automationdirect.com

December 6, 2023
Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

June 10, 2024
AI Compared: Which Assistant Is the Best?

AI Compared: Which Assistant Is the Best?

June 10, 2024
How insurance companies can use synthetic data to fight bias

How insurance companies can use synthetic data to fight bias

June 10, 2024
5 SLA metrics you should be monitoring

5 SLA metrics you should be monitoring

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

June 10, 2024
Facebook Twitter LinkedIn Pinterest RSS
News PouroverAI

The latest news and updates about the AI Technology and Latest Tech Updates around the world... PouroverAI keeps you in the loop.

CATEGORIES

  • AI Technology
  • Automation
  • Blockchain
  • Business
  • Cloud & Programming
  • Data Science & ML
  • Digital Marketing
  • Front-Tech
  • Uncategorized

SITEMAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 PouroverAI News.
PouroverAI News

No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing

Copyright © 2023 PouroverAI News.
PouroverAI News

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In