-
-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: setup audio component #1587
base: main
Are you sure you want to change the base?
Conversation
packages/loader/src/AudioLoader.ts
Outdated
return new AssetPromise((resolve) => { | ||
this.request<ArrayBuffer>(item.url, { type: "arraybuffer" }).then((arrayBuffer) => { | ||
AudioManager.context.decodeAudioData(arrayBuffer).then((result) => { | ||
resolve(result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
|
||
/** play the sound from the very beginning */ | ||
public play() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete public
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
: void
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
|
||
/** Current playback progress, in seconds */ | ||
get position(): number { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Position?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to "time"
* Plays the clip. | ||
*/ | ||
play(): void { | ||
if (!this._clip || !this.clip.duration || this.isPlaying) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.clip.duration why not this._clip.duration?
if (this.startTime > this._clip.duration || this.startTime < 0) return; | ||
if (this._duration && this._duration < 0) return; | ||
|
||
this._pausedTime = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_pausedTime type is number
@deepClone | ||
private _endTime: number = null; | ||
@deepClone | ||
private _duration: number = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
number init null is not good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
packages/core/src/audio/AudioClip.ts
Outdated
/** | ||
* the number of discrete audio channels | ||
*/ | ||
get channels(): Readonly<number> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get channels(): number
is OK
packages/core/src/audio/AudioClip.ts
Outdated
private _audioBuffer: AudioBuffer; | ||
|
||
/** | ||
* the number of discrete audio channels |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Comments need to add a period,param comments do not need
- Capitalize the first letter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unit test must add!
packages/core/src/audio/AudioClip.ts
Outdated
/** | ||
* get the clip's audio buffer | ||
*/ | ||
getData(): AudioBuffer { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid web native interfaces as much as possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think getData and setData is a must
packages/core/src/audio/index.ts
Outdated
@@ -0,0 +1,4 @@ | |||
export { AudioManager } from "./AudioManager"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AudioManager is internal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no export now
super(entity); | ||
const gain = AudioManager.context.createGain(); | ||
gain.connect(AudioManager.context.destination); | ||
AudioManager.listener = gain; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only allowed one listener?
constructor(entity: Entity) { | ||
super(entity); | ||
const gain = AudioManager.context.createGain(); | ||
gain.connect(AudioManager.context.destination); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be encapsulated
AudioManager._context = new window.AudioContext(); | ||
} | ||
if (AudioManager._context.state !== "running") { | ||
window.document.addEventListener("pointerdown", AudioManager._unlock, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should do this when call context
every time?
return new AssetPromise((resolve, reject) => { | ||
this.request<ArrayBuffer>(item.url, { type: "arraybuffer" }).then((arrayBuffer) => { | ||
AudioManager.context | ||
.decodeAudioData(arrayBuffer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should do some encapsulation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now it loads onto audioClip, not audio buffer
const duration = this.endTime ? this.endTime - this._pausedTime : null; | ||
this._play(this._pausedTime, duration); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the unique value that is different from play
from the perspective of usage
if (this.isPlaying) { | ||
return this._pausedTime | ||
? this.engine.time.elapsedTime - this._absoluteStartTime + this._pausedTime | ||
: this.engine.time.elapsedTime - this._absoluteStartTime + this.startTime; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks a bit complicated
@deepClone | ||
private _endTime: number = null; | ||
@deepClone | ||
private _duration: number = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
/** | ||
* Stops playing the clip. | ||
*/ | ||
stop(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the unique value that is different from pause
from the perspective of usage? It's call play
form 0 next time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now only has play stop and pause, removed unpause
return this._playbackRate; | ||
} | ||
|
||
set playbackRate(value: number) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your comments use speed
, API use rate
,try to use the unified concept
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use rate
set endTime(value: number) { | ||
this._endTime = value; | ||
this._duration = this._endTime - this._startTime; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean clip start and end time?
Please check if the PR fulfills these requirements
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
setup audio feature
UML API design
Other information:
Playground: galacean/galacean.github.io#752