Jump to content

Sound and players


grinseengel
 Share

Recommended Posts

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

1.png

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...