Continue crash sound

This commit is contained in:
Miloslav Ciz 2024-12-27 07:45:27 +01:00
parent b90f5bbfc8
commit 85b8b1352d
3 changed files with 65 additions and 64 deletions

88
audio.h
View file

@ -18,24 +18,32 @@ struct
{ {
uint32_t frame; uint32_t frame;
uint8_t soundPlayed; uint8_t soundPlayed;
uint16_t soundPlayedSample; uint16_t soundPlayedFrame;
uint32_t noise; uint32_t noise;
uint8_t crashSample;
int engineIntensity; int engineIntensity;
int engineOsc; int engineOsc;
int engineInc; int engineInc;
} LCR_audio; } LCR_audio;
void LCR_audioInit(void) void LCR_audioInit(void)
{ {
LCR_LOG0("initializing audio"); LCR_LOG0("initializing audio");
LCR_audio.frame = 0; LCR_audio.frame = 0;
LCR_audio.soundPlayed = LCR_SOUND_NONE; LCR_audio.soundPlayed = LCR_SOUND_NONE;
LCR_audio.soundPlayedSample = 0; LCR_audio.soundPlayedFrame = 0;
LCR_audio.noise = 0; LCR_audio.noise = 0;
LCR_audio.crashSample = 0;
LCR_audio.engineOsc = 0; LCR_audio.engineOsc = 0;
LCR_audio.engineInc = 1; LCR_audio.engineInc = 1;
@ -54,7 +62,7 @@ void LCR_audioPlaySound(uint8_t sound)
{ {
LCR_LOG2("playing sound"); LCR_LOG2("playing sound");
LCR_audio.soundPlayed = sound; LCR_audio.soundPlayed = sound;
LCR_audio.soundPlayedSample = 0; LCR_audio.soundPlayedFrame = 0;
} }
uint8_t _LCR_audioNoise(void) uint8_t _LCR_audioNoise(void)
@ -63,60 +71,60 @@ uint8_t _LCR_audioNoise(void)
return LCR_audio.noise >> 16; return LCR_audio.noise >> 16;
} }
uint8_t LCR_audioGetNextSample(void) uint8_t LCR_audioGetNextSample(void)
{ {
unsigned char result = 128; unsigned char result = 128;
switch (LCR_audio.soundPlayed) switch (LCR_audio.soundPlayed)
{ {
case LCR_SOUND_CRASH_SMALL: case LCR_SOUND_CRASH_SMALL:
result += (LCR_AUDIO_CRASH_LEN - LCR_audio.soundPlayedSample) * case LCR_SOUND_CRASH_BIG:
(_LCR_audioNoise() / 16) / LCR_AUDIO_CRASH_LEN; {
int limit = (LCR_audio.soundPlayed == LCR_SOUND_CRASH_BIG ? 256 : 128) -
(LCR_audio.soundPlayedFrame * 256) / LCR_AUDIO_CRASH_LEN;
if (LCR_audio.soundPlayedSample >= LCR_AUDIO_CRASH_LEN) if (LCR_audio.frame % 2 || // lower frequency
LCR_audio.soundPlayed = LCR_SOUND_NONE; LCR_audio.soundPlayedFrame < LCR_AUDIO_CRASH_LEN / 4)
LCR_audio.crashSample = _LCR_audioNoise();
break; if (LCR_audio.crashSample > limit)
LCR_audio.crashSample = (_LCR_audioNoise() % 2) ? 0 : limit;
case LCR_SOUND_CRASH_BIG: result += LCR_audio.crashSample - limit / 2;
result += ((LCR_AUDIO_CRASH_LEN * 2) - LCR_audio.soundPlayedSample) *
(_LCR_audioNoise() / 8) / (2 * LCR_AUDIO_CRASH_LEN);
if (LCR_audio.soundPlayedSample >= 2 * LCR_AUDIO_CRASH_LEN) if (limit == 0)
LCR_audio.soundPlayed = LCR_SOUND_NONE; LCR_audio.soundPlayed = LCR_SOUND_NONE;
break; break;
}
default: default:
break; break;
} }
if (LCR_audio.soundPlayed != LCR_SOUND_NONE) if (LCR_audio.soundPlayed != LCR_SOUND_NONE)
LCR_audio.soundPlayedSample++; LCR_audio.soundPlayedFrame++;
else else
{ {
LCR_audio.engineOsc += LCR_audio.engineOsc +=
LCR_audio.engineInc ? LCR_audio.engineInc ?
(((_LCR_audioNoise() % 256) < (((_LCR_audioNoise() % 256) <
(10 + LCR_audio.engineIntensity))) : -31; (10 + LCR_audio.engineIntensity))) : -31;
if (LCR_audio.engineInc && LCR_audio.engineOsc > if (LCR_audio.engineInc && LCR_audio.engineOsc >
(90 + (LCR_audio.engineIntensity / 8))) (90 + (LCR_audio.engineIntensity / 8)))
LCR_audio.engineInc = 0; LCR_audio.engineInc = 0;
else if ((!LCR_audio.engineInc) && LCR_audio.engineOsc < 10) else if ((!LCR_audio.engineInc) && LCR_audio.engineOsc < 10)
LCR_audio.engineInc = 1; LCR_audio.engineInc = 1;
result += result +=
LCR_audio.engineIntensity < 20 ? LCR_audio.engineIntensity < 20 ?
LCR_audio.engineOsc / 2 : LCR_audio.engineOsc; LCR_audio.engineOsc / 2 : LCR_audio.engineOsc;
} }
LCR_audio.frame++; LCR_audio.frame++;
return result; return result;
} }
#endif // guard #endif // guard

View file

@ -16,23 +16,14 @@ FILE *musicFile = 0;
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) if (musicFile)
{ {
unsigned char byte = 128; if (!fread(s,1,l,musicFile))
rewind(musicFile);
if (musicFile)
{
if (!fread(&byte,1,1,musicFile))
rewind(musicFile);
}
// s[i] = byte;
s[i] = byte / 2 + LCR_gameGetNextAudioSample() / 2;
//s[i] = LCR_gameGetNextAudioSample();
} }
for (int i = 0; i < l; ++i)
s[i] = s[i] / 2 + LCR_gameGetNextAudioSample() / 2;
} }
const uint8_t *keyboardState; const uint8_t *keyboardState;
@ -97,7 +88,7 @@ int main(int argc, char *argv[])
audioSpec.freq = 8000; audioSpec.freq = 8000;
audioSpec.format = AUDIO_U8; audioSpec.format = AUDIO_U8;
audioSpec.channels = 1; audioSpec.channels = 1;
audioSpec.samples = 256; audioSpec.samples = 64;
if (SDL_OpenAudio(&audioSpec,NULL) < 0) if (SDL_OpenAudio(&audioSpec,NULL) < 0)
fputs("could not initialize audio",stderr); fputs("could not initialize audio",stderr);

View file

@ -45,7 +45,10 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_CAR_DRIFT_THRESHOLD_1 (LCR_GAME_UNIT / 4) #define LCR_CAR_DRIFT_THRESHOLD_1 (LCR_GAME_UNIT / 4)
#define LCR_CAR_DRIFT_THRESHOLD_0 (LCR_GAME_UNIT / 200) #define LCR_CAR_DRIFT_THRESHOLD_0 (LCR_GAME_UNIT / 200)
#define LCR_CAR_CRASH_SPEED_THRESHOLD 25
#define LCR_CAR_CRASH_SPEED_DIFF 25
#define LCR_CAR_CRASH_SPEED_THRESHOLD 70
// multipliers (in 8ths) of friction and acceleration on concrete: // multipliers (in 8ths) of friction and acceleration on concrete:
#define LCR_CAR_GRASS_FACTOR 5 #define LCR_CAR_GRASS_FACTOR 5
@ -1077,7 +1080,9 @@ uint32_t LCR_racingStep(unsigned int input)
TPE_worldStep(&(LCR_racing.physicsWorld)); TPE_worldStep(&(LCR_racing.physicsWorld));
LCR_LOG2("stepping physics engine done"); LCR_LOG2("stepping physics engine done");
int speedDiff = LCR_racing.carSpeeds[0] - LCR_racing.carSpeeds[1]; int speedDiff =
TPE_abs(LCR_racing.carSpeeds[0]) -
TPE_abs(LCR_racing.carSpeeds[1]);
LCR_racing.carSpeeds[1] = LCR_racing.carSpeeds[0]; LCR_racing.carSpeeds[1] = LCR_racing.carSpeeds[0];
@ -1087,14 +1092,11 @@ uint32_t LCR_racingStep(unsigned int input)
if (TPE_vec3Dot(carVel,carForw) < 0) if (TPE_vec3Dot(carVel,carForw) < 0)
LCR_racing.carSpeeds[0] *= -1; LCR_racing.carSpeeds[0] *= -1;
else if (speedDiff < -1 * LCR_CAR_CRASH_SPEED_THRESHOLD) if (speedDiff < -1 * LCR_CAR_CRASH_SPEED_DIFF &&
{ TPE_abs(LCR_racing.carSpeeds[0]) <= LCR_CAR_CRASH_SPEED_THRESHOLD)
result |= (speedDiff < -2 * LCR_CAR_CRASH_SPEED_THRESHOLD) ? result |= (speedDiff < -2 * LCR_CAR_CRASH_SPEED_DIFF) ?
LCR_RACING_EVENT_CRASH_BIG : LCR_RACING_EVENT_CRASH_SMALL; LCR_RACING_EVENT_CRASH_BIG : LCR_RACING_EVENT_CRASH_SMALL;
LCR_racing.carSpeeds[1] = 0; // prevent several crash events in a row
}
_LCR_racingUpdateCarPosRot(); _LCR_racingUpdateCarPosRot();
TPE_Unit angle = TPE_vec3Dot(carUp,TPE_vec3Normalized(TPE_vec3Minus( TPE_Unit angle = TPE_vec3Dot(carUp,TPE_vec3Normalized(TPE_vec3Minus(