Document another audio playback behavior into XO-ChipSpecification
Kouzeru opened this issue · 0 comments
It's an inherent behavior since the merge #139 which is now it's important to document I think. Had to mention that when buzzer timer reaches or equals 0, the playback position / phase counter that generates waveform from loaded buffer would be always reset. It was directly inspired by NES and Gameboy APU.
In Gameboy, sound is generated when bytes written to specific register, especially a two-byte register that controls frequency which consist of frequency value and some bits as its parameters. One of its parameter is a trigger bit, which also resets phase counter.
In NES, sound is triggered simply when a byte is written to high-byte of its frequency register, which also resets phase counter.
Here's example codes,
When the sound is retriggered at every second.
: main
i := sound_buffer audio loop
vF := 62 delay := vF loop vF := delay if vF != 0 then again # wait a second
vF := 0 buzzer := vF # stops the currently playing sound, and resets its playback position
vF := 255 buzzer := vF # trigger sound
again
: sound_buffer 0 255 0 255 0 255 0 255 0 255 0 255 0 255 0 255
Also, when the buzzer timer was set to 1, then the playback position will reset on every next tick.
: main
i := sound_buffer audio loop
vF := 1 delay := vF loop vF := delay if vF != 0 then again # wait till next tick
vF := 1 buzzer := vF # trigger sound
again
: sound_buffer 0 255 0 255 0 255 0 255 0 255 0 255 0 255 0 255
This enables users to make sound restart / retrigger effect, and also to make sample playbacker playbacks properly.
In conclusion, my point is to make clear that, the sound would be always continuous as long the buzzer timer value never reach zero. Hence setting buzzer timer to some another value above zero while it's still playing doesn't retrigger / reset its playback position / phase counter / make clicks. Switching audio buffer also doesn't affect the playback position, so it'd possible to create sophisticated pwm effect without clicks when switching audio buffer.
Playback position would be only reset when buzzer timer reach zero, or when user sets it to zero. Replaying the same sound (after the user stopped it by setting the buzzer timer to zero) will give "retrigger" effect.
It's just important at least for me, as my interest is chiptune, for any 3rd-party emulators implements correct behavior of this.
To make the difference clear, I'm providing these videos as example:
with retrigger effect (which creates clicks):
2021-09-23.11-30-21.mp4
without retrigger effect:
2021-09-23.11-30-42.mp4
Try to play the both video at the same time, and notice the phase shifts made by the video with the retrigger.