-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I am commited to commiting this commit.
- Loading branch information
1 parent
478b186
commit 9e85436
Showing
28 changed files
with
1,358 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
window.AudioContext = window.AudioContext || window.webkitAudioContext; | ||
// Original JavaScript code by Chirp Internet: www.chirpinternet.eu | ||
// Please acknowledge use of this code by including this header. | ||
|
||
function SoundPlayer(audioContext, filterNode) { | ||
this.audioCtx = audioContext; | ||
this.gainNode = this.audioCtx.createGain(); | ||
|
||
if (filterNode) { | ||
// run output through extra filter (already connected to audioContext) | ||
this.gainNode.connect(filterNode); | ||
} else { | ||
this.gainNode.connect(this.audioCtx.destination); | ||
} | ||
|
||
this.oscillator = this.audioCtx.createOscillator(); | ||
} | ||
|
||
SoundPlayer.prototype.setFrequency = function (val, when) { | ||
//Frequency in Hz | ||
if (this.oscillator !== null) { | ||
if (when) { | ||
this.oscillator.frequency.setValueAtTime( | ||
val, | ||
this.audioCtx.currentTime + when | ||
); | ||
} else { | ||
this.oscillator.frequency.setValueAtTime(val, this.audioCtx.currentTime); | ||
} | ||
} | ||
return this; | ||
}; | ||
|
||
SoundPlayer.prototype.setVolume = function (val, when) { | ||
//Gain? | ||
//min: -3.4028235e38 | ||
//max: 3.4028235e38 | ||
if (when) { | ||
this.gainNode.gain.exponentialRampToValueAtTime( | ||
val, | ||
this.audioCtx.currentTime + when | ||
); | ||
} else { | ||
this.gainNode.gain.setValueAtTime(val, this.audioCtx.currentTime); | ||
} | ||
return this; | ||
}; | ||
|
||
SoundPlayer.prototype.setWaveType = function (waveType) { | ||
//sine, square, sawtooth or triangle | ||
this.oscillator.type = waveType; | ||
return this; | ||
}; | ||
|
||
SoundPlayer.prototype.play = function (freq, vol, wave, when) { | ||
this.oscillator = this.audioCtx.createOscillator(); | ||
this.oscillator.connect(this.gainNode); | ||
this.setFrequency(freq); | ||
if (wave) { | ||
this.setWaveType(wave); | ||
} | ||
this.setVolume(1 / 1000); | ||
|
||
if (when) { | ||
this.setVolume(1 / 1000, when - 0.02); | ||
this.oscillator.start(when - 0.02); | ||
this.setVolume(vol, when); | ||
} else { | ||
this.oscillator.start(); | ||
this.setVolume(vol, 0.02); | ||
} | ||
|
||
return this; | ||
}; | ||
|
||
SoundPlayer.prototype.stop = function (when) { | ||
if (when) { | ||
this.gainNode.gain.setTargetAtTime( | ||
1 / 1000, | ||
this.audioCtx.currentTime + when - 0.05, | ||
0.02 | ||
); | ||
this.oscillator.stop(this.audioCtx.currentTime + when); | ||
} else { | ||
this.gainNode.gain.setTargetAtTime( | ||
1 / 1000, | ||
this.audioCtx.currentTime, | ||
0.02 | ||
); | ||
this.oscillator.stop(this.audioCtx.currentTime + 0.05); | ||
} | ||
return this; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.