Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Help with CopperCube
Playing sound problem in webgl

Alireza
Guest
Quote
2023-01-15 03:10:13

I tried both .mp3 and .ogg format.
But in webgl , that only works in "when key pressed do..."
And "when click ....." .
But i want it work in my script, for example in "ccbInvokeAction()".

Also 3d sound don work in webgl at all.


okeoke
Registered User
Quote
2023-01-15 09:45:49

Hi Alireza,

Modern web browsers only allow audio context after user makes any actions on the page. You need to create another scene, there a player presses something to switch to your main scene. Please notice, that's not a coppercube limitation, it's how the modern browsers work.


Alireza
Guest
Quote
2023-01-15 14:35:27

thank you. So I think what you mean is that we play the sounds once in a scene, and then go to the main scene.
But what about 3d sound? It is not implemented under any circumstances. Is there a way for it?


okeoke
Registered User
Quote
2023-01-16 10:59:06

It actually shouldn't matter if you're using 3d sound node or play a sound from the script - once again it's a browser limitation, not coppercube. You just need any user input to happen before you sound plays.
If adding another scene doesn't work for you, it might be something wrong with the file format you're using. Both wav and ogg still can use different codecs and other different parameters like sampling rate or audio depth rate, which might not be supported by coppecube. Just try everything with the sound file that works for you. So you know that the cause of your issue is not the sound file itself.
Also if you're developing for browser it's a good idea to check browser devtools -> console and network tab for the errors. Do you see any errors there?


Alireza
Guest
Quote
2023-01-17 20:12:29

As you said i should play all sound by user input and then use them wherever i want.
But i dont know what file name i should set to
CcbPlaySound(filename).?
I put sound file beside ccb file and set it name(foe example : sound1.ogg) , it does not work .
Even i put full local address for example :
Ccbplaysound("E:\\myProj\sounds\sound1.ogg")
And it dose not work still.
I dont konw how to use this command


okeoke
Registered User
Quote
2023-01-18 11:02:44

Hi Alireza,

First of all sorry, it actually doesn't work if you use ccbPlaySound. For some reason, I thought it does, but it really doesn't.

Regarding the path you're using, you should avoid using the absolute paths, and use relative intead. In this case myProj - is your root directory, so the path whould be 'sounds/sound1.ogg'.

Also notice that "\" in js string is a special character used for the masking, so js reads your string as "E:\myProjsoundssound1.ogg". You can easily verify that by pressing f12 (if you're in chrome or ff) and executing
console.log('"E:\\myProj\sounds\sound1.ogg"')
under the Console tab.

Anyway, here is a working workaround, please notice it only works in browser:

// load your sound from sounds folder under your project root:
const sound = new Audio('sounds/sound1.ogg');

// add events listener which listens for the key with code 71 to be down
// 71 corresponds to G with ENG keyboard
document.addEventListener('keydown', event => {
if (event.keyCode === 71) {
// pause sound if it already plays
sound.pause();
// set current play time to 0
sound.currentTime = 0;
// play the sound
sound.play();
}
});


You can also find the example project here: https://drive.google.com/file/d/...


just_in_case
Moderator
Quote
2023-01-18 11:19:03

most of the webGL commands or HTML5 commands will work in CC, so if something isn't working then you can try searching the basic general web API alternative for that command. Glad that @okeoke already fixed your problem.

As CC's webGL is already open source, you can do many other additional things in there, that you can do with other web apps.


Alireza
Guest
Quote
2023-01-20 08:48:25

Hi okeoke and just-in-case. Thank you but i dont know how to use web and html related js code in coppercube. Can i directly use them in action and behavior ? Or should i use them only in html file?


just_in_case
Moderator
Quote
2023-01-21 07:18:32

You can use them both in HTML file or directly in the action or behavior but that action or behavior will only work on webGL platform.


vins
Registered User
Quote
2023-09-01 13:20:13

Simply adjusting this function in SDK can make it work
[scriptinginterface.js]https://www.ambiera.at/downloads...
function ccbPlaySound(name)
{
var sndmgr = CL3D.gSoundManager;
//var snd = sndmgr.getSoundFromName(name);
var snd = sndmgr.getSoundFromSoundName(name, true);
if (snd != null)
sndmgr.play2D(snd, false, 1.0);
}



Dieter
Guest
Quote
2023-09-01 16:35:43

SDK? You mean you have to recompile CC?

May I suggest to you guys (tho I see the thread is old), we
just had a similar discussion, and we figured out how to start and stop sounds by script in webGl, which is an undocumented feature:

The playmode of a sound-node can b set to 0 or 2, for "nothing" or "looping".
It would be ideal to obtain the actual sound handle and then simply use JS code to control the sound. But I didn't manage to achieve that so far. However, here's the syntax:

Assumin there is a sound node in the scenegraph, named "mysound"

kiddo=ccbGetSceneNodeFromName("mysound");
a_path=kiddo.Ub.n; // gives the file path
a_loaded=kiddo.Ub.loaded; // whether the sound is fully loaded
a_volume=kiddo.Yg;
a_play=kiddo.Vn; // playmode: 0=stop, 2=loop, you can set this to pause and play a sound node, like:
kiddo.Vn=2; // starts the sound that's initially passive (playmode set to "nothing")
a_radius=kiddo.Ae;
dd__Audio_playmode[dd__NumberOfSounds]=a_play;
kiddo.Vn=0; // stop potentially playing CC sounds
// alert(a_play);
a_2d=kiddo.Bg; // whether the sound is set to be 3D or 2D.

That said, CC 3D sound in webGl supports only volume fall-off by distance. But I have just recently developed a complete spatial 3D sound engine for CC, so you might check back in the near future, as I plan to release it the sooner or later.

Or you can try to implement it by your own using my mini3Dsoundengine.js and add it "somehow" to CC. That was the actual problem, kind of tricky, as I couldn't just use the sound handles that CC uses internally, but had to check the paths and reload a copy of each sound, in order to control it.

Anyway, the nitty gritty of the sound engine is the setup of the audio panning reconnection of the sound pipeline, which was everything but intuitive code, containing lines such as:



x=dd__ambientAudio[c]; // the js audio object handle
x.crossOrigin="anonymous";
dd__ambientContext[c] = new AudioContext();
dd__source[c] = dd__ambientContext[c].createMediaElementSource(x);
dd__ambientPan[c] = dd__ambientContext[c].createStereoPanner();
dd__source[c].connect(dd__ambientPan[c]);
dd__ambientPan[c].connect(dd__ambientContext[c].destination);


(this is in a for loop, with c being the counter, and dd__ambientAudio[c] is an array/ list of audio handles, used in the game)

You'd find it here:
https://jfkeo1010etc.itch.io/mini3dsoundenginejs


Dieter
Guest
Quote
2023-09-01 17:05:04

May I add, yes, people are correct in that browser restrictions demand a user action for a sound to be played.

This is especially true for the very first sound - often the music, because if you just try to play it, regardless of user input, the browser completely snaps and disables all following sounds too, even despite later user action. So that'd be a game with no sound whatsoever.

Simple solution is a start screen scene, eg. a 100% scaled 2D overlay of a beautiful start image, and a suggestion to "click to start game". Then you can have a behavior on this start scene root node that will ccbSwitchToScene("maingamescene"); when a key is hit, specifically when the left mousebutton is released.
That click then makes sure the music is played, aswell as the following sounds.

Once the user is playing the game, luckily there is user action anyway most of the time, so only sounds that should be played strictly based on a timeline, or random event, may be suppressed. But even then, when the player is frenetically hammering the keys in game, there is plenty user activity for js to be convinced things are all fine. And you can also play multiple sounds on a single user action.

Furthermore, if the user opened a new tab for this game manually, this is also considered a user action. Tho, browsers seem to be a bit inconsistent, sometimes this latter trick works, sometimes not. You see this with unity games that open a new tab when you click "play game" on itch.io.

I'd rather suggest the start screen scene method, seems more robust.


Dieter
Guest
Quote
2023-09-01 17:10:20

Appendix:
As a matter of fact, I can clearly verify that sounds can be played without direct user action (at least not in a narrow time frame), as the doors in my game close automatically after a few seconds, and the sound that's played during that animation, it plays every time, even when I just stand there, watching and waiting 5 seconds for the door to close.


Dieter
Guest
Quote
2023-09-01 17:17:34

Oh, btw, I forgot :)

the sound files will always be stored in the folder copperlichtdata. So if you want to use ccbPlaySound(), you may best copy them to that folder in the first place.
Then a relative path can be used:
"copperlichtdata/mysound.mp3"


Dieter
Guest
Quote
2023-09-01 18:24:22

WOW!
I just found out how to get the javascript handle of a sound node in CC (compare to my above comments about undocumented CC webgl sound features)

kiddo=ccbGetSceneNodeFromName("mysound");
myhandle= kiddo.Ub.hc;

now you can use the whole javascript audio api, like:
myhandle.play();

If your code should run on multiple target platforms, not just webGl, use a code switch based on ccbGetPlatform().


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internationa?" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact