From 2d9cbe79972ac5218972d4e922610871e974de34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Thu, 27 Feb 2020 20:18:35 +0100 Subject: [PATCH] Add pokitto sound --- main.c | 7 +----- platform_pokitto.h | 61 +++++++++++++++++++++++++++++++++++++++++++++- sounds.h | 3 +++ 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index f05eb17..f0094bc 100755 --- a/main.c +++ b/main.c @@ -2436,7 +2436,7 @@ void SFG_gameStepPlaying() { case SFG_WEAPON_KNIFE: sound = 255; break; case SFG_WEAPON_ROCKET_LAUNCHER: - case SFG_WEAPON_SHOTGUN: sound = 2; break; + case SFG_WEAPON_SHOTGUN: sound = 2; break; case SFG_WEAPON_PLASMAGUN: case SFG_WEAPON_SOLUTION: sound = 4; break; default: sound = 0; break; @@ -2445,11 +2445,6 @@ void SFG_gameStepPlaying() if (sound != 255) SFG_playSoundSafe(sound,255); - if (SFG_player.weapon != SFG_WEAPON_KNIFE) - SFG_playSoundSafe( - (SFG_player.weapon == SFG_WEAPON_SHOTGUN || - SFG_player.weapon == SFG_WEAPON_MACHINE_GUN) ? 0 : 4,255); - if (ammo != SFG_AMMO_NONE) SFG_player.ammo[ammo] -= projectileCount; diff --git a/platform_pokitto.h b/platform_pokitto.h index d21e982..728faf3 100644 --- a/platform_pokitto.h +++ b/platform_pokitto.h @@ -1,7 +1,8 @@ /** @file platform_pokitto.h - This is Pokitto implementation of the game front end. + This is Pokitto implementation of the game front end, using the official + PokittoLib. by Miloslav Ciz (drummyfish), 2019 @@ -44,8 +45,12 @@ #define SFG_RAYCASTING_SUBSAMPLE 2 #include "Pokitto.h" +#include "clock_11u6x.h" +#include "timer_11u6x.h" #include "palette.h" +#include "sounds.h" + Pokitto::Core pokitto; uint8_t *pokittoScreen; @@ -85,13 +90,66 @@ void SFG_getMouseOffset(int16_t *x, int16_t *y) *y = 0; } +uint8_t audioBuff[SFG_SFX_SAMPLE_COUNT]; +uint16_t audioPos = 0; + +void onTimer() // for sound +{ + if (Chip_TIMER_MatchPending(LPC_TIMER32_0, 1)) + { + Chip_TIMER_ClearMatch(LPC_TIMER32_0, 1); + + Pokitto::dac_write(audioBuff[audioPos]); + + audioBuff[audioPos] = 127; + + audioPos = (audioPos + 1) % SFG_SFX_SAMPLE_COUNT; + } +} + +void timerInit(uint32_t samplingRate) +{ + Chip_TIMER_Init(LPC_TIMER32_0); + Chip_TIMER_Reset(LPC_TIMER32_0); + Chip_TIMER_MatchEnableInt(LPC_TIMER32_0, 1); + Chip_TIMER_SetMatch(LPC_TIMER32_0, 1, + (Chip_Clock_GetSystemClockRate() / samplingRate)); + Chip_TIMER_ResetOnMatchEnable(LPC_TIMER32_0, 1); + Chip_TIMER_Enable(LPC_TIMER32_0); + + #define weirdNumber ((IRQn_Type) 18) + NVIC_ClearPendingIRQ(weirdNumber); + NVIC_SetVector(weirdNumber, (uint32_t) &onTimer); + NVIC_EnableIRQ(weirdNumber); + #undef weirdNumber +} + void SFG_playSound(uint8_t soundIndex, uint8_t volume) { + uint8_t volumeStep = volume / 16; + + uint16_t pos = audioPos; + + for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i) + { + int16_t mixedValue = + audioBuff[pos] - 127 + SFG_GET_SFX_SAMPLE(soundIndex,i) * volumeStep; + + mixedValue = (mixedValue > 0) ? ((mixedValue < 255) ? mixedValue : 255) : 0; + + audioBuff[pos] = mixedValue;// SFG_GET_SFX_SAMPLE(soundIndex,i) * volumeStep; + + pos = (pos < SFG_SFX_SAMPLE_COUNT - 1) ? (pos + 1) : 0; + } } int main() { pokitto.begin(); + timerInit(8000); + + for (uint16_t i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i) + audioBuff[i] = 127; pokitto.setFrameRate(255); pokitto.display.setFont(fontTiny); @@ -115,3 +173,4 @@ int main() } #endif // guard + diff --git a/sounds.h b/sounds.h index d311f03..fcc5f69 100644 --- a/sounds.h +++ b/sounds.h @@ -22,6 +22,9 @@ #define SFG_SFX_SAMPLE_COUNT 2048 #define SFG_SFX_SIZE (SFG_SFX_SAMPLE_COUNT / 2) +/** + Gets a 4-bit sound sample. +*/ #define SFG_GET_SFX_SAMPLE(soundIndex,sampleIndex) \ ((sampleIndex % 2 == 0) ? \ (SFG_sounds[soundIndex][sampleIndex / 2] >> 4) : \