Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
move init audio state (#9317)
Browse files Browse the repository at this point in the history
  • Loading branch information
HexaField authored Nov 22, 2023
1 parent 8d3d20d commit 4aece7a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
30 changes: 23 additions & 7 deletions packages/engine/src/audio/AudioState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,19 @@ export const useAudioState = () => {
const audioState = useHookstate(getMutableState(AudioState))

useEffect(() => {
const audioContext = getState(AudioState).audioContext
const AudioContext = globalThis.AudioContext || globalThis.webkitAudioContext
if (!AudioContext) return

const audioContext = new AudioContext()
audioContext.resume()

const audioState = getMutableState(AudioState)
audioState.audioContext.set(audioContext)

const cameraGainNode = audioContext.createGain()
audioState.cameraGainNode.set(cameraGainNode)
cameraGainNode.connect(audioContext.destination)

const currentTime = audioState.audioContext.currentTime.value

audioState.cameraGainNode.gain.value.setTargetAtTime(audioState.masterVolume.value, currentTime, 0.01)
Expand Down Expand Up @@ -131,48 +141,54 @@ export const useAudioState = () => {
}, [])

useEffect(() => {
if (!audioState.audioContext.value) return
audioState.cameraGainNode.value.gain.setTargetAtTime(
audioState.masterVolume.value,
audioState.audioContext.value.currentTime,
0.01
)
}, [audioState.masterVolume])
}, [audioState.audioContext, audioState.masterVolume])

useEffect(() => {
if (!audioState.audioContext.value) return
audioState.gainNodeMixBuses.value.mediaStreams.gain.setTargetAtTime(
audioState.mediaStreamVolume.value,
audioState.audioContext.value.currentTime,
0.01
)
}, [audioState.mediaStreamVolume])
}, [audioState.audioContext, audioState.mediaStreamVolume])

useEffect(() => {
if (!audioState.audioContext.value) return
audioState.gainNodeMixBuses.value.notifications.gain.setTargetAtTime(
audioState.notificationVolume.value,
audioState.audioContext.value.currentTime,
0.01
)
}, [audioState.notificationVolume])
}, [audioState.audioContext, audioState.notificationVolume])

useEffect(() => {
if (!audioState.audioContext.value) return
audioState.gainNodeMixBuses.value.soundEffects.gain.setTargetAtTime(
audioState.soundEffectsVolume.value,
audioState.audioContext.value.currentTime,
0.01
)
}, [audioState.soundEffectsVolume])
}, [audioState.audioContext, audioState.soundEffectsVolume])

useEffect(() => {
if (!audioState.audioContext.value) return
audioState.gainNodeMixBuses.value.music.gain.setTargetAtTime(
audioState.backgroundMusicVolume.value,
audioState.audioContext.value.currentTime,
0.01
)
}, [audioState.backgroundMusicVolume])
}, [audioState.audioContext, audioState.backgroundMusicVolume])

useEffect(() => {
if (!audioState.positionalMedia.value) return
getMutableState(MediaSettingsState).immersiveMedia.set(audioState.positionalMedia.value)
}, [audioState.positionalMedia])
}, [audioState.audioContext, audioState.positionalMedia])
}

export const getPositionalMedia = () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/engine/src/audio/systems/MediaSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ const reactor = () => {
if (!isClient) return null

useEffect(() => {
const audioContext = getState(AudioState).audioContext

const enableAudioContext = () => {
const audioContext = getState(AudioState).audioContext
if (audioContext.state === 'suspended') audioContext.resume()
}

Expand Down
13 changes: 1 addition & 12 deletions packages/engine/src/initializeBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { BotUserAgent } from '@etherealengine/common/src/constants/BotUserAgent'
import { getMutableState } from '@etherealengine/hyperflux'
import { WebLayerManager } from '@etherealengine/xrui'

import { AudioState } from './audio/AudioState'
import { CameraComponent } from './camera/components/CameraComponent'
import { Engine } from './ecs/classes/Engine'
import { EngineState } from './ecs/classes/EngineState'
Expand All @@ -41,16 +40,6 @@ import { ObjectLayers } from './scene/constants/ObjectLayers'
* initializes everything for the browser context
*/
export const initializeBrowser = () => {
const audioState = getMutableState(AudioState)

const audioContext = new (globalThis.AudioContext || globalThis.webkitAudioContext)()
audioContext.resume()
audioState.audioContext.set(audioContext)

const cameraGainNode = audioContext.createGain()
audioState.cameraGainNode.set(cameraGainNode)
cameraGainNode.connect(audioContext.destination)

const camera = getComponent(Engine.instance.cameraEntity, CameraComponent)

camera.layers.disableAll()
Expand All @@ -63,7 +52,7 @@ export const initializeBrowser = () => {

EngineRenderer.instance.initialize()
const renderer = EngineRenderer.instance.renderer
if (!renderer) throw new Error('EngineRenderer.instance.renderer must exist before initializing XRUISystem')
if (!renderer) throw new Error('EngineRenderer.instance.renderer does not exist!')

WebLayerManager.initialize(renderer)
WebLayerManager.instance.ktx2Encoder.pool.setWorkerLimit(1)
Expand Down

0 comments on commit 4aece7a

Please sign in to comment.