first commit

This commit is contained in:
2026-06-24 09:48:54 +02:00
commit 41e62ddcad
33739 changed files with 4266226 additions and 0 deletions
+438
View File
@@ -0,0 +1,438 @@
import { Object3D } from '../core/Object3D.js';
class Audio extends Object3D {
constructor( listener ) {
super();
this.type = 'Audio';
this.listener = listener;
this.context = listener.context;
this.gain = this.context.createGain();
this.gain.connect( listener.getInput() );
this.autoplay = false;
this.buffer = null;
this.detune = 0;
this.loop = false;
this.loopStart = 0;
this.loopEnd = 0;
this.offset = 0;
this.duration = undefined;
this.playbackRate = 1;
this.isPlaying = false;
this.hasPlaybackControl = true;
this.source = null;
this.sourceType = 'empty';
this._startedAt = 0;
this._progress = 0;
this._connected = false;
this.filters = [];
}
getOutput() {
return this.gain;
}
setNodeSource( audioNode ) {
this.hasPlaybackControl = false;
this.sourceType = 'audioNode';
this.source = audioNode;
this.connect();
return this;
}
setMediaElementSource( mediaElement ) {
this.hasPlaybackControl = false;
this.sourceType = 'mediaNode';
this.source = this.context.createMediaElementSource( mediaElement );
this.connect();
return this;
}
setMediaStreamSource( mediaStream ) {
this.hasPlaybackControl = false;
this.sourceType = 'mediaStreamNode';
this.source = this.context.createMediaStreamSource( mediaStream );
this.connect();
return this;
}
setBuffer( audioBuffer ) {
this.buffer = audioBuffer;
this.sourceType = 'buffer';
if ( this.autoplay ) this.play();
return this;
}
play( delay = 0 ) {
if ( this.isPlaying === true ) {
console.warn( 'THREE.Audio: Audio is already playing.' );
return;
}
if ( this.hasPlaybackControl === false ) {
console.warn( 'THREE.Audio: this Audio has no playback control.' );
return;
}
this._startedAt = this.context.currentTime + delay;
const source = this.context.createBufferSource();
source.buffer = this.buffer;
source.loop = this.loop;
source.loopStart = this.loopStart;
source.loopEnd = this.loopEnd;
source.onended = this.onEnded.bind( this );
source.start( this._startedAt, this._progress + this.offset, this.duration );
this.isPlaying = true;
this.source = source;
this.setDetune( this.detune );
this.setPlaybackRate( this.playbackRate );
return this.connect();
}
pause() {
if ( this.hasPlaybackControl === false ) {
console.warn( 'THREE.Audio: this Audio has no playback control.' );
return;
}
if ( this.isPlaying === true ) {
// update current progress
this._progress += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate;
if ( this.loop === true ) {
// ensure _progress does not exceed duration with looped audios
this._progress = this._progress % ( this.duration || this.buffer.duration );
}
this.source.stop();
this.source.onended = null;
this.isPlaying = false;
}
return this;
}
stop( delay = 0 ) {
if ( this.hasPlaybackControl === false ) {
console.warn( 'THREE.Audio: this Audio has no playback control.' );
return;
}
this._progress = 0;
if ( this.source !== null ) {
this.source.stop( this.context.currentTime + delay );
this.source.onended = null;
}
this.isPlaying = false;
return this;
}
connect() {
if ( this.filters.length > 0 ) {
this.source.connect( this.filters[ 0 ] );
for ( let i = 1, l = this.filters.length; i < l; i ++ ) {
this.filters[ i - 1 ].connect( this.filters[ i ] );
}
this.filters[ this.filters.length - 1 ].connect( this.getOutput() );
} else {
this.source.connect( this.getOutput() );
}
this._connected = true;
return this;
}
disconnect() {
if ( this._connected === false ) {
return;
}
if ( this.filters.length > 0 ) {
this.source.disconnect( this.filters[ 0 ] );
for ( let i = 1, l = this.filters.length; i < l; i ++ ) {
this.filters[ i - 1 ].disconnect( this.filters[ i ] );
}
this.filters[ this.filters.length - 1 ].disconnect( this.getOutput() );
} else {
this.source.disconnect( this.getOutput() );
}
this._connected = false;
return this;
}
getFilters() {
return this.filters;
}
setFilters( value ) {
if ( ! value ) value = [];
if ( this._connected === true ) {
this.disconnect();
this.filters = value.slice();
this.connect();
} else {
this.filters = value.slice();
}
return this;
}
setDetune( value ) {
this.detune = value;
if ( this.isPlaying === true && this.source.detune !== undefined ) {
this.source.detune.setTargetAtTime( this.detune, this.context.currentTime, 0.01 );
}
return this;
}
getDetune() {
return this.detune;
}
getFilter() {
return this.getFilters()[ 0 ];
}
setFilter( filter ) {
return this.setFilters( filter ? [ filter ] : [] );
}
setPlaybackRate( value ) {
if ( this.hasPlaybackControl === false ) {
console.warn( 'THREE.Audio: this Audio has no playback control.' );
return;
}
this.playbackRate = value;
if ( this.isPlaying === true ) {
this.source.playbackRate.setTargetAtTime( this.playbackRate, this.context.currentTime, 0.01 );
}
return this;
}
getPlaybackRate() {
return this.playbackRate;
}
onEnded() {
this.isPlaying = false;
this._progress = 0;
}
getLoop() {
if ( this.hasPlaybackControl === false ) {
console.warn( 'THREE.Audio: this Audio has no playback control.' );
return false;
}
return this.loop;
}
setLoop( value ) {
if ( this.hasPlaybackControl === false ) {
console.warn( 'THREE.Audio: this Audio has no playback control.' );
return;
}
this.loop = value;
if ( this.isPlaying === true ) {
this.source.loop = this.loop;
}
return this;
}
setLoopStart( value ) {
this.loopStart = value;
return this;
}
setLoopEnd( value ) {
this.loopEnd = value;
return this;
}
getVolume() {
return this.gain.gain.value;
}
setVolume( value ) {
this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
return this;
}
copy( source, recursive ) {
super.copy( source, recursive );
if ( source.sourceType !== 'buffer' ) {
console.warn( 'THREE.Audio: Audio source type cannot be copied.' );
return this;
}
this.autoplay = source.autoplay;
this.buffer = source.buffer;
this.detune = source.detune;
this.loop = source.loop;
this.loopStart = source.loopStart;
this.loopEnd = source.loopEnd;
this.offset = source.offset;
this.duration = source.duration;
this.playbackRate = source.playbackRate;
this.hasPlaybackControl = source.hasPlaybackControl;
this.sourceType = source.sourceType;
this.filters = source.filters.slice();
return this;
}
clone( recursive ) {
return new this.constructor( this.listener ).copy( this, recursive );
}
}
export { Audio };
+40
View File
@@ -0,0 +1,40 @@
class AudioAnalyser {
constructor( audio, fftSize = 2048 ) {
this.analyser = audio.context.createAnalyser();
this.analyser.fftSize = fftSize;
this.data = new Uint8Array( this.analyser.frequencyBinCount );
audio.getOutput().connect( this.analyser );
}
getFrequencyData() {
this.analyser.getByteFrequencyData( this.data );
return this.data;
}
getAverageFrequency() {
let value = 0;
const data = this.getFrequencyData();
for ( let i = 0; i < data.length; i ++ ) {
value += data[ i ];
}
return value / data.length;
}
}
export { AudioAnalyser };
+25
View File
@@ -0,0 +1,25 @@
let _context;
class AudioContext {
static getContext() {
if ( _context === undefined ) {
_context = new ( window.AudioContext || window.webkitAudioContext )();
}
return _context;
}
static setContext( value ) {
_context = value;
}
}
export { AudioContext };
+137
View File
@@ -0,0 +1,137 @@
import { Vector3 } from '../math/Vector3.js';
import { Quaternion } from '../math/Quaternion.js';
import { Clock } from '../core/Clock.js';
import { Object3D } from '../core/Object3D.js';
import { AudioContext } from './AudioContext.js';
const _position = /*@__PURE__*/ new Vector3();
const _quaternion = /*@__PURE__*/ new Quaternion();
const _scale = /*@__PURE__*/ new Vector3();
const _orientation = /*@__PURE__*/ new Vector3();
class AudioListener extends Object3D {
constructor() {
super();
this.type = 'AudioListener';
this.context = AudioContext.getContext();
this.gain = this.context.createGain();
this.gain.connect( this.context.destination );
this.filter = null;
this.timeDelta = 0;
// private
this._clock = new Clock();
}
getInput() {
return this.gain;
}
removeFilter() {
if ( this.filter !== null ) {
this.gain.disconnect( this.filter );
this.filter.disconnect( this.context.destination );
this.gain.connect( this.context.destination );
this.filter = null;
}
return this;
}
getFilter() {
return this.filter;
}
setFilter( value ) {
if ( this.filter !== null ) {
this.gain.disconnect( this.filter );
this.filter.disconnect( this.context.destination );
} else {
this.gain.disconnect( this.context.destination );
}
this.filter = value;
this.gain.connect( this.filter );
this.filter.connect( this.context.destination );
return this;
}
getMasterVolume() {
return this.gain.gain.value;
}
setMasterVolume( value ) {
this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
return this;
}
updateMatrixWorld( force ) {
super.updateMatrixWorld( force );
const listener = this.context.listener;
const up = this.up;
this.timeDelta = this._clock.getDelta();
this.matrixWorld.decompose( _position, _quaternion, _scale );
_orientation.set( 0, 0, - 1 ).applyQuaternion( _quaternion );
if ( listener.positionX ) {
// code path for Chrome (see #14393)
const endTime = this.context.currentTime + this.timeDelta;
listener.positionX.linearRampToValueAtTime( _position.x, endTime );
listener.positionY.linearRampToValueAtTime( _position.y, endTime );
listener.positionZ.linearRampToValueAtTime( _position.z, endTime );
listener.forwardX.linearRampToValueAtTime( _orientation.x, endTime );
listener.forwardY.linearRampToValueAtTime( _orientation.y, endTime );
listener.forwardZ.linearRampToValueAtTime( _orientation.z, endTime );
listener.upX.linearRampToValueAtTime( up.x, endTime );
listener.upY.linearRampToValueAtTime( up.y, endTime );
listener.upZ.linearRampToValueAtTime( up.z, endTime );
} else {
listener.setPosition( _position.x, _position.y, _position.z );
listener.setOrientation( _orientation.x, _orientation.y, _orientation.z, up.x, up.y, up.z );
}
}
}
export { AudioListener };
+146
View File
@@ -0,0 +1,146 @@
import { Vector3 } from '../math/Vector3.js';
import { Quaternion } from '../math/Quaternion.js';
import { Audio } from './Audio.js';
const _position = /*@__PURE__*/ new Vector3();
const _quaternion = /*@__PURE__*/ new Quaternion();
const _scale = /*@__PURE__*/ new Vector3();
const _orientation = /*@__PURE__*/ new Vector3();
class PositionalAudio extends Audio {
constructor( listener ) {
super( listener );
this.panner = this.context.createPanner();
this.panner.panningModel = 'HRTF';
this.panner.connect( this.gain );
}
connect() {
super.connect();
this.panner.connect( this.gain );
}
disconnect() {
super.disconnect();
this.panner.disconnect( this.gain );
}
getOutput() {
return this.panner;
}
getRefDistance() {
return this.panner.refDistance;
}
setRefDistance( value ) {
this.panner.refDistance = value;
return this;
}
getRolloffFactor() {
return this.panner.rolloffFactor;
}
setRolloffFactor( value ) {
this.panner.rolloffFactor = value;
return this;
}
getDistanceModel() {
return this.panner.distanceModel;
}
setDistanceModel( value ) {
this.panner.distanceModel = value;
return this;
}
getMaxDistance() {
return this.panner.maxDistance;
}
setMaxDistance( value ) {
this.panner.maxDistance = value;
return this;
}
setDirectionalCone( coneInnerAngle, coneOuterAngle, coneOuterGain ) {
this.panner.coneInnerAngle = coneInnerAngle;
this.panner.coneOuterAngle = coneOuterAngle;
this.panner.coneOuterGain = coneOuterGain;
return this;
}
updateMatrixWorld( force ) {
super.updateMatrixWorld( force );
if ( this.hasPlaybackControl === true && this.isPlaying === false ) return;
this.matrixWorld.decompose( _position, _quaternion, _scale );
_orientation.set( 0, 0, 1 ).applyQuaternion( _quaternion );
const panner = this.panner;
if ( panner.positionX ) {
// code path for Chrome and Firefox (see #14393)
const endTime = this.context.currentTime + this.listener.timeDelta;
panner.positionX.linearRampToValueAtTime( _position.x, endTime );
panner.positionY.linearRampToValueAtTime( _position.y, endTime );
panner.positionZ.linearRampToValueAtTime( _position.z, endTime );
panner.orientationX.linearRampToValueAtTime( _orientation.x, endTime );
panner.orientationY.linearRampToValueAtTime( _orientation.y, endTime );
panner.orientationZ.linearRampToValueAtTime( _orientation.z, endTime );
} else {
panner.setPosition( _position.x, _position.y, _position.z );
panner.setOrientation( _orientation.x, _orientation.y, _orientation.z );
}
}
}
export { PositionalAudio };