Jump to content

getMusicPosition()


Nebula

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.

Link to comment
Share on other sites

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

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

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

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

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

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

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
Link to comment
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?

Link to comment
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?

Link to comment
Share on other sites

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
Link to comment
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)

 

...

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

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

Link to comment
Share on other sites

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

  • 1 month later...
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).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...