Skip to content

Commit

Permalink
✨ Add a "Fine Updates" option
Browse files Browse the repository at this point in the history
  • Loading branch information
leolabs committed Jan 11, 2025
1 parent 5f5087c commit 0158a20
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import { InstanceBase, InstanceStatus, Regex, SomeCompanionConfigField, runEntrypoint } from '@companion-module/base'
import { InstanceBase, InstanceStatus, SomeCompanionConfigField, runEntrypoint } from '@companion-module/base'
import { ArgumentType, Client, Server } from 'node-osc'
import shortUuid from 'short-uuid'

import { BOOLEAN_SETTINGS, COUNT_IN_DURATIONS, JUMP_MODES, SECTION_PRESET_COUNT, SONG_PRESET_COUNT } from './constants'
import { Action, Feedback } from './enums'
import { presets } from './presets'
import { COLOR_GREEN_500, COLOR_GREEN_800, COLOR_RED_600, COLOR_WHITE, COLORS } from './utils/colors'
import { debounce, debounceGather } from './utils/debounce'
import { debounceGather } from './utils/debounce'
import { makeRange } from './utils/range'
import { variables } from './variables'
import { getProgressIcon } from './icons'
import { debounce, throttle } from 'lodash'

/** The port that AbleSet is listening on */
const SERVER_PORT = 39041

interface Config {
/** The hostname(s) or IP address(es) to connect to, comma-separated */
serverHost: string
/** Whether to request fine */
fineUpdates: boolean
}

class ModuleInstance extends InstanceBase<Config> {
config: Config = { serverHost: '127.0.0.1' }
config: Config = { serverHost: '127.0.0.1', fineUpdates: true }
oscServer: Server | null = null
oscClients: Client[] = []

connectInterval: NodeJS.Timeout | null = null
cancelHeartbeat = () => {}

songs: string[] = []
sections: string[] = []
Expand Down Expand Up @@ -61,6 +65,8 @@ class ModuleInstance extends InstanceBase<Config> {
isConnected = false
}, 2500)

this.cancelHeartbeat = () => handleHeartbeat.cancel()

this.oscServer.once('/global/isPlaying', () => {
isConnected = true
this.log('info', 'Connection established')
Expand All @@ -84,7 +90,7 @@ class ModuleInstance extends InstanceBase<Config> {

const tryConnecting = () => {
this.log('info', 'Trying to connect to AbleSet...')
this.sendOsc(['/subscribe', 'auto', SERVER_PORT, 'Companion'])
this.sendOsc(['/subscribe', 'auto', SERVER_PORT, 'Companion', config.fineUpdates ?? false])
this.sendOsc(['/getValues', SERVER_PORT])
}

Expand Down Expand Up @@ -200,6 +206,13 @@ class ModuleInstance extends InstanceBase<Config> {
Feedback.SectionProgressByNumber,
)
})
server.on(
'/global/finePosition',
throttle(([, beats]) => {
this.setVariableValues({ finePosition: Number(beats) })
this.checkFeedbacks(Feedback.SongProgress, Feedback.SectionProgress, Feedback.SectionProgressByNumber)
}, 40),
)
server.on('/global/humanPosition', ([, bars, beats]) => {
this.setVariableValues({ humanPosition: `${bars ?? 0}.${beats ?? 0}`, humanPositionBeats: Number(beats ?? 0) })
this.checkFeedbacks(Feedback.IsBeat)
Expand Down Expand Up @@ -454,6 +467,7 @@ class ModuleInstance extends InstanceBase<Config> {
clearInterval(this.connectInterval)
}

this.cancelHeartbeat()
await Promise.all(this.oscClients.map((c) => new Promise<void>((res) => c.close(res))))
await new Promise<void>((res) => this.oscServer?.close(res))
this.log('debug', 'module destroyed')
Expand All @@ -479,6 +493,15 @@ class ModuleInstance extends InstanceBase<Config> {
"Please enter the host(s) you'd like to connect to. If AbleSet is running on the same computer as Companion, leave this as 127.0.0.1. You can provide multiple hosts separated by commas for a redundant setup.",
width: 12,
},
{
id: 'fineUpdates',
type: 'checkbox',
label: 'Fine Updates',
default: true,
tooltip:
'Disable this if it causes performance issues. With this enabled, progress bars are updated every 40ms, otherwise they are updated every beat.',
width: 12,
},
]
}

Expand Down Expand Up @@ -1041,8 +1064,8 @@ class ModuleInstance extends InstanceBase<Config> {
callback: ({ options }) => {
const activeSongStart = Number(this.getVariableValue('activeSongStart') ?? 0)
const activeSongEnd = Number(this.getVariableValue('activeSongEnd') ?? 0)
const beatsPosition = Number(this.getVariableValue('beatsPosition') ?? 0)
const totalPercent = (beatsPosition - activeSongStart) / (activeSongEnd - activeSongStart)
const position = Number(this.getVariableValue('finePosition') || this.getVariableValue('beatsPosition') || 0)
const totalPercent = (position - activeSongStart) / (activeSongEnd - activeSongStart)

const buttonCount = Number(options.buttonCount)
const buttonIndex = Number(options.buttonNumber) - 1
Expand Down Expand Up @@ -1166,8 +1189,8 @@ class ModuleInstance extends InstanceBase<Config> {
callback: ({ options }) => {
const activeSongStart = Number(this.getVariableValue('activeSectionStart') ?? 0)
const activeSongEnd = Number(this.getVariableValue('activeSectionEnd') ?? 0)
const beatsPosition = Number(this.getVariableValue('beatsPosition') ?? 0)
const totalPercent = (beatsPosition - activeSongStart) / (activeSongEnd - activeSongStart)
const position = Number(this.getVariableValue('finePosition') || this.getVariableValue('beatsPosition') || 0)
const totalPercent = (position - activeSongStart) / (activeSongEnd - activeSongStart)

const buttonCount = Number(options.buttonCount)
const buttonIndex = Number(options.buttonNumber) - 1
Expand Down
1 change: 1 addition & 0 deletions src/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SECTION_PRESET_COUNT, SONG_PRESET_COUNT } from './constants'

export const variables: CompanionVariableDefinition[] = [
{ variableId: 'beatsPosition', name: 'Playhead Position in Beats' },
{ variableId: 'finePosition', name: 'Fine Playhead Position in Beats, Needs "Fine Updates" Option to be Enabled' },
{ variableId: 'humanPosition', name: 'Playhead Position in Bars.Beats' },
{ variableId: 'humanPositionBeats', name: 'Beats Part of the Playhead Position' },
{ variableId: 'currentMeasure', name: 'Current Measure in Bars.Beats, taken from a measure track if available' },
Expand Down

0 comments on commit 0158a20

Please sign in to comment.