From 98d8c47f59d0ee7cfca5d59b8fffde9ddabf79d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 19 Sep 2020 11:52:24 +0200 Subject: [PATCH] Improve sound --- TODO.txt | 1 + game.h | 10 +++++----- main_sdl.c | 40 +++++++++++++++++++++++++--------------- settings.h | 9 ++++++++- sounds.h | 6 +++--- texts.h | 2 +- 6 files changed, 43 insertions(+), 25 deletions(-) diff --git a/TODO.txt b/TODO.txt index b3b6f7b..ff2ed5a 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,5 +1,6 @@ general: +- Force monsters that are squeezed (e.g. on door) to always move. - Ability to play SFX slower to e.g. give some monsters lower pitch? - Rewrite python scripts to C (faster, less bloat). - Try to recolor textures and give them a bit more of variety. diff --git a/game.h b/game.h index b4f3d29..2954bf0 100755 --- a/game.h +++ b/game.h @@ -3509,12 +3509,12 @@ void SFG_gameStepMenu() (SFG_game.selectedMenuItem < menuItems - 1)) { SFG_game.selectedMenuItem++; - SFG_playGameSound(3,64); + SFG_playGameSound(3,SFG_MENU_CLICK_VOLUME); } else if (SFG_keyRegisters(SFG_KEY_UP) && (SFG_game.selectedMenuItem > 0)) { SFG_game.selectedMenuItem--; - SFG_playGameSound(3,64); + SFG_playGameSound(3,SFG_MENU_CLICK_VOLUME); } else if (SFG_keyJustPressed(SFG_KEY_A)) { @@ -3545,7 +3545,7 @@ void SFG_gameStepMenu() SFG_game.settings = (SFG_game.settings & ~0x03) | ((SFG_game.settings + 1) & 0x03); - SFG_playGameSound(3,64); + SFG_playGameSound(3,SFG_MENU_CLICK_VOLUME); if ((SFG_game.settings & 0x02) != ((SFG_game.settings - 1) & 0x02)) @@ -3576,12 +3576,12 @@ void SFG_gameStepMenu() (SFG_game.selectedLevel < SFG_NUMBER_OF_LEVELS - 1)) { SFG_game.selectedLevel++; - SFG_playGameSound(3,64); + SFG_playGameSound(3,SFG_MENU_CLICK_VOLUME); } else if (SFG_keyRegisters(SFG_KEY_LEFT) && SFG_game.selectedLevel > 0) { SFG_game.selectedLevel--; - SFG_playGameSound(3,64); + SFG_playGameSound(3,SFG_MENU_CLICK_VOLUME); } } } diff --git a/main_sdl.c b/main_sdl.c index c6858b2..437c6f4 100644 --- a/main_sdl.c +++ b/main_sdl.c @@ -27,11 +27,13 @@ #define SFG_LOG(str) puts(str); - #define SFG_START_LEVEL 6 +// #define SFG_START_LEVEL 6 #define SFG_IMMORTAL 1 // #define SFG_UNLOCK_DOOR 1 #define SFG_REVEAL_MAP 1 -// #define SFG_INFINITE_AMMO 1 + #define SFG_INFINITE_AMMO 1 + +#define MUSIC_VOLUME 4 #include #include @@ -207,10 +209,12 @@ typedef void (*em_callback_func)(void); void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop); #endif -uint8_t audioBuff[SFG_SFX_SAMPLE_COUNT]; +#define AUDIO_ZERO 32768 + +uint16_t audioBuff[SFG_SFX_SAMPLE_COUNT]; uint16_t audioPos = 0; -static inline uint8_t mixSamples(uint8_t sample1, uint8_t sample2) +static inline uint16_t mixSamples(uint16_t sample1, uint16_t sample2) { return (sample1 >> 1) + (sample2 >> 1); } @@ -219,13 +223,15 @@ uint8_t musicOn = 1; void audioFillCallback(void *userdata, uint8_t *s, int l) { - for (int i = 0; i < l; ++i) + uint16_t *s16 = (uint16_t *) s; + + for (int i = 0; i < l / 2; ++i) { - s[i] = musicOn ? - mixSamples(audioBuff[audioPos],SFG_getNextMusicSample() / 2) : + s16[i] = musicOn ? + mixSamples(audioBuff[audioPos],SFG_getNextMusicSample() << MUSIC_VOLUME) : audioBuff[audioPos]; - audioBuff[audioPos] = 127; + audioBuff[audioPos] = AUDIO_ZERO; audioPos = (audioPos < SFG_SFX_SAMPLE_COUNT - 1) ? (audioPos + 1) : 0; } } @@ -237,14 +243,18 @@ void SFG_enableMusic(uint8_t enable) void SFG_playSound(uint8_t soundIndex, uint8_t volume) { - uint8_t volumeStep = volume / 16; - uint16_t pos = audioPos; + int8_t volumeShift = volume / 16 - 7; // -7 to 8 + + uint16_t baseLevel = AUDIO_ZERO - (0x0001 << (volumeShift + 7)); + for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i) { - audioBuff[pos] = - mixSamples(audioBuff[pos],SFG_GET_SFX_SAMPLE(soundIndex,i) * volumeStep); + audioBuff[pos] = mixSamples(audioBuff[pos],baseLevel + + ((volumeShift >= 0) ? + (SFG_GET_SFX_SAMPLE(soundIndex,i) << volumeShift) : + (SFG_GET_SFX_SAMPLE(soundIndex,i) >> (-1 * volumeShift)))); pos = (pos < SFG_SFX_SAMPLE_COUNT - 1) ? (pos + 1) : 0; } @@ -320,15 +330,15 @@ int main(int argc, char *argv[]) audioSpec.callback = audioFillCallback; audioSpec.userdata = 0; audioSpec.freq = 8000; - audioSpec.format = AUDIO_U8; + audioSpec.format = AUDIO_U16; audioSpec.channels = 1; audioSpec.samples = 128; if (SDL_OpenAudio(&audioSpec,0) < 0) puts("SDL: could not initialize audio"); - for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i) - audioBuff[i] = 127; + for (int16_t i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i) + audioBuff[i] = AUDIO_ZERO; SDL_PauseAudio(0); diff --git a/settings.h b/settings.h index fbaa18a..543f05a 100644 --- a/settings.h +++ b/settings.h @@ -242,7 +242,7 @@ will gradually drop towards this distance. */ #ifndef SFG_SFX_MAX_DISTANCE - #define SFG_SFX_MAX_DISTANCE (1024 * 20) + #define SFG_SFX_MAX_DISTANCE (1024 * 60) #endif /** @@ -316,6 +316,13 @@ #define SFG_VERTICAL_AUTOAIM_ANGLE_THRESHOLD 50 #endif +/** + Byte (0 - 255) volume of the menu click sound. +*/ +#ifndef SFG_MENU_CLICK_VOLUME + #define SFG_MENU_CLICK_VOLUME 220 +#endif + //------ developer/debug settings ------ /** diff --git a/sounds.h b/sounds.h index 346f281..28c9493 100644 --- a/sounds.h +++ b/sounds.h @@ -26,12 +26,12 @@ #define SFG_SFX_SIZE (SFG_SFX_SAMPLE_COUNT / 2) /** - Gets a 4-bit sound sample. + Gets a 8-bit sound sample. */ #define SFG_GET_SFX_SAMPLE(soundIndex,sampleIndex) \ - ((sampleIndex % 2 == 0) ? \ + (((sampleIndex % 2 == 0) ? \ (SFG_sounds[soundIndex][sampleIndex / 2] >> 4) : \ - (SFG_sounds[soundIndex][sampleIndex / 2] & 0x0f)) + (SFG_sounds[soundIndex][sampleIndex / 2] & 0x0f)) << 4) #define SFG_TRACK_SAMPLES (512 * 1024) diff --git a/texts.h b/texts.h index b121252..b9263eb 100644 --- a/texts.h +++ b/texts.h @@ -37,7 +37,7 @@ SFG_PROGRAM_MEMORY char *SFG_introText = "and run towards Macrochip HQ."; SFG_PROGRAM_MEMORY char *SFG_outroText = - "You killed the main computer, the world is saved! Thank you my friend. We " + "You killed the main computer, the world is saved! Thank you, my friend. We " "learned a lesson, never again allow capitalism and hierarchy. We can now " "rebuild society in peaceful anarchy.";