DatumHueEnterprise v0.0.24datumhue.com
Contents

Make music

Play music in tracks and change it as it plays, keep sounds on the beat, and give them the voices of early sound chips.

Play music in tracks

audio.music plays its patterns in the order of its sequence. An entry of the sequence is one pattern, or a list of patterns played together as tracks on one clock; the first track sets how long the entry lasts. The Music it returns edits the music while it plays:

  • Music:set_pattern rewrites a pattern from the point the music has reached, without restarting it.
  • Music:set_sequence sets a new order, which goes on after the entry playing now.
  • Music:seek jumps to an entry and a step.
  • Music.tempo plays the written notes faster or slower, at the same pitch.

Music.slot and Music.step say where the music is as you hear it. Music:on_step calls you as each step is heard, so what you draw for a beat lands with its sound. If your app falls behind, the calls it missed fold into one, which carries the latest step:

song.lualua
local audio = datumhue.audio

local function lead(hz)
    return { waveform = "square", frequency = hz, duration = 0.2 }
end
local function bass(hz)
    return { waveform = "triangle", frequency = hz, duration = 0.2 }
end
-- A note with no volume is a rest.
local rest = { waveform = "sine", volume = 0, duration = 0.2 }

local song = audio.music({
    patterns = {
        { lead(523), lead(659), rest, lead(784) },
        { bass(131), rest, bass(196), rest },
    },
    -- One entry: patterns 1 and 2 at once, as two tracks.
    sequence = { { 1, 2 } },
    looping = true,
    volume = 0.4,
})

-- Called as each step is heard.
song:on_step(function(music, slot, step)
    print(("slot %d, step %d"):format(slot, step))
end)

-- A second in, a new tune takes over, a little faster.
datumhue.time.after(1, function()
    song:set_pattern(1, { lead(784), rest, lead(659), rest })
    song.tempo = 1.25
end)

In this example: datumhue.audio datumhue.audio.music Music:on_step datumhue.time.after Music:set_pattern Music.tempo

Start sounds on time

Callbacks run once a frame, so sounds started from a timer can each land up to a frame late. For a steady beat, give each sound a start_time on the clock audio.now reads, a little ahead of now. The sound starts at exactly that time, and sounds given the same time start on the same sample. This metronome looks a quarter of a second ahead and queues each tick due by then:

metronome.lualua
local audio = datumhue.audio

local tick = { waveform = "noise", frequency = 6000, duration = 0.02 }
local beat = 0.5
local next_tick = audio.now() + 0.1

-- Queue the ticks due in the next quarter of a second, each at its
-- exact time: now, and then every tenth of a second.
local function queue()
    while next_tick < audio.now() + 0.25 do
        audio.sfx({ tick }, { start_time = next_tick })
        next_tick = next_tick + beat
    end
end
queue()
datumhue.time.every(0.1, queue)

In this example: datumhue.audio datumhue.audio.now datumhue.audio.sfx datumhue.time.every

audio.now runs a little ahead of what you hear, by audio.latency seconds. Music.step, Audio.position and a sound's end already report what you hear; subtract audio.latency from audio.now to draw in time with a sound you gave a start_time.

Make chip sounds

The waveforms can sound like the sound chips of early game consoles and home computers:

  • duty sets the width of a square wave's pulse, from a thin, nasal 0.125 to a hollow 0.5. Audio:duty_to and a note's duty_slide sweep it while it plays.
  • noise_mode picks the noise: "long" hisses, and "periodic" buzzes at the note's frequency.
  • A Wavetable plays one cycle you write: audio.wavetable takes levels from -1 to 1, and Bytes:wavetable takes bytes from 0 to 255.
  • sync runs the wave that many times faster and restarts it at each cycle of the note's frequency; a sync_slide sweep tears through the harmonics. ring multiplies the wave by a square that many times the frequency, for bells and metallic tones.
  • arpeggio_ticks sets how many 1/60 s ticks each step of an arpeggio lasts, and a vibrato's delay holds it back at the start of a note.
chip.lualua
local audio = datumhue.audio

-- A 16-step wave of 4-bit levels, 0 to 15, scaled to -1..1.
local steps = { 0, 3, 6, 9, 12, 14, 15, 15, 14, 12, 9, 6, 3, 0, 0, 0 }
local levels = {}
for i, level in ipairs(steps) do
    levels[i] = level / 7.5 - 1
end
local hump = audio.wavetable(levels)

audio.sfx({
    -- A thin pulse that widens as it falls.
    {
        waveform = "square",
        frequency = 440,
        slide = 330,
        duty = 0.125,
        duty_slide = 0.5,
        duration = 0.3,
    },
    -- A buzz at the note's own pitch.
    {
        waveform = "noise",
        noise_mode = "periodic",
        frequency = 220,
        duration = 0.2,
    },
    -- A tearing lead: sync swept from 1 to 4 times the frequency.
    {
        waveform = "sawtooth",
        frequency = 165,
        sync = 1,
        sync_slide = 4,
        duration = 0.4,
    },
    -- A bell: the written wave, ring-modulated.
    { waveform = hump, frequency = 330, ring = 2.4, duration = 0.4 },
    -- A chord as a fast arpeggio, a step every two ticks.
    {
        waveform = "square",
        frequency = 262,
        arpeggio = { 0, 4, 7 },
        arpeggio_ticks = 2,
        duration = 0.5,
    },
}, { volume = 0.4 })

In this example: datumhue.audio datumhue.audio.wavetable datumhue.audio.sfx

See also

Reference used on this page: datumhue.audio datumhue.audio.music Music:on_step datumhue.time.after Music:set_pattern Music.tempo datumhue.audio.now datumhue.audio.sfx datumhue.time.every datumhue.audio.wavetable Music:set_sequence Music:seek Music.slot Music.step datumhue.audio.latency Audio.position Audio:duty_to Bytes:wavetable

datumhue.com

(c) 2026 Shuusei AB. Quote excerpts of this manual, with attribution to DatumHue, in your own documentation, tutorials, course materials, reviews, and other writing or video. Republishing it in whole or in substantial part needs written permission: support@datumhue.com.

Victor Mono is licensed under the SIL Open Font License 1.1
Copyright (c) 2024, Rune Bjørnerås (https://github.com/rubjo)

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

Try

    ↑↓ selectEnter openTab filter

    Settings

    Theme
    Text
    Print

    Keyboard shortcuts

    / or Ctrl KSearch
    ↑ ↓ EnterPick a result
    TabNext result filter
    j kNext or previous entry
    [ ]Previous or next page
    yCopy a link to the current entry
    ?These shortcuts
    EscClose

    On this page