Page 1 of 1

Adding sound to track (asking)

Posted: Sat Dec 25, 2021 3:02 am
by mxrewind665
I’m working on a detailed project. I’ve been out of the community for a couple years and have returned. I played the new RF MXGP tracks and to my surprise there were horns, fans screaming ect all around the track. I would love to add this element to my coming project but I have no clue where to start or have any open source material to reference.

So my question is, how do you do it? Or is there a track with a open folder I could peek into to learn it and see how it was done?

Thank you.

Re: Adding sound to track (asking)

Posted: Sun Dec 26, 2021 2:59 am
by jlv
Here's an example:
https://mxsimulator.com/tracks/jstest2.zip

The sound stuff starts at line 814 in "frills.js".

The javascript interface is described here.

Re: Adding sound to track (asking)

Posted: Sun Dec 26, 2021 10:53 pm
by mxrewind665
jlv wrote: Sun Dec 26, 2021 2:59 am Here's an example:
https://mxsimulator.com/tracks/jstest2.zip

The sound stuff starts at line 814 in "frills.js".

The javascript interface is described here.
Thank you. My JS knowledge is 0. However I was able to create a 1:48 .sw file using Awave Audio. When it came to actually getting that file to read in game I have no understanding of any of it. Do I have to attach the sound to an object/billboard or can I have it just play in game constantly?

Re: Adding sound to track (asking)

Posted: Mon Dec 27, 2021 12:51 am
by jlv
If you just want it to play continuously this should do it:

Code: Select all

var g_mysound = mx.add_sound("@mytrack/mysound.sw")
mx.set_sound_freq(g_mysound, 22050);
mx.set_sound_vol(g_mysound, 1.0);
mx.set_sound_pos(g_mysound, 512, 0, 512);
mx.set_sound_loop(g_mysound, 1);
mx.start_sound(g_mysound);

Re: Adding sound to track (asking)

Posted: Mon Dec 27, 2021 1:25 pm
by Jakob Hubbard
mxrewind665 wrote: Sun Dec 26, 2021 10:53 pm Thank you. My JS knowledge is 0. However I was able to create a 1:48 .sw file using Awave Audio. When it came to actually getting that file to read in game I have no understanding of any of it. Do I have to attach the sound to an object/billboard or can I have it just play in game constantly?
If you ever want to learn how to use the javascript interface like triggering a sound from a hook message me on discord and I'd be happy to help.

Jakob Hubbard#3334

Re: Adding sound to track (asking)

Posted: Mon Dec 27, 2021 4:55 pm
by mxrewind665
jlv wrote: Mon Dec 27, 2021 12:51 am If you just want it to play continuously this should do it
That helped me eliminate the fact it was the .sw file. I found one that works but it is too short and loops a car horn too often to not be annoying :lol: is there a limit to the length of time the audio can be? But this did help and I got something so thank you a ton.
Jakob Hubbard wrote: Mon Dec 27, 2021 1:25 pmIf you ever want to learn how to use the javascript interface like triggering a sound from a hook message me on discord and I'd be happy to help.

Jakob Hubbard#3334
I’ll be getting in touch with you for sure. Much appreciated.

Re: Adding sound to track (asking)

Posted: Tue Dec 28, 2021 2:32 am
by jlv
mxrewind665 wrote: Mon Dec 27, 2021 4:55 pm That helped me eliminate the fact it was the .sw file. I found one that works but it is too short and loops a car horn too often to not be annoying :lol: is there a limit to the length of time the audio can be? But this did help and I got something so thank you a ton.
Not sure if there's a DirectSound limit but in any case it's not really meant for super long sounds.

Here's a completely untested script to repeat a sound at an interval.

Code: Select all

var g_mysound = mx.add_sound("@mytrack/mysound.sw")
mx.set_sound_freq(g_mysound, 22050);
mx.set_sound_vol(g_mysound, 1.0);
mx.set_sound_pos(g_mysound, 512, 0, 512);
mx.set_sound_loop(g_mysound, 0);

var g_mysound_last_played = 0;

function play_mysound(seconds) {
	var repeat_time = 1; /* set this to the interval it should repeat at in seconds */

        g_play_mysound_prev(seconds);

	if (Math.floor(seconds / repeat_time) == Math.floor(g_mysound_last_played / repeat_time))
		return;

	mx.start_sound(g_mysound);

	g_mysound_last_played = seconds;
}

var g_play_mysound_prev = mx.frame_handler;
mx.frame_handler = play_mysound;

Re: Adding sound to track (asking)

Posted: Tue Dec 28, 2021 6:35 am
by mxrewind665
jlv wrote: Tue Dec 28, 2021 2:32 am Not sure if there's a DirectSound limit but in any case it's not really meant for super long sounds.

Here's a completely untested script to repeat a sound at an interval.
I got that to work perfect. It does help. I was also able to use the sproing.sw as a test to add multiple sounds at different time intervals and that worked. I’m just trying now to create another sound file that will work like this current one I made. Thank you again for that.

Re: Adding sound to track (asking)

Posted: Thu Dec 30, 2021 2:14 am
by baker
jlv wrote: Tue Dec 28, 2021 2:32 am
mxrewind665 wrote: Mon Dec 27, 2021 4:55 pm That helped me eliminate the fact it was the .sw file. I found one that works but it is too short and loops a car horn too often to not be annoying :lol: is there a limit to the length of time the audio can be? But this did help and I got something so thank you a ton.
Not sure if there's a DirectSound limit but in any case it's not really meant for super long sounds.

Here's a completely untested script to repeat a sound at an interval.

Code: Select all

var g_mysound = mx.add_sound("@mytrack/mysound.sw")
mx.set_sound_freq(g_mysound, 22050);
mx.set_sound_vol(g_mysound, 1.0);
mx.set_sound_pos(g_mysound, 512, 0, 512);
mx.set_sound_loop(g_mysound, 0);

var g_mysound_last_played = 0;

function play_mysound(seconds) {
	var repeat_time = 1; /* set this to the interval it should repeat at in seconds */

        g_play_mysound_prev(seconds);

	if (Math.floor(seconds / repeat_time) == Math.floor(g_mysound_last_played / repeat_time))
		return;

	mx.start_sound(g_mysound);

	g_mysound_last_played = seconds;
}

var g_play_mysound_prev = mx.frame_handler;
mx.frame_handler = play_mysound;
Any suggestions on how to convert to sw or find sw files? I’m not so familiar with that file format. Is that all that is supported, or are mp3s or wav files supported?

Re: Adding sound to track (asking)

Posted: Thu Dec 30, 2021 7:03 am
by Wahlamt
baker wrote: Thu Dec 30, 2021 2:14 am
Any suggestions on how to convert to sw or find sw files? I’m not so familiar with that file format. Is that all that is supported, or are mp3s or wav files supported?
Here you got some info, also some more info in JLV's first comment: viewtopic.php?t=9820

Re: Adding sound to track (asking)

Posted: Thu Dec 30, 2021 11:23 pm
by baker
Wahlamt wrote: Thu Dec 30, 2021 7:03 am
baker wrote: Thu Dec 30, 2021 2:14 am
Any suggestions on how to convert to sw or find sw files? I’m not so familiar with that file format. Is that all that is supported, or are mp3s or wav files supported?
Here you got some info, also some more info in JLV's first comment: viewtopic.php?t=9820
Thank you sir 🙂

Re: Adding sound to track (asking)

Posted: Fri Dec 31, 2021 12:59 am
by jlv
baker wrote: Thu Dec 30, 2021 2:14 am Any suggestions on how to convert to sw or find sw files? I’m not so familiar with that file format. Is that all that is supported, or are mp3s or wav files supported?
It's just raw 16 bit samples. In Audacity, from the menu choose File / Export / Export Audio, then set "Header" to "RAW (headerless)" and set "Encoding" to "Signed 16-bit PCM".