8000 ToneJS (vR12) flips the user around in spatial audio · Issue #7410 · cables-gl/cables · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
ToneJS (vR12) flips the user around in spatial audio #7410
Open
@sonofluiz

Description

@sonofluiz

I found a discrepancy in the version r12 of ToneJS hosted on cables.

When working with spatial audio (3D audio), webaudio defaults to having the listener looking downing the -Z axis, similar to how we view the coordinates in cablesGL. This seems to be standard across webGL,openGL, webaudio, etc...

right handed cartesian, I believe

Newer versions of toneJS does this as well but version r12 sets the listener facing down the +Z axis (a 180 flip). So Tone sets the listener with their back to default starting positions of spatial audio sources.

Web Audio API
forwardZ = -1

AudioListener.forwardX
Represents the horizontal position of the listener's forward direction in the same cartesian coordinate system as the position (positionX, positionY, and positionZ) values. The forward and up values are linearly independent of each other. The default is 0.

AudioListener.forwardY
Represents the vertical position of the listener's forward direction in the same cartesian coordinate system as the position (positionX, positionY, and positionZ) values. The forward and up values are linearly independent of each other. The default is 0.

AudioListener.forwardZ
Represents the longitudinal (back and forth) position of the listener's forward direction in the same cartesian coordinate system as the position (positionX, positionY, and positionZ) values. The forward and up values are linearly independent of each other. The default is -1.

Newer ToneJS versions get values from the wrapped WebAudio context
forwardZ = -1

	readonly forwardX: Param = new Param({
		context: this.context,
		param: this.context.rawContext.listener.forwardX,
	})

	readonly forwardY: Param = new Param({
		context: this.context,
		param: this.context.rawContext.listener.forwardY,
	})

	readonly forwardZ: Param = new Param({
		context: this.context,
		param: this.context.rawContext.listener.forwardZ,
	})

Tone JS vR12 does not reference WebAudio defaults, instead sets its own values
forwardZ = 1

Tone.Listener.defaults = {
	        'positionX': 0,
	        'positionY': 0,
	        'positionZ': 0,
	        'forwardX': 0,
	        'forwardY': 0,
	        'forwardZ': 1,
	        'upX': 0,
	        'upY': 1,
	        'upZ': 0
	    };

Seems like conventions changed or the tone JS devs were confused and made a mistake. Not a big deal but anyone working with vR12 of tone and spatial audio might get confused or lost. This can be easily remedied by editing some lines in the toneJS file cables hosts or by adding adding a line to the CABLES.WEBAUDIO class. If the source code isn't edited user's will have to reset this listener position every time they want to work with spatial audio + tone.

In https://sandbox.cables.gl/api/lib/tone.js

Change

Tone.Listener.defaults = {
	        'positionX': 0,
	        'positionY': 0,
	        'positionZ': 0,
	        'forwardX': 0,
	        'forwardY': 0,
	        'forwardZ': 1,
	        'upX': 0,
	        'upY': 1,
	        'upZ': 0
	    };

To

Tone.Listener.defaults = {
	        'positionX': 0,
	        'positionY': 0,
	        'positionZ': 0,
	        'forwardX': 0,
	        'forwardY': 0,
	        'forwardZ': -1,
	        'upX': 0,
	        'upY': 1,
	        'upZ': 0
	    };

Or, in cables/src/corelibs/webaudio
/webaudio.js

Change

// check if tone.js lib is being used
            if (window.Tone && !this.toneJsInitialized)
            {
                // set current audio context in tone.js
                Tone.setContext(window.audioContext);
                this.toneJsInitialized = true;
            }

To

// check if tone.js lib is being used
            if (window.Tone && !this.toneJsInitialized)
            {
                // set current audio context in tone.js
                Tone.setContext(window.audioContext);
                Tone.context._context.listener.forwardZ.value = -1;
                this.toneJsInitialized = true;
            }

This is not really a bug since Tone is behaving as it is documented in older versions, but it is not following convention and overriding the default webaudio context. For example if I wanted to handle spatial audio purely with the webaudio api and lets say i just wanted to use a tone instrument in the signal path. Just adding the R12 version tone library would override the default listener position without warning.

An even better fix than my proposed fixes would be to do what the newer tone lib does: just pass the current webaudio position values.

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0