Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
munshkr committed Apr 13, 2020
0 parents commit ebba663
Show file tree
Hide file tree
Showing 9 changed files with 54,477 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 munshkr

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# threejs-3d-sound-sandbox

This is an exploration of applying filters to a ThreeJS PositionalAudio object
(WebAudio Panner wrapper), for a richer 3D sound simulation.

The original code was taken from [ThreeJS WebAudio sandbox
example](https://threejs.org/examples/?q=audio#webaudio_sandbox).

## Usage

Clone repository and install dependnecies by running `yarn`.

Then, run the development server:

```bash
npm run dev
# or
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see
the result.

You can start editing the page by modifying `pages/index.js`. The page
auto-updates as you edit the file.
231 changes: 231 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<title>three.js webaudio - sandbox</title>
</head>
<body>
<div id="overlay">
<div>
<button id="startButton">Click to Play</button>
<p>Audio playback requires user interaction.</p>
</div>
</div>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webaudio - sandbox<br/>
music by <a href="http://www.newgrounds.com/audio/listen/358232" target="_blank" rel="noopener">larrylarrybb</a>,
<a href="http://www.newgrounds.com/audio/listen/376737" target="_blank" rel="noopener">skullbeatz</a> and
<a href="http://opengameart.org/content/project-utopia-seamless-loop" target="_blank" rel="noopener">congusbongus</a><br/><br/>
navigate with WASD / arrows / mouse
</div>

<script type="module">

import * as THREE from './jsm/three.module.js';
import { GUI } from './jsm/dat.gui.module.js';
import { FirstPersonControls } from './jsm/FirstPersonControls.js';
import PositionalAudioFilter from './jsm/PositionalAudioFilter.js';

var camera, controls, scene, renderer, light;

var material1, analyser1, filter1;

var soundControls, filterControls;

var clock = new THREE.Clock();

var startButton = document.getElementById( 'startButton' );
startButton.addEventListener( 'click', init );

function init() {

var overlay = document.getElementById( 'overlay' );
overlay.remove();

camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 0, 25, 0 );

var listener = new THREE.AudioListener();
camera.add( listener );

scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0x000000, 0.0025 );

light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 0, 0.5, 1 ).normalize();
scene.add( light );

var sphere = new THREE.SphereBufferGeometry( 20, 32, 16 );

material1 = new THREE.MeshPhongMaterial( { color: 0xffaa00, flatShading: true, shininess: 0 } );

// sound spheres

var audioLoader = new THREE.AudioLoader();

var mesh1 = new THREE.Mesh( sphere, material1 );
mesh1.position.set( 0, 30, -150 );
scene.add( mesh1 );

var sound1 = new THREE.PositionalAudio( listener );

filter1 = new PositionalAudioFilter(sound1);
filter1.type = 'lowshelf';

audioLoader.load( 'sounds/cipater-sample.ogg', function ( buffer ) {

sound1.setBuffer( buffer );
sound1.setDistanceModel("inverse")
sound1.setRefDistance(20);
sound1.setRolloffFactor(2);
sound1.setVolume(1);
sound1.setLoop(true);
filter1.connect();
sound1.play();

} );
mesh1.add( sound1 );

// analysers

analyser1 = new THREE.AudioAnalyser( sound1, 32 );

// ground

var helper = new THREE.GridHelper( 1000, 10, 0x444444, 0x444444 );
helper.position.y = 0.1;
scene.add( helper );

//

const SoundControls = function () {
this.master = listener.getMasterVolume();
this.volume = sound1.getVolume();
this.refDistance = sound1.getRefDistance();
this.rolloffFactor = sound1.getRolloffFactor();
this.maxDistance = sound1.getMaxDistance();
};

const FilterControls = function () {
this.frequency = filter1.frequency;
this.Q = filter1.Q;
this.gain = filter1.gain;
this.minFreq = filter1.minFreq;
this.maxFreq = filter1.maxFreq;
}

var gui = new GUI();

soundControls = new SoundControls();
filterControls = new FilterControls();

const soundFolder = gui.addFolder('volume');
const filterFolder = gui.addFolder('filter');

soundFolder.add(soundControls, 'master').min(0.0).max(1.0).step(0.01).onChange(() => {
listener.setMasterVolume(soundControls.master);
});
soundFolder.add(soundControls, 'volume').min(0.0).max(1.0).step(0.01).onChange(() => {
sound1.setVolume(soundControls.volume);
});
soundFolder.add(soundControls, 'refDistance').min(0.0).max(30).step(0.1).onChange(() => {
sound1.setRefDistance(soundControls.refDistance);
});
soundFolder.add(soundControls, 'rolloffFactor').min(0.0).max(20).step(0.1).onChange(() => {
sound1.setRolloffFactor(soundControls.rolloffFactor)
});
soundFolder.add(soundControls, 'maxDistance').min(1).max(100).step(1).onChange(() => {
sound1.setMaxDistance(soundControls.maxDistance)
});

filterFolder.add(filterControls, 'gain').min(0.0).max(25).step(0.01).onChange(() => {
filter1.gain = filterControls.gain;
});
filterFolder.add(filterControls, 'frequency').min(0.0).max(10000).step(10).onChange(() => {
filter1.frequency = filterControls.frequency;
}).listen();
filterFolder.add(filterControls, 'Q').min(0.0).max(10).step(0.01).onChange(() => {
filter1.Q = filterControls.Q;
});
filterFolder.add(filterControls, 'minFreq').min(0.0).max(2000).step(0.1).onChange(() => {
filter1.minFreq = filterControls.minFreq;
});
filterFolder.add(filterControls, 'maxFreq').min(2000).max(24000).step(0.1).onChange(() => {
filter1.maxFreq = filterControls.maxFreq;
});

soundFolder.open();
filterFolder.open();

//

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

//

controls = new FirstPersonControls( camera, renderer.domElement );

controls.movementSpeed = 70;
controls.lookSpeed = 0.05;
controls.noFly = true;
controls.lookVertical = false;

controls.onMove = (camera) => onCameraMove(camera);

//

window.addEventListener( 'resize', onWindowResize, false );

animate();

}

function onCameraMove(_camera) {
filter1.update();

// Update filter control to show change
filterControls.frequency = filter1.frequency;

console.log("Filter Frequency:", filter1.frequency);
}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

controls.handleResize();

}

function animate() {

requestAnimationFrame( animate );
render();

}


function render() {

var delta = clock.getDelta();

controls.update( delta );

material1.emissive.b = analyser1.getAverageFrequency() / 256;

renderer.render( scene, camera );

}

</script>
</body>
</html>
Loading

0 comments on commit ebba663

Please sign in to comment.