-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
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
Three.Audio evolution #7485
Comments
Couldn't you do A and B by adding the |
i think you are right, it could work for A and B. But i remember (maybe i am wrong) trying to simply copy my camera position in the Audio (i add the listener to the camera) and had weird result when rotating, which is when i think now probably because i did not copy the rotation. I ended placing the audio where i look at (1 meter forward) and it worked for me. i will try this. still, i think adding the possibility to reuse same buffer, or connect to a node would be nice. |
Maybe we need a |
It is a possibility The AudioSource could "abstract/hide" from where the audio data come (file, buffer, or node). If we introduce an AudioSource class, it will surely encapsulate loading/source, and not the positioning (which is Three.Audio role), but what about playback control? |
i am still thinking, but what i am trying to see which could be a good addition. |
just a little followup concerning case A and B. There definitively is something strange happening when i add a THREE.Audio to the Listener, or to the camera (the listener itself is added to the camera). just for testing purpose, i added a little function to my custom Audio test class THREE.Audio.prototype.createFromBuffer = function ( buffer ) {
this.source.buffer = buffer;
if ( this.autoplay ) this.play();
return this;
}; and preloading/decoding myself the audio buffer. Seems to work normally. |
Another option would be |
I honestly am not sure which one is the more intuitive. if we try to (very broadly) categorize what is available in webaudio API, there is 3 type of "object".
right now, Audio does the two first roles. I suppose the best is to not break (or not much) existing code relying on Audio while providing more flexibility? Plus, I don't think 'encapsulating' to much of webaudio api is really useful (for now). what we could do:
regarding AudioBase itself, i can see also a use case for disabling the panner filter if the user wants to have an 'ambient' sound with no position. Also, there are some setter/getter which can be added for finer control of panner (like distanceModel and maxDistance). And one last thing, but i do not know yet where (or if) we could put this (maybe in the listener), generally in game, you can have multiple sound playing at the same time and using a DynamicCompressor Node just at the end can avoid clipping sound/volume problems. So having this possibility would be useful(but only just before the Listener...) |
I would add, if you prefer better naming (but with side effect of "breaking" old code, though easy to fix):
|
I was suggesting So the API would be something like this... var source = new THREE.AudioSource().load( file );
var audio1 = new THREE.Audio( listener ).setSource( source );
var audio2 = new THREE.Audio( listener ).setSource( source ); Or maybe this would be more intuitive... var buffer = new THREE.AudioBuffer().load( file );
var audio1 = new THREE.Audio( listener ).setBuffer( buffer );
var audio2 = new THREE.Audio( listener ).setBuffer( buffer ); As per Ambient audio... Maybe we can make |
Ok, i understand now what you are suggesting. That would solve all the buffer sharing quite simply. I am leaning for THREE.AudioBuffer because, well, in fact it is just an webaudio audioBuffer inside with convenience method for loading. PositionAudio with the panner seems a good idea to me too. with that, positioning sounds, ambient sounds and buffer sharing would be handled easily. now, we can also try to go a little further (the whole "connecting any audio source") with, i think, not a lot more code. this is the second reason i prefer THREE.AudioBuffer, because we could add a second (or more) class for no-buffer source, which could be named THREE.AudioSource like that for example: var source = new THREE.AudioSource().setInputNode( audioNode );
var audio1 = new THREE.Audio( listener ).setSource( source );
var audio2 = new THREE.Audio( listener ).setSource( source ); there could be any audio source (or audio node graph) connected like that ( OscillatorNode, MediaElementAudioSourceNode, MediaStreamAudioSourceNode) without having to handle the details oursevles (you want to connect you're own AudioNode to an audio, you handle their details/setup). last, currently, the THREE.Audio directly connect to the listener with this line: this.gain.connect( this.context.destination ); i propose to change it like this this.gain.connect( listener.getOutputNode() ); where getOutputNode() is a method of THREE.AudioListener returning a gain node which has been setup internally when THREE.AudioListener is created. thus, we would have some sort of master volume (for the output) and could add a setfilter if someone want to add for example a DynamicCompressor or whatever he wants just before global audio output. if it is ok with you, i can try to do a quick proof of concept? |
Sounds good! |
just a little followup. I am still working on it (should have more time this week-end). One thing i noticed while refactoring the code and reading about the details of pannerNode, which maybe the source of the strange effect i had while adding the THREE.Audio to the camera. in the original code of THREE.Audio return function updateMatrixWorld( force ) {
THREE.Object3D.prototype.updateMatrixWorld.call( this, force );
position.setFromMatrixPosition( this.matrixWorld );
this.panner.setPosition( position.x, position.y, position.z );
}; here the panner has its position updated... but a panner also has an orientation https://developer.mozilla.org/en-US/docs/Web/API/PannerNode/setOrientation this one is currently never updated which may be important if the panner use a soundcone. There is no definition of the soundcone in the current code, which i suppose mean it must use the "default" soundcone of the navigator. i suppose for now the soundcone is 360° by default, so maybe it is not important here, but we should check it later... |
Yep! At the moment is omnidirectional. |
I Have now a working, albeit not complete, evolution of the Audio system. here my three.js github clone with the work done on audio (branch dev_audio_#7485) https://github.com/vincent-courtalon/three.js/tree/dev_audio_%237485 I modified THREE.Audio (remove panner/position and add buffer support) and THREE.AudioListener(added filter and master volume capacity). Some controls are still missing (like finer control of panner in PositionAudio) and i have not yet added the possibility to 'connect' a node as a source for Three.Audio. what do you think, should we just silently ignore when you call play/pause/start... on a THREE.Audio connected to a node source ( and not a buffer) or should we generate an error? When these are added and if you are ok with it, i will submit you a pull request. ps: i did not commit the build, you need to build first to test the example |
Do you mind doing Pull Request? That way it'll be easy to see what changed. |
@mrdoob - i used this to see the changes: dev...vincent-courtalon:dev_audio_#7485 but yes it was trouble to dig that up and pullreqs show everything nicely so certainly the right way at this point. just figured to share that comparison link for convenience. |
Yeah, that's what Pull Requests are for, to show the changes. A Pull Request can still be modified until is good to merge. |
However, that does the trick too. @vincent-courtalon I would rename |
Sorry for the lack of pull Request, i will make one now (i commit the renaming of PositionAudio just now). edit: made the pull request: #7562 |
Vincent, just wanted to add I'm very excited to see this move forward. Thanks for your work! |
Thanks, glad to know it will be useful to other peoples! I just added support for connecting an audioNode to a THREE.Audio. i updated the example with a very basic oscillator: var sound3 = new THREE.PositionalAudio( listener );
var oscillator = listener.context.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.value = 144;
oscillator.start(0);
sound3.setNodeSource(oscillator); if anyone has any simple more interesting example... I am not completely happy with the code yet, but it seem to work (although i only tested on chrome and firefox). I will next add finer panner controls (like distance model) and i think i will then be ok for now. |
I Will test it more, but i don't plan to add more feature right now. |
Hello, just a little followup after the merge. I will make a pull request now |
Sounds good! |
I just want to comment that this is excellent work - I'm doing a project using the THREE.js library that is very focused on audio now and it's very clear. If I have some time, I may make an example that shows how to add a filter, and how to use envelopes for gain changes (to avoid clicking caused by instantaneous setters). |
@spiricom That sounds good! |
Closing this as the suggested features have now been implemented. |
Hello, i recently upgraded from R71 to R73 and am pretty happy with the evolution of Three.Audio
I am currently developing some sort of game, and i need different type of sound capabilities
A) ambient sound (no position, just some background)
B) "event" sound, like footstep, punch... (once again, no position needed)
C) positional sound (like a torch on a wall, a monster, a fountain...)
Three.audio is not really useful for A and B, which is ok (but you loose some of the ease of use that provide Three.audio)
C is clearly the intended use of Three.audio, with the use of the panner to position the sound seamlessly with position update from Three.
but i could see some enhancement (some easy, some more complex) added.
what do other peoples think of this ? any idea ?
if it is interesting and possible to have that in Three, i could try and do an implementation candidate for a pull request instead of just having my own code (case in point, if it is something needed and acceptable for Three).
The text was updated successfully, but these errors were encountered: