commit 57e73449101c74e382b4d0386793f04142a37e22
Author: Jon Bell <jondbell@protonmail.com>
AuthorDate: Thu May 28 14:25:00 2020 -0600
Commit: Jon Bell <jondbell@protonmail.com>
CommitDate: Thu May 28 14:25:00 2020 -0600
move audio state to PicoRam and reset when loading carts
---
source/Audio.cpp | 163 ++++++++++++++++++++++++++++---------------------------
source/Audio.h | 63 ++-------------------
source/PicoRam.h | 60 ++++++++++++++++++++
source/vm.cpp | 12 +++-
4 files changed, 157 insertions(+), 141 deletions(-)
diff --git a/source/Audio.cpp b/source/Audio.cpp
index 2d60103..71a76b5 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -1,5 +1,6 @@
#include "Audio.h"
#include "synth.h"
+#include "hostVmShared.h"
#include <string>
#include <sstream>
@@ -10,9 +11,11 @@
//playback implemenation based on zetpo 8's
//https://github.com/samhocevar/zepto8/blob/master/src/pico8/sfx.cpp
-Audio::Audio(){
+Audio::Audio(PicoRam* memory){
+ _memory = memory;
+
for(int i = 0; i < 4; i++) {
- _sfxChannels[i].sfxId = -1;
+ _memory->_sfxChannels[i].sfxId = -1;
}
}
@@ -43,7 +46,7 @@ void Audio::setMusic(std::string musicString){
buf[1] = line[10];
uint8_t channel4byte = (uint8_t)strtol(buf, NULL, 16);
- _songs[musicIdx++] = {
+ _memory->_songs[musicIdx++] = {
flagByte,
channel1byte,
channel2byte,
@@ -77,10 +80,10 @@ void Audio::setSfx(std::string sfxString) {
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;
+ _memory->_sfx[sfxIdx].editorMode = editorMode;
+ _memory->_sfx[sfxIdx].speed = noteDuration;
+ _memory->_sfx[sfxIdx].loopRangeStart = loopRangeStart;
+ _memory->_sfx[sfxIdx].loopRangeEnd = loopRangeEnd;
//32 notes, 5 chars each
int noteIdx = 0;
@@ -101,7 +104,7 @@ void Audio::setSfx(std::string sfxString) {
buf[1] = line[i + 4];
uint8_t effect = (uint8_t)strtol(buf, NULL, 16);
- _sfx[sfxIdx].notes[noteIdx++] = {
+ _memory->_sfx[sfxIdx].notes[noteIdx++] = {
key,
waveform,
volume,
@@ -123,14 +126,14 @@ void Audio::api_sfx(uint8_t sfx, int channel, int offset){
{
// Stop playing the current channel
if (channel != -1) {
- _sfxChannels[channel].sfxId = -1;
+ _memory->_sfxChannels[channel].sfxId = -1;
}
}
else if (sfx == -2)
{
// Stop looping the current channel
if (channel != -1) {
- _sfxChannels[channel].can_loop = false;
+ _memory->_sfxChannels[channel].can_loop = false;
}
}
else
@@ -142,8 +145,8 @@ void Audio::api_sfx(uint8_t sfx, int channel, int offset){
if (channel == -1)
{
for (int i = 0; i < 4; ++i)
- if (_sfxChannels[i].sfxId == -1 ||
- _sfxChannels[i].sfxId == sfx)
+ if (_memory->_sfxChannels[i].sfxId == -1 ||
+ _memory->_sfxChannels[i].sfxId == sfx)
{
channel = i;
break;
@@ -155,7 +158,7 @@ void Audio::api_sfx(uint8_t sfx, int channel, int offset){
if (channel == -1)
{
for (int i = 0; i < 4; ++i) {
- if (channel == -1 || _sfxChannels[i].sfxId < _sfxChannels[channel].sfxId) {
+ if (channel == -1 || _memory->_sfxChannels[i].sfxId < _memory->_sfxChannels[channel].sfxId) {
channel = i;
}
}
@@ -163,23 +166,23 @@ void Audio::api_sfx(uint8_t sfx, int channel, int offset){
// Stop any channel playing the same sfx
for (int i = 0; i < 4; ++i) {
- if (_sfxChannels[i].sfxId == sfx) {
- _sfxChannels[i].sfxId = -1;
+ if (_memory->_sfxChannels[i].sfxId == sfx) {
+ _memory->_sfxChannels[i].sfxId = -1;
}
}
// Play this sound!
- _sfxChannels[channel].sfxId = sfx;
- _sfxChannels[channel].offset = std::max(0.f, (float)offset);
- _sfxChannels[channel].phi = 0.f;
- _sfxChannels[channel].can_loop = true;
- _sfxChannels[channel].is_music = false;
+ _memory->_sfxChannels[channel].sfxId = sfx;
+ _memory->_sfxChannels[channel].offset = std::max(0.f, (float)offset);
+ _memory->_sfxChannels[channel].phi = 0.f;
+ _memory->_sfxChannels[channel].can_loop = true;
+ _memory->_sfxChannels[channel].is_music = false;
// Playing an instrument starting with the note C-2 and the
// slide effect causes no noticeable pitch variation in PICO-8,
// so I assume this is the default value for “previous key”.
- _sfxChannels[channel].prev_key = 24;
+ _memory->_sfxChannels[channel].prev_key = 24;
// There is no default value for “previous volume”.
- _sfxChannels[channel].prev_vol = 0.f;
+ _memory->_sfxChannels[channel].prev_vol = 0.f;
}
}
@@ -191,39 +194,39 @@ void Audio::api_music(uint8_t pattern, int16_t fade_len, int16_t mask){
if (pattern == -1)
{
// Music will stop when fade out is finished
- _musicChannel.volume_step = fade_len <= 0 ? -FLT_MAX
- : -_musicChannel.volume * (1000.f / fade_len);
+ _memory->_musicChannel.volume_step = fade_len <= 0 ? -FLT_MAX
+ : -_memory->_musicChannel.volume * (1000.f / fade_len);
return;
}
- _musicChannel.count = 0;
- _musicChannel.mask = mask ? mask & 0xf : 0xf;
+ _memory->_musicChannel.count = 0;
+ _memory->_musicChannel.mask = mask ? mask & 0xf : 0xf;
- _musicChannel.volume = 1.f;
- _musicChannel.volume_step = 0.f;
+ _memory->_musicChannel.volume = 1.f;
+ _memory->_musicChannel.volume_step = 0.f;
if (fade_len > 0)
{
- _musicChannel.volume = 0.f;
- _musicChannel.volume_step = 1000.f / fade_len;
+ _memory->_musicChannel.volume = 0.f;
+ _memory->_musicChannel.volume_step = 1000.f / fade_len;
}
set_music_pattern(pattern);
}
void Audio::set_music_pattern(int pattern) {
- _musicChannel.pattern = pattern;
- _musicChannel.offset = 0;
+ _memory->_musicChannel.pattern = pattern;
+ _memory->_musicChannel.offset = 0;
//array to access song's channels. may be better to have this part of the struct?
uint8_t channels[] = {
- _songs[pattern].channel1,
- _songs[pattern].channel2,
- _songs[pattern].channel3,
- _songs[pattern].channel4,
+ _memory->_songs[pattern].channel1,
+ _memory->_songs[pattern].channel2,
+ _memory->_songs[pattern].channel3,
+ _memory->_songs[pattern].channel4,
};
// Find music speed; it’s the speed of the fastest sfx
- _musicChannel.master = _musicChannel.speed = -1;
+ _memory->_musicChannel.master = _memory->_musicChannel.speed = -1;
for (int i = 0; i < 4; ++i)
{
uint8_t n = channels[i];
@@ -231,31 +234,31 @@ void Audio::set_music_pattern(int pattern) {
if (n & 0x40)
continue;
- auto &sfx = _sfx[n & 0x3f];
- if (_musicChannel.master == -1 || _musicChannel.speed > sfx.speed)
+ auto &sfx = _memory->_sfx[n & 0x3f];
+ if (_memory->_musicChannel.master == -1 || _memory->_musicChannel.speed > sfx.speed)
{
- _musicChannel.master = i;
- _musicChannel.speed = std::max(1, (int)sfx.speed);
+ _memory->_musicChannel.master = i;
+ _memory->_musicChannel.speed = std::max(1, (int)sfx.speed);
}
}
// Play music sfx on active channels
for (int i = 0; i < 4; ++i)
{
- if (((1 << i) & _musicChannel.mask) == 0)
+ if (((1 << i) & _memory->_musicChannel.mask) == 0)
continue;
uint8_t n = channels[i];
if (n & 0x40)
continue;
- _sfxChannels[i].sfxId = n;
- _sfxChannels[i].offset = 0.f;
- _sfxChannels[i].phi = 0.f;
- _sfxChannels[i].can_loop = false;
- _sfxChannels[i].is_music = true;
- _sfxChannels[i].prev_key = 24;
- _sfxChannels[i].prev_vol = 0.f;
+ _memory->_sfxChannels[i].sfxId = n;
+ _memory->_sfxChannels[i].offset = 0.f;
+ _memory->_sfxChannels[i].phi = 0.f;
+ _memory->_sfxChannels[i].can_loop = false;
+ _memory->_sfxChannels[i].is_music = true;
+ _memory->_sfxChannels[i].prev_key = 24;
+ _memory->_sfxChannels[i].prev_vol = 0.f;
}
}
@@ -292,43 +295,43 @@ int16_t Audio::getSampleForChannel(int channel){
int16_t sample = 0;
- const int index = _sfxChannels[channel].sfxId;
+ const int index = _memory->_sfxChannels[channel].sfxId;
// Advance music using the master channel
- if (channel == _musicChannel.master && _musicChannel.pattern != -1)
+ if (channel == _memory->_musicChannel.master && _memory->_musicChannel.pattern != -1)
{
- float const offset_per_second = 22050.f / (183.f * _musicChannel.speed);
+ float const offset_per_second = 22050.f / (183.f * _memory->_musicChannel.speed);
float const offset_per_sample = offset_per_second / samples_per_second;
- _musicChannel.offset += offset_per_sample;
- _musicChannel.volume += _musicChannel.volume_step / samples_per_second;
- _musicChannel.volume = std::clamp(_musicChannel.volume, 0.f, 1.f);
+ _memory->_musicChannel.offset += offset_per_sample;
+ _memory->_musicChannel.volume += _memory->_musicChannel.volume_step / samples_per_second;
+ _memory->_musicChannel.volume = std::clamp(_memory->_musicChannel.volume, 0.f, 1.f);
- if (_musicChannel.volume_step < 0 && _musicChannel.volume <= 0)
+ if (_memory->_musicChannel.volume_step < 0 && _memory->_musicChannel.volume <= 0)
{
// Fade out is finished, stop playing the current song
for (int i = 0; i < 4; ++i) {
- if (_sfxChannels[i].is_music) {
- _sfxChannels[i].sfxId = -1;
+ if (_memory->_sfxChannels[i].is_music) {
+ _memory->_sfxChannels[i].sfxId = -1;
}
}
- _musicChannel.pattern = -1;
+ _memory->_musicChannel.pattern = -1;
}
- else if (_musicChannel.offset >= 32.f)
+ else if (_memory->_musicChannel.offset >= 32.f)
{
- int16_t next_pattern = _musicChannel.pattern + 1;
- int16_t next_count = _musicChannel.count + 1;
+ int16_t next_pattern = _memory->_musicChannel.pattern + 1;
+ int16_t next_count = _memory->_musicChannel.count + 1;
//todo: pull out these flags, get memory storage correct as well
- if (BITMASK(2) & _songs[_musicChannel.pattern].loop) //stop part of the loop flag
+ if (BITMASK(2) & _memory->_songs[_memory->_musicChannel.pattern].loop) //stop part of the loop flag
{
next_pattern = -1;
- next_count = _musicChannel.count;
+ next_count = _memory->_musicChannel.count;
}
- else if (BITMASK(1) &_songs[_musicChannel.pattern].loop){
- while (--next_pattern > 0 && !(BITMASK(0) &_songs[next_pattern].loop))
+ else if (BITMASK(1) &_memory->_songs[_memory->_musicChannel.pattern].loop){
+ while (--next_pattern > 0 && !(BITMASK(0) &_memory->_songs[next_pattern].loop))
;
}
- _musicChannel.count = next_count;
+ _memory->_musicChannel.count = next_count;
set_music_pattern(next_pattern);
}
}
@@ -338,13 +341,13 @@ int16_t Audio::getSampleForChannel(int channel){
return 0;
}
- struct sfx const &sfx = _sfx[index];
+ struct sfx const &sfx = _memory->_sfx[index];
// Speed must be 1—255 otherwise the SFX is invalid
int const speed = max(1, (int)sfx.speed);
- float const offset = _sfxChannels[channel].offset;
- float const phi = _sfxChannels[channel].phi;
+ float const offset = _memory->_sfxChannels[channel].offset;
+ float const phi = _memory->_sfxChannels[channel].phi;
// 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
@@ -355,7 +358,7 @@ int16_t Audio::getSampleForChannel(int channel){
// 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 && _sfxChannels[channel].can_loop) {
+ if (loop_range > 0.f && next_offset >= sfx.loopRangeStart && _memory->_sfxChannels[channel].can_loop) {
next_offset = fmod(next_offset - sfx.loopRangeStart, loop_range)
+ sfx.loopRangeStart;
}
@@ -369,14 +372,14 @@ int16_t Audio::getSampleForChannel(int channel){
if (volume == 0.f){
//volume all the way off. return silence, but make sure to set stuff
- _sfxChannels[channel].offset = next_offset;
+ _memory->_sfxChannels[channel].offset = next_offset;
if (next_offset >= 32.f){
- _sfxChannels[channel].sfxId = -1;
+ _memory->_sfxChannels[channel].sfxId = -1;
}
else if (next_note_idx != note_idx){
- _sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
- _sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
+ _memory->_sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
+ _memory->_sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
}
return 0;
@@ -401,16 +404,16 @@ int16_t Audio::getSampleForChannel(int channel){
// sample = sample / 0x1000 * 0x1249;
//}
- _sfxChannels[channel].phi = phi + freq / samples_per_second;
+ _memory->_sfxChannels[channel].phi = phi + freq / samples_per_second;
- _sfxChannels[channel].offset = next_offset;
+ _memory->_sfxChannels[channel].offset = next_offset;
if (next_offset >= 32.f){
- _sfxChannels[channel].sfxId = -1;
+ _memory->_sfxChannels[channel].sfxId = -1;
}
else if (next_note_idx != note_idx){
- _sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
- _sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
+ _memory->_sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
+ _memory->_sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
}
return sample;
diff --git a/source/Audio.h b/source/Audio.h
index e3e6801..e438f44 100644
--- a/source/Audio.h
+++ b/source/Audio.h
@@ -1,5 +1,7 @@
#pragma once
+#include "PicoRam.h"
+
#include <string>
#define MAX_SFX = 64
@@ -70,74 +72,17 @@ note is encoded in 16 bits, LSB first, like so:
//is easier for me to wrap my mind araound
//this is also defined in audio? should probably consolidate
-#define BITMASK(n) (1U<<(n))
-
-struct song {
- uint8_t loop;
-
- uint8_t channel1;
- uint8_t channel2;
- uint8_t channel3;
- uint8_t channel4;
-
-};
-
-struct note {
- uint8_t key;
- 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];
- song _songs[64];
-
- //uint8_t _sfxRam[64 * 68];
- sfx _sfx[64];
-
- musicChannel _musicChannel;
- sfxChannel _sfxChannels[4];
+ PicoRam* _memory;
int16_t getSampleForChannel(int channel);
void set_music_pattern(int pattern);
public:
- Audio();
+ Audio(PicoRam* memory);
void setSfx(std::string sfxString);
void setMusic(std::string musicString);
diff --git a/source/PicoRam.h b/source/PicoRam.h
index eb8f2c0..485cef6 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -1,3 +1,5 @@
+#pragma once
+
#include <string>
/*
@@ -15,12 +17,70 @@
0x6000 0x7fff Screen data (8k)
*/
+struct song {
+ uint8_t loop;
+
+ uint8_t channel1;
+ uint8_t channel2;
+ uint8_t channel3;
+ uint8_t channel4;
+
+};
+
+struct note {
+ uint8_t key;
+ 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;
+};
+
+
struct PicoRam
{
uint8_t spriteSheetData[128 * 64];
uint8_t mapData[128 * 32];
uint8_t spriteFlags[256];
+ //uint8_t _musicRam[64 * 4];
+ song _songs[64];
+
+ //uint8_t _sfxRam[64 * 68];
+ sfx _sfx[64];
+
+ musicChannel _musicChannel;
+ sfxChannel _sfxChannels[4];
+
+
uint8_t _gfxState_color;
uint8_t _gfxState_bgColor;
diff --git a/source/vm.cpp b/source/vm.cpp
index d2ba1e9..f4748e8 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -37,7 +37,7 @@ Vm::Vm(){
Input* input = new Input();
_input = input;
- Audio* audio = new Audio();
+ Audio* audio = new Audio(&_memory);
_audio = audio;
//this can probably go away when I'm loading actual carts and just have to expose api to lua
@@ -58,12 +58,20 @@ Vm::~Vm(){
bool Vm::loadCart(Cart* cart) {
_picoFrameCount = 0;
-
+
+ //reset memory (may have to be more selective about zeroing out to be accurate?)
_memory = {0};
+ //set graphics state
_memory._gfxState_color = 7;
_graphics->clip();
_graphics->pal();
+ //reset audio
+ for(int i = 0; i < 4; i++) {
+ _memory._sfxChannels[i].sfxId = -1;
+ }
+ _memory._musicChannel.pattern = -1;
+
_graphics->setSpriteSheet(cart->SpriteSheetString);
_graphics->setSpriteFlags(cart->SpriteFlagsString);