commit f46a21eaa6156ac9941b0ae5b9e2f63a6d7b7ae2
Merge: 41dec8f f08bbd8
Author: Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Jan 3 11:50:35 2026 -0700
Commit: Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Jan 3 11:50:35 2026 -0700
Merge branch 'audio-update'
platform/libretro/Makefile.common | 1 +
source/Audio.cpp | 1225 ++++++++++++++++++++++++-------------
source/Audio.h | 12 +-
source/PicoRam.h | 80 +--
source/cart.cpp | 4 +-
source/filter.cpp | 100 +++
source/filter.h | 44 ++
source/synth.cpp | 140 ++---
source/synth.h | 30 +-
test/audiotests.cpp | 223 +------
test/carttests.cpp | 4 +-
test/vmtests.cpp | 2 +-
12 files changed, 1121 insertions(+), 744 deletions(-)
diff --cc source/Audio.cpp
index 91816c1,8d1c0fb..c81829c
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@@ -22,28 -21,42 +22,46 @@@ Audio::Audio(PicoRam* memory)
resetAudioState();
}
+void Audio::setPaused(bool paused) {
+ _paused = paused;
+}
+
void Audio::resetAudioState() {
+ _audioState._musicChannel.count = -1;
+ _audioState._musicChannel.pattern = -1;
+ _audioState._musicChannel.mask = 0;
+ _audioState._musicChannel.volume_music = 0.5f;
+ _audioState._musicChannel.volume_sfx = 0.5f;
+ _audioState._musicChannel.fade_volume = 0.f;
+ _audioState._musicChannel.fade_volume_step = 0.f;
+ _audioState._musicChannel.offset = -1;
+ _audioState._musicChannel.length = 0;
+
for(int i = 0; i < 4; i++) {
- _audioState._sfxChannels[i].sfxId = -1;
- _audioState._sfxChannels[i].offset = 0;
- _audioState._sfxChannels[i].current_note.phi = 0;
+ _audioState._sfxChannels[i].main_sfx.sfx = -1;
+ _audioState._sfxChannels[i].main_sfx.offset = 0;
+ _audioState._sfxChannels[i].main_sfx.time = 0;
+ _audioState._sfxChannels[i].main_sfx.prev_key = 24;
+ _audioState._sfxChannels[i].main_sfx.prev_vol = 0;
+
+ _audioState._sfxChannels[i].custom_sfx.sfx = -1;
+ _audioState._sfxChannels[i].sfx_music = -1;
+ _audioState._sfxChannels[i].length = 0;
_audioState._sfxChannels[i].can_loop = true;
_audioState._sfxChannels[i].is_music = false;
- _audioState._sfxChannels[i].prev_note.n.setKey(0);
- _audioState._sfxChannels[i].prev_note.n.setVolume(0);
+ _audioState._sfxChannels[i].fade = 0.0f;
+ _audioState._sfxChannels[i].last_main_instrument = 0;
+ _audioState._sfxChannels[i].last_main_key = 0;
+ _audioState._sfxChannels[i].reverb_index = 0;
+
+ // Reset reverb buffers
+ std::memset(_audioState._sfxChannels[i].reverb_2, 0, sizeof(_audioState._sfxChannels[i].reverb_2));
+ std::memset(_audioState._sfxChannels[i].reverb_4, 0, sizeof(_audioState._sfxChannels[i].reverb_4));
+
+ // Reset damp filters
+ _audioState._sfxChannels[i].damp1 = z8::filter(z8::filter::type::highshelf, 2400.0f, 1.0f, -6.0f);
+ _audioState._sfxChannels[i].damp2 = z8::filter(z8::filter::type::highshelf, 1000.0f, 1.0f, -12.0f);
}
- _audioState._musicChannel.count = 0;
- _audioState._musicChannel.pattern = -1;
- _audioState._musicChannel.master = -1;
- _audioState._musicChannel.mask = 0xf;
- _audioState._musicChannel.speed = 0;
- _audioState._musicChannel.volume = 0.f;
- _audioState._musicChannel.volume_step = 0.f;
- _audioState._musicChannel.offset = 0.f;
}
audioState_t* Audio::getAudioState() {
@@@ -352,248 -392,592 +397,608 @@@ void Audio::update_sfx_state(sfx_state
// PICO-8 exports instruments as 22050 Hz WAV files with 183 samples
// per speed unit per note, so this is how much we should advance
- float const offset_per_second = 22050.f / (183.f * speed);
- float const offset_per_sample = offset_per_second / samples_per_second;
- float next_offset = channel.offset + offset_per_sample;
-
- // Handle SFX loops. From the documentation: “Looping is turned
- // off when the start index >= end index”.
- float const loop_range = float(sfx.loopRangeEnd - sfx.loopRangeStart);
- if (loop_range > 0.f && next_offset >= sfx.loopRangeStart && channel.can_loop) {
- next_offset = fmod(next_offset - sfx.loopRangeStart, loop_range)
- + sfx.loopRangeStart;
+ double const offset_per_second = 22050.0 / (183.0 * speed);
+ double const offset_per_frame = offset_per_second * inv_frames_per_second;
+ double next_offset = offset + offset_per_frame;
+ double next_time = time + offset_per_frame;
+
+ // Handle SFX loops. From the documentation: "Looping is turned
+ // off when the start index >= end index".
+ float const loop_range = float(sfx_data.loopRangeEnd - sfx_data.loopRangeStart);
+ if (loop_range > 0.f && next_offset >= sfx_data.loopRangeEnd && can_loop)
+ {
+ next_offset = fmod(next_offset - sfx_data.loopRangeStart, loop_range)
+ + sfx_data.loopRangeStart;
}
- int const note_idx = (int)floor(channel.offset);
- channel.current_note.n=sfx.notes[note_idx];
- int const next_note_idx = (int)floor(next_offset);
+ bool has_end = false;
+ float end_time = 32.f;
+ if (length > 0.0f)
+ {
+ has_end = true;
+ end_time = length;
+ }
+ // in pico 8, strangely, len is not applied to musical sfx except for pattern len calculation
+ // it's probably a bug
+ if (!is_music && sfx_data.loopRangeEnd == 0 && sfx_data.loopRangeStart > 0)
+ {
+ has_end = true;
+ end_time = std::min<float>(end_time, sfx_data.loopRangeStart);
+ }
+ // if there is no loop, we end after the length
+ if (loop_range <= 0.f)
+ {
+ has_end = true;
+ // if not a music sfx, check where is the last note to early stop
+ if (!is_music)
+ {
+ int last_note = 0;
+ for (int n = 0; n < 32; ++n)
+ {
+ if (sfx_data.notes[n].getVolume() > 0)
+ {
+ last_note = std::min(32, n + 1);
+ }
+ }
+ end_time = std::min(end_time, float(last_note));
+ }
+ }
+ if (offset < 32)
+ {
+ int const note_id = (int)floor(offset);
+ int const next_note_id = (int)floor(next_offset);
+ uint8_t key = sfx_data.notes[note_id].getKey();
+ float volume = sfx_data.notes[note_id].getVolume() / 7.f;
+ float freq = key_to_freq(key) * freq_factor;
- /*
- if (volume == 0.f){
- //volume all the way off. return silence, but make sure to set stuff
- channel.offset = next_offset;
+ if (volume > 0.f)
+ {
+ int const fx = sfx_data.notes[note_id].getEffect();
- if (next_offset >= 32.f){
- channel.sfxId = -1;
- if (channel.getChildChannel()) {
- channel.getChildChannel()->sfxId = -1;
+ // Apply effect, if any
+ switch (fx)
+ {
+ case FX_NO_EFFECT:
+ break;
+ case FX_SLIDE:
+ {
+ float t = (float)fmod(offset, 1.0);
+ // From the documentation: "Slide to the next note and volume",
+ // but it's actually _from_ the _prev_ note and volume.
+ freq = lerp(key_to_freq((float)cur_sfx.prev_key), freq, t);
+ if (cur_sfx.prev_vol > 0.f)
+ volume = lerp(cur_sfx.prev_vol, volume, t);
+ break;
}
- }
- else if (next_note_idx != note_idx){
- channel.prev_note = sfx.notes[note_idx];
+ case FX_VIBRATO:
+ {
+ // 7.5f and 0.25f were found empirically by matching
+ // frequency graphs of PICO-8 instruments.
+ float t = (float)(fabs(fmod(7.5 * offset / offset_per_second, 1.0)) - 0.5) - 0.25f;
+ // Vibrato half a semi-tone, so multiply by pow(2,1/12)
+ freq = lerp(freq, freq * 1.059463094359f, t);
+ break;
+ }
+ case FX_DROP:
+ freq *= 1.f - (float)fmod(offset, 1.0);
+ break;
+ case FX_FADE_IN:
+ volume *= (float)fmod(offset, 1.0);
+ break;
+ case FX_FADE_OUT:
+ volume *= 1.f - (float)fmod(offset, 1.0);
+ break;
+ case FX_ARP_FAST:
+ case FX_ARP_SLOW:
+ {
+ // From the documentation:
+ // "6 arpeggio fast // Iterate over groups of 4 notes at speed of 4
+ // 7 arpeggio slow // Iterate over groups of 4 notes at speed of 8"
+ // "If the SFX speed is <= 8, arpeggio speeds are halved to 2, 4"
+ int const m = (speed <= 8 ? 32 : 16) / (fx == FX_ARP_FAST ? 4 : 8);
+ int const n = (int)(m * 7.5 * offset / offset_per_second);
+ int const arp_note = (note_id & ~3) | (n & 3);
+ freq = key_to_freq(sfx_data.notes[arp_note].getKey());
+ break;
+ }
+ }
+
+ if (half_rate) freq *= 0.5f;
+
+ new_synth.key = key;
+ new_synth.freq = freq;
+ new_synth.instrument = sfx_data.notes[note_id].getWaveform();
+ new_synth.custom = sfx_data.notes[note_id].getCustom();
+ new_synth.filters = sfx_data.filters;
+ new_synth.volume = volume;
+ new_synth.is_music = is_music;
+
+ new_synth.phi = new_synth.phi + (float)(freq * inv_frames_per_second);
}
- return 0;
+ if (next_note_id != note_id)
+ {
+ cur_sfx.prev_key = sfx_data.notes[note_id].getKey();
+ cur_sfx.prev_vol = sfx_data.notes[note_id].getVolume() / 7.f;
+ }
}
- */
-
- // tiniest fade in/out to fix popping
- // the real version uses a crossfade it looks like
- // 25 samples was estimated from looking at pcm out from pico-8
- float const fade_duration = offset_per_sample * 25;
- float offset_part = fmod(channel.offset, 1.f);
- float crossfade = 0;
- if (offset_part < fade_duration) {
- crossfade = (fade_duration-offset_part)/fade_duration;
+
+ cur_sfx.offset = next_offset;
+ cur_sfx.time = next_time;
+
+ if (has_end && next_time >= end_time)
+ {
+ cur_sfx.sfx = -1;
}
-
+ }
+
+ float Audio::get_synth_sample(z8::synth_param& params)
+ {
+ // Play note
+ float waveform = z8::synth::waveform(params);
+
+ uint8_t detune = (params.filters / 8) % 3;
+ if (detune != 0 && params.instrument != z8::synth::INST_NOISE)
+ {
+ // detune is a second wave slightly offset
+ float factor = 1.0f;
+ if (params.instrument == z8::synth::INST_TRIANGLE)
+ factor = (detune == 1) ? 3.0f / 4.0f : 3.0f / 2.0f; // triangle detune adds a fourth or a fifth
+ else if (params.instrument == z8::synth::INST_ORGAN)
+ factor = (detune == 1) ? 200.0f / 199.0f : 800.0f / 199.0f; // slight offset, detune 2 at 2 octave above
+ else if (params.instrument == z8::synth::INST_PHASER)
+ factor = (detune == 1) ? 49.0f / 50.0f : 400.0f / 199.0f;
+ else
+ factor = (detune == 1) ? 200.0f / 199.0f : 400.0f / 199.0f; // others are slight offset, detune 2 at 1 octave above
+
+ z8::synth_param second_wave = params;
+ second_wave.phi *= factor;
+ if (detune == 2 && params.instrument == z8::synth::INST_ORGAN)
+ second_wave.instrument = z8::synth::INST_TRIANGLE; // organ second wave seems to be simpler
+ waveform += z8::synth::waveform(second_wave) * 0.5f;
+ }
+
+ float volume = params.volume;
- bool custom = (bool) sfx.notes[note_idx].getCustom() && channel.getChildChannel() != NULL;
- // it seems we're not allowed to play custom instruments
- // recursively inside a custom instrument.
- float waveform = this->getSampleForNote(channel.current_note, channel, channel.getChildChannel(), channel.prev_note.n, freqShift, false);
- if (crossfade > 0) {
- waveform *= (1.0f-crossfade);
- note dummyNote;
- waveform+= crossfade * this->getSampleForNote(channel.prev_note, channel, channel.getPrevChildChannel(), dummyNote, freqShift, true);
+ // Apply master music volume from fade in/out
+ if (params.is_music)
+ {
+ volume *= _audioState._musicChannel.fade_volume * _audioState._musicChannel.volume_music;
}
- uint8_t len = sfx.loopRangeEnd == 0 ? 32 : sfx.loopRangeEnd;
- bool lastNote = note_idx == len - 1;
- if (lastNote && 1.0f - offset_part < fade_duration) {
- waveform *= (fade_duration - 1.0f + offset_part)/fade_duration;
+ else
+ {
+ volume *= _audioState._musicChannel.volume_sfx;
}
- // Apply master music volume from fade in/out
- // FIXME: check whether this should be done after distortion
- if (channel.is_music) {
- waveform *= _audioState._musicChannel.volume;
+ return std::clamp(waveform * volume, -1.0f, 1.0f);
+ }
+
+ void Audio::FillAudioBuffer(void *audioBuffer, size_t offset, size_t size){
+ if (audioBuffer == nullptr) {
+ return;
}
- channel.offset = next_offset;
+ uint32_t *buffer = (uint32_t *)audioBuffer;
- if (next_offset >= 32.f){
- channel.sfxId = -1;
- if (custom) {
- channel.getChildChannel()->sfxId = -1;
++ // Output silence when paused
++ if (_paused) {
++ for (size_t i = 0; i < size; ++i){
++ buffer[i] = 0;
+ }
++ return;
+ }
- else if (next_note_idx != note_idx){
- channel.prev_note = channel.current_note; //sfx.notes[note_idx].getKey();
- channel.current_note.n = sfx.notes[next_note_idx];
- channel.current_note.phi = channel.prev_note.phi;
- if (custom) {
- if (!sfx.notes[next_note_idx].getCustom() ||
- sfx.notes[next_note_idx].getKey() != sfx.notes[note_idx].getKey() ||
- sfx.notes[next_note_idx].getWaveform() != sfx.notes[note_idx].getWaveform()
- ) {
- channel.rotateChannels();
- channel.getChildChannel()->sfxId = -1;
++
+ for (size_t i = 0; i < size; ++i){
+ int32_t sample = 0;
+ float channel_mix = 0.0f;
+
+ bool is_pause = _memory->drawState.soundPauseState == 1;
+
+ for (int chan = 0; chan < 4; ++chan) {
+ double inv_frames_per_second = ((_memory->hwState.half_rate & (1 << chan)) ? 0.5 : 1.0) / 22050.0;
+
+ sfxChannel& channel_state = _audioState._sfxChannels[chan];
+
+ // Advance music using the first channel
+ if (chan == 0 && _audioState._musicChannel.pattern != -1 && !is_pause)
+ {
+ double const offset_per_second = 22050.0 / 183.0;
+ double const offset_per_frame = offset_per_second * inv_frames_per_second;
+ _audioState._musicChannel.offset += offset_per_frame;
+ _audioState._musicChannel.fade_volume += (float)(_audioState._musicChannel.fade_volume_step * inv_frames_per_second);
+ _audioState._musicChannel.fade_volume = clamp(_audioState._musicChannel.fade_volume, 0.f, 1.f);
+
+ if (_audioState._musicChannel.fade_volume_step < 0 && _audioState._musicChannel.fade_volume <= 0)
+ {
+ set_music_pattern(-1);
+ }
+ else if (_audioState._musicChannel.offset >= _audioState._musicChannel.length)
+ {
+ int16_t next_pattern = _audioState._musicChannel.pattern + 1;
+ int16_t next_count = _audioState._musicChannel.count + 1;
+ if (_memory->songs[_audioState._musicChannel.pattern].getStop())
+ {
+ next_pattern = -1;
+ next_count = -1;
+ }
+ else if (_memory->songs[_audioState._musicChannel.pattern].getLoop())
+ while (--next_pattern > 0 && !_memory->songs[next_pattern].getStart())
+ ;
+
+ _audioState._musicChannel.count = next_count;
+ set_music_pattern(next_pattern);
+ }
+ }
+
+ // if no sfx is playing and there is a music sfx stored
+ if (channel_state.main_sfx.sfx == -1 && channel_state.sfx_music != -1 && !is_pause)
+ {
+ int const index = channel_state.sfx_music;
+ assert(index >= 0 && index < 64);
+ struct sfx const& sfx_data = _memory->sfx[index];
+
+ // compute offset to start the sfx to
+ bool want_play = true;
+ int const speed = std::max(1, (int)sfx_data.speed);
+ double new_offset = _audioState._musicChannel.offset / speed;
+
+ float const loop_range = (float)(sfx_data.loopRangeEnd - sfx_data.loopRangeStart);
+ if (loop_range > 0.f && channel_state.can_loop)
+ {
+ if (new_offset > sfx_data.loopRangeStart)
+ new_offset = std::fmod(new_offset - sfx_data.loopRangeStart, loop_range) + sfx_data.loopRangeStart;
+ }
+ else
+ {
+ if (new_offset > 32.0)
+ want_play = false;
+ }
+
+ if (want_play)
+ {
+ launch_sfx(index, chan, (float)new_offset, 0, true);
+ }
+ channel_state.sfx_music = -1;
+ }
+
+ z8::synth_param& last_synth = channel_state.last_synth;
+ z8::synth_param new_synth;
+ new_synth.phi = last_synth.phi;
+ new_synth.last_advance = last_synth.last_advance;
+ new_synth.last_sample = last_synth.last_sample;
+ float value = 0.0f;
+
+ if (!is_pause)
+ {
+ double main_sfx_base_offset = channel_state.main_sfx.offset;
+ bool half_rate = _memory->hwState.half_rate & (1 << (chan + 4));
+ // update main sfx
+ update_sfx_state(channel_state.main_sfx, new_synth, 1.0f, channel_state.length,
+ channel_state.is_music, channel_state.can_loop, half_rate, inv_frames_per_second);
+
+ bool restart_custom = new_synth.instrument != channel_state.last_main_instrument ||
+ new_synth.key != channel_state.last_main_key;
+ channel_state.last_main_instrument = new_synth.instrument;
+ channel_state.last_main_key = new_synth.key;
+
+ if (new_synth.volume > 0.0f)
+ {
+ if (new_synth.custom)
+ {
+ // also need to restart if main_sfx loops (new offset is before base offset)
+ if (channel_state.main_sfx.offset < main_sfx_base_offset) restart_custom = true;
+ // also need to restart if custom_sfx.sfx == -1 (it has ended) and main_sfx.offset is changing integer
+ if (channel_state.custom_sfx.sfx == -1 &&
+ std::floor(main_sfx_base_offset) != std::floor(channel_state.main_sfx.offset))
+ restart_custom = true;
+
+ if (restart_custom)
+ {
+ channel_state.custom_sfx.sfx = new_synth.instrument;
+ channel_state.custom_sfx.offset = 0.0;
+ channel_state.custom_sfx.time = 0.0;
+ }
+ new_synth.phi = last_synth.phi;
+ float const freq_base = key_to_freq(24); // C2
+ float freq_factor = new_synth.freq / freq_base;
+ float main_sfx_volume = new_synth.volume;
+ update_sfx_state(channel_state.custom_sfx, new_synth, freq_factor, 0.0f, false, true, half_rate, inv_frames_per_second);
+ new_synth.volume *= main_sfx_volume;
+ }
+ value = get_synth_sample(new_synth);
+ }
+ }
+
+ // detect harsh changes of states, and do a small fade
+ float freq_threshold = std::min(new_synth.freq, last_synth.freq) * 0.01f;
+ if (std::abs(new_synth.volume - last_synth.volume) > 0.1f
+ || std::abs(new_synth.freq - last_synth.freq) > freq_threshold
+ || new_synth.instrument != last_synth.instrument)
+ {
+ if (channel_state.fade <= 0.0f) // avoid continuous fades, it messes with noise algo
+ {
+ channel_state.fade_synth = last_synth;
+ }
+ channel_state.fade = 1.0f;
+ // reset phi between notes so we don't get very big values that would lose precision
+ new_synth.phi = std::fmod(new_synth.phi, 1.0f);
+ }
+ last_synth = new_synth;
+
+ uint8_t reverb = (last_synth.filters / 24) % 3;
+ uint8_t dampen = (last_synth.filters / 72) % 3;
+ float chan_reverb1_value = reverb == 1 ? 1.0f : 0.0f;
+ float chan_reverb2_value = reverb == 2 ? 1.0f : 0.0f;
+ float chan_damp1_value = dampen == 1 ? 1.0f : 0.0f;
+ float chan_damp2_value = dampen == 2 ? 1.0f : 0.0f;
+
+ if (channel_state.fade > 0.0f)
+ {
+ channel_state.fade_synth.phi = channel_state.fade_synth.phi +
+ (float)(channel_state.fade_synth.freq * inv_frames_per_second);
+ float value_fade = get_synth_sample(channel_state.fade_synth);
+
+ value = lerp(value, value_fade, channel_state.fade);
+
+ // Also mix fade filter values
+ uint8_t fade_reverb = (channel_state.fade_synth.filters / 24) % 3;
+ uint8_t fade_dampen = (channel_state.fade_synth.filters / 72) % 3;
+ chan_reverb1_value = lerp(chan_reverb1_value, fade_reverb == 1 ? 1.0f : 0.0f, channel_state.fade);
+ chan_reverb2_value = lerp(chan_reverb2_value, fade_reverb == 2 ? 1.0f : 0.0f, channel_state.fade);
+ chan_damp1_value = lerp(chan_damp1_value, fade_dampen == 1 ? 1.0f : 0.0f, channel_state.fade);
+ chan_damp2_value = lerp(chan_damp2_value, fade_dampen == 2 ? 1.0f : 0.0f, channel_state.fade);
+
+ channel_state.fade -= (float)(130.0 * inv_frames_per_second);
+ }
+
+ // hw can force fx passes
+ if (_memory->hwState.reverb & (1 << (chan + 4))) chan_reverb1_value = 1.0f;
+ if (_memory->hwState.reverb & (1 << chan)) chan_reverb2_value = 1.0f;
+ if (_memory->hwState.lowpass & (1 << (chan + 4))) chan_damp1_value = 1.0f;
+ if (_memory->hwState.lowpass & (1 << chan)) chan_damp2_value = 1.0f;
+
+ if (chan_reverb1_value > 0.0f)
+ value += chan_reverb1_value * channel_state.reverb_2[channel_state.reverb_index % 366] * 0.5f;
+ if (chan_reverb2_value > 0.0f)
+ value += chan_reverb2_value * channel_state.reverb_4[channel_state.reverb_index % 732] * 0.5f;
+
+ channel_state.reverb_2[channel_state.reverb_index % 366] = value;
+ channel_state.reverb_4[channel_state.reverb_index % 732] = value;
+ ++channel_state.reverb_index;
+
+ float value_damp1 = channel_state.damp1.run(value);
+ if (chan_damp1_value > 0.0f) value = lerp(value, value_damp1, chan_damp1_value);
+
+ float value_damp2 = channel_state.damp2.run(value);
+ if (chan_damp2_value > 0.0f) value = lerp(value, value_damp2, chan_damp2_value);
+
+ int16_t chan_sample = (int16_t)(32767.99f * std::clamp(value, -0.99f, 0.99f));
+
+ // Apply hardware distort
+ if (_memory->hwState.distort & (1 << chan))
+ {
+ chan_sample = chan_sample / 0x1000 * 0x1249;
+ }
+ else if (_memory->hwState.distort & (1 << (chan + 4)))
+ {
+ chan_sample = (chan_sample - (chan_sample < 0 ? 0x1000 : 0)) / 0x1000 * 0x1249;
}
+ channel_mix += chan_sample;
}
- }
- return waveform;
+ sample = (int32_t)std::clamp(channel_mix, -32767.9f, 32767.9f);
+
+ //buffer is stereo, so just send the mono sample to both channels
+ buffer[i] = ((uint32_t)(uint16_t)sample << 16) | ((uint16_t)sample);
+ }
}
- float Audio::getSampleForNote(noteChannel &channel, rawSfxChannel &parentChannel, rawSfxChannel *childChannel, note prev_note, float freqShift, bool forceRemainder) {
- using std::max;
- float offset = parentChannel.offset;
- int const samples_per_second = 22050;
- //TODO: apply effects
- int const fx = channel.n.getEffect();
- uint8_t key = channel.n.getKey();
- float volume = channel.n.getVolume() / 7.f;
- float freq = key_to_freq(key);
- struct sfx const &sfx = _memory->sfx[parentChannel.sfxId];
+ void Audio::FillMonoAudioBuffer(void *audioBuffer, size_t offset, size_t size){
+ if (audioBuffer == nullptr) {
+ return;
+ }
- // Speed must be 1—255 otherwise the SFX is invalid
- int const speed = max(1, (int)sfx.speed);
- float const offset_per_second = 22050.f / (183.f * speed);
- int const note_idx = (int)floor(offset);
-
- // previous note effectively goes beyond offset fmod 1 when in crossfade
- float tmod= 0;
- if (forceRemainder) tmod = 1.0f;
- tmod += fmod(offset, 1.f);
- // Apply effect, if any
- switch (fx)
- {
- case FX_NO_EFFECT:
- break;
- case FX_SLIDE:
- {
- // From the documentation: “Slide to the next note and volume”,
- // but it’s actually _from_ the _prev_ note and volume.
- freq = lerp(key_to_freq(prev_note.getKey()), freq, tmod);
- if (prev_note.getVolume() > 0)
- volume = lerp(prev_note.getVolume() / 7.0f, volume, tmod);
- break;
- }
- case FX_VIBRATO:
- {
- // 7.5f and 0.25f were found empirically by matching
- // frequency graphs of PICO-8 instruments.
- float t = fabs(fmod(7.5f * tmod / offset_per_second, 1.0f) - 0.5f) - 0.25f;
- // Vibrato half a semi-tone, so multiply by pow(2,1/12)
- freq = lerp(freq, freq * 1.059463094359f, t);
- break;
- }
- case FX_DROP:
- freq *= 1.f - fmod(offset, 1.f);
- break;
- case FX_FADE_IN:
- volume *= std::min(1.f, tmod);
- break;
- case FX_FADE_OUT:
- volume *= max(0.0f, 1.f - tmod);
- break;
- case FX_ARP_FAST:
- case FX_ARP_SLOW:
- {
- // From the documentation:
- // “6 arpeggio fast // Iterate over groups of 4 notes at speed of 4
- // 7 arpeggio slow // Iterate over groups of 4 notes at speed of 8”
- // “If the SFX speed is <= 8, arpeggio speeds are halved to 2, 4”
- int const m = (speed <= 8 ? 32 : 16) / (fx == FX_ARP_FAST ? 4 : 8);
- int const n = (int)(m * 7.5f * offset / offset_per_second);
- int const arp_note = (note_idx & ~3) | (n & 3);
- freq = key_to_freq(sfx.notes[arp_note].getKey());
- break;
+ int16_t *buffer = (int16_t *)audioBuffer;
+
++ // Output silence when paused
++ if (_paused) {
++ for (size_t i = 0; i < size; ++i){
++ buffer[i] = 0;
+ }
++ return;
+ }
- freq*=freqShift;
-
- bool custom = (bool) channel.n.getCustom() && childChannel != NULL;
- float waveform;
- if (custom) {
- if (childChannel->sfxId == -1) {
- // initialize child channel
- childChannel->sfxId = channel.n.getWaveform();
- childChannel->offset = 0;
- childChannel->current_note.phi = 0;
- childChannel->can_loop = true;
- // don't want to double lower volume for music subchannel
- childChannel->is_music = false;
- childChannel->prev_note.n.setKey(0);
- childChannel->prev_note.n.setVolume(0);
- }
- waveform = volume * this->getSampleForSfx(*childChannel, freq/C2_FREQ);
- } else {
- waveform = volume * z8::synth::waveform(channel.n.getWaveform(), channel.phi);
- }
- channel.phi = channel.phi + freq / samples_per_second;
- return waveform;
- }
+
+ for (size_t i = 0; i < size; ++i){
+ float channel_mix = 0.0f;
- //adapted from zepto8 sfx.cpp (wtfpl license)
- int16_t Audio::getSampleForChannel(int channel){
- using std::fabs;
- using std::fmod;
- using std::floor;
- using std::max;
+ bool is_pause = _memory->drawState.soundPauseState == 1;
- int const samples_per_second = 22050;
+ for (int chan = 0; chan < 4; ++chan) {
+ double inv_frames_per_second = ((_memory->hwState.half_rate & (1 << chan)) ? 0.5 : 1.0) / 22050.0;
- int16_t sample = 0;
+ sfxChannel& channel_state = _audioState._sfxChannels[chan];
- // Advance music using the master channel
- if (channel == _audioState._musicChannel.master && _audioState._musicChannel.pattern != -1)
- {
- float const offset_per_second = 22050.f / (183.f * _audioState._musicChannel.speed);
- float const offset_per_sample = offset_per_second / samples_per_second;
- _audioState._musicChannel.offset += offset_per_sample;
- _audioState._musicChannel.volume += _audioState._musicChannel.volume_step / samples_per_second;
- _audioState._musicChannel.volume = clamp(_audioState._musicChannel.volume, 0.f, 1.f);
+ // Advance music using the first channel
+ if (chan == 0 && _audioState._musicChannel.pattern != -1 && !is_pause)
+ {
+ double const offset_per_second = 22050.0 / 183.0;
+ double const offset_per_frame = offset_per_second * inv_frames_per_second;
+ _audioState._musicChannel.offset += offset_per_frame;
+ _audioState._musicChannel.fade_volume += (float)(_audioState._musicChannel.fade_volume_step * inv_frames_per_second);
+ _audioState._musicChannel.fade_volume = clamp(_audioState._musicChannel.fade_volume, 0.f, 1.f);
- if (_audioState._musicChannel.volume_step < 0 && _audioState._musicChannel.volume <= 0)
- {
- // Fade out is finished, stop playing the current song
- for (int i = 0; i < 4; ++i) {
- if (_audioState._sfxChannels[i].is_music) {
- _audioState._sfxChannels[i].sfxId = -1;
+ if (_audioState._musicChannel.fade_volume_step < 0 && _audioState._musicChannel.fade_volume <= 0)
+ {
+ set_music_pattern(-1);
+ }
+ else if (_audioState._musicChannel.offset >= _audioState._musicChannel.length)
+ {
+ int16_t next_pattern = _audioState._musicChannel.pattern + 1;
+ int16_t next_count = _audioState._musicChannel.count + 1;
+ if (_memory->songs[_audioState._musicChannel.pattern].getStop())
+ {
+ next_pattern = -1;
+ next_count = -1;
+ }
+ else if (_memory->songs[_audioState._musicChannel.pattern].getLoop())
+ while (--next_pattern > 0 && !_memory->songs[next_pattern].getStart())
+ ;
+
+ _audioState._musicChannel.count = next_count;
+ set_music_pattern(next_pattern);
}
}
- _audioState._musicChannel.pattern = -1;
- }
- else if (_audioState._musicChannel.offset >= _audioState._musicChannel.length)
- {
- int16_t next_pattern = _audioState._musicChannel.pattern + 1;
- int16_t next_count = _audioState._musicChannel.count + 1;
- //todo: pull out these flags, get memory storage correct as well
- if (_memory->songs[_audioState._musicChannel.pattern].getStop()) //stop part of the loop flag
+
+ // if no sfx is playing and there is a music sfx stored
+ if (channel_state.main_sfx.sfx == -1 && channel_state.sfx_music != -1 && !is_pause)
{
- next_pattern = -1;
- next_count = _audioState._musicChannel.count;
+ int const index = channel_state.sfx_music;
+ assert(index >= 0 && index < 64);
+ struct sfx const& sfx_data = _memory->sfx[index];
+
+ bool want_play = true;
+ int const speed = std::max(1, (int)sfx_data.speed);
+ double new_offset = _audioState._musicChannel.offset / speed;
+
+ float const loop_range = (float)(sfx_data.loopRangeEnd - sfx_data.loopRangeStart);
+ if (loop_range > 0.f && channel_state.can_loop)
+ {
+ if (new_offset > sfx_data.loopRangeStart)
+ new_offset = std::fmod(new_offset - sfx_data.loopRangeStart, loop_range) + sfx_data.loopRangeStart;
+ }
+ else
+ {
+ if (new_offset > 32.0)
+ want_play = false;
+ }
+
+ if (want_play)
+ {
+ launch_sfx(index, chan, (float)new_offset, 0, true);
+ }
+ channel_state.sfx_music = -1;
}
- else if (_memory->songs[_audioState._musicChannel.pattern].getLoop()){
- while (--next_pattern > 0 && !_memory->songs[next_pattern].getStart())
- ;
+
+ z8::synth_param& last_synth = channel_state.last_synth;
+ z8::synth_param new_synth;
+ new_synth.phi = last_synth.phi;
+ new_synth.last_advance = last_synth.last_advance;
+ new_synth.last_sample = last_synth.last_sample;
+ float value = 0.0f;
+
+ if (!is_pause)
+ {
+ double main_sfx_base_offset = channel_state.main_sfx.offset;
+ bool half_rate = _memory->hwState.half_rate & (1 << (chan + 4));
+ update_sfx_state(channel_state.main_sfx, new_synth, 1.0f, channel_state.length,
+ channel_state.is_music, channel_state.can_loop, half_rate, inv_frames_per_second);
+
+ bool restart_custom = new_synth.instrument != channel_state.last_main_instrument ||
+ new_synth.key != channel_state.last_main_key;
+ channel_state.last_main_instrument = new_synth.instrument;
+ channel_state.last_main_key = new_synth.key;
+
+ if (new_synth.volume > 0.0f)
+ {
+ if (new_synth.custom)
+ {
+ if (channel_state.main_sfx.offset < main_sfx_base_offset) restart_custom = true;
+ if (channel_state.custom_sfx.sfx == -1 &&
+ std::floor(main_sfx_base_offset) != std::floor(channel_state.main_sfx.offset))
+ restart_custom = true;
+
+ if (restart_custom)
+ {
+ channel_state.custom_sfx.sfx = new_synth.instrument;
+ channel_state.custom_sfx.offset = 0.0;
+ channel_state.custom_sfx.time = 0.0;
+ }
+ new_synth.phi = last_synth.phi;
+ float const freq_base = key_to_freq(24);
+ float freq_factor = new_synth.freq / freq_base;
+ float main_sfx_volume = new_synth.volume;
+ update_sfx_state(channel_state.custom_sfx, new_synth, freq_factor, 0.0f, false, true, half_rate, inv_frames_per_second);
+ new_synth.volume *= main_sfx_volume;
+ }
+ value = get_synth_sample(new_synth);
+ }
}
- _audioState._musicChannel.count = next_count;
- set_music_pattern(next_pattern);
- }
- }
+ float freq_threshold = std::min(new_synth.freq, last_synth.freq) * 0.01f;
+ if (std::abs(new_synth.volume - last_synth.volume) > 0.1f
+ || std::abs(new_synth.freq - last_synth.freq) > freq_threshold
+ || new_synth.instrument != last_synth.instrument)
+ {
+ if (channel_state.fade <= 0.0f)
+ {
+ channel_state.fade_synth = last_synth;
+ }
+ channel_state.fade = 1.0f;
+ new_synth.phi = std::fmod(new_synth.phi, 1.0f);
+ }
+ last_synth = new_synth;
+
+ uint8_t reverb = (last_synth.filters / 24) % 3;
+ uint8_t dampen = (last_synth.filters / 72) % 3;
+ float chan_reverb1_value = reverb == 1 ? 1.0f : 0.0f;
+ float chan_reverb2_value = reverb == 2 ? 1.0f : 0.0f;
+ float chan_damp1_value = dampen == 1 ? 1.0f : 0.0f;
+ float chan_damp2_value = dampen == 2 ? 1.0f : 0.0f;
+
+ if (channel_state.fade > 0.0f)
+ {
+ channel_state.fade_synth.phi = channel_state.fade_synth.phi +
+ (float)(channel_state.fade_synth.freq * inv_frames_per_second);
+ float value_fade = get_synth_sample(channel_state.fade_synth);
+
+ value = lerp(value, value_fade, channel_state.fade);
+
+ uint8_t fade_reverb = (channel_state.fade_synth.filters / 24) % 3;
+ uint8_t fade_dampen = (channel_state.fade_synth.filters / 72) % 3;
+ chan_reverb1_value = lerp(chan_reverb1_value, fade_reverb == 1 ? 1.0f : 0.0f, channel_state.fade);
+ chan_reverb2_value = lerp(chan_reverb2_value, fade_reverb == 2 ? 1.0f : 0.0f, channel_state.fade);
+ chan_damp1_value = lerp(chan_damp1_value, fade_dampen == 1 ? 1.0f : 0.0f, channel_state.fade);
+ chan_damp2_value = lerp(chan_damp2_value, fade_dampen == 2 ? 1.0f : 0.0f, channel_state.fade);
+
+ channel_state.fade -= (float)(130.0 * inv_frames_per_second);
+ }
+
+ if (_memory->hwState.reverb & (1 << (chan + 4))) chan_reverb1_value = 1.0f;
+ if (_memory->hwState.reverb & (1 << chan)) chan_reverb2_value = 1.0f;
+ if (_memory->hwState.lowpass & (1 << (chan + 4))) chan_damp1_value = 1.0f;
+ if (_memory->hwState.lowpass & (1 << chan)) chan_damp2_value = 1.0f;
+
+ if (chan_reverb1_value > 0.0f)
+ value += chan_reverb1_value * channel_state.reverb_2[channel_state.reverb_index % 366] * 0.5f;
+ if (chan_reverb2_value > 0.0f)
+ value += chan_reverb2_value * channel_state.reverb_4[channel_state.reverb_index % 732] * 0.5f;
- sample = (int16_t) (32767.99f * this->getSampleForSfx(_audioState._sfxChannels[channel]));
+ channel_state.reverb_2[channel_state.reverb_index % 366] = value;
+ channel_state.reverb_4[channel_state.reverb_index % 732] = value;
+ ++channel_state.reverb_index;
+
+ float value_damp1 = channel_state.damp1.run(value);
+ if (chan_damp1_value > 0.0f) value = lerp(value, value_damp1, chan_damp1_value);
+
+ float value_damp2 = channel_state.damp2.run(value);
+ if (chan_damp2_value > 0.0f) value = lerp(value, value_damp2, chan_damp2_value);
+
+ int16_t chan_sample = (int16_t)(32767.99f * std::clamp(value, -0.99f, 0.99f));
+
+ if (_memory->hwState.distort & (1 << chan))
+ {
+ chan_sample = chan_sample / 0x1000 * 0x1249;
+ }
+ else if (_memory->hwState.distort & (1 << (chan + 4)))
+ {
+ chan_sample = (chan_sample - (chan_sample < 0 ? 0x1000 : 0)) / 0x1000 * 0x1249;
+ }
+ channel_mix += chan_sample;
+ }
- // TODO: Apply hardware effects
- if (_memory->hwState.distort & (1 << channel)) {
- sample = sample / 0x1000 * 0x1249;
+ buffer[i] = (int16_t)std::clamp(channel_mix, -32767.9f, 32767.9f);
}
- return sample;
}
diff --cc source/Audio.h
index eb8f438,ba6b9f7..fa382be
--- a/source/Audio.h
+++ b/source/Audio.h
@@@ -89,15 -90,14 +90,15 @@@ enu
class Audio {
PicoRam* _memory;
audioState_t _audioState;
+ bool _paused;
void set_music_pattern(int pattern);
+ void update_sfx_state(sfx_state& cur_sfx, z8::synth_param& new_synth,
+ float freq_factor, float length, bool is_music,
+ bool can_loop, bool half_rate, double inv_frames_per_second);
+ float get_synth_sample(z8::synth_param& params);
+ void launch_sfx(int16_t sfx, int16_t chan, float offset, float length, bool is_music);
- public:
- float getSampleForSfx(rawSfxChannel &channel, float freqShift = 1.0f);
- int16_t getSampleForChannel(int channel);
- float getSampleForNote(noteChannel ¬e_channel, rawSfxChannel &parentChannel, rawSfxChannel *childChannel, note prev_note, float freqShift, bool forceRemainder);
-
public:
Audio(PicoRam* memory);
@@@ -115,7 -115,4 +116,6 @@@
void FillAudioBuffer(void *audioBuffer,size_t offset, size_t size);
void FillMonoAudioBuffer(void *audioBuffer,size_t offset, size_t size);
+
+ void setPaused(bool paused);
};
-