Skip to content

Commit

Permalink
Revert "Simplify logic around tones ending" and fix triangle tone pop…
Browse files Browse the repository at this point in the history
…ping

This reverts commit 4e50857. That change
was causing tones to stop before the end of their release if an ending
tick came in first. I've decided to let tones that aren't replaced with
another tone to overrun the ending tick instead, to prevent sudden stops
and popping.
  • Loading branch information
majaha committed May 1, 2024
1 parent 336596d commit 85a533f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions runtimes/native/src/apu.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void w4_apuTone (int frequency, int duration, int volume, int flags) {
// Restart the phase if the channel isn't already playing, but be
// careful to keep the phase if the channel is already playing to allow
// for continuous tones and smooth transitions to or from a glide etc.
if (ticks > channel->endTick) {
if (time > channel->releaseTime && ticks > channel->endTick) {
channel->phase = (channelIdx == 2) ? 0.25 : 0;
}
if (noteMode) {
Expand Down Expand Up @@ -220,7 +220,7 @@ void w4_apuWriteSamples (int16_t* output, unsigned long frames) {
for (int channelIdx = 0; channelIdx < 4; ++channelIdx) {
Channel* channel = &channels[channelIdx];

if (ticks <= channel->endTick) {
if (time < channel->releaseTime || ticks <= channel->endTick) {
float freq = getCurrentFrequency(channel);
int16_t volume = getCurrentVolume(channel);
int16_t sample;
Expand Down
9 changes: 5 additions & 4 deletions runtimes/web/src/apu-worklet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const SAMPLE_RATE = 44100;
const MAX_VOLUME = 0.15;
// The triangle channel sounds a bit quieter than the others, so give it higher amplitude
const MAX_VOLUME_TRIANGLE = 0.25;
// Also the triangle channel prevent popping on hard stops by adding a 1 ms release
// Also the triangle channel has a short release by default to reduce popping. Popping isn't
// as noticable on the other channels.
const RELEASE_TIME_TRIANGLE = Math.floor(SAMPLE_RATE / 1000);

class Channel {
Expand Down Expand Up @@ -113,7 +114,7 @@ class APUProcessor extends AudioWorkletProcessor {

getCurrentVolume (channel: Channel) {
const time = this.time;
if (time >= channel.sustainTime && (channel.releaseTime - channel.sustainTime) > RELEASE_TIME_TRIANGLE) {
if (time >= channel.sustainTime) {
// Release
return this.ramp(channel.sustainVolume, 0, channel.sustainTime, channel.releaseTime);
} else if (time >= channel.decayTime) {
Expand Down Expand Up @@ -158,7 +159,7 @@ class APUProcessor extends AudioWorkletProcessor {
// Restart the phase if the channel isn't already playing, but be
// careful to keep the phase if the channel is already playing to allow
// for continuous tones and smooth transitions to or from a glide etc.
if (this.ticks > channel.endTick) {
if (this.time > channel.releaseTime && this.ticks > channel.endTick) {
channel.phase = (channelIdx == 2) ? 0.25 : 0;
}
if (noteMode) {
Expand Down Expand Up @@ -209,7 +210,7 @@ class APUProcessor extends AudioWorkletProcessor {
for (let channelIdx = 0; channelIdx < 4; ++channelIdx) {
const channel = this.channels[channelIdx];

if (this.ticks <= channel.endTick) {
if (this.time < channel.releaseTime || this.ticks <= channel.endTick) {
const freq = this.getCurrentFrequency(channel);
const volume = this.getCurrentVolume(channel);
let sample;
Expand Down

0 comments on commit 85a533f

Please sign in to comment.