A source for time?

Hello,

I want to insert a timer in my session. I have seen an example code to format time in the FAQ but the example doesn’t explain which source can be used to get the current time.

Is it possible to use Javascript’s Date() function? How? If it’s not possible, is there an alternative?

Thank you.

The source would be a custom module, like the following:

// time reference
var time_zero

function resetTimer() {
    // set time reference to now
    time_zero = Date.now()
    // update interface
    setTimer(0)
}

function setTimer(time){

    // convert time (in seconds) to hh:mm:ss

    var h = Math.floor(time / 3600),
        m = Math.floor((time - h * 3600) / 60),
        s = Math.round((time - m * 60))

    var formattedTime = [h, m, s].map(x=>String(x).padStart(2, 0)).join(':')

    // send it to the interface
    receiveOsc({
        address: '/timer',
        args: [
            {type: 's', value: formattedTime}
        ]
    })

}

module.exports = {

    init: function(){

        // init the timer when the engine is started
        resetTimer()

        setInterval(()=>{

            // call setTimer once per second
            var elapsedSeconds = (Date.now() - time_zero) / 1000
            setTimer(elapsedSeconds)

        }, 1000)

    },

    oscOutFilter: function(data) {

        var {address, args, host, port} = data

        if (address === '/timer/reset') {
            // when the interface sends a message on this osc address
            // intercept it and reset the timer
            return resetTimer()
        }

        return data

    }

}
2 Likes

I have pasted this code into a text file then have chosen the file with the custom-module field of the launcher. However it prevents the launch of the server. The usual window where one can choose the session file never appears.

Are you running the latest version?

Yes, 0.45.2, downloaded today.

My bad. I pasted only part of the code at first. Now the session loads correctly and I have plenty of timer messages!

1 Like

I have set a push button to send a /timer/reset message, but then I have this error message in the console:
ReferenceError: resetTimer is not defined
at Object.oscOutFilter (D:_Son-params\Open Stage Control\timer1.js:71:13)
at OscServer.oscOutFilter (C:\Program Files\open-stage-control-0.45.2-win32-x64\open-stage-control-0.45.2-win32-x64\resources\app\server\src\server\osc\index.js:184:1)
at Object.send (C:\Program Files\open-stage-control-0.45.2-win32-x64\open-stage-control-0.45.2-win32-x64\resources\app\server\src\server\osc\index.js:256:1)
at Object.sendOsc (C:\Program Files\open-stage-control-0.45.2-win32-x64\open-stage-control-0.45.2-win32-x64\resources\app\server\src\server\callbacks.js:320:1)
at Socket.client.on (C:\Program Files\open-stage-control-0.45.2-win32-x64\open-stage-control-0.45.2-win32-x64\resources\app\server\src\server\server.js:61:1)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at Socket.receive (C:\Program Files\open-stage-control-0.45.2-win32-x64\open-stage-control-0.45.2-win32-x64\resources\app\server\src\server\ipc\client.js:90:1)
at WebSocket.socket.onmessage (C:\Program Files\open-stage-control-0.45.2-win32-x64\open-stage-control-0.45.2-win32-x64\resources\app\server\src\server\ipc\client.js:43:1)
at WebSocket.onMessage (C:\Program Files\open-stage-control-0.45.2-win32-x64\open-stage-control-0.45.2-win32-x64\resources\app\server\node_modules\ws\lib\event-target.js:120:1)

Sorry for not giving much informations with that piece of code, I had to leave earlier than I thought. It seems you managed to get it working anyway, cool !

Now on your issue, I guess you merged the snippet into your own custom module, could it be that you are reassigning the resetTimer at some point in your code ?

1 Like

It was another copy/paste error again. So now it’s working flawlessly. Thank you Jean-Emmanuel!

However it’s true that a commented version could help to understand everything.

It was another copy/paste error again

Code blocks now have a copy button !

2 Likes

This works wonderfully however. Is it possible to stop this? I would like to use this as a timer, but I have no need to have it continuously send messages. I only need it to set a timer for specific events. What I would really like to have is a countdown timer (hours:minutes:seconds) instead. Something like:

/ setTimer(0:5:30) to start countdown at 5 minutes 30 seconds
setTimer(h:m:s)

/timer would display (0:5:30, 0:5:29, 0:5:28 and so on)

/ resetTimer() will kill off timer and set time to 00:00:00
resetTimer()

Can something like that be done?