commit e3c7b11ab8b0032878055ea1de6e1eb1cf4350fd
Author: Jon Bell <jondbell@protonmail.com>
AuthorDate: Tue May 5 08:39:39 2020 -0600
Commit: Jon Bell <jondbell@protonmail.com>
CommitDate: Tue May 5 08:39:39 2020 -0600
Fix idiotic error parsing sfx
---
source/Audio.cpp | 81 ++++++++++++++++++++++++++++++--------------------------
source/Audio.h | 2 +-
twosfx.p8 | 8 +++---
3 files changed, 48 insertions(+), 43 deletions(-)
diff --git a/source/Audio.cpp b/source/Audio.cpp
index aa93214..5c56ea2 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -5,6 +5,8 @@
#include <sstream>
#include <cmath>
+#include "logger.h"
+
//playback implemenation based on zetpo 8's
//https://github.com/samhocevar/zepto8/blob/master/src/pico8/sfx.cpp
@@ -85,7 +87,7 @@ void Audio::setSfx(std::string sfxString) {
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);
+ uint8_t key = (uint8_t)strtol(buf, NULL, 16);
buf[0] = '0';
buf[1] = line[i + 2];
@@ -99,8 +101,8 @@ void Audio::setSfx(std::string sfxString) {
buf[1] = line[i + 4];
uint8_t effect = (uint8_t)strtol(buf, NULL, 16);
- _sfx[sfxIdx++].notes[noteIdx] = {
- pitch,
+ _sfx[sfxIdx].notes[noteIdx++] = {
+ key,
waveform,
volume,
effect
@@ -143,25 +145,6 @@ void Audio::api_music(uint8_t pattern, int16_t fade_len, int16_t mask){
}
-
-
-//todo: figure out where to store this
-#define SAMPLERATE 22050
-//----------------------------------------------------------------------------
-void fill_buffer(void *audioBuffer,size_t offset, size_t size, int frequency ) {
-//----------------------------------------------------------------------------
-
- uint32_t *dest = (uint32_t*)audioBuffer;
-
- for (size_t i=0; i<size; i++) {
-
- int16_t sample = INT16_MAX * sin(frequency*(2*M_PI)*(offset+i)/SAMPLERATE);
-
- dest[i] = (sample<<16) | (sample & 0xffff);
- }
-}
-
-
void Audio::FillAudioBuffer(void *audioBuffer, size_t offset, size_t size){
uint32_t *buffer = (uint32_t *)audioBuffer;
@@ -171,10 +154,9 @@ void Audio::FillAudioBuffer(void *audioBuffer, size_t offset, size_t size){
for (int c = 0; c < 4; ++c) {
sample += this->getSampleForChannel(c);
- //int16_t sample = INT16_MAX * sin(440*(2*M_PI)*(offset+i)/SAMPLERATE);
-
-
}
+
+ //buffer is stereo, so just send the mono sample to both channels
buffer[i] = (sample<<16) | (sample & 0xffff);
}
}
@@ -185,8 +167,6 @@ static float key_to_freq(float key)
return 440.f * exp2((key - 33.f) / 12.f);
}
-
-
//adapted from zepto8 sfx.cpp (wtfpl license)
int16_t Audio::getSampleForChannel(int channel){
using std::fabs, std::fmod, std::floor, std::max;
@@ -202,19 +182,28 @@ int16_t Audio::getSampleForChannel(int channel){
return 0;
}
+ //Logger::Write("have sfx id: %d \n", index);
+
+ //printf("sfx index: %d\n", index);
struct sfx const &sfx = _sfx[index];
// Speed must be 1—255 otherwise the SFX is invalid
int const speed = max(1, (int)sfx.speed);
+ //Logger::Write("sfx speed: %d \n", speed);
+
+ //Logger::Write("channel offset: %f \n", _sfxChannels[channel].offset);
float const offset = _sfxChannels[channel].offset;
+ //Logger::Write("const offset: %f \n", offset);
float const phi = _sfxChannels[channel].phi;
+ //Logger::Write("channel phi: %f \n", 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
float const offset_per_second = 22050.f / (183.f * speed);
float const offset_per_sample = offset_per_second / samples_per_second;
float next_offset = offset + offset_per_sample;
+ //Logger::Write("next_offset: %f \n", next_offset);
// Handle SFX loops. From the documentation: “Looping is turned
// off when the start index >= end index”.
@@ -222,25 +211,35 @@ int16_t Audio::getSampleForChannel(int channel){
if (loop_range > 0.f && next_offset >= sfx.loopRangeStart && _sfxChannels[channel].can_loop) {
next_offset = fmod(next_offset - sfx.loopRangeStart, loop_range)
+ sfx.loopRangeStart;
+ //Logger::Write("looped next_offset: %f \n", next_offset);
}
- int const note_id = (int)floor(offset);
- int const next_note_id = (int)floor(next_offset);
+ int const note_idx = (int)floor(offset);
+ //Logger::Write("note_id: %d \n", note_idx);
+ int const next_note_idx = (int)floor(next_offset);
+ //Logger::Write("next_note_id: %d \n", next_note_idx);
- uint8_t key = sfx.notes[note_id].pitch;
- float volume = sfx.notes[note_id].volume / 7.f;
+ uint8_t key = sfx.notes[note_idx].key;
+ //Logger::Write("key: %d \n", key);
+ float volume = sfx.notes[note_idx].volume / 7.f;
+ //Logger::Write("volume: %f \n", volume);
float freq = key_to_freq(key);
+ //Logger::Write("freq: %f \n", freq);
+
+ //printf("Note: %d, %d, %f, %f\n", note_idx, key, volume, freq);
if (volume == 0.f){
//volume all the way off. return silence, but make sure to set stuff
_sfxChannels[channel].offset = next_offset;
if (next_offset >= 32.f){
+ Logger::Write("DONE PLAYING SFX\n");
_sfxChannels[channel].sfxId = -1;
}
- else if (next_note_id != note_id){
- _sfxChannels[channel].prev_key = sfx.notes[note_id].pitch;
- _sfxChannels[channel].prev_vol = sfx.notes[note_id].volume / 7.f;
+ else if (next_note_idx != note_idx){
+ Logger::Write("Moving on to next note: %d\n", next_note_idx);
+ _sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
+ _sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
}
return 0;
@@ -253,7 +252,8 @@ int16_t Audio::getSampleForChannel(int channel){
// Play note
- float waveform = z8::synth::waveform(sfx.notes[note_id].waveform, phi);
+ float waveform = z8::synth::waveform(sfx.notes[note_idx].waveform, phi);
+ //printf("waveform: %f\n", waveform);
// Apply master music volume from fade in/out
// FIXME: check whether this should be done after distortion
@@ -262,6 +262,7 @@ int16_t Audio::getSampleForChannel(int channel){
//}
sample = (int16_t)(32767.99f * volume * waveform);
+ //printf("sample: %d\n", sample);
// TODO: Apply hardware effects
//if (m_ram.hw_state.distort & (1 << chan)) {
@@ -270,14 +271,18 @@ int16_t Audio::getSampleForChannel(int channel){
_sfxChannels[channel].phi = phi + freq / samples_per_second;
+ //Logger::Write("next_offset before setting: %f \n", next_offset);
_sfxChannels[channel].offset = next_offset;
+ //Logger::Write("channel offset afterward: %f \n", _sfxChannels[channel].offset);
if (next_offset >= 32.f){
+ Logger::Write("DONE PLAYING SFX\n");
_sfxChannels[channel].sfxId = -1;
}
- else if (next_note_id != note_id){
- _sfxChannels[channel].prev_key = sfx.notes[note_id].pitch;
- _sfxChannels[channel].prev_vol = sfx.notes[note_id].volume / 7.f;
+ else if (next_note_idx != note_idx){
+ Logger::Write("Moving on to next note: %d\n", next_note_idx);
+ _sfxChannels[channel].prev_key = sfx.notes[note_idx].key;
+ _sfxChannels[channel].prev_vol = sfx.notes[note_idx].volume / 7.f;
}
return sample;
diff --git a/source/Audio.h b/source/Audio.h
index 05d3f7b..5a5b0bf 100644
--- a/source/Audio.h
+++ b/source/Audio.h
@@ -79,7 +79,7 @@ struct music {
};
struct note {
- uint8_t pitch;
+ uint8_t key;
uint8_t waveform;
uint8_t volume;
uint8_t effect;
diff --git a/twosfx.p8 b/twosfx.p8
index 6abf16f..4e5114e 100644
--- a/twosfx.p8
+++ b/twosfx.p8
@@ -3,10 +3,10 @@ version 22
__lua__
function _update()
- if (btn(4)) then
+ if (btnp(4)) then
sfx(0, 0)
end
- if (btn(5)) then
+ if (btnp(5)) then
sfx(1, 1)
end
@@ -26,5 +26,5 @@ __gfx__
__map__
0000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__
-000900002b75025750227501f7501c7501875014750107500c7500975005750007500000000000000000000000700000000000000000000000000000000000000000000000000000000000000000000000000000
-000400002b7601e75034750317502c75026750207501d7501d7501d7501e75025750000000000000000263001e700000000000000000000000000000000000000000000000000000000000000000000000000000
+000100003f740037401b7303c730067203272010710287100c7000970005700007000000000000000000000000700000000000000000000000000000000000000000000000000000000000000000000000000000
+00140000060700607005070050600406004060040600507005070050700507005070050700000000000263001e700000000000000000000000000000000000000000000000000000000000000000000000000000