Snippet MusicTracks

quraji

zap
Reaction score
144
MusicTracks by quraji

The latest in my slew of extremely useful submissions.Humor aside, you might actually use this one :eek:
Nah, just kidding.

What is it?
This is a struct designed to simulate a 'musictrack' type. Create em' and play em'. More info in the code :)

Why?
Because.

Picture?
Not this time.

Code?
Right-o!
JASS:
library MusicTracks
/* by quraji v1.02

    About:
    This library introduces the musictrack struct. The purpose of the
    musictrack struct is to abstract a 'music' type, since there isnt
    one in Wc3. It also extends the functionality, offering easy
    pausing/resuming/playing of the track.
    
    Usage:
    Just copy this library into your map, then create musictrack structs
    as you would any other. Then use the provided methods to control them.
    One extra functionality is that you may specify a function to run when
    a particular track ends (if it doesnt loop).
    
    Definitions:
    
        Members:
        string path - This is the path of the music/sound file
        player porPlayer - This is the player for which the music will play
        boolean looping - If true, the track will loop
        
        Methods:
        method pause - This will pause the track
        method resume - This will play the track from the paused position
        method stop - This will stop a track (dead in its tracks...har har?)
        method play - This will play the track from the beginning
        
        method operator position= - This will set the track play position
                                    and will play from that position (if it was playing already)
                                    Ex:
                                    local musictrack mt = mt.create("path", Player(0))
                                    set mt.Play()
                                    set mt.Position=5000 // will jump to 5 seconds
        
        method operator position - Returns the play position of the track
        
        method operator onEnd= - This sets the function to run when the track ends (see notes)
        
    Notes:
    Some extra notes on the usages of OnEnd=. This method allows you to run
    a function whenever the track ends (and isnt looping). This will allow you
    to do some useful things. For example, you have an array of musictracks and 
    want the next track in the sequence to play automatically, you can do that.
    It must follow this interface:
        function interface trackendfunc takes nothing returns nothing
    Think of it as an event response to a musictrack ending. Within that function
    you may use EndingMusicTrack or GetEndingMusicTrack() to retrieve the musictrack.
*/

globals
    // this is how often the track positions will be updated
    // you don't want anything more than .1
    // smaller is better here <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite8" alt=":D" title="Big Grin    :D" loading="lazy" data-shortname=":D" />
    private constant real UPDATE_PERIOD = .01
endglobals

function interface trackendfunc takes nothing returns nothing

globals
    musictrack EndingMusicTrack
endglobals
function GetEndingMusicTrack takes nothing returns musictrack
    return EndingMusicTrack
endfunction

globals
    private musictrack array TracksToUpdate
    private integer Update_Top = 0
endglobals

struct musictrack
    
    // track info (user accessible)
    string path
    player forPlayer
    boolean looping = false
    
    // internal track stuff
    private trackendfunc onend
    private integer pos = 0
    private integer duration
    private boolean allowPause = false
    private boolean allowResume = false
    private boolean active = false
    private boolean remove = false
    
    ////////////////////
    // PRIVATE METHODS
    
    private method onDestroy takes nothing returns nothing
        set .remove = true
        if (GetLocalPlayer()==.forPlayer) then
            call StopMusic(false)
        endif
    endmethod
    
    private method add takes nothing returns nothing
        set TracksToUpdate[Update_Top] = this
        set Update_Top = Update_Top+1
    endmethod    
    private method update takes nothing returns boolean
        if (.remove==true) then
            set .remove = false
            return true
        endif
        set .pos = .pos+R2I(UPDATE_PERIOD*1000)
        if (.pos&gt;=.duration) then
            if (.looping==true) then
                call .play()
            else
                set .allowPause = false
                set .active = false
                if (GetLocalPlayer()==.forPlayer) then
                    call StopMusic(false)
                endif
                if (.onend!=0) then
                    set EndingMusicTrack = this
                    call .onend.execute()
                endif
                return true
            endif
        endif
        return false
    endmethod
    private static method updater takes nothing returns nothing
        local musictrack mt
        local integer i = 0
        loop
            exitwhen (i&gt;=Update_Top)
            set mt = TracksToUpdate<i>
            if (mt.update()!=false) then
                set TracksToUpdate<i> = TracksToUpdate[Update_Top-1]
                set Update_Top = Update_Top-1
            else
                set i = i+1
            endif
        endloop
    endmethod
    private static method onInit takes nothing returns nothing
        call TimerStart(CreateTimer(), UPDATE_PERIOD, true, function musictrack.updater)
    endmethod

    /////////////////
    // USER METHODS
        
        // pauses the track
    method pause takes nothing returns nothing
        if (.allowPause==true) then
            set .allowResume = true
            set .allowPause = false
            if (GetLocalPlayer()==.forPlayer) then
                call StopMusic(false)
            endif
            set .remove = true
        endif
    endmethod
        
        // resumes the track after a pause
    method resume takes nothing returns nothing
        if (.allowResume==true) then
            set .allowResume = false
            set .allowPause = true
            if (GetLocalPlayer()==.forPlayer) then
                call PlayMusicEx(.path, .pos, 0)
            endif
            call .add()
        endif
    endmethod
    
        // stop a track
    method stop takes nothing returns nothing
        if (GetLocalPlayer()==.forPlayer) then
            call StopMusic(false)
        endif
        set .allowPause = false
        set .allowResume = false
        set .active = false
        set .remove = true
    endmethod
    
        // plays track from beginning
    method play takes nothing returns nothing
        if (GetLocalPlayer()==.forPlayer) then
            call StopMusic(false)
            call PlayMusicEx(.path, 0, 0)
        endif
        set .allowPause = true
        set .allowResume = false
        set .pos = 0
        if (.active==false) then
            call .add()
            set .active = true
        endif
    endmethod
    
    method operator position= takes integer i returns nothing        
        call .pause()
        if (i&lt;0) then
            set .pos = 0
        else
            set .pos = i
        endif
        call .resume()
    endmethod
    method operator position takes nothing returns integer
        return .pos
    endmethod
    
    method operator onEnd= takes trackendfunc f returns nothing
        set .onend = f
    endmethod
    
    static method create takes string path, player forPlayer returns musictrack
        local musictrack t = musictrack.allocate()
        set t.path = path
        set t.forPlayer = forPlayer
        set t.duration = GetSoundFileDuration(path)
        return t
    endmethod
    
endstruct
    
    ////////////////////////
    // WC3 format functions, if you like them better
    
function SetMusicTrackPath takes musictrack mt, string path returns nothing
    set mt.path = path
endfunction
function SetMusicTrackPlayer takes musictrack mt, player p returns nothing
    set mt.forPlayer = p
endfunction

function IsMusicTrackLooping takes musictrack mt returns boolean
    return mt.looping
endfunction
function LoopMusicTrack takes musictrack mt, boolean flag returns nothing
    set mt.looping = flag
endfunction
function PauseMusicTrack takes musictrack mt returns nothing
    call mt.pause()
endfunction
function ResumeMusicTrack takes musictrack mt returns nothing
    call mt.resume()
endfunction
function StopMusicTrack takes musictrack mt returns nothing
    call mt.stop()
endfunction
function PlayMusicTrack takes musictrack mt returns nothing
    call mt.play()
endfunction

function SetMusicTrackPlayPosition takes musictrack mt, integer p returns nothing
    set mt.position = p
endfunction
function CreateMusicTrack takes string path, player forPlayer returns musictrack
    return musictrack.create(path, forPlayer)
endfunction

endlibrary
</i></i>


Change Log
v1.02
Fixed a bug which caused the onEnd function to not execute.
Fixed an omission which caused GetEndingMusicTrack() to return nothing.

v1.01
Tweaked some very minor things.
Changed method naming convention, to a first letter is lowercase style, as is most common.


Demo map?
Aye, have that too!
 

Attachments

  • MusicTracks v1.01 (w demo).w3m
    14.6 KB · Views: 190

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
If this uses sounds, shouldn't you add an intialize method?
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
Could you add fade/in/out or is this unsupported for music files?
 

quraji

zap
Reaction score
144
The reason I made these is to use them in another code I have I call WC3Player which manages these in playlists, and gives an interface to use both text commands and a multiboard to display/control them.
The multiboard is really neat, I made custom icons to display the buttons. It also display volume, and track info.
Just a neat thing :)

Anyways, expect that soon, I just have to polish it up :thup:

Ah... Then make a DJ system, that mix tracks, play with volume etc... :p
I wish, but the sound API is really limited.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top