Add pokitto sound

This commit is contained in:
Miloslav Číž 2020-02-27 20:18:35 +01:00
parent 1ca9e371e4
commit 2d9cbe7997
3 changed files with 64 additions and 7 deletions

7
main.c
View File

@ -2436,7 +2436,7 @@ void SFG_gameStepPlaying()
{ {
case SFG_WEAPON_KNIFE: sound = 255; break; case SFG_WEAPON_KNIFE: sound = 255; break;
case SFG_WEAPON_ROCKET_LAUNCHER: 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_PLASMAGUN:
case SFG_WEAPON_SOLUTION: sound = 4; break; case SFG_WEAPON_SOLUTION: sound = 4; break;
default: sound = 0; break; default: sound = 0; break;
@ -2445,11 +2445,6 @@ void SFG_gameStepPlaying()
if (sound != 255) if (sound != 255)
SFG_playSoundSafe(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) if (ammo != SFG_AMMO_NONE)
SFG_player.ammo[ammo] -= projectileCount; SFG_player.ammo[ammo] -= projectileCount;

View File

@ -1,7 +1,8 @@
/** /**
@file platform_pokitto.h @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 by Miloslav Ciz (drummyfish), 2019
@ -44,8 +45,12 @@
#define SFG_RAYCASTING_SUBSAMPLE 2 #define SFG_RAYCASTING_SUBSAMPLE 2
#include "Pokitto.h" #include "Pokitto.h"
#include "clock_11u6x.h"
#include "timer_11u6x.h"
#include "palette.h" #include "palette.h"
#include "sounds.h"
Pokitto::Core pokitto; Pokitto::Core pokitto;
uint8_t *pokittoScreen; uint8_t *pokittoScreen;
@ -85,13 +90,66 @@ void SFG_getMouseOffset(int16_t *x, int16_t *y)
*y = 0; *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) 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() int main()
{ {
pokitto.begin(); pokitto.begin();
timerInit(8000);
for (uint16_t i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i)
audioBuff[i] = 127;
pokitto.setFrameRate(255); pokitto.setFrameRate(255);
pokitto.display.setFont(fontTiny); pokitto.display.setFont(fontTiny);
@ -115,3 +173,4 @@ int main()
} }
#endif // guard #endif // guard

View File

@ -22,6 +22,9 @@
#define SFG_SFX_SAMPLE_COUNT 2048 #define SFG_SFX_SAMPLE_COUNT 2048
#define SFG_SFX_SIZE (SFG_SFX_SAMPLE_COUNT / 2) #define SFG_SFX_SIZE (SFG_SFX_SAMPLE_COUNT / 2)
/**
Gets a 4-bit sound sample.
*/
#define SFG_GET_SFX_SAMPLE(soundIndex,sampleIndex) \ #define SFG_GET_SFX_SAMPLE(soundIndex,sampleIndex) \
((sampleIndex % 2 == 0) ? \ ((sampleIndex % 2 == 0) ? \
(SFG_sounds[soundIndex][sampleIndex / 2] >> 4) : \ (SFG_sounds[soundIndex][sampleIndex / 2] >> 4) : \