foxygit / fake-08-xbox360 Log in
commit 25f23818062185bf85c2236cbb584e425fe85f7f
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Tue Apr 27 20:42:05 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Tue Apr 27 20:42:05 2021 -0600

    Refactor how sfx are stored

    Don't use byte spanning 16 bit ints, only use single bytes
    Manually set bits when necessary
    I think this should help Big Endian systems (Wii U) have good sound
---
 source/Audio.cpp   | 14 ++++++-------
 source/PicoRam.h   | 59 ++++++++++++++++++++++++++++++++++++++++++------------
 source/cart.cpp    | 26 ++++++++++++------------
 test/carttests.cpp | 18 +++++++++--------
 test/vmtests.cpp   | 32 +++++++++++++++++++++++------
 5 files changed, 102 insertions(+), 47 deletions(-)

diff --git a/source/Audio.cpp b/source/Audio.cpp
index d7ddcef..a500f82 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -296,8 +296,8 @@ int16_t Audio::getSampleForChannel(int channel){
     int const note_idx = (int)floor(offset);
     int const next_note_idx = (int)floor(next_offset);

-    uint8_t key = sfx.notes[note_idx].key;
-    float volume = sfx.notes[note_idx].volume / 7.f;
+    uint8_t key = sfx.notes[note_idx].getKey();
+    float volume = sfx.notes[note_idx].getVolume() / 7.f;
     float freq = key_to_freq(key);

     if (volume == 0.f){
@@ -308,8 +308,8 @@ int16_t Audio::getSampleForChannel(int channel){
             _audioState._sfxChannels[channel].sfxId = -1;
         }
         else if (next_note_idx != note_idx){
-            _audioState._sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
-            _audioState._sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
+            _audioState._sfxChannels[channel].prev_key = sfx.notes[note_idx].getKey();
+            _audioState._sfxChannels[channel].prev_vol = sfx.notes[note_idx].getVolume() / 7.f;
         }

         return 0;
@@ -319,7 +319,7 @@ int16_t Audio::getSampleForChannel(int channel){
     //int const fx = sfx.notes[note_id].effect;

     // Play note
-    float waveform = z8::synth::waveform(sfx.notes[note_idx].waveform, phi);
+    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
@@ -342,8 +342,8 @@ int16_t Audio::getSampleForChannel(int channel){
         _audioState._sfxChannels[channel].sfxId = -1;
     }
     else if (next_note_idx != note_idx){
-        _audioState._sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
-        _audioState._sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
+        _audioState._sfxChannels[channel].prev_key = sfx.notes[note_idx].getKey();
+        _audioState._sfxChannels[channel].prev_vol = sfx.notes[note_idx].getVolume() / 7.f;
     }

     return sample;
diff --git a/source/PicoRam.h b/source/PicoRam.h
index d2cd586..78d57b4 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -40,19 +40,52 @@ struct song {

 //using uint16_t may be necessary since waveform spans two bytes
 struct note {
-    union
-    {
-        struct
-        {
-            uint16_t key : 6;
-            uint16_t waveform : 3;
-            uint16_t volume : 3;
-            uint16_t effect : 3;
-            uint16_t custom : 1;
-        };
-
-        uint8_t data[2];
-    };
+    uint8_t data[2];
+
+    void setKey(uint8_t val){
+        uint8_t mask = 0b00111111;
+        data[0] = (data[0] & ~mask) | (val & mask);
+    }
+    void setWaveform(uint8_t val){
+        //waveform spans both bytes
+        uint8_t val0 = val << 6;
+        uint8_t mask0 = 0b11000000;
+        data[0] = (data[0] & ~mask0) | (val0 & mask0);
+
+        uint8_t val1 = val >> 2;
+        uint8_t mask1 = 0b00000001;
+        data[1] = (data[1] & ~mask1) | (val1 & mask1);
+    }
+    void setVolume(uint8_t val){
+        uint8_t mask = 0b00001110;
+        data[1] = (data[1] & ~mask) | ((val << 1) & mask);
+    }
+    void setEffect(uint8_t val){
+        uint8_t mask = 0b01110000;
+        data[1] = (data[1] & ~mask) | ((val << 4) & mask);
+    }
+    void setCustom(uint8_t val){
+        uint8_t mask = 0b10000000;
+        data[1] = (data[1] & ~mask) | ((val << 7) & mask);
+    }
+
+
+    uint8_t getKey()const {
+        return (data[0] & 0b00111111);
+    }
+    uint8_t getWaveform()const {
+        //waveform spans both bytes
+        return ((data[1] & 0b00000001) << 2) + ((data[0] & 0b11000000) >> 6);
+    }
+    uint8_t getVolume()const {
+        return ((data[1] & 0b00001110) >> 1);
+    }
+    uint8_t getEffect()const {
+        return ((data[1] & 0b01110000) >> 4);
+    }
+    uint8_t getCustom()const {
+        return ((data[1] & 0b10000000) >> 7);
+    }
 };

 struct sfx {
diff --git a/source/cart.cpp b/source/cart.cpp
index 89715f8..d2ee0b3 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -506,29 +506,29 @@ void Cart::setSfx(std::string sfxString) {
         for (int i = 8; i < 168; i+=5) {
             buf[0] = line[i];
             buf[1] = line[i + 1];
-            uint16_t key = (uint16_t)strtol(buf, NULL, 16);
+            uint8_t key = (uint8_t)strtol(buf, NULL, 16);


             buf[0] = '0';
             buf[1] = line[i + 2];
-            uint16_t waveform = (uint16_t)strtol(buf, NULL, 16);
-            uint16_t custom = waveform > 7 ? 1 : 0;
+            uint8_t waveform = (uint8_t)strtol(buf, NULL, 16);
+            uint8_t custom = waveform > 7 ? 1 : 0;

             buf[0] = '0';
             buf[1] = line[i + 3];
-            uint16_t volume = (uint16_t)strtol(buf, NULL, 16);
+            uint8_t volume = (uint8_t)strtol(buf, NULL, 16);

             buf[0] = '0';
             buf[1] = line[i + 4];
-            uint16_t effect = (uint16_t)strtol(buf, NULL, 16);
-
-            CartRom.SfxData[sfxIdx].notes[noteIdx++] = {
-                key,
-                waveform,
-                volume,
-                effect,
-                custom
-            };
+            uint8_t effect = (uint8_t)strtol(buf, NULL, 16);
+
+            CartRom.SfxData[sfxIdx].notes[noteIdx].setKey(key);
+            CartRom.SfxData[sfxIdx].notes[noteIdx].setWaveform(waveform);
+            CartRom.SfxData[sfxIdx].notes[noteIdx].setVolume(volume);
+            CartRom.SfxData[sfxIdx].notes[noteIdx].setEffect(effect);
+            CartRom.SfxData[sfxIdx].notes[noteIdx].setCustom(custom);
+
+            noteIdx++;
         }

         sfxIdx++;
diff --git a/test/carttests.cpp b/test/carttests.cpp
index e9579e3..f0e2d07 100644
--- a/test/carttests.cpp
+++ b/test/carttests.cpp
@@ -86,12 +86,14 @@ TEST_CASE("Load simple p8 cart") {
         CHECK(cart->CartRom.SfxData[0].loopRangeEnd == 0);
         CHECK(cart->CartRom.SfxData[0].speed == 29);

-        CHECK(cart->CartRom.SfxData[0].notes[0].waveform == 0);
-        CHECK(cart->CartRom.SfxData[0].notes[0].effect == 0);
-        CHECK(cart->CartRom.SfxData[0].notes[0].key == 13);
-        CHECK(cart->CartRom.SfxData[0].notes[0].volume == 3);
         CHECK(cart->CartRom.SfxData[0].notes[0].data[0] == 13);
         CHECK(cart->CartRom.SfxData[0].notes[0].data[1] == 6);
+
+        CHECK(cart->CartRom.SfxData[0].notes[0].getWaveform() == 0);
+        CHECK(cart->CartRom.SfxData[0].notes[0].getEffect() == 0);
+        CHECK(cart->CartRom.SfxData[0].notes[0].getKey() == 13);
+        CHECK(cart->CartRom.SfxData[0].notes[0].getVolume() == 3);
+        CHECK(cart->CartRom.SfxData[0].notes[0].getCustom() == 0);
     }
     SUBCASE("Sfx data byte array is populated correctly") {
         CHECK(cart->CartRom.SfxData[2].data[0] == 139);
@@ -167,10 +169,10 @@ TEST_CASE("Load simple png cart") {
         CHECK(cart->CartRom.SfxData[0].loopRangeEnd == 0);
         CHECK(cart->CartRom.SfxData[0].speed == 29);

-        CHECK(cart->CartRom.SfxData[0].notes[0].waveform == 0);
-        CHECK(cart->CartRom.SfxData[0].notes[0].effect == 0);
-        CHECK(cart->CartRom.SfxData[0].notes[0].key == 13);
-        CHECK(cart->CartRom.SfxData[0].notes[0].volume == 3);
+        CHECK(cart->CartRom.SfxData[0].notes[0].getWaveform() == 0);
+        CHECK(cart->CartRom.SfxData[0].notes[0].getEffect() == 0);
+        CHECK(cart->CartRom.SfxData[0].notes[0].getKey() == 13);
+        CHECK(cart->CartRom.SfxData[0].notes[0].getVolume() == 3);
         CHECK(cart->CartRom.SfxData[0].notes[0].data[0] == 13);
         CHECK(cart->CartRom.SfxData[0].notes[0].data[1] == 6);
     }
diff --git a/test/vmtests.cpp b/test/vmtests.cpp
index 1108b6b..a723af2 100644
--- a/test/vmtests.cpp
+++ b/test/vmtests.cpp
@@ -97,8 +97,8 @@ TEST_CASE("Vm memory functions") {
         //waveform is next 3: (0) 01 : 1
         vm->vm_poke(0x3200, 97);

-        CHECK_EQ(memory->sfx[0].notes[0].key, 33);
-        CHECK_EQ(memory->sfx[0].notes[0].waveform, 1);
+        CHECK_EQ(memory->sfx[0].notes[0].getKey(), 33);
+        CHECK_EQ(memory->sfx[0].notes[0].getWaveform(), 1);
     }
     SUBCASE("poking general use ram"){
         vm->vm_poke(0x4300, 215);
@@ -344,10 +344,10 @@ TEST_CASE("Vm memory functions") {
             CHECK(memory->sfx[0].loopRangeEnd == 0);
             CHECK(memory->sfx[0].speed == 29);

-            CHECK(memory->sfx[0].notes[0].waveform == 0);
-            CHECK(memory->sfx[0].notes[0].effect == 0);
-            CHECK(memory->sfx[0].notes[0].key == 13);
-            CHECK(memory->sfx[0].notes[0].volume == 3);
+            CHECK(memory->sfx[0].notes[0].getWaveform() == 0);
+            CHECK(memory->sfx[0].notes[0].getEffect() == 0);
+            CHECK(memory->sfx[0].notes[0].getKey() == 13);
+            CHECK(memory->sfx[0].notes[0].getVolume() == 3);
             CHECK(memory->sfx[0].notes[0].data[0] == 13);
             CHECK(memory->sfx[0].notes[0].data[1] == 6);
         }
@@ -601,6 +601,26 @@ TEST_CASE("Vm memory functions") {

         CHECK_EQ(expected, actual);
     }
+    SUBCASE("SFX Note getters match expected values"){
+        memory->sfx[0].notes[0].data[0] = 205;
+        memory->sfx[0].notes[0].data[1] = 233;
+
+        CHECK(memory->sfx[0].notes[0].getWaveform() == 7);
+        CHECK(memory->sfx[0].notes[0].getEffect() == 6);
+        CHECK(memory->sfx[0].notes[0].getKey() == 13);
+        CHECK(memory->sfx[0].notes[0].getVolume() == 4);
+        CHECK(memory->sfx[0].notes[0].getCustom() == 1);
+    }
+    SUBCASE("SFX Note settiers set correct values"){
+        memory->sfx[0].notes[0].setWaveform(7);
+        memory->sfx[0].notes[0].setEffect(6);
+        memory->sfx[0].notes[0].setKey(13);
+        memory->sfx[0].notes[0].setVolume(4);
+        memory->sfx[0].notes[0].setCustom(1);
+
+        CHECK(memory->sfx[0].notes[0].data[0] == 205);
+        CHECK(memory->sfx[0].notes[0].data[1] == 233);
+    }

     delete stubHost;
     delete graphics;