quaintitative

I write about my quantitative explorations in visualisation, data science, machine and deep learning here, as well as other random musings.

For more about me and my other interests, visit playgrd or socials below


Categories
Subscribe

Music and three.js

We had a simple primer on three.js here, and one on manipulating vertices in three.js here.

Now, let’s do something fancier. There is a reason for the term audiovisual, so let’s create something that is not only visually appealing, but moves with music.

The code here is very similar to the last one on manipulating vertices. However, we focus on just one sphere here.

Let’s load the media (music) element, and add an Analyser instance to extract numerical data out of the music.

var fftSize = 128;
var listener = new THREE.AudioListener();
var audio = new THREE.Audio( listener );
var mediaElement = new Audio( 'music/Jester_IanPost.mp3' );
mediaElement.loop = true;
                
mediaElement.play();
audio.setMediaElementSource( mediaElement );
analyser = new THREE.AudioAnalyser( audio, fftSize );

With the Analyser instance, we can compute the average of the numbers with a simple reduce function.

var sum = analyser.data.reduce(function(a,b){return a+b;});
var avg = sum/analyser.data.length;

We can then use the average value to change the speed of change of the positions of each of the vertices, and the rotation of the sphere.

sphere_one.geometry.vertices.forEach(function(i){
var noisy = noise.simplex3(i.x,i.y,i.z)*0.0002;
                        i.x+=noisy*avg;
                        i.y+=noisy*avg;
                        i.z+=noisy*avg;
                    });

group.rotation.x += 0.0005*avg;
group.rotation.y += 0.0005*avg;
group.rotation.z += 0.0005*avg;

We need to update the Analyser during each frame.

analyser.getFrequencyData();

And that’s it. Enjoy!

The full code is here.


Articles

Comparing Prompts for Different Large Language Models (Other than ChatGPT)
AI and UIs
Listing NFTs
Extracting and Processing Wikidata datasets
Extracting and Processing Google Trends data
Extracting and Processing Reddit datasets from PushShift
Extracting and Processing GDELT GKG datasets from BigQuery
Some notes relating to Machine Learning
Some notes relating to Python
Using CCapture.js library with p5.js and three.js
Introduction to PoseNet with three.js
Topic Modelling
Three.js Series - Manipulating vertices in three.js
Three.js Series - Music and three.js
Three.js Series - Simple primer on three.js
HTML Scraping 101
(Almost) The Simplest Server Ever
Tweening in p5.js
Logistic Regression Classification in plain ole Javascript
Introduction to Machine Learning Right Inside the Browser
Nature and Math - Particle Swarm Optimisation
Growing a network garden in D3
Data Analytics with Blender
The Nature of Code Ported to Three.js
Primer on Generative Art in Blender
How normal are you? Checking distributional assumptions.
Monte Carlo Simulation of Value at Risk in Python
Measuring Expected Shortfall in Python
Style Transfer X Generative Art
Measuring Market Risk in Python
Simple charts | crossfilter.js and dc.js
d3.js vs. p5.js for visualisation
Portfolio Optimisation with Tensorflow and D3 Dashboard
Setting Up a Data Lab Environment - Part 6
Setting Up a Data Lab Environment - Part 5
Setting Up a Data Lab Environment - Part 4
Setting Up a Data Lab Environment - Part 3
Setting Up a Data Lab Environment - Part 2
Setting Up a Data Lab Environment - Part 1
Generating a Strange Attractor in three.js
(Almost) All the Most Common Machine Learning Algorithms in Javascript
3 Days of Hand Coding Visualisations - Day 3
3 Days of Hand Coding Visualisations - Day 2
3 Days of Hand Coding Visualisations - Day 1
3 Days of Hand Coding Visualisations - Introduction