Skip to content

Commit 0c51c82

Browse files
committed
Audio code clean up.
1 parent 00551c6 commit 0c51c82

4 files changed

Lines changed: 74 additions & 69 deletions

File tree

src/audio/Audio.js

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ THREE.Audio = function ( listener ) {
1313
this.source.onended = this.onEnded.bind( this );
1414

1515
this.gain = this.context.createGain();
16-
this.gain.connect( listener.getOutputNode());
16+
this.gain.connect( listener.getOutputNode() );
1717

1818
this.autoplay = false;
1919

@@ -29,37 +29,38 @@ THREE.Audio = function ( listener ) {
2929
THREE.Audio.prototype = Object.create( THREE.Object3D.prototype );
3030
THREE.Audio.prototype.constructor = THREE.Audio;
3131

32-
THREE.Audio.prototype.getOutput = function() {
32+
THREE.Audio.prototype.getOutput = function () {
33+
3334
return this.gain;
35+
3436
};
3537

36-
THREE.Audio.prototype.load = function ( fileName ) {
38+
THREE.Audio.prototype.load = function ( file ) {
39+
40+
var buffer = new THREE.AudioBuffer( this.context );
41+
buffer.load( file );
42+
43+
this.setBuffer( buffer );
3744

38-
var audioBuffer = new THREE.AudioBuffer(this.context);
39-
audioBuffer.load(fileName);
40-
this.setBuffer(audioBuffer);
4145
return this;
4246

4347
};
4448

45-
4649
THREE.Audio.prototype.setNodeSource = function ( audioNode ) {
4750

4851
this.hasPlaybackControl = false;
4952
this.sourceType = 'audioNode';
5053
this.source = audioNode;
5154
this.connect();
52-
55+
5356
return this;
5457

5558
};
5659

57-
58-
5960
THREE.Audio.prototype.setBuffer = function ( audioBuffer ) {
6061

6162
var scope = this;
62-
63+
6364
audioBuffer.onReady(function(buffer) {
6465
scope.source.buffer = buffer;
6566
scope.sourceType = 'buffer';
@@ -70,8 +71,6 @@ THREE.Audio.prototype.setBuffer = function ( audioBuffer ) {
7071

7172
};
7273

73-
74-
7574
THREE.Audio.prototype.play = function () {
7675

7776
if ( this.isPlaying === true ) {
@@ -80,15 +79,14 @@ THREE.Audio.prototype.play = function () {
8079
return;
8180

8281
}
83-
82+
8483
if ( this.hasPlaybackControl === false ) {
8584

8685
console.warn( 'THREE.Audio: this Audio has no playback control.' );
8786
return;
8887

8988
}
9089

91-
9290
var source = this.context.createBufferSource();
9391

9492
source.buffer = this.source.buffer;
@@ -107,25 +105,25 @@ THREE.Audio.prototype.play = function () {
107105

108106
THREE.Audio.prototype.pause = function () {
109107

110-
if (this.hasPlaybackControl === false) {
111-
108+
if ( this.hasPlaybackControl === false ) {
109+
112110
console.warn( 'THREE.Audio: this Audio has no playback control.' );
113111
return;
114-
112+
115113
}
116-
114+
117115
this.source.stop();
118116
this.startTime = this.context.currentTime;
119117

120118
};
121119

122120
THREE.Audio.prototype.stop = function () {
123121

124-
if (this.hasPlaybackControl === false) {
125-
122+
if ( this.hasPlaybackControl === false ) {
123+
126124
console.warn( 'THREE.Audio: this Audio has no playback control.' );
127125
return;
128-
126+
129127
}
130128

131129
this.source.stop();
@@ -138,11 +136,11 @@ THREE.Audio.prototype.connect = function () {
138136
if ( this.filter !== null ) {
139137

140138
this.source.connect( this.filter );
141-
this.filter.connect( this.getOutput());
139+
this.filter.connect( this.getOutput() );
142140

143141
} else {
144142

145-
this.source.connect( this.getOutput());
143+
this.source.connect( this.getOutput() );
146144

147145
}
148146

@@ -153,11 +151,11 @@ THREE.Audio.prototype.disconnect = function () {
153151
if ( this.filter !== null ) {
154152

155153
this.source.disconnect( this.filter );
156-
this.filter.disconnect( this.getOutput());
154+
this.filter.disconnect( this.getOutput() );
157155

158156
} else {
159157

160-
this.source.disconnect( this.getOutput());
158+
this.source.disconnect( this.getOutput() );
161159

162160
}
163161

@@ -187,11 +185,11 @@ THREE.Audio.prototype.getFilter = function () {
187185

188186
THREE.Audio.prototype.setPlaybackRate = function ( value ) {
189187

190-
if (this.hasPlaybackControl === false) {
191-
188+
if ( this.hasPlaybackControl === false ) {
189+
192190
console.warn( 'THREE.Audio: this Audio has no playback control.' );
193191
return;
194-
192+
195193
}
196194

197195
this.playbackRate = value;
@@ -218,24 +216,24 @@ THREE.Audio.prototype.onEnded = function() {
218216

219217
THREE.Audio.prototype.setLoop = function ( value ) {
220218

221-
if (this.hasPlaybackControl === false) {
222-
219+
if ( this.hasPlaybackControl === false ) {
220+
223221
console.warn( 'THREE.Audio: this Audio has no playback control.' );
224222
return;
225-
223+
226224
}
227225

228226
this.source.loop = value;
229227

230228
};
231229

232230
THREE.Audio.prototype.getLoop = function () {
233-
234-
if (this.hasPlaybackControl === false) {
235-
231+
232+
if ( this.hasPlaybackControl === false ) {
233+
236234
console.warn( 'THREE.Audio: this Audio has no playback control.' );
237235
return false;
238-
236+
239237
}
240238

241239
return this.source.loop;
@@ -254,4 +252,3 @@ THREE.Audio.prototype.getVolume = function () {
254252
return this.gain.gain.value;
255253

256254
};
257-

src/audio/AudioBuffer.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ THREE.AudioBuffer.prototype.load = function ( file ) {
2323

2424
scope.buffer = buffer;
2525
scope.ready = true;
26-
27-
for (var i = 0; i < scope.readyCallbacks.length; i++) {
26+
27+
for ( var i = 0; i < scope.readyCallbacks.length; i ++ ) {
28+
2829
scope.readyCallbacks[i](scope.buffer);
30+
2931
}
32+
3033
scope.readyCallbacks = [];
31-
34+
3235
} );
3336

3437
};
@@ -39,13 +42,15 @@ THREE.AudioBuffer.prototype.load = function ( file ) {
3942
};
4043

4144
THREE.AudioBuffer.prototype.onReady = function ( callback ) {
42-
43-
if (this.ready) {
44-
callback(this.buffer);
45-
}
46-
else {
47-
this.readyCallbacks.push(callback);
45+
46+
if ( this.ready ) {
47+
48+
callback( this.buffer );
49+
50+
} else {
51+
52+
this.readyCallbacks.push( callback );
53+
4854
}
4955

5056
};
51-

src/audio/AudioListener.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,48 @@ THREE.AudioListener = function () {
1212
this.masterGain = this.context.createGain();
1313
this.masterGain.connect(this.context.destination);
1414
this.filter = null;
15-
15+
1616
};
1717

1818
THREE.AudioListener.prototype = Object.create( THREE.Object3D.prototype );
1919
THREE.AudioListener.prototype.constructor = THREE.AudioListener;
2020

2121
THREE.AudioListener.prototype.getOutputNode = function () {
22-
22+
2323
return this.masterGain;
24-
24+
2525
};
2626

2727
THREE.AudioListener.prototype.removeFilter = function ( ) {
28-
29-
if (this.filter !== null) {
30-
31-
this.masterGain.disconnect(this.filter);
32-
this.filter.disconnect(this.context.destination);
33-
this.masterGain.connect(this.context.destination);
28+
29+
if ( this.filter !== null ) {
30+
31+
this.masterGain.disconnect( this.filter );
32+
this.filter.disconnect( this.context.destination );
33+
this.masterGain.connect( this.context.destination );
3434
this.filter = null;
35-
35+
3636
}
37-
37+
3838
}
3939

4040
THREE.AudioListener.prototype.setFilter = function ( value ) {
4141

42-
if (this.filter !== null) {
43-
44-
this.masterGain.disconnect(this.filter);
45-
this.filter.disconnect(this.context.destination);
42+
if ( this.filter !== null ) {
43+
44+
this.masterGain.disconnect( this.filter );
45+
this.filter.disconnect( this.context.destination );
4646

4747
} else {
48-
49-
this.masterGain.disconnect(this.context.destination);
50-
48+
49+
this.masterGain.disconnect( this.context.destination );
50+
5151
}
52+
5253
this.filter = value;
53-
this.masterGain.connect(this.filter);
54-
this.filter.connect(this.context.destination);
55-
54+
this.masterGain.connect( this.filter );
55+
this.filter.connect( this.context.destination );
56+
5657
};
5758

5859
THREE.AudioListener.prototype.getFilter = function () {

src/audio/PositionalAudio.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ THREE.PositionalAudio = function ( listener ) {
1414
THREE.PositionalAudio.prototype = Object.create( THREE.Audio.prototype );
1515
THREE.PositionalAudio.prototype.constructor = THREE.PositionalAudio;
1616

17-
THREE.PositionalAudio.prototype.getOutput = function() {
17+
THREE.PositionalAudio.prototype.getOutput = function () {
18+
1819
return this.panner;
20+
1921
};
2022

2123

0 commit comments

Comments
 (0)