foxygit / fake-08-xbox360 Log in
commit b8e5c245d0146ec4cd7a7e1108941e67706515bb
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Fri May 1 17:50:58 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Fri May 1 17:50:58 2020 -0600

    Parse audio data from cart and store
---
 source/Audio.cpp   | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++
 source/Audio.h     | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 source/console.cpp |   7 +++
 source/console.h   |   2 +
 4 files changed, 284 insertions(+)

diff --git a/source/Audio.cpp b/source/Audio.cpp
new file mode 100644
index 0000000..82f3ead
--- /dev/null
+++ b/source/Audio.cpp
@@ -0,0 +1,136 @@
+#include "Audio.h"
+
+#include <string>
+#include <sstream>
+
+//playback implemenation based on zetpo 8's
+//https://github.com/samhocevar/zepto8/blob/master/src/pico8/sfx.cpp
+
+void Audio::setMusic(std::string musicString){
+    std::istringstream s(musicString);
+    std::string line;
+    char buf[3] = {0};
+    int musicIdx = 0;
+
+    while (std::getline(s, line)) {
+        buf[0] = line[0];
+        buf[1] = line[1];
+        uint8_t flagByte = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[3];
+        buf[1] = line[4];
+        uint8_t channel1byte = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[5];
+        buf[1] = line[6];
+        uint8_t channel2byte = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[7];
+        buf[1] = line[8];
+        uint8_t channel3byte = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[9];
+        buf[1] = line[10];
+        uint8_t channel4byte = (uint8_t)strtol(buf, NULL, 16);
+
+        _music[musicIdx++] = {
+            flagByte,
+            channel1byte,
+            channel2byte,
+            channel3byte,
+            channel4byte
+        };
+    }
+
+}
+
+void Audio::setSfx(std::string sfxString) {
+    std::istringstream s(sfxString);
+    std::string line;
+    char buf[3] = {0};
+    int sfxIdx = 0;
+
+    while (std::getline(s, line)) {
+        buf[0] = line[0];
+        buf[1] = line[1];
+        uint8_t editorMode = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[2];
+        buf[1] = line[3];
+        uint8_t noteDuration = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[4];
+        buf[1] = line[5];
+        uint8_t loopRangeStart = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[6];
+        buf[1] = line[7];
+        uint8_t loopRangeEnd = (uint8_t)strtol(buf, NULL, 16);
+
+        _sfx[sfxIdx].editorMode = editorMode;
+        _sfx[sfxIdx].speed = noteDuration;
+        _sfx[sfxIdx].loopRangeStart = loopRangeStart;
+        _sfx[sfxIdx].loopRangeEnd = loopRangeEnd;
+
+        //32 notes, 5 chars each
+        int noteIdx = 0;
+        for (int i = 8; i < 168; i+=5) {
+            buf[0] = line[i];
+            buf[1] = line[i + 1];
+            uint8_t pitch = (uint8_t)strtol(buf, NULL, 16);
+
+            buf[0] = '0';
+            buf[1] = line[i + 2];
+            uint8_t waveform = (uint8_t)strtol(buf, NULL, 16);
+
+            buf[0] = '0';
+            buf[1] = line[i + 3];
+            uint8_t volume = (uint8_t)strtol(buf, NULL, 16);
+
+            buf[0] = '0';
+            buf[1] = line[i + 4];
+            uint8_t effect = (uint8_t)strtol(buf, NULL, 16);
+
+            _sfx[sfxIdx++].notes[noteIdx] = {
+                pitch,
+                waveform,
+                volume,
+                effect
+            };
+        }
+
+        sfxIdx++;
+    }
+}
+
+void Audio::api_sfx(uint8_t sfx, int channel, int offset){
+
+    if (channel < 0 ||channel > 3) {
+        //todo: handle these options later
+        return;
+    }
+
+    this->_sfxChannels[channel].sfxId = sfx;
+    this->_sfxChannels[channel].offset = std::max(0.f, (float)offset);
+    this->_sfxChannels[channel].phi = 0.f;
+    this->_sfxChannels[channel].can_loop = true;
+    this->_sfxChannels[channel].is_music = false;
+    this->_sfxChannels[channel].prev_key = 24;
+    this->_sfxChannels[channel].prev_vol = 0.f;
+
+}
+
+void Audio::api_music(uint8_t pattern, int16_t fade_len, int16_t mask){
+    if (pattern < 0 || pattern > 63) {
+        return;
+    }
+
+    this->_musicChannel.count = 0;
+
+    //todo: come back to this after sfx working
+
+    this->_musicChannel.pattern = pattern;
+    this->_musicChannel.offset = 0;
+
+
+}
diff --git a/source/Audio.h b/source/Audio.h
new file mode 100644
index 0000000..acd6ef9
--- /dev/null
+++ b/source/Audio.h
@@ -0,0 +1,139 @@
+#pragma once
+
+#include <string>
+
+#define MAX_SFX = 64
+#define BYTES_PER_SFX = 68;
+
+#define MAX_MUSIC = 64
+#define BYTES_PER_MUSIC = 4;
+
+//best info about sfx/music and differences between cart and memory that I've found
+//https://github.com/dansanderson/picotool:
+/*
+
+MUSIC:
+The music region consists of 256 bytes. The .p8 representation is one
+line for each of 64 patterns, with a hex-encoded flags byte, a space,
+and four hex-encoded one-byte sound numbers.
+The flags are:
+ 1: begin pattern loop
+ 2: end pattern loop
+ 4: stop at end of pattern
+The sound numbers represents four channels played simultaneously up to
+the shortest pattern. The sounds are numbered 0 through 63
+(0x00-0x3f). If a channel is silent for a song pattern, its number is
+64 + the channel number (0x41, 0x42, 0x43, or 0x44).
+The in-memory (and PNG) representation is slightly different from the
+.p8 representation. Instead of storing the flags in a separate byte,
+the flags are stored in the highest bit of the first three channels,
+for a total of four bytes:
+ chan1 & 128: stop at end of pattern
+ chan2 & 128: end pattern loop
+ chan3 & 128: begin pattern loop
+
+SFX:
+ The sound effects region consists of 4352 bytes. The .p8
+representation is 64 lines of 168 hexadecimal digits (84 bytes).
+Each line represents one sound effect/music pattern. The values are as follows:
+ 0    The editor mode: 0 for pitch mode, 1 for note entry mode.
+ 1    The note duration, in multiples of 1/128 second.
+ 2    Loop range start, as a note number (0-63).
+ 3    Loop range end, as a note number (0-63).
+4-84  32 notes:
+        0: pitch (0-63): c-0 to d#-5, chromatic scale
+        1-high: waveform (0-F):
+          0 sine, 1 triangle, 2 sawtooth, 3 long square, 4 short square,
+          5 ringing, 6 noise, 7 ringing sine; 8-F are the custom instruments
+          corresponding to sfx 0-7
+        1-low: volume (0-7)
+        2-high: effect (0-7):
+          0 none, 1 slide, 2 vibrato, 3 drop, 4 fade_in, 5 fade_out,
+          6 arp fast, 7 arp slow; arpeggio commands loop over groups of
+          four notes at speed 2 (fast) and 4 (slow)
+      One note uses five nibbles, so two notes use five bytes.
+The RAM representation is different. Each pattern is 68 bytes, with
+two bytes for each of 32 notes, one byte for the editor mode, one byte
+for the speed, and two bytes for the loop range (start, end). Each
+note is encoded in 16 bits, LSB first, like so:
+  w2-w1-pppppp c-eee-vvv-w3
+  eee: effect (0-7)
+  vvv: volume (0-7)
+  w3w2w1: waveform (0-7)
+  pppppp: pitch (0-63)
+  c: if 1, waveform is a custom instrument corresponding to sfx 0-7;
+    otherwise it's one of the eight built-in waveforms
+(Considering waveform as a value from 0-15, c is w4.)
+
+*/
+//todo: make these memory compatible later, but for now, this representation
+//is easier for me to wrap my mind araound
+struct music {
+    uint8_t loop;
+
+    uint8_t channel1;
+    uint8_t channel2;
+    uint8_t channel3;
+    uint8_t channel4;
+
+};
+
+struct note {
+    uint8_t pitch;
+    uint8_t waveform;
+    uint8_t volume;
+    uint8_t effect;
+};
+
+struct sfx {
+    note notes[32];
+
+    uint8_t editorMode;
+    uint8_t speed;
+    uint8_t loopRangeStart;
+    uint8_t loopRangeEnd;
+};
+
+struct musicChannel {
+    int16_t count = 0;
+    int16_t pattern = -1;
+    int8_t master = -1;
+    uint8_t mask = 0xf;
+    uint8_t speed = 0;
+    float volume = 0.f;
+    float volume_step = 0.f;
+    float offset = 0.f;
+
+};
+
+struct sfxChannel {
+    int16_t sfxId = -1;
+    float offset = 0;
+    float phi = 0;
+    bool can_loop = true;
+    bool is_music = false;
+    int8_t prev_key = 0;
+    float prev_vol = 0;
+};
+
+
+
+class Audio {
+    //uint8_t _musicRam[64 * 4];
+    music _music[64];
+
+    //uint8_t _sfxRam[64 * 68];
+    sfx _sfx[64];
+
+    musicChannel _musicChannel;
+    sfxChannel _sfxChannels[4];
+
+    public:
+
+    void setSfx(std::string sfxString);
+    void setMusic(std::string musicString);
+
+    void api_sfx(uint8_t sfx, int channel, int offset);
+    void api_music(uint8_t pattern, int16_t fade_len, int16_t mask);
+};
+
diff --git a/source/console.cpp b/source/console.cpp
index c941259..04ad84f 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -28,6 +28,9 @@ Console::Console(){
     Input* input = new Input();
     _input = input;

+    Audio* audio = new Audio();
+    _audio = audio;
+
     //this can probably go away when I'm loading actual carts and just have to expose api to lua
     Logger::Write("Initializing global api\n");
     initPicoApi(_graphics, _input, this);
@@ -39,6 +42,7 @@ Console::Console(){
 Console::~Console(){
     delete _graphics;
     delete _input;
+    delete _audio;

     if (_loadedCart){
         delete _loadedCart;
@@ -54,6 +58,9 @@ void Console::LoadCart(std::string filename){
     _graphics->setSpriteFlags(cart->SpriteFlagsString);
     _graphics->setMapData(cart->MapString);

+    _audio->setSfx(cart->SfxString);
+    _audio->setMusic(cart->MusicString);
+
     _loadedCart = cart;


diff --git a/source/console.h b/source/console.h
index df0722c..3d7d0b1 100644
--- a/source/console.h
+++ b/source/console.h
@@ -3,6 +3,7 @@

 #include "cart.h"
 #include "Input.h"
+#include "Audio.h"

 extern "C" {
   #include <lua.h>
@@ -19,6 +20,7 @@ enum StretchOption {
 class Console {
     Cart* _loadedCart;
     Graphics* _graphics;
+    Audio* _audio;
     lua_State* _luaState;
     Input* _input;