Skip to content

Commit

Permalink
Refactor Audio params constants
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Jul 9, 2021
1 parent 35ad5c0 commit 05d3fdc
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
49 changes: 47 additions & 2 deletions src/components/audio-params.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Vector3 } from "three";
import { AudioNormalizer } from "../utils/audio-normalizer";
import { CLIPPING_THRESHOLD_ENABLED, CLIPPING_THRESHOLD_DEFAULT } from "../react-components/preferences-screen";
import { AvatarAudioDefaults, DISTANCE_MODEL_OPTIONS } from "../systems/audio-settings-system";

export const DISTANCE_MODEL_OPTIONS = ["linear", "inverse", "exponential"];

export const SourceType = Object.freeze({
MEDIA_VIDEO: 0,
Expand All @@ -11,6 +12,50 @@ export const SourceType = Object.freeze({
AUDIO_ZONE: 4
});

export const AudioType = {
Stereo: "stereo",
PannerNode: "pannernode"
};

export const DistanceModelType = {
Linear: "linear",
Inverse: "inverse",
Exponential: "exponential"
};

export const AvatarAudioDefaults = Object.freeze({
DISTANCE_MODEL: DistanceModelType.Inverse,
ROLLOFF_FACTOR: 2,
REF_DISTANCE: 1,
MAX_DISTANCE: 10000,
INNER_ANGLE: 180,
OUTER_ANGLE: 360,
OUTER_GAIN: 0,
VOLUME: 1.0
});

export const MediaAudioDefaults = Object.freeze({
DISTANCE_MODEL: DistanceModelType.Inverse,
ROLLOFF_FACTOR: 1,
REF_DISTANCE: 1,
MAX_DISTANCE: 10000,
INNER_ANGLE: 360,
OUTER_ANGLE: 0,
OUTER_GAIN: 0,
VOLUME: 0.5
});

export const TargetAudioDefaults = Object.freeze({
DISTANCE_MODEL: DistanceModelType.Inverse,
ROLLOFF_FACTOR: 5,
REF_DISTANCE: 8,
MAX_DISTANCE: 10000,
INNER_ANGLE: 170,
OUTER_ANGLE: 300,
OUTER_GAIN: 0.3,
VOLUME: 1.0
});

const MUTE_DELAY_SECS = 1;

const distanceModels = {
Expand All @@ -26,6 +71,7 @@ const distanceModels = {
};

AFRAME.registerComponent("audio-params", {
multiple: true,
schema: {
enabled: { default: true },
debuggable: { default: true },
Expand Down Expand Up @@ -308,7 +354,6 @@ AFRAME.registerComponent("audio-params", {
this.data.gain = this.data.gain * Math.min(1, 10 / Math.max(1, this.data.squaredDistance));
}
gainFilter?.gain.exponentialRampToValueAtTime(this.data.gain, audio.context.currentTime + MUTE_DELAY_SECS);
console.log(`gain updated: ${newGain}`);
},

updateClipping() {
Expand Down
3 changes: 1 addition & 2 deletions src/components/avatar-audio-source.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AvatarAudioDefaults, TargetAudioDefaults } from "../systems/audio-settings-system";
import { SourceType } from "./audio-params";
import { SourceType, AvatarAudioDefaults, TargetAudioDefaults } from "./audio-params";

const INFO_INIT_FAILED = "Failed to initialize avatar-audio-source.";
const INFO_NO_NETWORKED_EL = "Could not find networked el.";
Expand Down
2 changes: 1 addition & 1 deletion src/react-components/debug-panel/AudioDebugPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
GLOBAL_VOLUME_DEFAULT
} from "../../react-components/preferences-screen";
import { SelectInputField } from "../input/SelectInputField";
import { DISTANCE_MODEL_OPTIONS } from "../../systems/audio-settings-system";
import { DISTANCE_MODEL_OPTIONS, DistanceModelType } from "../../components/audio-params"

const ROLLOFF_MIN = 0.0;
const ROLLOFF_MAX = 20.0;
Expand Down
7 changes: 4 additions & 3 deletions src/systems/audio-debug-system.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { THREE } from "aframe";
import audioDebugVert from "./audio-debug.vert";
import audioDebugFrag from "./audio-debug.frag";
import { DistanceModelType } from "../components/audio-params";

const MAX_DEBUG_SOURCES = 64;

Expand Down Expand Up @@ -103,11 +104,11 @@ AFRAME.registerSystem("audio-debug", {
this.sourcePositions[sourceNum] = source.data.position;
this.sourceOrientations[sourceNum] = source.data.orientation;
this.distanceModels[sourceNum] = 0;
if (source.data.distanceModel === "linear") {
if (source.data.distanceModel === DistanceModelType.Linear) {
this.distanceModels[sourceNum] = 0;
} else if (source.data.distanceModel === "inverse") {
} else if (source.data.distanceModel === DistanceModelType.Inverse) {
this.distanceModels[sourceNum] = 1;
} else if (source.data.distanceModel === "exponential") {
} else if (source.data.distanceModel === DistanceModelType.Exponential) {
this.distanceModels[sourceNum] = 2;
}
this.maxDistances[sourceNum] = source.data.maxDistance;
Expand Down
35 changes: 1 addition & 34 deletions src/systems/audio-settings-system.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,4 @@
export const AvatarAudioDefaults = Object.freeze({
DISTANCE_MODEL: "inverse",
ROLLOFF_FACTOR: 2,
REF_DISTANCE: 1,
MAX_DISTANCE: 10000,
INNER_ANGLE: 180,
OUTER_ANGLE: 360,
OUTER_GAIN: 0,
VOLUME: 1.0
});

export const MediaAudioDefaults = Object.freeze({
DISTANCE_MODEL: "inverse",
ROLLOFF_FACTOR: 1,
REF_DISTANCE: 1,
MAX_DISTANCE: 10000,
INNER_ANGLE: 360,
OUTER_ANGLE: 0,
OUTER_GAIN: 0,
VOLUME: 0.5
});

export const TargetAudioDefaults = Object.freeze({
DISTANCE_MODEL: "inverse",
ROLLOFF_FACTOR: 5,
REF_DISTANCE: 8,
MAX_DISTANCE: 10000,
INNER_ANGLE: 170,
OUTER_ANGLE: 300,
OUTER_GAIN: 0.3,
VOLUME: 1.0
});

export const DISTANCE_MODEL_OPTIONS = ["linear", "inverse", "exponential"];
import { AvatarAudioDefaults, MediaAudioDefaults } from "../components/audio-params";

function updateMediaAudioSettings(mediaVideo, settings) {
mediaVideo.el.setAttribute("audio-params", {
Expand Down

0 comments on commit 05d3fdc

Please sign in to comment.