-
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.
- Loading branch information
1 parent
8a77fc5
commit 771af92
Showing
4 changed files
with
106 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Timbre from "./Timbre.mjs"; | ||
|
||
export default class AudioPlayer { | ||
/** @type {Timbre} */ | ||
timbre = null; | ||
frequency = null; | ||
oscillatorNode = null; | ||
gainNode = null; | ||
|
||
constructor(timbre, frequency) { | ||
this.timbre = timbre; | ||
this.frequency = frequency; | ||
|
||
this.oscillatorNode = timbre.audioCtx.createOscillator(); | ||
if (this.timbre.type === 'custom') { | ||
this.oscillatorNode.setPeriodicWave(timbre.wave); | ||
} else { | ||
this.oscillatorNode.type = timbre.type; | ||
} | ||
this.oscillatorNode.frequency.value = frequency; | ||
|
||
this.gainNode = timbre.audioCtx.createGain(); | ||
this.gainNode.gain.value = 0; | ||
|
||
this.oscillatorNode | ||
.connect(this.gainNode) | ||
.connect(timbre.audioCtx.destination); | ||
this.oscillatorNode.start(); | ||
} | ||
|
||
start() { | ||
const adsr = this.timbre.adsr; | ||
let time = this.timbre.audioCtx.currentTime; | ||
this.gainNode.gain.cancelScheduledValues(time); | ||
|
||
this.gainNode.gain.setValueAtTime(0, time); | ||
|
||
time += adsr.attackDuration; | ||
this.gainNode.gain.linearRampToValueAtTime(adsr.attackAmplitude, time); | ||
|
||
time += adsr.decayDuration; | ||
this.gainNode.gain.linearRampToValueAtTime(adsr.decayAmplitude, time); | ||
|
||
if (adsr.sustainDuration) { | ||
time += adsr.sustainDuration; | ||
this.gainNode.gain.linearRampToValueAtTime(0, time); | ||
} | ||
} | ||
|
||
stop() { | ||
const adsr = this.timbre.adsr; | ||
let time = this.timbre.audioCtx.currentTime; | ||
this.gainNode.gain.cancelScheduledValues(time); | ||
|
||
time += adsr.releaseDuration; | ||
this.gainNode.gain.linearRampToValueAtTime(0, time); | ||
} | ||
|
||
}; |
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,27 @@ | ||
export default class Timbre { | ||
audioCtx = null; | ||
type = 'sine'; | ||
wave = null; | ||
adsr = { | ||
attackAmplitude: 1, | ||
attackDuration: 0.1, | ||
decayAmplitude: 0.5, | ||
decayDuration: 0.2, | ||
sustainDuration: 3, | ||
releaseDuration: 1, | ||
}; | ||
|
||
constructor() { | ||
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)(); | ||
} | ||
|
||
setWithHarmonics(harmonics) { | ||
this.harmonics = harmonics; | ||
|
||
this.type = 'custom'; | ||
this.wave = new PeriodicWave(this.audioCtx, { | ||
real: new Float32Array(harmonics), | ||
imag: new Float32Array(harmonics.length), | ||
}); | ||
} | ||
}; |
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 was deleted.
Oops, something went wrong.