Forum Navigation
Please or Register to create posts and topics.

Lua Sound Programming by Alan Norris

(Original thread started on 06-30-13 by Alan Norris)

I have managed to program one of the Test switch positions to play a sound. This applies to the Stall position but this tutorial applies to any switch that plays a sound. Find a .WAV file that plays "STALL STALL STALL". I haven't done that yet and may record my own (I used a WAV file from the sounds folder as a test). Once you have the .WAV file put it in the sounds folder inside FSX.

 

Next you need to write a Lua script to play the WAV file. It took me a little while to figure this out but you can use my example which looks like this which you can create in Wordpad.

 

function stall(stall warning)

sound.play(stall warning)

end

 

stall("C:\\Program Files (x86)\\Microsoft Games\\Microsoft Flight Simulator X\\sounds\\stallwarning.wav)"

 

Save it as Stall Warning.lua and put it in the modules folder inside FSX.

 

In InterfaceIT find the test switch position for STALL add an entry for FSUIPC for the Down (On) position and use offset 3340, Byte, Setbit 0 and for the Up (Off) position use Clrbit 0.

 

In FSX go to the Buttons + Switches section of FSUIPC and press the switch which will show up as Joystick 64 Button 0

 

Check the Use FS box and in the drop down list for Action taken when the switch is pressed find and select Lua Stall Warning. In the drop down list for Action taken when the button is released find and select LuaKill Stall Warning.

 

You are done. Just press the switch once and release and the sound will play to the end of the WAV file.

 

You can use the same script for the other positions that play a sound -- just put the correct WAV file in the Lua script and use offset 3340 Byte Setbit 1 and Clrbit 1 -- incrementing the bit for each switch.

 

A couple of caveats. Make sure that you use double backslashes (\\) where there is one in the file path string. Also make sure that sounds is in lower case and put the whole file location string in "".

 

If anyone ever finds a "real" Stall, Stall, Stall WAV file, let me know as I had to record one.

 

BTW I have my FIRE DET button programmed with the proper switches illuminating and the Left Engine Fire -- Right Engine Fire aural.

 

I'm going to talk to Eric to see if I can create an XML file that you can use.

 

(Posted by Alan Norris on 07-06-13)

I found a simpler way of writing a Lua script for playing a sound. Just create a .LUA file with this line in it.

 

Sound.play("E:\\Program Files (x86)\\Microsoft Games\\Microsoft Flight Simulator x\\sound\your file name.wav")

 

Obviously changing the drive letter and file name to whatever you're needs are. In my case I used this for the LTS position of the test switch and as the warning chime needed to play three times I just made a Lua file with three lines the same as this example.

 

If you need to play multiple sounds you need to add a pause between each wav file otherwise they will all play at the same time. For instance the GPWS test switch position should play all of the GPWS warnings such as Pull Up, Terrain, etc. To do this you need to add a line for each WAV file. For example:

 

Sound.play("E:\\Program Files (x86)\\Microsoft Games\\Microsoft Flight Simulator x\\sound\pullup.wav")

ipc.sleep(n)

Sound.play("E:\\Program Files (x86)\\Microsoft Games\\Microsoft Flight Simulator x\\sound\terrain.wav")

ipc.sleep(n)

etc.

 

The "n" value for ipc.sleep is the delay time in milliseconds. For the warning aural sounds, I found that 1500 (1.5 seconds) worked okay but you can adjust this to get a break so they don't all get garbled.

 

Please post here if you have any questions.

 

(Posted by Ron Rollo on 09-15-17)

Shane found this easy to follow "Sound Scripting in LUA" document written by Michael Lehkamp. We have it in PDF form but I want to post it here for easy reference for us in the future. Here is the document:

 

Sound Scripting in LUA

Adding Sounds to Your Cockpit by Michael Lehkamp

 

Contents:

I. [Definition]

LUA is a lightweight multi-paradigm programming language designed as a scripting language with extensible semantics as a primary goal. Lua is cross-platform since it is written in ANSI C, and has a relatively simple C API.

 

II. [Purpose / Credits]

For builders who want the ability to assign a button or switch in their cockpits to trigger a sound file(s) to play or loop.

 

Credits: 1. Peter Dowson, Author of FSUIPC. 2. Paul Henty - United Kingdom. 3. Microsoft Corporation.

 

III. [Examples of Use]

1. Play flight attendant sounds files. Ex: Takeoff, landing, taxi, arrival, etc.

2. Tie in with a switch which turns on and off air conditioning (loop) noise.

3. Sound test. Ex: To test your GPWS warning system.

 

IV. [ Level of Programming Difficulty]

Very minimal using supplied code

 

V. [Requirements]

Paid (Licensed) version of Pete Dowson’s FSUIPC.

 

VI. [Suggested Reference Material]

FSUIPC Lua Library.pdf (This file is included with FSUIPC and can be found in a sub directory of your Modules folder.)

 

VII. [General Instructions]

VII.1 Set up

VII.2 Sound files path

VII.3 Sound routines – programming

VII.4 Registering sound files in FSUIPC - Single file

VII.5 Registering sound files in FSUIPC - Multiple files

 

VII.1 [Set up]

1. You begin by selecting sound files you wish to trigger by switches. Note: must be in .WAV format.

2. Place the files in a folder of your choice.

3. Open a blank file in Windows Notepad and name it whatever you want . (dot) lua. Example: mysound.lua

4. Place your LUA file directly into your Flight Simulator/Modules folder. Works both in FS9 & FSX.

 

VII.2 [Sound files path]

1. Open your LUA file. We need to tell LUA where our sound folders are located. Examples given below:

 

Example A.

sound.path(“E:\\Program Files (x86)\\Microsoft Games\\Microsoft Flight Simulator \\sound ")

 

Example B.

sound.path(“C:\\Flightsim Stuff\\Cockpit Sounds”)

 

Special Note: the routine “sound.path”, as well as any other routines, must not contain any capital letters. And you must use “\\” to separate directories.

 

NOTE 1: LUA is convenient in that you do not need to re-boot flight simulator after editing your file. Changes and/or additions can be made to the LUA file with Flight Simulator running.

 

VII.3 [Sound routines - programming]

Now we need to tell LUA what sound we want to play.

 

Examples given below.

Example A. (Single sound file)

sound.path(“C:\\ Flightsim Stuff\\Cockpit Sounds”)

sound.play(“Firebell.wav”)

 

Example B. (Multiple sound files)

sound.path(“C:\\ Flightsim Stuff\\Cockpit Sounds”)

if ipcPARAM == 1 then

sound.play("Firebell.wav")

elseif ipcPARAM == 2 then

sound.play("Chime.wav")

elseif ipcPARAM == 3 then

sound.play("Dingdong.wav")

elseif ipcPARAM == 4 then

sound.play("Stallhorn.wav")

end

 

NOTE 1 : You can copy and paste what is in colored RED into your LUA file and merely change the path to your sounds as well as the names of your files.

 

NOTE 2: If you want the sound to loop, enter the routine “sound.playloop” rather than “sound.play ”.

 

NOTE 3: You can omit (.wav) after each sound entry if you wish. It is already assumed.

 

VII.4 [Registering in FSUIPC in a Single Sound File]

Now it’s time to start FS and register the sounds in FSUIPC.

 

Step 1:

Once you open the FSUIPC GUI, click on “Buttons & Switches” tab.

 

NOTE: The instructions from this point on assume your buttons are visible by FSUIPC such as joystick buttons etc. If they are not, you will need to program your switches using the associated software for whatever I/O hardware card you are using. I am using the EPIC card and have made all my switches visible to FSUIPC.

 

Step 2:

At this point, simply push your button or switch you want to assign a sound to. As you can see FSUIPC sees the switch as Joy 5, Btn 0.

 

Step 3:

Put a check mark in the “Select for FS control” box. Now in the first drop down menu, scroll down to your LUA file and select it. Now in the above example you can see where I selected my LUA Firebell file. In my simulator, momentary switch used only to test the sound. Obviously I do not want this sound to continue after I release the button. Therefore, I needed to go to the second drop down menu and choose: LuaKill Firebell. The LuaKill is automatically added when you placed your LUA file in the Modules folder.

 

NOTE: If the sound is a file in which you don’t want to kill, such as a flight attendant boarding message, you can skip entering anything in the second drop down menu.

 

Step 4:

Test. Push your button and you should here the WAV file play.

 

VII.5 [Registering in FSUIPC Multiple Sound Files]

Follow Steps 1-3 above.

 

Now in the Parameter box, type in the number you assigned in your LUA file you want to associate with this switch.

 

For example, if you want the Chime.wav to play, type in the number “2”. Or if you want to associate the switch with the Stallhorn.wav, enter 4. If you need to kill the sound after the switch is off, make sure you choose the LuaKill command in the second drop down menu. You will also need to add the same parameter number you entered in the first drop down menu.

 

Once you have registered this button or switch, press the next button or switch one and do the same making sure you enter the correct parameter number.  Once you have concluded, hit OK and exit the FSUIPC GUI.

 

Test all switches. That’s it.

--- End of Document ---