uvi-script
Musical event scripting with Lua
Loading...
Searching...
No Matches
Math Conversions

Functions

function beat2ms (beat)
 Convert beat duration to milliseconds based on the current tempo.
 
function ms2beat (ms)
 Convert milliseconds to beat duration based on the current tempo.
 
function ms2samples (milli)
 Convert milliseconds to sample count based on the current sampling rate.
 
function samples2ms (samples)
 Convert sample count to milliseconds based on the current sampling rate.
 

Detailed Description

Utility functions for converting between different time and sample units

Mathematical conversion and utility functions for musical calculations.

Provides conversions between different musical units (notes to frequencies, beats to milliseconds, MIDI values to gain, etc.) and musical timing functions that are tempo-aware.

Common Conversions:

Tempo-Synced Functions:

See also
Musical Context, Time and Threading

This module provides conversion functions for working with different time representations commonly used in audio and MIDI programming:

These conversions are context-aware and automatically use the current tempo and sampling rate from the engine.

Function Documentation

◆ beat2ms()

function beat2ms ( beat )

Convert beat duration to milliseconds based on the current tempo.

Converts a musical time value (in beats) to absolute time (in milliseconds) using the current tempo setting. This is useful for scheduling events in musical time that need to be executed at specific clock times.

Parameters
beatThe duration in beats (quarter notes). Can be fractional. For example: 1.0 = quarter note, 0.5 = eighth note, 2.0 = half note
Returns
The equivalent duration in milliseconds
Example:
-- Convert 1 beat to milliseconds at 120 BPM (= 500ms)
local delay_ms = beat2ms(1.0)
-- Convert a dotted quarter note (1.5 beats)
local dotted_quarter = beat2ms(1.5)
function beat2ms(beat)
Convert beat duration to milliseconds based on the current tempo.
Definition conversions.lua:38
See also
ms2beat, getBeatDuration

◆ ms2beat()

function ms2beat ( ms )

Convert milliseconds to beat duration based on the current tempo.

Converts absolute time (in milliseconds) to musical time (in beats) using the current tempo setting. This is the inverse operation of beat2ms().

Parameters
msThe duration in milliseconds
Returns
The equivalent duration in beats (quarter notes)
Example:
-- Convert 500ms to beats at 120 BPM (= 1.0 beat)
local beats = ms2beat(500)
-- Calculate how many beats a 1-second delay represents
local beats_per_second = ms2beat(1000)
function ms2beat(ms)
Convert milliseconds to beat duration based on the current tempo.
Definition conversions.lua:61
See also
beat2ms, getBeatDuration

◆ ms2samples()

function ms2samples ( milli )

Convert milliseconds to sample count based on the current sampling rate.

Converts a time duration in milliseconds to the number of audio samples at the current sampling rate. Useful for buffer size calculations and sample-accurate timing.

Parameters
milliThe duration in milliseconds
Returns
The number of samples. May be fractional; round as needed for your use case.
Example:
-- Calculate buffer size for 10ms at 48kHz (= 480 samples)
local buffer_size = ms2samples(10)
-- Convert a 50ms delay to samples
local delay_samples = math.floor(ms2samples(50))
function ms2samples(milli)
Convert milliseconds to sample count based on the current sampling rate.
Definition conversions.lua:88
Note
The result may be a fractional value. Use math.floor() or math.ceil() if you need an integer sample count.
See also
samples2ms, getSamplingRate

◆ samples2ms()

function samples2ms ( samples )

Convert sample count to milliseconds based on the current sampling rate.

Converts a number of audio samples to time duration in milliseconds at the current sampling rate. This is the inverse operation of ms2samples().

Parameters
samplesThe number of samples
Returns
The equivalent duration in milliseconds
Example:
-- Convert 480 samples to milliseconds at 48kHz (= 10ms)
local duration_ms = samples2ms(480)
-- Calculate the duration of a 1024-sample buffer
local buffer_duration = samples2ms(1024)
function samples2ms(samples)
Convert sample count to milliseconds based on the current sampling rate.
Definition conversions.lua:111
See also
ms2samples, getSamplingRate