--!strict --!native -- codecs/audio/Hively/Player: the AHX / HivelyTracker replayer and integer mixer, as hvl2wav's replay.c 1.9 computes them. -- Ported from HivelyTracker (BSD-3-Clause, (c) 2006-2018 Pete Gordon; licenses/hivelytracker-LICENSE.txt). Where the C -- code reads outside its arrays on bad data this port clamps instead. local Tables = require("@rosetta/codecs/audio/Hively/Tables") local Load = require("@rosetta/codecs/audio/Hively/Load") local Player = {} local readu8, readi8, writei8, writef32 = buffer.readu8, buffer.readi8, buffer.writei8, buffer.writef32 local band, bor, lshift, rshift = bit32.band, bit32.bor, bit32.lshift, bit32.rshift local FILTER_SET = Tables.FILTER_SET local WO_TRIANGLE_04, WO_SAWTOOTH_04 = Tables.WO_TRIANGLE_04, Tables.WO_SAWTOOTH_04 local WO_SQUARES, WO_WHITENOISE = Tables.WO_SQUARES, Tables.WO_WHITENOISE local OFFSETS, VIB, PERIOD = Tables.OFFSETS, Tables.VIB, Tables.PERIOD local WAVEFORM_AT = { [0] = WO_TRIANGLE_04, WO_SAWTOOTH_04, 0, WO_WHITENOISE } local WRAP = 0x280 * 65536 local TWO32 = 4294967296 local f32scratch = buffer.create(4) local function s16(v: number): number return (v + 32768) % 65536 - 32768 end local function tdiv(a: number, b: number): number -- C integer division (truncates) local q = a / b return if q >= 0 then math.floor(q) else -math.floor(-q) end local function period(i: number): number return PERIOD[i] or 0 end -- (3546897.f * 65536.f) / period as a float, then / mix rate as a double, truncated local function deltaOf(p: number, freq: number): number if p == 0 then return 1 end writef32(f32scratch, 0, 232449441792 / p) -- 3546897 * 65536 = 232449441792 (exact in a float) local d = math.floor(buffer.readf32(f32scratch, 0) / freq) if d < 0 then d = 0 end if d > WRAP then d -= WRAP end if d == 0 then d = 1 end return d end export type Voice = { [string]: any } export type State = { tune: Load.Tune, freq: number, version: number, channels: number, voices: { Voice }, tempo: number, posNr: number, posJump: number, posJumpNote: number, noteNr: number, stepWaitFrames: number, getNewPosition: boolean, patternBreak: boolean, songEndReached: boolean, playingTime: number, mixgain: number, waves: buffer, panL: { number }, panR: { number }, } local function newVoice(i: number): Voice return { track = 0, nextTrack = 0, transpose = 0, nextTranspose = 0, overrideTranspose = 1000, adsrVolume = 0, aFrames = 0, aVolume = 0, dFrames = 0, dVolume = 0, sFrames = 0, rFrames = 0, rVolume = 0, instrument = false :: any, samplePos = 0, delta = 1, instrPeriod = 0, trackPeriod = 0, vibratoPeriod = 0, waveLength = 0, noteMaxVolume = 0, perfSubVolume = 0, newWaveform = false, waveform = 0, plantPeriod = false, voiceVolume = 0, plantSquare = false, ignoreSquare = 0, fixedNote = 0, volumeSlideUp = 0, volumeSlideDown = 0, hardCut = 0, hardCutRelease = 0, hardCutReleaseF = 0, periodSlideOn = false, periodSlideSpeed = 0, periodSlidePeriod = 0, periodSlideLimit = 0, periodSlideWithLimit = false, periodPerfSlideSpeed = 0, periodPerfSlidePeriod = 0, periodPerfSlideOn = false, vibratoDelay = 0, vibratoSpeed = 0, vibratoCurrent = 0, vibratoDepth = 0, squareOn = 0, squareInit = 0, squareWait = 0, squareLowerLimit = 0, squareUpperLimit = 0, squarePos = 0, squareSign = 0, squareSlidingIn = false, squareReverse = false, filterOn = 0, filterInit = 0, filterWait = 0, filterSpeed = 0, filterUpperLimit = 0, filterLowerLimit = 0, filterPos = 0, filterSign = 0, filterSlidingIn = false, ignoreFilter = 0, perfCurrent = 0, perfSpeed = 0, perfWait = 0, perfList = false :: any, audioSrc = false :: any, audioOff = 0, noteDelayOn = false, noteCutOn = false, noteDelayWait = 0, noteCutWait = 0, audioPeriod = 0, audioVolume = 0, wnHi = 0, wnLo = 0x280, squareBuf = buffer.create(0x80), voiceBuf = buffer.create(0x284), voiceNum = i, trackMasterVolume = 0x40, pan = 0, setPan = 0, panMultLeft = 0, panMultRight = 0, ringSamplePos = 0, ringDelta = 0, ringMix = false, ringPlantPeriod = false, ringBasePeriod = 0, ringAudioPeriod = 0, ringAudioOff = -1, ringNewWaveform = false, ringWaveform = 0, ringFixedPeriod = 0, ringBuf = buffer.create(0x284), } end function Player.new(tune: Load.Tune, freq: number, stereo: number, subsong: number): State? if subsong < 0 or subsong > #tune.subsongs then return nil end local t = Tables.get() local defstereo = if tune.format == "ahx" then math.clamp(math.floor(stereo), 0, 4) else tune.stereo local mixgain = tune.mixgain if tune.format == "ahx" then local defgain = { [0] = 71, 72, 76, 85, 100 } mixgain = (defgain[defstereo] * 256) // 100 end local st: State = { tune = tune, freq = freq, version = if tune.format == "hvl" then tune.version else 0, channels = tune.channels, voices = {}, tempo = 6, posNr = if subsong > 0 then tune.subsongs[subsong] else 0, posJump = 0, posJumpNote = 0, noteNr = 0, stepWaitFrames = 0, getNewPosition = true, patternBreak = false, songEndReached = false, playingTime = 0, mixgain = mixgain, waves = t.waves, panL = t.panL, panR = t.panR, } local left, right = Tables.STEREO_LEFT[defstereo], Tables.STEREO_RIGHT[defstereo] for i = 0, 15 do local v = newVoice(i) local p = if i % 4 == 0 or i % 4 == 3 then left else right v.pan, v.setPan, v.panMultLeft, v.panMultRight = p, p, t.panL[p], t.panR[p] st.voices[i + 1] = v end return st end local function setPan(st: State, v: Voice, p: number) v.pan = p v.panMultLeft, v.panMultRight = st.panL[p], st.panR[p] end local function stepfx1(st: State, v: Voice, fx: number, param: number) if fx == 0x0 then local lo = band(param, 0x0f) if lo > 0 and lo <= 9 then st.posJump = lo end elseif fx == 0x5 or fx == 0xa then v.volumeSlideDown = band(param, 0x0f) v.volumeSlideUp = rshift(param, 4) elseif fx == 0x7 then if param > 127 then param -= 256 end setPan(st, v, param + 128) v.setPan = param + 128 elseif fx == 0xb then st.posJump = (st.posJump * 100 + band(param, 0x0f) + rshift(param, 4) * 10) % 65536 st.patternBreak = true if st.posJump <= st.posNr then st.songEndReached = true end elseif fx == 0xd then st.posJump = st.posNr + 1 st.posJumpNote = band(param, 0x0f) + rshift(param, 4) * 10 st.patternBreak = true if st.posJumpNote > st.tune.trackLength then st.posJumpNote = 0 end elseif fx == 0xe then if rshift(param, 4) == 0xc and band(param, 0x0f) < st.tempo then v.noteCutWait = band(param, 0x0f) if v.noteCutWait ~= 0 then v.noteCutOn = true v.hardCutRelease = 0 end end elseif fx == 0xf then st.tempo = param if param == 0 then st.songEndReached = true end end end local function stepfx2(v: Voice, fx: number, param: number, note: number): number if fx == 0x9 then v.squarePos = rshift(param, 5 - v.waveLength) v.ignoreSquare = 1 elseif fx == 0x3 or fx == 0x5 then if fx == 0x3 and param ~= 0 then v.periodSlideSpeed = param end if note ~= 0 then local new = period(note) local diff = period(v.trackPeriod) - new new = diff + v.periodSlidePeriod if new ~= 0 then v.periodSlideLimit = s16(-diff) end end v.periodSlideOn = true v.periodSlideWithLimit = true note = 0 end return note end local function stepfx3(st: State, v: Voice, fx: number, param: number) if fx == 0x01 then v.periodSlideSpeed = -param v.periodSlideOn = true v.periodSlideWithLimit = false elseif fx == 0x02 then v.periodSlideSpeed = param v.periodSlideOn = true v.periodSlideWithLimit = false elseif fx == 0x04 then if param == 0 or param == 0x40 then return end if param < 0x40 then v.ignoreFilter = param return end if param > 0x7f then return end v.filterPos = param - 0x40 elseif fx == 0x0c then if param <= 0x40 then v.noteMaxVolume = param return end param -= 0x50 if param < 0 then return end if param <= 0x40 then for i = 1, st.channels do st.voices[i].trackMasterVolume = param end return end param -= 0xa0 - 0x50 if param < 0 then return end if param <= 0x40 then v.trackMasterVolume = param end elseif fx == 0xe then local sub, lo = rshift(param, 4), band(param, 0x0f) if sub == 0x1 then v.periodSlidePeriod = s16(v.periodSlidePeriod - lo) v.plantPeriod = true elseif sub == 0x2 then v.periodSlidePeriod = s16(v.periodSlidePeriod + lo) v.plantPeriod = true elseif sub == 0x4 then v.vibratoDepth = lo elseif sub == 0xa then v.noteMaxVolume = math.min(v.noteMaxVolume + lo, 0x40) elseif sub == 0xb then v.noteMaxVolume = math.max(v.noteMaxVolume - lo, 0) elseif sub == 0xf then if st.version >= 1 and lo == 1 then v.overrideTranspose = v.transpose end end end end local function processStep(st: State, v: Voice) v.volumeSlideUp, v.volumeSlideDown = 0, 0 local tune = st.tune local track = readu8(tune.positions, (st.posNr * 16 + v.voiceNum) * 2) local so = (track * 64 + st.noteNr) * 6 local tracks = tune.tracks local note, instr = readu8(tracks, so), readu8(tracks, so + 1) local fx, fxp, fxb, fxbp = band(readu8(tracks, so + 2), 0xf), readu8(tracks, so + 3), band(readu8(tracks, so + 4), 0xf), readu8(tracks, so + 5) local doneNoteDel = false if fx == 0xe and band(fxp, 0xf0) == 0xd0 then if v.noteDelayOn then v.noteDelayOn = false doneNoteDel = true elseif band(fxp, 0x0f) < st.tempo then v.noteDelayWait = band(fxp, 0x0f) if v.noteDelayWait ~= 0 then v.noteDelayOn = true return end end end if not doneNoteDel and fxb == 0xe and band(fxbp, 0xf0) == 0xd0 then if v.noteDelayOn then v.noteDelayOn = false elseif band(fxbp, 0x0f) < st.tempo then v.noteDelayWait = band(fxbp, 0x0f) if v.noteDelayWait ~= 0 then v.noteDelayOn = true return end end end if note ~= 0 then v.overrideTranspose = 1000 end stepfx1(st, v, fx, fxp) stepfx1(st, v, fxb, fxbp) if instr ~= 0 and instr <= tune.instrumentNr then local ins = tune.instruments[instr] setPan(st, v, v.setPan) v.periodSlideSpeed, v.periodSlidePeriod, v.periodSlideLimit = 0, 0, 0 v.perfSubVolume = 0x40 v.adsrVolume = 0 v.instrument = ins v.samplePos = 0 local env = ins.envelope v.aFrames = env.aFrames v.aVolume = s16(if v.aFrames ~= 0 then tdiv(env.aVolume * 256, v.aFrames) else env.aVolume * 256) v.dFrames = env.dFrames v.dVolume = s16(if v.dFrames ~= 0 then tdiv((env.dVolume - env.aVolume) * 256, v.dFrames) else env.dVolume * 256) v.sFrames = env.sFrames v.rFrames = env.rFrames v.rVolume = s16(if v.rFrames ~= 0 then tdiv((env.rVolume - env.dVolume) * 256, v.rFrames) else env.rVolume * 256) v.waveLength = math.min(ins.waveLength, 5) v.noteMaxVolume = ins.volume v.vibratoCurrent = 0 v.vibratoDelay = ins.vibratoDelay v.vibratoDepth = ins.vibratoDepth v.vibratoSpeed = ins.vibratoSpeed v.vibratoPeriod = 0 v.hardCutRelease = ins.hardCutRelease v.hardCut = ins.hardCutReleaseFrames v.ignoreSquare, v.squareSlidingIn = 0, false v.squareWait, v.squareOn = 0, 0 local lower = rshift(ins.squareLower, 5 - v.waveLength) local upper = rshift(ins.squareUpper, 5 - v.waveLength) if upper < lower then lower, upper = upper, lower end v.squareUpperLimit, v.squareLowerLimit = upper, lower v.ignoreFilter, v.filterWait, v.filterOn = 0, 0, 0 v.filterSlidingIn = false local d6, d3, d4 = ins.filterSpeed, ins.filterLower, ins.filterUpper v.filterSpeed = d6 if d3 > d4 then d3, d4 = d4, d3 end v.filterUpperLimit, v.filterLowerLimit = d4, d3 v.filterPos = 32 v.perfWait, v.perfCurrent = 0, 0 v.perfSpeed = ins.plist.speed v.perfList = ins.plist v.ringMix = false v.ringSamplePos = 0 v.ringPlantPeriod = false v.ringNewWaveform = false end v.periodSlideOn = false note = stepfx2(v, fx, fxp, note) note = stepfx2(v, fxb, fxbp, note) if note ~= 0 then v.trackPeriod = note v.plantPeriod = true end stepfx3(st, v, fx, fxp) stepfx3(st, v, fxb, fxbp) end local function plistCommand(st: State, v: Voice, fx: number, param: number) if fx == 0 then if param > 0 and param < 0x40 then if v.ignoreFilter ~= 0 then v.filterPos = v.ignoreFilter v.ignoreFilter = 0 else v.filterPos = param end v.newWaveform = true end elseif fx == 1 then v.periodPerfSlideSpeed = param v.periodPerfSlideOn = true elseif fx == 2 then v.periodPerfSlideSpeed = -param v.periodPerfSlideOn = true elseif fx == 3 then if v.ignoreSquare == 0 then v.squarePos = rshift(param, 5 - v.waveLength) else v.ignoreSquare = 0 end elseif fx == 4 then if param == 0 then v.squareOn = 1 - v.squareOn v.squareInit = v.squareOn v.squareSign = 1 else if band(param, 0x0f) ~= 0 then v.squareOn = 1 - v.squareOn v.squareInit = v.squareOn v.squareSign = if band(param, 0x0f) == 0x0f then -1 else 1 end if band(param, 0xf0) ~= 0 then v.filterOn = 1 - v.filterOn v.filterInit = v.filterOn v.filterSign = if band(param, 0xf0) == 0xf0 then -1 else 1 end end elseif fx == 5 then v.perfCurrent = param elseif fx == 7 or fx == 8 then if param >= 1 and param <= 0x3c then v.ringBasePeriod = param v.ringFixedPeriod = 1 elseif param >= 0x81 and param <= 0xbc then v.ringBasePeriod = param - 0x80 v.ringFixedPeriod = 0 else v.ringBasePeriod = 0 v.ringFixedPeriod = 0 v.ringNewWaveform = false v.ringAudioOff = -1 v.ringMix = false return end v.ringWaveform = fx - 7 v.ringNewWaveform = true v.ringPlantPeriod = true elseif fx == 9 then if param > 127 then param -= 256 end setPan(st, v, param + 128) elseif fx == 12 then if param <= 0x40 then v.noteMaxVolume = param return end param -= 0x50 if param < 0 then return end if param <= 0x40 then v.perfSubVolume = param return end param -= 0xa0 - 0x50 if param < 0 then return end if param <= 0x40 then v.trackMasterVolume = param end elseif fx == 15 then v.perfSpeed = param v.perfWait = param end end -- the noise offset generator (vc_WNRandom, a 64-bit `long` in hvl2wav): W += 2239384; -- W = ((((W >> 8) | (W << 24)) + 782323) ^ 75) - 6735 local function wnStep(v: Voice) local hi, lo = v.wnHi, v.wnLo + 2239384 if lo >= TWO32 then lo -= TWO32 hi = (hi + 1) % TWO32 end local shi = rshift(hi, 8) if hi >= 0x80000000 then shi = bor(shi, 0xff000000) end local nlo = bor(rshift(lo, 8), lshift(band(hi, 0xff), 24), lshift(lo, 24)) local nhi = bor(shi, lshift(hi, 24), rshift(lo, 8)) nlo += 782323 if nlo >= TWO32 then nlo -= TWO32 nhi = (nhi + 1) % TWO32 end nlo = bit32.bxor(nlo, 75) nlo -= 6735 if nlo < 0 then nlo += TWO32 nhi = (nhi + TWO32 - 1) % TWO32 end v.wnHi, v.wnLo = nhi, nlo end local function processFrame(st: State, v: Voice) local tune = st.tune if v.noteDelayOn then if v.noteDelayWait <= 0 then processStep(st, v) else v.noteDelayWait -= 1 end end if v.hardCut ~= 0 then local row = st.noteNr + 1 local nextinst if row < tune.trackLength then nextinst = readu8(tune.tracks, ((v.track * 64) + row) * 6 + 1) else nextinst = readu8(tune.tracks, (v.nextTrack * 64) * 6 + 1) end if nextinst ~= 0 then local d1 = math.max(st.tempo - v.hardCut, 0) if not v.noteCutOn then v.noteCutOn = true v.noteCutWait = d1 v.hardCutReleaseF = -(d1 - st.tempo) else v.hardCut = 0 end end end if v.noteCutOn then if v.noteCutWait <= 0 then v.noteCutOn = false if v.hardCutRelease ~= 0 then local ins = v.instrument local f = v.hardCutReleaseF v.rVolume = if f ~= 0 then s16(tdiv(-(v.adsrVolume - ins.envelope.rVolume * 256), f)) else 0 v.rFrames = f v.aFrames, v.dFrames, v.sFrames = 0, 0, 0 else v.noteMaxVolume = 0 end else v.noteCutWait -= 1 end end if v.aFrames ~= 0 then v.adsrVolume += v.aVolume v.aFrames -= 1 if v.aFrames <= 0 then v.adsrVolume = v.instrument.envelope.aVolume * 256 end elseif v.dFrames ~= 0 then v.adsrVolume += v.dVolume v.dFrames -= 1 if v.dFrames <= 0 then v.adsrVolume = v.instrument.envelope.dVolume * 256 end elseif v.sFrames ~= 0 then v.sFrames -= 1 elseif v.rFrames ~= 0 then v.adsrVolume += v.rVolume v.rFrames -= 1 if v.rFrames <= 0 then v.adsrVolume = v.instrument.envelope.rVolume * 256 end end v.noteMaxVolume = math.clamp(v.noteMaxVolume + v.volumeSlideUp - v.volumeSlideDown, 0, 0x40) if v.periodSlideOn then if v.periodSlideWithLimit then local d0 = v.periodSlidePeriod - v.periodSlideLimit local d2 = v.periodSlideSpeed if d0 > 0 then d2 = -d2 end if d0 ~= 0 then local sum = d0 + d2 -- (d0 + d2) ^ d0 >= 0: the same sign (or zero) if (sum >= 0) == (d0 >= 0) then d0 = v.periodSlidePeriod + d2 else d0 = v.periodSlideLimit end v.periodSlidePeriod = s16(d0) v.plantPeriod = true end else v.periodSlidePeriod = s16(v.periodSlidePeriod + v.periodSlideSpeed) v.plantPeriod = true end end if v.vibratoDepth ~= 0 then if v.vibratoDelay <= 0 then v.vibratoPeriod = (VIB[v.vibratoCurrent] * v.vibratoDepth) // 128 v.plantPeriod = true v.vibratoCurrent = band(v.vibratoCurrent + v.vibratoSpeed, 0x3f) else v.vibratoDelay -= 1 end end local pl = v.perfList if pl then if v.instrument and v.perfCurrent < v.instrument.plist.length then local overflow = v.perfWait == 128 v.perfWait = s16(v.perfWait - 1) local w8 = (v.perfWait + 128) % 256 - 128 if overflow or w8 <= 0 then local cur = v.perfCurrent v.perfCurrent += 1 v.perfWait = v.perfSpeed local e = pl.entries[cur + 1] if e then if e.waveform ~= 0 then v.waveform = e.waveform - 1 v.newWaveform = true v.periodPerfSlideSpeed, v.periodPerfSlidePeriod = 0, 0 end v.periodPerfSlideOn = false plistCommand(st, v, e.fx[1], e.param[1]) plistCommand(st, v, e.fx[2], e.param[2]) if e.note ~= 0 then v.instrPeriod = e.note v.plantPeriod = true v.fixedNote = e.fixed end end end else if v.perfWait ~= 0 then v.perfWait -= 1 else v.periodPerfSlideSpeed = 0 end end end if v.periodPerfSlideOn then v.periodPerfSlidePeriod = s16(v.periodPerfSlidePeriod - v.periodPerfSlideSpeed) if v.periodPerfSlidePeriod ~= 0 then v.plantPeriod = true end end if v.waveform == 2 and v.squareOn ~= 0 then v.squareWait -= 1 if v.squareWait <= 0 then local d1, d2, d3 = v.squareLowerLimit, v.squareUpperLimit, v.squarePos if v.squareInit ~= 0 then v.squareInit = 0 if d3 <= d1 then v.squareSlidingIn = true v.squareSign = 1 elseif d3 >= d2 then v.squareSlidingIn = true v.squareSign = -1 end end if d1 == d3 or d2 == d3 then if v.squareSlidingIn then v.squareSlidingIn = false else v.squareSign = -v.squareSign end end d3 += v.squareSign v.squarePos = s16(d3) v.plantSquare = true local ins: any = v.instrument v.squareWait = if ins then ins.squareSpeed else 0 end end if v.filterOn ~= 0 then v.filterWait -= 1 if v.filterWait <= 0 then local d1, d2, d3 = v.filterLowerLimit, v.filterUpperLimit, v.filterPos if v.filterInit ~= 0 then v.filterInit = 0 if d3 <= d1 then v.filterSlidingIn = true v.filterSign = 1 elseif d3 >= d2 then v.filterSlidingIn = true v.filterSign = -1 end end local fmax = if v.filterSpeed < 4 then 5 - v.filterSpeed else 1 for _ = 1, fmax do if d1 == d3 or d2 == d3 then if v.filterSlidingIn then v.filterSlidingIn = false else v.filterSign = -v.filterSign end end d3 += v.filterSign end v.filterPos = math.clamp(d3, 1, 63) v.newWaveform = true v.filterWait = math.max(v.filterSpeed - 3, 1) end end local waves = st.waves local fpos = math.clamp(v.filterPos, 1, 63) if v.waveform == 2 or v.plantSquare then -- CalcSquare local sp = WO_SQUARES + (fpos - 0x20) * FILTER_SET local x = v.squarePos * 2 ^ (5 - v.waveLength) if x > 0x20 then x = 0x40 - x v.squareReverse = true end if x > 0 then sp += (x - 1) * 128 end local delta = 32 // 2 ^ v.waveLength local sb = v.squareBuf for i = 0, 4 * 2 ^ v.waveLength - 1 do writei8(sb, i, readi8(waves, sp)) sp += delta end v.newWaveform = true v.waveform = 2 v.plantSquare = false end if v.waveform == 3 then v.newWaveform = true end if v.ringNewWaveform then if v.ringWaveform > 1 then v.ringWaveform = 1 end v.ringAudioOff = WAVEFORM_AT[v.ringWaveform] + OFFSETS[v.waveLength] end if v.newWaveform and v.waveform <= 3 then if v.waveform == 2 then v.audioSrc, v.audioOff = v.squareBuf, 0 else local off = WAVEFORM_AT[v.waveform] + (fpos - 0x20) * FILTER_SET if v.waveform < 2 then off += OFFSETS[v.waveLength] else off += band(band(v.wnLo, 2 * 0x280 - 1), 0xfffffffe) wnStep(v) end v.audioSrc, v.audioOff = waves, off end end if v.ringAudioOff >= 0 then local rp = v.ringBasePeriod if v.ringFixedPeriod == 0 then if v.overrideTranspose ~= 1000 then rp += v.overrideTranspose + v.trackPeriod - 1 else rp += v.transpose + v.trackPeriod - 1 end end rp = period(math.clamp(s16(rp), 0, 60)) if v.ringFixedPeriod == 0 then rp += v.periodSlidePeriod end rp = s16(rp + v.periodPerfSlidePeriod + v.vibratoPeriod) v.ringAudioPeriod = math.clamp(rp, 0x71, 0xd60) end local ap = v.instrPeriod if v.fixedNote == 0 then if v.overrideTranspose ~= 1000 then ap += v.overrideTranspose + v.trackPeriod - 1 else ap += v.transpose + v.trackPeriod - 1 end end ap = period(math.clamp(s16(ap), 0, 60)) if v.fixedNote == 0 then ap += v.periodSlidePeriod end ap = s16(ap + v.periodPerfSlidePeriod + v.vibratoPeriod) v.audioPeriod = math.clamp(ap, 0x71, 0xd60) local vol = (v.adsrVolume // 256 * v.noteMaxVolume) // 64 vol = (vol * v.perfSubVolume) // 64 vol = (vol * v.trackMasterVolume) // 64 v.audioVolume = s16(vol) end local function fillCycles(dst: buffer, src: buffer, off: number, len: number) if off < 0 or off + len > buffer.len(src) then buffer.fill(dst, 0, 0, 0x281) return end buffer.copy(dst, 0, src, off, len) local filled = len while filled < 0x280 do local k = math.min(filled, 0x280 - filled) buffer.copy(dst, filled, dst, 0, k) filled += k end writei8(dst, 0x280, readi8(dst, 0)) end local function setAudio(st: State, v: Voice) v.voiceVolume = v.audioVolume % 256 if v.plantPeriod then v.plantPeriod = false v.delta = deltaOf(v.audioPeriod, st.freq) end local src: buffer? = v.audioSrc or nil if v.newWaveform and src then fillCycles(v.voiceBuf, src, v.audioOff, if v.waveform == 3 then 0x280 else 4 * 2 ^ v.waveLength) end if v.ringPlantPeriod then v.ringPlantPeriod = false v.ringDelta = deltaOf(v.ringAudioPeriod, st.freq) end if v.ringNewWaveform and v.ringAudioOff >= 0 then fillCycles(v.ringBuf, st.waves, v.ringAudioOff, 4 * 2 ^ v.waveLength) v.ringMix = true end end -- hvl_play_irq (audio = false skips the audio side) function Player.irq(st: State, audio: boolean) local tune = st.tune local nch = st.channels local voices = st.voices if st.stepWaitFrames == 0 then if st.getNewPosition then local nextpos = if st.posNr + 1 == tune.positionNr then 0 else st.posNr + 1 local P = tune.positions for i = 0, nch - 1 do local v = voices[i + 1] v.track = readu8(P, (st.posNr * 16 + i) * 2) v.transpose = readi8(P, (st.posNr * 16 + i) * 2 + 1) v.nextTrack = readu8(P, (nextpos * 16 + i) * 2) v.nextTranspose = readi8(P, (nextpos * 16 + i) * 2 + 1) end st.getNewPosition = false end for i = 1, nch do processStep(st, voices[i]) end st.stepWaitFrames = st.tempo end for i = 1, nch do processFrame(st, voices[i]) end st.playingTime += 1 st.stepWaitFrames = s16(st.stepWaitFrames - 1) if st.stepWaitFrames == 0 then if not st.patternBreak then st.noteNr += 1 if st.noteNr >= tune.trackLength then st.posJump = st.posNr + 1 st.posJumpNote = 0 st.patternBreak = true end end if st.patternBreak then st.patternBreak = false st.posNr = st.posJump st.noteNr = st.posJumpNote if st.posNr >= tune.positionNr then -- (past the end is bad data: the C code reads outside its position list) st.songEndReached = true st.posNr = tune.restart end st.posJumpNote = 0 st.posJump = 0 st.getNewPosition = true end end if audio then for i = 1, nch do setAudio(st, voices[i]) end end end -- hvl_mixchunk: integer mixing, s16 clipping, / 32768 function Player.mix(st: State, out: buffer, at: number, n: number) local nch = st.channels local voices = st.voices local src, rsrc, delta, rdelta, vol, pos, rpos, panl, panr = {}, {}, {}, {}, {}, {}, {}, {}, {} for i = 1, nch do local v = voices[i] delta[i] = math.max(v.delta, 1) vol[i] = v.voiceVolume pos[i] = v.samplePos src[i] = v.voiceBuf panl[i] = v.panMultLeft panr[i] = v.panMultRight rdelta[i] = math.max(v.ringDelta, 1) rpos[i] = v.ringSamplePos rsrc[i] = if v.ringMix then v.ringBuf else false end local gain = st.mixgain local o = at * 8 local samples = n while samples > 0 do local loops = samples for i = 1, nch do if pos[i] >= WRAP then pos[i] -= WRAP end local cnt = (WRAP - pos[i] - 1) // delta[i] + 1 if cnt < loops then loops = cnt end if rsrc[i] then if rpos[i] >= WRAP then rpos[i] -= WRAP end cnt = (WRAP - rpos[i] - 1) // rdelta[i] + 1 if cnt < loops then loops = cnt end end end samples -= loops for _ = 1, loops do local a, b = 0, 0 for i = 1, nch do local j local r = rsrc[i] if r then j = ((readi8(src[i], pos[i] // 65536) * readi8(r, rpos[i] // 65536)) // 128) * vol[i] rpos[i] += rdelta[i] else j = readi8(src[i], pos[i] // 65536) * vol[i] end a += (j * panl[i]) // 128 b += (j * panr[i]) // 128 pos[i] += delta[i] end a = (a * gain) // 256 b = (b * gain) // 256 if a < -32768 then a = -32768 elseif a > 32767 then a = 32767 end if b < -32768 then b = -32768 elseif b > 32767 then b = 32767 end writef32(out, o, a / 32768) writef32(out, o + 4, b / 32768) o += 8 end end for i = 1, nch do voices[i].samplePos = pos[i] voices[i].ringSamplePos = rpos[i] end end return Player