Skip to content
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

Disable positional audio for Safari as a temporal workaround for its positional audio bug #4503

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/components/audio-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,22 @@ AFRAME.registerComponent("audio-params", {
audio.panner.orientationY.value,
audio.panner.orientationZ.value
);
this.updateAttenuation();
} else {
this.el.object3D.getWorldDirection(this.data.orientation);
this.el.object3D.getWorldPosition(this.data.position);
this.data.rolloffFactor = 0;
}

this.updateDistances();

if (audio.panner) {
this.updateAttenuation();
} else {
const { audioOutputMode } = window.APP.store.state.preferences;
if (audioOutputMode === "audio") {
this.updateGain(this.data.gain * Math.min(1, 10 / Math.max(1, this.data.squaredDistance)));
}
}
}

if (this.normalizer !== null) {
Expand Down Expand Up @@ -360,11 +369,7 @@ AFRAME.registerComponent("audio-params", {
break;
}
}
const { audioOutputMode } = window.APP.store.state.preferences;
if (audioOutputMode === "audio") {
this.data.gain = newGain * Math.min(1, 10 / Math.max(1, this.data.squaredDistance));
}
gainFilter?.gain.setTargetAtTime(this.data.gain, audio.context.currentTime, GAIN_TIME_CONST);
gainFilter?.gain.setTargetAtTime(newGain, audio.context.currentTime, GAIN_TIME_CONST);
},

updateClipping() {
Expand Down
23 changes: 20 additions & 3 deletions src/components/avatar-audio-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,20 @@ AFRAME.registerComponent("avatar-audio-source", {
const isRemoved = !this.el.parentNode;
if (!stream || isRemoved) return;

const ouputMode = window.APP.store.state.preferences.audioOutputMode;
if (ouputMode === "audio") {
this.el.setAttribute("audio-params", {
audioType: AudioType.Stereo
});
}

let audio = null;
const audioListener = this.el.sceneEl.audioListener;
const audio = new THREE.PositionalAudio(audioListener);
if (this.el.components["audio-params"].data.audioType === AudioType.PannerNode) {
audio = new THREE.PositionalAudio(audioListener);
} else {
audio = new THREE.Audio(audioListener);
}
this.el.components["audio-params"].setAudio(audio);

this.audioSystem.removeAudio(audio);
Expand Down Expand Up @@ -271,11 +283,16 @@ AFRAME.registerComponent("audio-target", {
this.el.removeAttribute("audio-params");
this.el.removeAttribute("audio-zone-source");
},

createAudio: function() {
const audioListener = this.el.sceneEl.audioListener;
const ouputMode = window.APP.store.state.preferences.audioOutputMode;
if (ouputMode === "audio") {
this.el.setAttribute("audio-params", {
audioType: AudioType.Stereo
});
}

let audio = null;
const audioListener = this.el.sceneEl.audioListener;
if (this.el.components["audio-params"].data.audioType === AudioType.PannerNode) {
audio = new THREE.PositionalAudio(audioListener);
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/components/media-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,13 @@ AFRAME.registerComponent("media-video", {
this.el.removeObject3D("sound");
}

const ouputMode = window.APP.store.state.preferences.audioOutputMode;
if (ouputMode === "audio") {
this.el.setAttribute("audio-params", {
audioType: AudioType.Stereo
});
}

const audioListener = this.el.sceneEl.audioListener;
if (this.el.components["audio-params"].data.audioType === AudioType.PannerNode) {
this.audio = new THREE.PositionalAudio(audioListener);
Expand Down
6 changes: 2 additions & 4 deletions src/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import "./utils/threejs-positional-audio-updatematrixworld";
import "./utils/threejs-world-update";
import "./utils/threejs-raycast-patches";
import patchThreeAllocations from "./utils/threejs-allocation-patches";
import { detectOS, detect } from "detect-browser";
import { isSafari } from "./utils/detect-safari";
import {
getReticulumFetchUrl,
getReticulumMeta,
Expand Down Expand Up @@ -670,11 +670,9 @@ document.addEventListener("DOMContentLoaded", async () => {
return;
}

const detectedOS = detectOS(navigator.userAgent);
const browser = detect();
// HACK - it seems if we don't initialize the mic track up-front, voices can drop out on iOS
// safari when initializing it later.
if (["iOS", "Mac OS"].includes(detectedOS) && ["safari", "ios"].includes(browser.name)) {
if (isSafari()) {
try {
await navigator.mediaDevices.getUserMedia({ audio: true });
} catch (e) {
Expand Down
7 changes: 7 additions & 0 deletions src/storage/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import merge from "deepmerge";
import Cookies from "js-cookie";
import jwtDecode from "jwt-decode";
import { qsGet } from "../utils/qs_truthy.js";
import { isSafari } from "../utils/detect-safari";

const LOCAL_STORE_KEY = "___hubs_store";
const STORE_STATE_CACHE_KEY = Symbol();
Expand Down Expand Up @@ -243,6 +244,12 @@ export default class Store extends EventTarget {
preferences: {}
});

// Temporary fix for distorted audio in Safari.
// See https://github.com/mozilla/hubs/issues/4411
if (isSafari()) {
this.update({ preferences: { audioOutputMode: "audio" } });
}

this._shouldResetAvatarOnInit = false;

const oauthFlowCredentials = Cookies.getJSON(OAUTH_FLOW_CREDENTIALS_KEY);
Expand Down
8 changes: 6 additions & 2 deletions src/systems/audio-settings-system.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AvatarAudioDefaults, MediaAudioDefaults } from "../components/audio-params";
import { isSafari } from "../utils/detect-safari";

function updateMediaAudioSettings(mediaVideo, settings) {
mediaVideo.el.setAttribute("audio-params", {
Expand Down Expand Up @@ -51,9 +52,12 @@ export class AudioSettingsSystem {

this.sceneEl.addEventListener("reset_scene", this.onSceneReset);

// Do not force panner audio in Safari as a temporary fix for distorted audio.
// See https://github.com/mozilla/hubs/issues/4411
if (
!window.APP.store.state.preferences.audioOutputMode ||
window.APP.store.state.preferences.audioOutputMode === "audio"
!isSafari() &&
(!window.APP.store.state.preferences.audioOutputMode ||
window.APP.store.state.preferences.audioOutputMode === "audio")
) {
//hack to always reset to "panner"
window.APP.store.update({
Expand Down
6 changes: 6 additions & 0 deletions src/utils/detect-safari.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { detect } from "detect-browser";

export function isSafari() {
const browser = detect();
return ["iOS", "Mac OS"].includes(browser.os) && ["safari", "ios"].includes(browser.name);
}