Improve sound

This commit is contained in:
Miloslav Číž 2020-09-19 11:52:24 +02:00
parent e2b73f6d4a
commit 98d8c47f59
6 changed files with 43 additions and 25 deletions

View File

@ -1,5 +1,6 @@
general: 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? - Ability to play SFX slower to e.g. give some monsters lower pitch?
- Rewrite python scripts to C (faster, less bloat). - Rewrite python scripts to C (faster, less bloat).
- Try to recolor textures and give them a bit more of variety. - Try to recolor textures and give them a bit more of variety.

10
game.h
View File

@ -3509,12 +3509,12 @@ void SFG_gameStepMenu()
(SFG_game.selectedMenuItem < menuItems - 1)) (SFG_game.selectedMenuItem < menuItems - 1))
{ {
SFG_game.selectedMenuItem++; 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)) else if (SFG_keyRegisters(SFG_KEY_UP) && (SFG_game.selectedMenuItem > 0))
{ {
SFG_game.selectedMenuItem--; SFG_game.selectedMenuItem--;
SFG_playGameSound(3,64); SFG_playGameSound(3,SFG_MENU_CLICK_VOLUME);
} }
else if (SFG_keyJustPressed(SFG_KEY_A)) else if (SFG_keyJustPressed(SFG_KEY_A))
{ {
@ -3545,7 +3545,7 @@ void SFG_gameStepMenu()
SFG_game.settings = SFG_game.settings =
(SFG_game.settings & ~0x03) | ((SFG_game.settings + 1) & 0x03); (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) != if ((SFG_game.settings & 0x02) !=
((SFG_game.settings - 1) & 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_NUMBER_OF_LEVELS - 1))
{ {
SFG_game.selectedLevel++; 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) else if (SFG_keyRegisters(SFG_KEY_LEFT) && SFG_game.selectedLevel > 0)
{ {
SFG_game.selectedLevel--; SFG_game.selectedLevel--;
SFG_playGameSound(3,64); SFG_playGameSound(3,SFG_MENU_CLICK_VOLUME);
} }
} }
} }

View File

@ -27,11 +27,13 @@
#define SFG_LOG(str) puts(str); #define SFG_LOG(str) puts(str);
#define SFG_START_LEVEL 6 // #define SFG_START_LEVEL 6
#define SFG_IMMORTAL 1 #define SFG_IMMORTAL 1
// #define SFG_UNLOCK_DOOR 1 // #define SFG_UNLOCK_DOOR 1
#define SFG_REVEAL_MAP 1 #define SFG_REVEAL_MAP 1
// #define SFG_INFINITE_AMMO 1 #define SFG_INFINITE_AMMO 1
#define MUSIC_VOLUME 4
#include <stdio.h> #include <stdio.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
@ -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); void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop);
#endif #endif
uint8_t audioBuff[SFG_SFX_SAMPLE_COUNT]; #define AUDIO_ZERO 32768
uint16_t audioBuff[SFG_SFX_SAMPLE_COUNT];
uint16_t audioPos = 0; 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); return (sample1 >> 1) + (sample2 >> 1);
} }
@ -219,13 +223,15 @@ uint8_t musicOn = 1;
void audioFillCallback(void *userdata, uint8_t *s, int l) 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 ? s16[i] = musicOn ?
mixSamples(audioBuff[audioPos],SFG_getNextMusicSample() / 2) : mixSamples(audioBuff[audioPos],SFG_getNextMusicSample() << MUSIC_VOLUME) :
audioBuff[audioPos]; audioBuff[audioPos];
audioBuff[audioPos] = 127; audioBuff[audioPos] = AUDIO_ZERO;
audioPos = (audioPos < SFG_SFX_SAMPLE_COUNT - 1) ? (audioPos + 1) : 0; 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) void SFG_playSound(uint8_t soundIndex, uint8_t volume)
{ {
uint8_t volumeStep = volume / 16;
uint16_t pos = audioPos; 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) for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i)
{ {
audioBuff[pos] = audioBuff[pos] = mixSamples(audioBuff[pos],baseLevel +
mixSamples(audioBuff[pos],SFG_GET_SFX_SAMPLE(soundIndex,i) * volumeStep); ((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; pos = (pos < SFG_SFX_SAMPLE_COUNT - 1) ? (pos + 1) : 0;
} }
@ -320,15 +330,15 @@ int main(int argc, char *argv[])
audioSpec.callback = audioFillCallback; audioSpec.callback = audioFillCallback;
audioSpec.userdata = 0; audioSpec.userdata = 0;
audioSpec.freq = 8000; audioSpec.freq = 8000;
audioSpec.format = AUDIO_U8; audioSpec.format = AUDIO_U16;
audioSpec.channels = 1; audioSpec.channels = 1;
audioSpec.samples = 128; audioSpec.samples = 128;
if (SDL_OpenAudio(&audioSpec,0) < 0) if (SDL_OpenAudio(&audioSpec,0) < 0)
puts("SDL: could not initialize audio"); puts("SDL: could not initialize audio");
for (int i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i) for (int16_t i = 0; i < SFG_SFX_SAMPLE_COUNT; ++i)
audioBuff[i] = 127; audioBuff[i] = AUDIO_ZERO;
SDL_PauseAudio(0); SDL_PauseAudio(0);

View File

@ -242,7 +242,7 @@
will gradually drop towards this distance. will gradually drop towards this distance.
*/ */
#ifndef SFG_SFX_MAX_DISTANCE #ifndef SFG_SFX_MAX_DISTANCE
#define SFG_SFX_MAX_DISTANCE (1024 * 20) #define SFG_SFX_MAX_DISTANCE (1024 * 60)
#endif #endif
/** /**
@ -316,6 +316,13 @@
#define SFG_VERTICAL_AUTOAIM_ANGLE_THRESHOLD 50 #define SFG_VERTICAL_AUTOAIM_ANGLE_THRESHOLD 50
#endif #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 ------ //------ developer/debug settings ------
/** /**

View File

@ -26,12 +26,12 @@
#define SFG_SFX_SIZE (SFG_SFX_SAMPLE_COUNT / 2) #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) \ #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) : \
(SFG_sounds[soundIndex][sampleIndex / 2] & 0x0f)) (SFG_sounds[soundIndex][sampleIndex / 2] & 0x0f)) << 4)
#define SFG_TRACK_SAMPLES (512 * 1024) #define SFG_TRACK_SAMPLES (512 * 1024)

View File

@ -37,7 +37,7 @@ SFG_PROGRAM_MEMORY char *SFG_introText =
"and run towards Macrochip HQ."; "and run towards Macrochip HQ.";
SFG_PROGRAM_MEMORY char *SFG_outroText = 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 " "learned a lesson, never again allow capitalism and hierarchy. We can now "
"rebuild society in peaceful anarchy."; "rebuild society in peaceful anarchy.";