diff --git a/game.h b/game.h index 2954bf0..bac5351 100755 --- a/game.h +++ b/game.h @@ -101,8 +101,8 @@ static inline void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex); /** Play given sound effect (SFX). This function may or may not use the sound - samples provided in sounds.h, and it may or may not ignore the volume - parameter (which is 0 to 255). Depending on the platform the function can play + samples provided in sounds.h, and it may or may not ignore the (logarithmic) + volume parameter (0 to 255). Depending on the platform the function can play completely different samples or even e.g. just beeps. If the platform can't play sounds, this function implementation can simply be left empty. This function doesn't have to implement safety measures, the back end takes cares diff --git a/main_sdl.c b/main_sdl.c index 437c6f4..f184212 100644 --- a/main_sdl.c +++ b/main_sdl.c @@ -245,6 +245,19 @@ void SFG_playSound(uint8_t soundIndex, uint8_t volume) { uint16_t pos = audioPos; + uint8_t volumeShift = 15 - volume / 16; + + uint16_t baseLevel = AUDIO_ZERO - (0x8000 >> volumeShift); + + for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i) + { + audioBuff[pos] = mixSamples(audioBuff[pos],baseLevel + + ((SFG_GET_SFX_SAMPLE(soundIndex,i) << 8) >> volumeShift)); + + pos = (pos < SFG_SFX_SAMPLE_COUNT - 1) ? (pos + 1) : 0; + } + +/* int8_t volumeShift = volume / 16 - 7; // -7 to 8 uint16_t baseLevel = AUDIO_ZERO - (0x0001 << (volumeShift + 7)); @@ -258,6 +271,7 @@ void SFG_playSound(uint8_t soundIndex, uint8_t volume) pos = (pos < SFG_SFX_SAMPLE_COUNT - 1) ? (pos + 1) : 0; } +*/ } int main(int argc, char *argv[])