Jump to content
Nebula

getMusicPosition()

Recommended Posts

I start playing sound ...

 

local obj = getSoundManager():PlaySound("1",false,1);
        getSoundManager():PlayAsMusic("1",obj,false,1);

How do I get his position in seconds?

 

sound_position = getSoundManager():getMusicPosition():PrepareMusic("1");

I will try this way, but I get an error. Tell me the right way.

Share this post


Link to post
Share on other sites
Posted (edited)

You can use "getSoundManager():getMusicPosition()".

You can also test and play around with "getSoundManager():getPosition()".

 

Both functions "getMusicPosition()" and "getPosition()" return only floats.

To be exact, they both return Time Elapsed in milliseconds (ms).

 

To get seconds, you need to divide the music position by 1000.

local sound_position = getSoundManager():getMusicPosition()
local totalSeconds = sound_position / 1000

 

I created a function that prints the time of the music being played in many various forms.

You can choose which time formats you'd like from this, or you can edit or reference this code.

-- Prints in various forms, the current elapsed time of the music that is currently playing.
function printCurrentMusicTime(isoPlayer)
    local sound_position = getSoundManager():getMusicPosition()
    print(string.format("Current Music Time Elapsed (ms): %.2f", sound_position))
    
    local totalHours = sound_position / 1000 / 60 / 60
    local totalMinutes = sound_position / 1000 / 60
    local totalSeconds = sound_position / 1000
    print(string.format("Current Music Time Elapsed (hours): %.1f", totalHours))
    print(string.format("Current Music Time Elapsed (minutes): %.1f", totalMinutes))
    print(string.format("Current Music Time Elapsed (seconds): %.1f", totalSeconds))
    
    local totalHoursRounded = math.floor(sound_position / 1000 / 60 / 60)
    local totalMinutesRounded = math.floor(sound_position / 1000 / 60)
    local totalSecondsRounded = math.floor(sound_position / 1000)
    print(string.format("Current Music Time Elapsed (hoursRounded): %.1f", totalHoursRounded))
    print(string.format("Current Music Time Elapsed (minutesRounded): %.1f", totalMinutesRounded))
    print(string.format("Current Music Time Elapsed (secondsRounded): %.1f", totalSecondsRounded))
    
    local music_clock = millisecondsToClock(sound_position)
    print(string.format("Music Clock: %s", music_clock))
    
end

-- Returns a clock as a String in "hh:mm:ss" format.
function millisecondsToClock(ms)
    local sec = tonumber(ms)/1000

    if sec <= 0 then
        return "00:00:00";
    else
        local hours = string.format("%02.f", math.floor(sec/3600));
        local minutes = string.format("%02.f", math.floor(sec/60 - (hours*60)));
        local seconds = string.format("%02.f", math.floor(sec - hours*3600 - minutes*60));
        return string.format("%s:%s:%s", hours, minutes, seconds)
    end
end

Events.OnRenderTick.Add(printCurrentMusicTime)

 

When this function "printCurrentMusicTime" is called, it prints the various time formats on console.

image.png.5a012262548ee7ecfa238766126fe5d0.png

 

Here's the actual SoundManager functions. I hope this all helps. Enjoy :)

image.png.d722ab51fb4964a045db06d217fd02cc.png

 

image.png.ffb650767ecb306c606d5c7ec52a285a.png

 

 

 

MusicTimePrinter.lua

Edited by ATPHHe
small edit to lua code

Share this post


Link to post
Share on other sites
Posted (edited)

OK. But this only works with music from the Zomboid itself, but how can I do this for the sound that I launch?
 

local obj = getSoundManager():PlaySound("1",false,1);
        getSoundManager():PlayAsMusic("1",obj,false,1);


And by the way, it doesn’t overlap the music built into the zomboid for me ... :( How can I turn it off while my track is playing?

Edited by Nebula

Share this post


Link to post
Share on other sites
Posted (edited)

Ah, I took a closer look at "PlayAsMusic()" and I guess the function "getSoundManager():PlayAsMusic("1", obj, false, 1)" has not been implemented/finished yet.

image.png.1e0556470fb31995ca2f3bef3b6f5faa.png

 

There's no getSoundPosition for sounds that I could find in the Decompiler yet.

You can stop music with "getSoundManager():StopMusic()"

 

You can try calling "playMusic()", then printing out the time. I used the parameter String "1".

getSoundManager():playMusic("1")

image.png.5dc0f2df6bcecc549b08016db1e621f4.png

 

 

Edited by ATPHHe

Share this post


Link to post
Share on other sites
Posted (edited)

I looked all over the SoundManager class and could not find a way to get the timeline position of Sounds.

The devs may have to add the function or feature for sounds, or the function "PlayAsMusic" would need to be finished.

Edited by ATPHHe

Share this post


Link to post
Share on other sites
Posted (edited)
8 hours ago, ATPHHe said:

I looked all over the SoundManager class and could not find a way to get the timeline position of Sounds.

The devs may have to add the function or feature for sounds, or the function "PlayAsMusic" would need to be finished. This could be a good suggestion to ask the devs for.

I also tried everything .... I did not achieve a result.
by getSoundManager (): StopMusic () - the current vanilla track stops, and the next one starts playing immediately.


In any case, thanks for the help! You have clarified.

Edited by Nebula

Share this post


Link to post
Share on other sites
Posted (edited)

You're welcome. Glad to try and help out. Hopefully this function will be added sometime in an update. :)

Edited by ATPHHe

Share this post


Link to post
Share on other sites
Posted (edited)
1 minute ago, ATPHHe said:

You're welcome. Glad to try and help out.

float getPosition()
Return the current playing position in the sound
Returns:
The current position in seconds.

Hm. I just don’t understand how to adapt this to my sound?
Edited by Nebula

Share this post


Link to post
Share on other sites

Ah, getPosition() doesn't work now that I have tested it.

It's only mean't for "Music" objects and doesn't work with the "SoundManager" object.

Share this post


Link to post
Share on other sites
Posted (edited)

Here you go, you can stop all music that is forced to play by muting like this.

-- Mute Music
getSoundManager():setMusicVolume(0.0);

-- Unmute Music
getSoundManager():setMusicVolume(getCore().getOptionMusicVolume() / 10.0);

 

Edited by ATPHHe

Share this post


Link to post
Share on other sites

It is unlikely that they will add this feature ... I get the impression that they do not give a damn about us, they are only interested in selling the game.

Share this post


Link to post
Share on other sites

Good.
Why I tried to use PlaySound () ...
The fact is that when I use playMusic (), then the music plays in the world in the coordinates of the map where it was launched. When using PlaySound () - this does not happen, and the music follows the character ... How can playMusic () be attached to the player?

Share this post


Link to post
Share on other sites
2 hours ago, Nebula said:

It is unlikely that they will add this feature ... I get the impression that they do not give a damn about us, they are only interested in selling the game.

I see someone answering your post with helpful information, what's here that I need "give a damn" about?

 

Is the problem that music objects don't have a world position, since they don't use a sound emitter on the game map?

Share this post


Link to post
Share on other sites
Posted (edited)

How then to get getPosition () for PlaySound ()?

I need to get ...
Return the current playing position in the sound


I tried to use PlayAsMusic () for this
but nothing came of it.

Edited by Nebula

Share this post


Link to post
Share on other sites

I wouldn't think you can? You'd want to get the emitter that produces the sound and set its position instead of trying to use PlaySound directly?

A quick search through the Lua files shows examples like ...

function ISRevolverWeapon:reloadPerform(char, square)
    local weapon = char:getPrimaryHandItem();
    char:playSound(self.insertRoundSound)

 

...

Share this post


Link to post
Share on other sites

Mono sound tried already. Does not help.
It would be great if we added to getSoundManager () the ability for us to change the sound volume - in an already running (playing) track - through PlaySound (..., ...), as well as get the current playback position and the ability to start the sound from the desired seconds ....
But anyway, thanks for the answer.

Share this post


Link to post
Share on other sites
On 3/31/2020 at 9:36 PM, EasyPickins said:

Try using a mono sound instead of stereo sound.

You could also add "is3D = false," to the sound definition in sounds.txt.

 

Didn't work for me either. Mono makes no difference, music is still played with 3d on and still twitches when using the player's emitter. I tried to duplicate the sound definition with "is3D = false", but now i get an error "couldn't find an FMOD event or .ogg or .wav file". It seems duplicates are simply ignored if they reference the same .ogg

 

Having more access to emitters & FMOD would be nice. Not sure what Nebula is working on, but I'm trying to make devices play audio.

Share this post


Link to post
Share on other sites

The last thing I tried is this:

emitter = IsoWorld.instance:getFreeEmitter()
emitter:setPos( getPlayer():getX(), getPlayer():getY(), getPlayer():getZ() )
emitter:playSound("Test")
emitter:tick()

Can't hear the sound. No idea why.

Share this post


Link to post
Share on other sites
Posted (edited)

Ok, so the above version doesn't work because the sound won't get registered.

 

This version works:

 

local sound = "test" 
local gameSound = GameSounds.getSound(sound)
local gameSoundClip = gameSound:getRandomClip()
local emitter = IsoWorld.instance:getFreeEmitter()
local id = emitter:playClip(gameSoundClip, nil)
emitter:set3D(id, false)
emitter:tick()

 

Edited by ZombiesLoveBrainiacs

Share this post


Link to post
Share on other sites
On 4/3/2020 at 2:58 AM, EasyPickins said:

Setting distanceMax=0 forces the sound to be 2D.


sound TEST
{
	category = Player,
	clip
	{
		file = media/sound/ambientOutsideRainWindThunder.ogg,
		distanceMax = 0,
	}
}

 

 

But then this music sounds all over the map.

The question of how to eliminate sound twitches when the character moves remains open (distanceMax must be maintained).

Share this post


Link to post
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.

Loading...

×
×
  • Create New...