commit 0363b6edf4ef773b801b0fbb9d75812c78ea85b6
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Wed Aug 11 08:11:51 2021 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Wed Aug 11 08:11:51 2021 -0600
Port audio effects over from zepto8
---
source/Audio.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++------
source/Audio.h | 12 ++++++++++
source/PicoRam.h | 6 ++++-
source/mathhelpers.cpp | 6 ++++-
source/mathhelpers.h | 5 +++-
test/vmtests.cpp | 9 ++++++--
6 files changed, 89 insertions(+), 12 deletions(-)
diff --git a/source/Audio.cpp b/source/Audio.cpp
index 5319303..4bf6a10 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -337,23 +337,72 @@ int16_t Audio::getSampleForChannel(int channel){
}
//TODO: apply effects
- //int const fx = sfx.notes[note_id].effect;
+ int const fx = sfx.notes[note_idx].getEffect();
+
+ // Apply effect, if any
+ switch (fx)
+ {
+ case FX_NO_EFFECT:
+ break;
+ case FX_SLIDE:
+ {
+ float t = fmod(offset, 1.f);
+ // 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(_audioState._sfxChannels[channel].prev_key), freq, t);
+ if (_audioState._sfxChannels[channel].prev_vol > 0.f)
+ volume = lerp(_audioState._sfxChannels[channel].prev_vol, volume, t);
+ 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 * offset / 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 *= fmod(offset, 1.f);
+ break;
+ case FX_FADE_OUT:
+ volume *= 1.f - fmod(offset, 1.f);
+ 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;
+ }
+ }
+
// Play note
float waveform = z8::synth::waveform(sfx.notes[note_idx].getWaveform(), phi);
// Apply master music volume from fade in/out
// FIXME: check whether this should be done after distortion
- //if (_sfxChannels[chan].is_music) {
- // volume *= _musicChannel.volume;
- //}
+ if (_audioState._sfxChannels[channel].is_music) {
+ volume *= _audioState._musicChannel.volume;
+ }
sample = (int16_t)(32767.99f * volume * waveform);
// TODO: Apply hardware effects
- //if (m_ram.hw_state.distort & (1 << chan)) {
- // sample = sample / 0x1000 * 0x1249;
- //}
+ if (_memory->hwState.distort & (1 << channel)) {
+ sample = sample / 0x1000 * 0x1249;
+ }
_audioState._sfxChannels[channel].phi = phi + freq / samples_per_second;
diff --git a/source/Audio.h b/source/Audio.h
index 8ca851d..a0695e1 100644
--- a/source/Audio.h
+++ b/source/Audio.h
@@ -73,6 +73,18 @@ note is encoded in 16 bits, LSB first, like so:
//this is also defined in audio? should probably consolidate
+enum
+{
+ FX_NO_EFFECT = 0,
+ FX_SLIDE = 1,
+ FX_VIBRATO = 2,
+ FX_DROP = 3,
+ FX_FADE_IN = 4,
+ FX_FADE_OUT = 5,
+ FX_ARP_FAST = 6,
+ FX_ARP_SLOW = 7,
+};
+
class Audio {
PicoRam* _memory;
diff --git a/source/PicoRam.h b/source/PicoRam.h
index ec090ec..401ea91 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -195,7 +195,11 @@ struct drawState_t {
struct hwState_t {
- uint8_t audioHardwareState[4];
+ //audio hardware mods
+ uint8_t half_rate;
+ uint8_t reverb;
+ uint8_t distort;
+ uint8_t lowpass;
uint32_t rngState[2];
diff --git a/source/mathhelpers.cpp b/source/mathhelpers.cpp
index ffe6201..bb2e53b 100644
--- a/source/mathhelpers.cpp
+++ b/source/mathhelpers.cpp
@@ -9,4 +9,8 @@ int clamp (int val, int lo, int hi) {
float clamp (float val, float lo, float hi) {
val = (val > hi) ? hi : val;
return (val < lo) ? lo : val;
-}
\ No newline at end of file
+}
+
+float lerp(float a, float b, float t) {
+ return (b - a) * t + a;
+}
diff --git a/source/mathhelpers.h b/source/mathhelpers.h
index 9829276..9084905 100644
--- a/source/mathhelpers.h
+++ b/source/mathhelpers.h
@@ -3,4 +3,7 @@
//std::clamp is in c++17, but not c++11
int clamp (int val, int lo, int hi);
-float clamp (float val, float lo, float hi);
\ No newline at end of file
+float clamp (float val, float lo, float hi);
+
+float lerp (float a, float b, float t);
+
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index cf7ea7b..4be3221 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -244,10 +244,15 @@ TEST_CASE("Vm memory functions") {
CHECK_EQ(memory->drawState.line_y, 262);
}
- SUBCASE("poking audio hardware state") {
+ SUBCASE("poking audio hardware state half rate") {
vm->vm_poke(0x5f40, 3);
- CHECK_EQ(memory->hwState.audioHardwareState[0], 3);
+ CHECK_EQ(memory->hwState.half_rate, 3);
+ }
+ SUBCASE("poking audio hardware state distort") {
+ vm->vm_poke(0x5f42, 5);
+
+ CHECK_EQ(memory->hwState.distort, 5);
}
SUBCASE("poking rng state") {
//rng gets seeded, we need to zero it out to test this easier