forked from jspsych/jsPsych
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jspsych#3179 from jspsych/audio-player
Add AudioPlayer class that handles both WebAudio and HTML5 audio
- Loading branch information
Showing
14 changed files
with
1,272 additions
and
601 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,8 @@ | ||
--- | ||
"jspsych": major | ||
"@jspsych/plugin-audio-button-response": minor | ||
"@jspsych/plugin-audio-keyboard-response": minor | ||
"@jspsych/plugin-audio-slider-response": minor | ||
--- | ||
|
||
Changed plugins to use AudioPlayer class; added tests using AudioPlayer mock; plugins now use AudioPlayerInterface. |
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
--- | ||
"@jspsych/test-utils": minor | ||
--- | ||
|
||
clickTarget method now respects disabled tag on form elements. |
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,101 @@ | ||
export interface AudioPlayerOptions { | ||
useWebAudio: boolean; | ||
audioContext?: AudioContext; | ||
} | ||
|
||
export interface AudioPlayerInterface { | ||
load(): Promise<void>; | ||
play(): void; | ||
stop(): void; | ||
addEventListener(eventName: string, callback: EventListenerOrEventListenerObject): void; | ||
removeEventListener(eventName: string, callback: EventListenerOrEventListenerObject): void; | ||
} | ||
|
||
export class AudioPlayer implements AudioPlayerInterface { | ||
private audio: HTMLAudioElement | AudioBufferSourceNode; | ||
private webAudioBuffer: AudioBuffer; | ||
private audioContext: AudioContext | null; | ||
private useWebAudio: boolean; | ||
private src: string; | ||
|
||
constructor(src: string, options: AudioPlayerOptions = { useWebAudio: false }) { | ||
this.src = src; | ||
this.useWebAudio = options.useWebAudio; | ||
this.audioContext = options.audioContext || null; | ||
} | ||
|
||
async load() { | ||
if (this.useWebAudio) { | ||
this.webAudioBuffer = await this.preloadWebAudio(this.src); | ||
} else { | ||
this.audio = await this.preloadHTMLAudio(this.src); | ||
} | ||
} | ||
|
||
play() { | ||
if (this.audio instanceof HTMLAudioElement) { | ||
this.audio.play(); | ||
} else { | ||
// If audio is not HTMLAudioElement, it must be a WebAudio API object, so create a source node. | ||
if (!this.audio) this.audio = this.getAudioSourceNode(this.webAudioBuffer); | ||
this.audio.start(); | ||
} | ||
} | ||
|
||
stop() { | ||
if (this.audio instanceof HTMLAudioElement) { | ||
this.audio.pause(); | ||
this.audio.currentTime = 0; | ||
} else { | ||
this.audio!.stop(); | ||
// Regenerate source node for audio since the previous one is stopped and unusable. | ||
this.audio = this.getAudioSourceNode(this.webAudioBuffer); | ||
} | ||
} | ||
|
||
addEventListener(eventName: string, callback: EventListenerOrEventListenerObject) { | ||
// If WebAudio buffer exists but source node doesn't, create it. | ||
if (!this.audio && this.webAudioBuffer) | ||
this.audio = this.getAudioSourceNode(this.webAudioBuffer); | ||
this.audio.addEventListener(eventName, callback); | ||
} | ||
|
||
removeEventListener(eventName: string, callback: EventListenerOrEventListenerObject) { | ||
// If WebAudio buffer exists but source node doesn't, create it. | ||
if (!this.audio && this.webAudioBuffer) | ||
this.audio = this.getAudioSourceNode(this.webAudioBuffer); | ||
this.audio.removeEventListener(eventName, callback); | ||
} | ||
|
||
private getAudioSourceNode(audioBuffer: AudioBuffer): AudioBufferSourceNode { | ||
const source = this.audioContext!.createBufferSource(); | ||
source.buffer = audioBuffer; | ||
source.connect(this.audioContext!.destination); | ||
return source; | ||
} | ||
|
||
private async preloadWebAudio(src: string): Promise<AudioBuffer> { | ||
const buffer = await fetch(src); | ||
const arrayBuffer = await buffer.arrayBuffer(); | ||
const audioBuffer = await this.audioContext!.decodeAudioData(arrayBuffer); | ||
const source = this.audioContext!.createBufferSource(); | ||
source.buffer = audioBuffer; | ||
source.connect(this.audioContext!.destination); | ||
return audioBuffer; | ||
} | ||
|
||
private async preloadHTMLAudio(src: string): Promise<HTMLAudioElement> { | ||
return new Promise<HTMLAudioElement>((resolve, reject) => { | ||
const audio = new Audio(src); | ||
audio.addEventListener("canplaythrough", () => { | ||
resolve(audio); | ||
}); | ||
audio.addEventListener("error", (err) => { | ||
reject(err); | ||
}); | ||
audio.addEventListener("abort", (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
} |
Oops, something went wrong.