grinseengel Posted January 26, 2019 Share Posted January 26, 2019 Hello, I have a question about music and sound. In my project I have a helicopter to which I have assigned a sound. How can I program the sound louder and quieter depending on the distance of the player? The following code I have now for the fundamental play. sound = Sound:Load("Sound/military_helicopter.wav") source = Source:Create() source:SetSound(sound) sound:Release() source:SetLoopMode(true) source:Play() Are there any 3D sounds in Leadwerk? In this context I would like to know how to determine the distance between two entities. Andreas Quote Link to comment Share on other sites More sharing options...
gamecreator Posted January 26, 2019 Share Posted January 26, 2019 Sounds like you're looking for the SetRange command but check out the other Source commands too. Quote Link to comment Share on other sites More sharing options...
Thirsty Panther Posted January 26, 2019 Share Posted January 26, 2019 Doesn't Leadwerks do this for you? Place your sound pivot on the Helicopter ,SetRange for the distance that the helicopter can be heard from, and a Listener object on your player (FPS script has one already) and Leadwerks will do the rest. Or have I got this all wrong? Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 27, 2019 Share Posted January 27, 2019 @Thirsty Panther Is right, leadwerks can do this for you. Sound file needs to be mono formatted though. 1 Quote Link to comment Share on other sites More sharing options...
grinseengel Posted January 27, 2019 Author Share Posted January 27, 2019 OK thanks for the help. I have the following code now. range = 100 --Load a sound local sound = Sound:Load("Sound/heli2.wav") --Create a source source = Source:Create() source:SetSound(sound) sound:Release() source:SetLoopMode(true) source:Play() source:SetPosition(Vec3(0,0,1)) source:SetRange(range) local listener = Listener:Create() listener:SetPosition(0,0,0) The problem now is that the sound is not heard anymore. It does not matter which range I enter. The sound is converted to mono. I do not understand that now with the listener. Where do I have to integrate and which parameters do I have to set? I ask for help. Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 28, 2019 Share Posted January 28, 2019 If you try out the example from the documentation and swap the audio with your helicopter sound, does it work? https://www.leadwerks.com/learn?page=API-Reference_Object_Source_SetRange range = 1.5 --Create a window window = Window:Create() context = Context:Create(window) --Load a sound local sound = Sound:Load("Sound/heli2.wav") --Create a source source = Source:Create() source:SetSound(sound) sound:Release() source:SetLoopMode(true) source:Play() source:SetPosition(Vec3(0,0,1)) --Create a listener local listener = Listener:Create() listener:SetPosition(0,0,0) while true do --Press up/down keys to adjust the source range if (window:KeyDown(Key.Up))then range = range + Time:GetSpeed()*0.1 end if (window:KeyDown(Key.Down))then range =range - Time:GetSpeed()*0.1 end if (range < 0)then range=0 end source:SetRange(range) if window:Closed() or window:KeyHit(Key.Escape) then return false end Time:Update() context:SetColor(0,0,0) context:Clear() context:SetColor(1,1,1) context:SetBlendMode(Blend.Alpha) context:DrawText("Range: "..range,2,2) context:Sync() end Quote Link to comment Share on other sites More sharing options...
grinseengel Posted January 28, 2019 Author Share Posted January 28, 2019 With the original script, it works with the volume control. Now the question arises to me how I can integrate this for the player. Sorry for my beginner questions. Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 28, 2019 Share Posted January 28, 2019 Are you using the default character controller like @Thirsty Panther mentioned? If you do, the character controller script already creates a listener for you. Which is then probably conflicting with the listener you made. Can you share the entire script you are using right now? Quote Link to comment Share on other sites More sharing options...
grinseengel Posted January 28, 2019 Author Share Posted January 28, 2019 I use the FSP Player script which pretends leadwerk. Which script should I show? The player script or the script with the helisound? Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 28, 2019 Share Posted January 28, 2019 1 hour ago, grinseengel said: I use the FSP Player script which pretends leadwerk I am assuming this means 'I use the default FPS player script that comes with Leadwerks'. This script creates a listenere. So if you have antther script in your scene that also creates a listener (either by attaching the script to an object or by using main.lua) than these 2 listeners are conflicting. Try creating your sound, without creating an additional listener and move your character close to the position of the source. Quote Link to comment Share on other sites More sharing options...
grinseengel Posted January 28, 2019 Author Share Posted January 28, 2019 range = 1000 --Load a sound local sound = Sound:Load("Sound/heli2.wav") --Create a source source = Source:Create() source:SetSound(sound) sound:Release() source:SetLoopMode(true) source:Play() source:SetPosition(Vec3(0,0,1)) source:SetRange(range) I have now added the following scripts and given the heli model. When I start the project then I can hear at the beginning while the program loads the helisound. If the project is completely done then the sound will stop. Even if I go close to the model, nothing changes. This ist the listener from FPS-Script. --Create listener self.listener = Listener:Create(self.camera) Complement: The higher I set range I hear the heliosound. However, it does not become quieter or silenced when I move away from the model. Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 28, 2019 Share Posted January 28, 2019 The source is now fixed at position vec3(0,0,1) which might not be the actual position of your helicopter. Can you upload the sound? I will try out on my pc and send you the results. source:SetPosition(sel.entity:GetPosition(true)) --Retrieve the position of the helicopter Note that if your helicopter is moving, you need to update the position of the source. Quote Link to comment Share on other sites More sharing options...
grinseengel Posted January 28, 2019 Author Share Posted January 28, 2019 ok, this is the sound: heli.wav Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 28, 2019 Share Posted January 28, 2019 Works fine for me. 1000 is a very high range value. I tested it with 50 1 character controller with default fps script 1 pivot with your soundsource script Quote Link to comment Share on other sites More sharing options...
grinseengel Posted January 29, 2019 Author Share Posted January 29, 2019 Now it works for me too. Thanks for the information. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.