Skip to content

Ramp a bus's volume across the block instead of stepping it - #1262

Open
mateusz28011 wants to merge 1 commit into
shorepine:mainfrom
mateusz28011:fix/bus-volume-ramp
Open

mateusz28011 wants to merge 1 commit into
shorepine:mainfrom
mateusz28011:fix/bus-volume-ramp

Conversation

@mateusz28011

Copy link
Copy Markdown
Contributor

Problem. A bus's output gain is one constant per block (volume_scale[bus] in amy_fill_buffer), so amy.send(bus=N, volume=x) under a sounding note lands as a step at the block edge, which can click. Automating a bus's volume runs into it; my case is ducking a bass bus on every beat.

What the new test renders:

amy.send(synth=1, num_voices=1, oscs_per_voice=1, bus=1)
amy.send(synth=1, osc=0, wave=amy.SINE, bp0='0,1,10000,1,50,0')
amy.send(synth=1, osc=0, freq=55, vel=0.3)
# 300 ms later, while the note sounds:
amy.send(bus=1, volume=0.2)

On main (9106f4c) the largest sample-to-sample step of the output is about 50 times the note's own largest step. With this change it is 1.0 times.

bus_volume_ramp

Change. The gain runs linearly from where the last block left it towards the bus's volume over one block (5.8 ms at 44.1 kHz), reaching it at the next block boundary. volume_scale[bus] now stores that target and a new volume_step[bus] the per-sample step; the mix loop is otherwise as it was.

What stays the same.

  • A bus whose volume does not change gets the same gain as before, sample for sample (the step is 0).
  • A volume set before a bus's first processed block after start or reset applies at once, so volume=0 sent before anything has rendered is silent from the first sample, as now.
  • No reference WAV changes: python -m amy.test passes at its default threshold (140 tests).

What changes, on purpose. Once a bus is being processed, a volume change is no longer instant: it takes one block. That includes a mute. The tests that set a bus volume up front (TestAlgo2, TestXanaduFM, TestBuses, TestSynthBusCmds, TestTranceGlitch among them) are unaffected for the reason above.

Cost. One more array of max_buses samples; per block, one subtract and divide per bus to set the step; in the mix loop, one more multiply and subtract per bus per channel sample. Host build (macOS, arm64), four buses each with one sine: 7.15 µs per block with the change, 7.25 µs on main, medians of nine runs of 4000 blocks. On an AMYboard (ESP32-S3) playing a sequenced drum pattern with four buses active, tulip.cpu() read about 44 % before and after, inside the run-to-run spread. make timing currently fails on time= (amy/timing.py), so the host numbers are from this script:

benchmark script
import time, statistics, amy, c_amy as _amy
def run():
    _amy.stop(); _amy.start(0)
    for b in range(4):
        amy.send(synth=b + 1, num_voices=1, oscs_per_voice=1, bus=b)
        amy.send(synth=b + 1, osc=0, wave=amy.SINE, freq=110 * (b + 1), vel=0.2)
        amy.send(bus=b, volume=0.5)
    for _ in range(50): _amy.render_to_list()
    t = time.perf_counter()
    for _ in range(4000): _amy.render_to_list()
    return (time.perf_counter() - t) / 4000 * 1e6
print(statistics.median([run() for _ in range(9)]))

Test. TestBusVolumeRamp (custom test()), with a held 55 Hz note and the volume moved 1 → 0.2 and 0.2 → 1:

  • the largest sample-to-sample change, over both channels, stays under 1.5 times the steady note's;
  • the RMS over 4096 frames starting two blocks after the change is within 0.3 dB of the new volume;
  • a bus muted before its first block is exactly silent, both from the start and when it is first used after rendering has begun;
  • after a reset, the first 128 samples of the next note are within 5 % (RMS) of the same note after a fresh start, i.e. it does not start on a ramp.

A bus's output gain was one constant per block, so a volume change landed as
a step at the block edge. Under a sounding note that is a click: with a 55 Hz
sine on a bus and its volume set from 1 to 0.2, the largest sample-to-sample
step of the output was about 50 times the note's own.

The gain now runs linearly from where the last block left it to the bus's
volume over the block (5.8 ms at 44.1 kHz). A bus whose volume does not
change gets the same gain as before, sample for sample, and a bus's first
block after start or reset takes its volume as it stands, so a bus muted
before it has played stays silent. No reference output moves.

TestBusVolumeRamp moves a bus's volume down and up under a held note and
checks the output takes no step and is at the new level two blocks later,
that a bus muted before its first block makes no sound (from the start, and
when it is first used later), and that the first note after a reset starts
at full level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant